|
| 1 | +# Azure Batch client module for Go |
| 2 | + |
| 3 | +Azure Batch allows users to run large-scale parallel and high-performance computing (HPC) batch jobs efficiently in Azure. |
| 4 | + |
| 5 | +Use this module to: |
| 6 | + |
| 7 | +- Create and manage Batch jobs and tasks |
| 8 | +- View and perform operations on nodes in a Batch pool |
| 9 | + |
| 10 | +## Getting started |
| 11 | + |
| 12 | +### Install the module |
| 13 | + |
| 14 | +Install the `azbatch` and `azidentity` modules with `go get`: |
| 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). |
| 26 | + |
| 27 | +### Authenticate the client |
| 28 | + |
| 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. |
| 30 | + |
| 31 | +## Key concepts |
| 32 | + |
| 33 | +[Azure Batch Overview](https://learn.microsoft.com/azure/batch/batch-technical-overview) |
| 34 | + |
| 35 | +## Examples |
| 36 | + |
| 37 | +See the [package documentation][pkgsite] for code samples. |
| 38 | + |
| 39 | +## Troubleshooting |
| 40 | + |
| 41 | +Please see [Troubleshooting common batch issues](https://learn.microsoft.com/troubleshoot/azure/hpc/batch/welcome-hpc-batch). |
| 42 | + |
| 43 | +### Error Handling |
| 44 | + |
| 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. |
| 46 | + |
| 47 | +```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 | + } |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +### Logging |
| 62 | + |
| 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: |
| 64 | + |
| 65 | +```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) |
| 75 | +``` |
| 76 | + |
| 77 | +### Accessing `http.Response` |
| 78 | + |
| 79 | +You can access the `http.Response` returned by Azure Batch to any client method using `runtime.WithCaptureResponse`: |
| 80 | + |
| 81 | +```go |
| 82 | +import "github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime" |
| 83 | + |
| 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 | +``` |
| 92 | + |
| 93 | +## Contributing |
| 94 | + |
| 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/). |
| 98 | + |
| 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. |
| 102 | + |
| 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. |
| 105 | + |
| 106 | +[pkgsite]: https://aka.ms/azsdk/go/azbatch |
0 commit comments