azure-sdk-for-node/runtime/ms-rest-azure
Qiaoqiao Zhang 6702fa4f63
try fix cg issue for ms-rest-azure 3.x.x (#5233)
* try fix cg issue for ms-rest-azure 3.x.x

* update root package.json
2022-04-28 10:53:18 +08:00
..
.vscode Fix unit tests 2019-01-11 12:15:37 -08:00
lib Remove leading dot from azure environment settings 2019-07-11 09:50:39 -07:00
test resolve-ms-rest-azure-security-issue (#5213) 2021-11-10 10:38:05 +08:00
.gitignore runtimes from version 1.15.1. From now onwards the runtimes would stay in the node sdk. 2016-10-06 12:22:41 -07:00
.npmignore do not publish the test folder 2018-09-26 15:08:37 -07:00
Changelog.md resolve-ms-rest-azure-security-issue (#5213) 2021-11-10 10:38:05 +08:00
LICENSE
README.md adding impression pixel 2019-03-04 15:13:55 -08:00
index.d.ts fix-ms-rest-azure-type 2020-05-26 13:34:58 +08:00
ms-rest-azure.njsproj Deserialize addtionalInfo in ARM error 2018-06-13 15:25:08 -07:00
package.json try fix cg issue for ms-rest-azure 3.x.x (#5233) 2022-04-28 10:53:18 +08:00

README.md

MS-Rest-Azure

Infrastructure for error handling, tracing, and http client pipeline configuration. Required by nodeJS Azure client libraries, generated using AutoRest.

  • Node.js version: 4.x.x or higher

How to Install

npm install ms-rest-azure

Usage

var msrestAzure = require('ms-rest-azure');

Authentication

Interactive Login is the simplest and the best way to authenticate.

It provides a url and code that needs to be copied and pasted in a browser and authenticated over there. If successful, the user will get a DeviceTokenCredentials object.

 var someAzureServiceClient = require('azure-arm-someService');
 msRestAzure.interactiveLogin(function(err, credentials) {
   if (err) return console.log(err);
   var client = new someAzureServiceClient(credentials, 'your-subscriptionId');
   client.someOperationGroup.method(param1, param2, function(err, result) {
     if (err) return console.log(err);
     return console.log(result);
   });
 });

Login with username and password

This mechanism will only work for organizational ids and ids that are not 2FA enabled. Otherwise it is better to use the above mechanism (interactive login).

 var someAzureServiceClient = require('azure-arm-someService');
 msRestAzure.loginWithUsernamePassword(username, password, function(err, credentials) {
   if (err) return console.log(err);
   var client = new someAzureServiceClient(credentials, 'your-subscriptionId');
   client.someOperationGroup.method(param1, param2, function(err, result) {
     if (err) return console.log(err);
     return console.log(result);
   });
 });

Non-Interactive Authentication

If you need to create an automation account for non interactive or scripting scenarios then please take a look at the documentation over here. Once you have created a service principal you can authenticate using the following code snippet.

Login with service principal name and secret

 var someAzureServiceClient = require('azure-arm-someService');
 msRestAzure.loginWithServicePrincipalSecret(clientId, secret, domain, function(err, credentials) {
   if (err) return console.log(err);
   var client = new someAzureServiceClient(credentials, 'your-subscriptionId');
   client.someOperationGroup.method(param1, param2, function(err, result) {
     if (err) retutrn console.log(err);
     return console.log(result);
   });
 });

Using the generic (authenticated) AzureServiceClient to make custom requests to Azure.

This can be very useful in doing something custom or while debugging.

A simple client to make a request using the sendRequest() method.

To find out the power of sendRequest(), please visit this link for detailed documentation of supported options while sending a request.

const msrest = require('ms-rest');
const msRestAzure = require('ms-rest-azure');
const AzureServiceClient = msRestAzure.AzureServiceClient;

const clientId = process.env['CLIENT_ID'];
const secret = process.env['APPLICATION_SECRET'];
const domain = process.env['DOMAIN']; //also known as tenantId
const subscriptionId = process.env['AZURE_SUBSCRIPTION_ID'];
var client;

//an example to list resource groups in a subscription
msRestAzure.loginWithServicePrincipalSecret(clientId, secret, domain).then((creds) => {
  client = new AzureServiceClient(creds);
  let options = {
    method: 'GET',
    url: `https://management.azure.com/subscriptions/${subscriptionId}/resourcegroups?api-version=2016-09-01`,
    headers: {
      'user-agent': 'MyTestApp/1.0'
    }
  }
  return client.sendRequest(options);
}).then((result) => {
  console.dir(result, {depth: null, colors: true});
}).catch((err) => {
  console.dir(err, {depth: null, colors: true});
});

Impressions