GithubHelp home page GithubHelp logo

servercharlie / resource-manager-node-resources-and-groups Goto Github PK

View Code? Open in Web Editor NEW

This project forked from azure-samples/resource-manager-node-resources-and-groups

0.0 0.0 0.0 11 KB

An example illustrating how to use node.js to manipulate Azure resources and resource groups

License: MIT License

JavaScript 100.00%

resource-manager-node-resources-and-groups's Introduction

services platforms author
azure-resource-manager
nodejs
haocs

Manage Azure resources and resource groups with Node.js

This sample explains how to manage your resources and resource groups in Azure using the Azure SDK for Node.js.

On this page

Tasks done in this sample

  1. Create a resource group
  2. List a resource group
  3. Update a resource group
  4. Create a key vault resource in the resource group
  5. Get details for a given resource
  6. Export the resource group template
## Running this sample
  1. If you don't already have it, get node.js.

  2. Clone the repository.

    git clone [email protected]:Azure-Samples/resource-manager-node-resources-and-groups.git
    
  3. Install the dependencies.

    cd resource-manager-node-resources-and-groups
    npm install
    
  4. Create an Azure service principal either through Azure CLI, PowerShell or the portal.

  5. Set the following environment variables using the information from the service principle that you created.

    export AZURE_SUBSCRIPTION_ID={your subscription id}
    export CLIENT_ID={your client id}
    export APPLICATION_SECRET={your client secret}
    export DOMAIN={your tenant id as a guid OR the domain name of your org <contosocorp.com>}
    

    [AZURE.NOTE] On Windows, use set instead of export.

  6. Run the sample.

    node index.js
    
  7. To clean up after index.js, run the cleanup script.

    node cleanup.js <resourceGroupName> <resourceName>
    

What is index.js doing?

The sample creates, lists and updates a website. It starts by logging in using your service principal.

_validateEnvironmentVariables();
var clientId = process.env['CLIENT_ID'];
var domain = process.env['DOMAIN'];
var secret = process.env['APPLICATION_SECRET'];
var subscriptionId = process.env['AZURE_SUBSCRIPTION_ID'];
var resourceClient;
//Sample Config
var randomIds = {};
var location = 'westus';
var resourceGroupName = _generateRandomId('testrg', randomIds);
var resourceName = _generateRandomId('testresource', randomIds);

var resourceProviderNamespace = 'Microsoft.KeyVault';
var parentResourcePath = '';
var resourceType = 'vaults';
var apiVersion = '2015-06-01';

///////////////////////////////////////
//Entrypoint for the sample script   //
///////////////////////////////////////

msRestAzure.loginWithServicePrincipalSecret(clientId, secret, domain, function (err, credentials) {
  if (err) return console.log(err);
  resourceClient = new ResourceManagementClient(credentials, subscriptionId);

With that set up, the sample performs these operations.

Create a resource group

var groupParameters = { location: location, tags: { sampletag: 'sampleValue' } };
resourceClient.resourceGroups.createOrUpdate(resourceGroupName, groupParameters, callback);

List resource groups

List the resource groups in your subscription.

resourceClient.resourceGroups.list(callback);

Update a resource group

The sample adds a tag to the resource group.

var groupParameters = { location: location, tags: { sampletag: 'helloworld' } };
resourceClient.resourceGroups.createOrUpdate(resourceGroupName, groupParameters, callback);

Create a key vault in the resource group

var keyvaultParameter = {
  location : "West US",
  properties : {
    sku : {
      family : 'A',
      name : 'standard'
    },
    accessPolicies : [],
    enabledForDeployment: true,
    enabledForTemplateDeployment: true,
    tenantId : domain
  },
  tags : {}
};
resourceClient.resources.createOrUpdate(resourceGroupName, 
                                        resourceProviderNamespace, 
                                        parentResourcePath, 
                                        resourceType, 
                                        resourceName, 
                                        apiVersion, 
                                        keyvaultParameter, 
                                        callback);

Get a resource

resourceClient.resources.get(resourceGroupName, 
                             resourceProviderNamespace, 
                             parentResourcePath, 
                             resourceType, 
                             resourceName, 
                             apiVersion, 
                             callback);

Export the resource group template

You can export the resource group as a template and then use that to deploy your resources to Azure.

var rgParameter = {
  resources: ['*']
};
resourceClient.resourceGroups.exportTemplate(resourceGroupName, rgParameter, callback);

Delete a resource

resourceClient.resources.deleteMethod(resourceGroupName, 
                                      resourceProviderNamespace, 
                                      parentResourcePath, 
                                      resourceType, 
                                      resourceName, 
                                      apiVersion, 
                                      callback);

More information

Please refer to Azure SDK for Node for more information.

resource-manager-node-resources-and-groups's People

Contributors

acomsmpbot avatar allclark avatar eduardkoller avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.