你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

Azure LoadTest client library for JavaScript - version 1.0.0

This package contains an isomorphic SDK (runs both in Node.js and in browsers) for Azure LoadTest client.

LoadTest client provides access to LoadTest Resource and it's status operations.

Source code | Package (NPM) | API reference documentation | Samples

Getting started

Currently supported environments

See our support policy for more details.

Prerequisites

Install the @azure/arm-loadtesting package

Install the Azure LoadTest client library for JavaScript with npm:

npm install @azure/arm-loadtesting

Create and authenticate a LoadTestClient

To create a client object to access the Azure LoadTest API, you will need the endpoint of your Azure LoadTest resource and a credential. The Azure LoadTest client can use Azure Active Directory credentials to authenticate. You can find the endpoint for your Azure LoadTest resource in the Azure Portal.

You can authenticate with Azure Active Directory using a credential from the @azure/identity library or an existing AAD Token.

To use the DefaultAzureCredential provider shown below, or other credential providers provided with the Azure SDK, please install the @azure/identity package:

npm install @azure/identity

You will also need to register a new AAD application and grant access to Azure LoadTest by assigning the suitable role to your service principal (note: roles such as "Owner" will not grant the necessary permissions). 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.

For more information about how to create an Azure AD Application check out this guide.

const { LoadTestClient } = require("@azure/arm-loadtesting");
const { DefaultAzureCredential } = require("@azure/identity");
// For client-side applications running in the browser, use InteractiveBrowserCredential instead of DefaultAzureCredential. See https://aka.ms/azsdk/js/identity/examples for more details.

const subscriptionId = "00000000-0000-0000-0000-000000000000";
const client = new LoadTestClient(new DefaultAzureCredential(), subscriptionId);

// For client-side applications running in the browser, use this code instead:
// const credential = new InteractiveBrowserCredential({
//   tenantId: "<YOUR_TENANT_ID>",
//   clientId: "<YOUR_CLIENT_ID>"
// });
// const client = new LoadTestClient(credential, subscriptionId);

Create an Azure Load Testing resource

Create a new Azure Load Testing resource.

loadTestResourceCreatePayload = {
  location: "westus2"
};

const resource = await client.loadTests.beginCreateOrUpdateAndWait(
  "sample-rg",
  "sample-loadtesting-resource",
  loadTestResourceCreatePayload
);

console.log(resource);

Create a new Azure Load Testing resource with managed identity and customer managed key encryption.

loadTestResourceCreatePayload = {
  location: "westus2",
  tags: { team: "testing" },
  identity: {
    type: 'SystemAssigned, UserAssigned',
    userAssignedIdentities: {
      '/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/sample-rg/providers/microsoft.managedidentity/userassignedidentities/identity1': {}
    }
  },
  encryption: {
    identity: {
      type: 'UserAssigned',
      resourceId: '/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/sample-rg/providers/microsoft.managedidentity/userassignedidentities/identity1'
    },
    keyUrl: 'https://sample-kv.vault.azure.net/keys/cmkkey/2d1ccd5c50234ea2a0858fe148b69cde'
  }
};

const resource = await client.loadTests.beginCreateOrUpdateAndWait(
  "sample-rg",
  "sample-loadtesting-resource",
  loadTestResourceCreatePayload
);

console.log(resource);

Get an Azure Load Testing resource

let resourceName = 'sample-loadtesting-resource';
let resourceGroupName = 'sample-rg';

const resource = await client.loadTests.get(
  resourceGroupName,
  resourceName
);

console.log(resource);

Update an Azure Load Testing resource

loadTestResourcePatchPayload = {
  tags: { team: "testing-dev" },
  identity: {
    type: 'SystemAssigned, UserAssigned',
    userAssignedIdentities: {
      // removing a user-assigned managed identity by assigning the value in the payload as null
      '/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/sample-rg/providers/microsoft.managedidentity/userassignedidentities/identity1': null,
      '/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/sample-rg/providers/microsoft.managedidentity/userassignedidentities/identity2': {}
    }
  },
  encryption: {
    // use system-assigned managed identity for CMK encryption
    identity: {
      type: 'SystemAssigned',
      resourceId: null
    },
    keyUrl: 'https://sample-kv.vault.azure.net/keys/cmkkey/2d1ccd5c50234ea2a0858fe148b69cde'
  }
};

const resource = await client.loadTests.beginUpdateAndWait(
  "sample-rg",
  "sample-loadtesting-resource",
  loadTestResourcePatchPayload
);

console.log(resource);

Delete an Azure Load Testing resource

let resourceName = 'sample-loadtesting-resource';
let resourceGroupName = 'sample-rg';

const result = await client.loadTests.beginDeleteAndWait(
  resourceGroupName,
  resourceName
);

JavaScript Bundle

To use this client library in the browser, first you need to use a bundler. For details on how to do this, please refer to our bundling documentation.

Key concepts

LoadTestClient

LoadTestClient is the primary interface for developers using the Azure LoadTest client library. Explore the methods on this client object to understand the different features of the Azure LoadTest service that you can access.

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:

const { setLogLevel } = require("@azure/logger");
setLogLevel("info");

For more detailed instructions on how to enable logs, you can look at the @azure/logger package docs.

Next steps

Please take a look at the samples directory for detailed examples on how to use this library.

Contributing

If you'd like to contribute to this library, please read the contributing guide to learn more about how to build and test the code.

Impressions