What is ms-rest-azure?
The ms-rest-azure npm package is a library for Node.js that provides authentication and client creation functionalities for Azure services. It simplifies the process of interacting with Azure resources by handling the complexities of authentication and service client management.
What are ms-rest-azure's main functionalities?
Authentication with Azure
This feature allows you to authenticate with Azure using a service principal. The code sample demonstrates how to log in using a client ID, secret, and domain.
const msRestAzure = require('ms-rest-azure');
msRestAzure.loginWithServicePrincipalSecret(clientId, secret, domain, (err, credentials) => {
if (err) return console.log(err);
console.log('Authenticated successfully');
});
Creating a Resource Management Client
This feature allows you to create a client for managing Azure resources. The code sample shows how to create a Resource Management Client after authenticating with Azure.
const msRestAzure = require('ms-rest-azure');
const AzureArmResource = require('azure-arm-resource');
msRestAzure.loginWithServicePrincipalSecret(clientId, secret, domain, (err, credentials) => {
if (err) return console.log(err);
const client = new AzureArmResource.ResourceManagementClient(credentials, subscriptionId);
console.log('Resource Management Client created successfully');
});
Listing Resource Groups
This feature allows you to list all resource groups in a subscription. The code sample demonstrates how to list resource groups using the Resource Management Client.
const msRestAzure = require('ms-rest-azure');
const AzureArmResource = require('azure-arm-resource');
msRestAzure.loginWithServicePrincipalSecret(clientId, secret, domain, (err, credentials) => {
if (err) return console.log(err);
const client = new AzureArmResource.ResourceManagementClient(credentials, subscriptionId);
client.resourceGroups.list((err, result) => {
if (err) return console.log(err);
console.log('Resource Groups:', result);
});
});
Other packages similar to ms-rest-azure
azure-sdk-for-js
The azure-sdk-for-js is a collection of libraries for various Azure services. It provides more modern and modular packages compared to ms-rest-azure, allowing you to include only the specific services you need. It also supports the latest Azure features and improvements.
azure-arm-resource
The azure-arm-resource package is specifically designed for managing Azure resources. It provides functionalities similar to ms-rest-azure but focuses solely on resource management. It can be used in conjunction with ms-rest-azure for authentication.
azure-storage
The azure-storage package is designed for interacting with Azure Storage services. While ms-rest-azure provides general authentication and client creation, azure-storage focuses on storage-specific operations like managing blobs, queues, and tables.
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'];
const subscriptionId = process.env['AZURE_SUBSCRIPTION_ID'];
var client;
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});
});
Related Projects