|
1 | | -# Azure Batch client module for Go |
| 1 | +# Azure Batch Module for Go |
2 | 2 |
|
3 | | -Azure Batch allows users to run large-scale parallel and high-performance computing (HPC) batch jobs efficiently in Azure. |
| 3 | +The `azbatch` module provides operations for working with Azure Batch. |
4 | 4 |
|
5 | | -Use this module to: |
| 5 | +[Source code](https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/batch/azbatch) |
6 | 6 |
|
7 | | -- Create and manage Batch jobs and tasks |
8 | | -- View and perform operations on nodes in a Batch pool |
| 7 | +# Getting started |
9 | 8 |
|
10 | | -## Getting started |
| 9 | +## Prerequisites |
11 | 10 |
|
12 | | -### Install the module |
| 11 | +- an [Azure subscription](https://azure.microsoft.com/free/) |
| 12 | +- Go 1.18 or above (You could download and install the latest version of Go from [here](https://go.dev/doc/install). It will replace the existing Go on your machine. If you want to install multiple Go versions on the same machine, you could refer this [doc](https://go.dev/doc/manage-install).) |
13 | 13 |
|
14 | | -Install the `azbatch` and `azidentity` modules with `go get`: |
| 14 | +## Install the package |
15 | 15 |
|
16 | | -```bash |
17 | | -go get github.com/Azure/azure-sdk-for-go/sdk/batch/azbatch |
18 | | -go get github.com/Azure/azure-sdk-for-go/sdk/azidentity |
19 | | -``` |
20 | | - |
21 | | -### Prerequisites |
22 | | - |
23 | | -- Go, version 1.18 or higher - [Install Go](https://go.dev/doc/install) |
24 | | -- Azure subscription - [Create a free account](https://azure.microsoft.com/free) |
25 | | -- A Batch account with a linked Azure Storage account. You can create the accounts by using any of the following methods: [Azure CLI](https://learn.microsoft.com/azure/batch/quick-create-cli) | [Azure portal](https://learn.microsoft.com/azure/batch/quick-create-portal) | [Bicep](https://learn.microsoft.com/azure/batch/quick-create-bicep) | [ARM template](https://learn.microsoft.com/azure/batch/quick-create-template) | [Terraform](https://learn.microsoft.com/azure/batch/quick-create-terraform). |
| 16 | +This project uses [Go modules](https://github.com/golang/go/wiki/Modules) for versioning and dependency management. |
26 | 17 |
|
27 | | -### Authenticate the client |
| 18 | +Install the Azure Batch module: |
28 | 19 |
|
29 | | -Azure Batch integrates with Microsoft Entra ID for identity-based authentication of requests. You can use role-based access control (RBAC) to grant access to your Azure Batch resources to users, groups, or applications. The [Azure Identity module](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity) provides types that implement Microsoft Entra ID authentication. |
| 20 | +```sh |
| 21 | +go get github.com/Azure/azure-sdk-for-go/sdk/batch/azbatch |
| 22 | +``` |
30 | 23 |
|
31 | | -## Key concepts |
| 24 | +## Authorization |
32 | 25 |
|
33 | | -[Azure Batch Overview](https://learn.microsoft.com/azure/batch/batch-technical-overview) |
| 26 | +When creating a client, you will need to provide a credential for authenticating with Azure Batch. The `azidentity` module provides facilities for various ways of authenticating with Azure including client/secret, certificate, managed identity, and more. |
34 | 27 |
|
35 | | -## Examples |
| 28 | +```go |
| 29 | +cred, err := azidentity.NewDefaultAzureCredential(nil) |
| 30 | +``` |
36 | 31 |
|
37 | | -See the [package documentation][pkgsite] for code samples. |
| 32 | +For more information on authentication, please see the documentation for `azidentity` at [pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity). |
38 | 33 |
|
39 | | -## Troubleshooting |
| 34 | +## Client Factory |
40 | 35 |
|
41 | | -Please see [Troubleshooting common batch issues](https://learn.microsoft.com/troubleshoot/azure/hpc/batch/welcome-hpc-batch). |
| 36 | +Azure Batch module consists of one or more clients. We provide a client factory which could be used to create any client in this module. |
42 | 37 |
|
43 | | -### Error Handling |
| 38 | +```go |
| 39 | +clientFactory, err := azbatch.NewClientFactory(<subscription ID>, cred, nil) |
| 40 | +``` |
44 | 41 |
|
45 | | -All methods which send HTTP requests return `*azcore.ResponseError` when these requests fail. `ResponseError` has error details and the raw response from Key Vault. |
| 42 | +You can use `ClientOptions` in package `github.com/Azure/azure-sdk-for-go/sdk/azcore/arm` to set endpoint to connect with public and sovereign clouds as well as Azure Stack. For more information, please see the documentation for `azcore` at [pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azcore](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azcore). |
46 | 43 |
|
47 | 44 | ```go |
48 | | -import "github.com/Azure/azure-sdk-for-go/sdk/azcore" |
49 | | - |
50 | | -resp, err = client.CreateJob(context.TODO(), jobContent, nil) |
51 | | -if err != nil { |
52 | | - var httpErr *azcore.ResponseError |
53 | | - if errors.As(err, &httpErr) { |
54 | | - // TODO: investigate httpErr |
55 | | - } else { |
56 | | - // TODO: not an HTTP error |
57 | | - } |
| 45 | +options := arm.ClientOptions { |
| 46 | + ClientOptions: azcore.ClientOptions { |
| 47 | + Cloud: cloud.AzureChina, |
| 48 | + }, |
58 | 49 | } |
| 50 | +clientFactory, err := azbatch.NewClientFactory(<subscription ID>, cred, &options) |
59 | 51 | ``` |
60 | 52 |
|
61 | | -### Logging |
| 53 | +## Clients |
62 | 54 |
|
63 | | -This module uses the logging implementation in `azcore`. To turn on logging for all Azure SDK modules, set `AZURE_SDK_GO_LOGGING` to `all`. By default the logger writes to stderr. Use the `azcore/log` package to control log output. For example, logging only HTTP request and response events, and printing them to stdout: |
| 55 | +A client groups a set of related APIs, providing access to its functionality. Create one or more clients to access the APIs you require using client factory. |
64 | 56 |
|
65 | 57 | ```go |
66 | | -import azlog "github.com/Azure/azure-sdk-for-go/sdk/azcore/log" |
67 | | - |
68 | | -// Print log events to stdout |
69 | | -azlog.SetListener(func (_ azlog.Event, msg string) { |
70 | | - fmt.Println(msg) |
71 | | -}) |
72 | | - |
73 | | -// Includes only requests and responses in logs |
74 | | -azlog.SetEvents(azlog.EventRequest, azlog.EventResponse) |
| 58 | +client := clientFactory.() |
75 | 59 | ``` |
76 | 60 |
|
77 | | -### Accessing `http.Response` |
| 61 | +## Fakes |
78 | 62 |
|
79 | | -You can access the `http.Response` returned by Azure Batch to any client method using `runtime.WithCaptureResponse`: |
| 63 | +The fake package contains types used for constructing in-memory fake servers used in unit tests. |
| 64 | +This allows writing tests to cover various success/error conditions without the need for connecting to a live service. |
80 | 65 |
|
81 | | -```go |
82 | | -import "github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime" |
| 66 | +Please see https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/samples/fakes for details and examples on how to use fakes. |
83 | 67 |
|
84 | | -var response *http.Response |
85 | | -ctx := runtime.WithCaptureResponse(context.TODO(), &response) |
86 | | -resp, err = client.CreateJob(ctx, jobContent, nil) |
87 | | -if err != nil { |
88 | | - // TODO: handle error |
89 | | -} |
90 | | -// TODO: do something with response |
91 | | -``` |
| 68 | +## Provide Feedback |
92 | 69 |
|
93 | | -## Contributing |
| 70 | +If you encounter bugs or have suggestions, please |
| 71 | +[open an issue](https://github.com/Azure/azure-sdk-for-go/issues) and assign the `Batch` label. |
94 | 72 |
|
95 | | -This project welcomes contributions and suggestions. |
96 | | -Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. |
97 | | -For details, visit [Contributor License Agreements](https://opensource.microsoft.com/cla/). |
| 73 | +# Contributing |
98 | 74 |
|
99 | | -When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). |
100 | | -Simply follow the instructions provided by the bot. |
101 | | -You will only need to do this once across all repos using our CLA. |
| 75 | +This project welcomes contributions and suggestions. Most contributions require |
| 76 | +you to agree to a Contributor License Agreement (CLA) declaring that you have |
| 77 | +the right to, and actually do, grant us the rights to use your contribution. |
| 78 | +For details, visit [https://cla.microsoft.com](https://cla.microsoft.com). |
102 | 79 |
|
103 | | -This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). |
104 | | -For more information see the [Code of Conduct FAQ ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [[email protected]](mailto:[email protected]) with any additional questions or comments. |
| 80 | +When you submit a pull request, a CLA-bot will automatically determine whether |
| 81 | +you need to provide a CLA and decorate the PR appropriately (e.g., label, |
| 82 | +comment). Simply follow the instructions provided by the bot. You will only |
| 83 | +need to do this once across all repos using our CLA. |
105 | 84 |
|
106 | | -[pkgsite]: https://aka.ms/azsdk/go/azbatch |
| 85 | +This project has adopted the |
| 86 | +[Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). |
| 87 | +For more information, see the |
| 88 | +[Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) |
| 89 | + |
| 90 | +additional questions or comments. |
0 commit comments