UNPKG

@azure-rest/arm-compute

Version:
105 lines (71 loc) 4.27 kB
# Azure ComputeManagement REST client library for JavaScript Compute Management Rest Client **If you are not familiar with our REST client, please spend 5 minutes to take a look at our [REST client docs](https://github.com/Azure/azure-sdk-for-js/blob/main/documentation/rest-clients.md) to use this library, the REST client provides a light-weighted & developer friendly way to call azure rest api** Key links: - [Source code](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/compute/arm-compute-rest) - [Package (NPM)](https://www.npmjs.com/package/@azure-rest/arm-compute) - [API reference documentation](https://learn.microsoft.com/javascript/api/@azure-rest/arm-compute?view=azure-node-preview) - [Samples](https://github.com/Azure-Samples/azure-samples-js-management) ## Getting started ### Currently supported environments - [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule) ### Prerequisites - You must have an [Azure subscription](https://azure.microsoft.com/free/) to use this package. ### Install the `@azure-rest/arm-compute` package Install the Azure ComputeManagement client REST client library for JavaScript with `npm`: ```bash npm install @azure-rest/arm-compute ``` ### Create and authenticate a `ComputeManagementClient` To use an [Azure Active Directory (AAD) token credential](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/identity/identity/samples/AzureIdentityExamples.md#authenticating-with-a-pre-fetched-access-token), provide an instance of the desired credential type obtained from the [@azure/identity](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#credentials) library. To authenticate with AAD, you must first `npm` install [`@azure/identity`](https://www.npmjs.com/package/@azure/identity) After setup, you can choose which type of [credential](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#credentials) from `@azure/identity` to use. As an example, [DefaultAzureCredential](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#defaultazurecredential) can be used to authenticate the client. Set the values of the client ID, tenant ID, and client secret of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET Use the returned token credential to authenticate the client: ```ts snippet:ReadmeSampleCreateClient import { DefaultAzureCredential } from "@azure/identity"; import ComputeManagementClient from "@azure-rest/arm-compute"; const credential = new DefaultAzureCredential(); const client = ComputeManagementClient(credential); ``` ## Examples The following section shows you how to initialize and authenticate your client, then list all of your Virtual Machines within a resource group. ### List all virtual machines within a resource group ```ts snippet:ReadmeSampleVirtualMachinesList import { DefaultAzureCredential } from "@azure/identity"; import ComputeManagementClient, { paginate } from "@azure-rest/arm-compute"; const credential = new DefaultAzureCredential(); const client = ComputeManagementClient(credential); const subscriptionId = ""; const resourceGroupName = "rgcompute"; const options = { queryParameters: { $filter: "aaaaaaaaaaaaaaaaaaaaaaa", "api-version": "2022-08-01", }, }; const initialResponse = await client .path( "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines", subscriptionId, resourceGroupName, ) .get(options); const pageData = paginate(client, initialResponse); for await (const item of pageData) { console.log(item); } ``` ## Troubleshooting ### Logging Enabling logging may help uncover useful information about failures. In order to see a log of HTTP requests and responses, set the `AZURE_LOG_LEVEL` environment variable to `info`. Alternatively, logging can be enabled at runtime by calling `setLogLevel` in the `@azure/logger`: ```ts snippet:SetLogLevel import { setLogLevel } from "@azure/logger"; setLogLevel("info"); ``` For more detailed instructions on how to enable logs, you can look at the [@azure/logger package docs](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/core/logger).