Quantcast
Channel: Fabric Controller
Viewing all articles
Browse latest Browse all 23

Introducing node-armclient, the lightweight Node.js client for Azure Resource Manager

$
0
0
Introducing node-armclient, the lightweight Node.js client for Azure Resource Manager

Azure Resource Manager is a great way to manage an deploy your Azure resources in a consistent way. There are quite a few SDKs available for Azure Resource Manager (including Node.js).

While the current Node.js SDKs cover many of the endpoints in available today in the REST API, these SDKs are not always fully documented (some don't have any documentation available at all). And others might not yet support newer endpoints.

This is why I created an extremely simple and small SDK which makes it easy to just call any endpoint out there, send a payload if required and process the result.

node-armclient

npm install --save armclient

This library (source) does only a few things:

  • Allow you to choose how to authenticate
  • Create a "scoped client" for a given Resource Group and Resource Provider
  • Execute GET/POST/PUT/DELETE requests on specific endpoints

Authentication

The library currently supports 2 authentication providers:

  • Client Credentials: This is basically an Azure AD application that has access to your application. Different tutorials are available explaining how this works
  • Token Credentials: You get a token somewhere else (eg: OIDC login, ...) and just provide that one in the provider. Useful when integrating this in web applications

For Client Credentials authentication you would simply need to provide the information about your subscription and your Azure AD application:

import ArmClient, { clientCredentials } from 'armclient';

const client = ArmClient({  
  subscriptionId: '111111-2222-3333333',
  auth: clientCredentials({
    tenantId: '444444-555555-666666666',
    clientId: '777777-888888-999999999',
    clientSecret: 'aaaabbbbbccccc' // or servicePrincipalPassword
  })
});

For Token Credentials you simply provide the token you acquired from an external flow:

import ArmClient, { tokenCredentials } from 'armclient';

const client = ArmClient({  
  subscriptionId: '111111-2222-3333333',
  auth: ArmClient.tokenCredentials({
    accessToken: 'abcdefg'
  })
});

Calling the REST API

After setting up the client you can call an endpoint by providing the full path:

client.get('https://management.azure.com/subscriptions/111-222-333-444/resourceGroups/lab/providers/Microsoft.Automation/automationAccounts', { 'api-version': '2015-10-31' })  
  .then((res) => {
    console.log(res.body);
    console.log(res.headers);
  })
  .catch((err) => {
    console.log(err);
  });

Or a path in your subscription:

client.get('/resourceGroups/lab/providers/Microsoft.Automation/automationAccounts', { 'api-version': '2015-10-31' })  
  .then((res) => {
    console.log(res.body);
    console.log(res.headers);
  })
  .catch((err) => {
    console.log(err);
  });

Or you can even create a scoped client for a given Resource Group and Resource Provider:

const automationClient = client.provider('my-production-rg', 'Microsoft.Automation');

automationClient  
    .get(`/automationAccounts/my-automation-account`, { 'api-version': '2015-10-31' })
    .then((res) => {
        console.log(res.body);
        console.log(res.headers);
    })
    .catch((err) => {
        console.log(err);
    });

For a GET you would need to provide the path and the querystring (eg: { 'api-version': '2015-10-31' }). For POST/PUT and DELETE you should also specify the payload:

const payload = {  
  name: 'abc',
  storageAccount: 'def' 
};

client.post('/resourceGroups/lab/providers/Something', { 'api-version': '2015-10-31' }, payload)  
  .then((res) => {
    console.log(res.body);
    console.log(res.headers);
  })
  .catch((err) => {
    console.log(err);
  });

client.put('/resourceGroups/lab/providers/Something', { 'api-version': '2015-10-31' }, payload)  
  .then((res) => {
    console.log(res.body);
    console.log(res.headers);
  })
  .catch((err) => {
    console.log(err);
  });

client.del('/resourceGroups/lab/providers/Something', { 'api-version': '2015-10-31' }, payload)  
  .then((res) => {
    console.log(res.body);
    console.log(res.headers);
  })
  .catch((err) => {
    console.log(err);
  });

And finally, if successful the promise will return the response containing the body and the header.

Endpoints and Payload

Since this is a low level client you'll need to know which endpoints to hit and what payload you need to send. A few good places to start:

Sample applications:

Have fun!


Viewing all articles
Browse latest Browse all 23

Trending Articles