page_type | languages | products | description | urlFragment | ||
---|---|---|---|---|---|---|
sample |
|
|
This sample explains how to manage your resources and resource groups in Azure using the Azure SDK for Node.js. |
resource-manager-node-resources-and-groups |
This sample explains how to manage your resources and resource groups in Azure using the Azure SDK for Node.js.
On this page
- Create a resource group
- List a resource group
- Update a resource group
- Create a key vault resource in the resource group
- Get details for a given resource
- Export the resource group template
-
If you don't already have it, get node.js.
-
Clone the repository.
git clone [email protected]:Azure-Samples/resource-manager-node-resources-and-groups.git
-
Install the dependencies.
cd resource-manager-node-resources-and-groups npm install
-
Create an Azure service principal either through Azure CLI, PowerShell or the portal.
-
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 ofexport
. -
Run the sample.
node index.js
-
To clean up after index.js, run the cleanup script.
node cleanup.js <resourceGroupName> <resourceName>
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.
var groupParameters = { location: location, tags: { sampletag: 'sampleValue' } };
resourceClient.resourceGroups.createOrUpdate(resourceGroupName, groupParameters, callback);
List the resource groups in your subscription.
resourceClient.resourceGroups.list(callback);
The sample adds a tag to the resource group.
var groupParameters = { location: location, tags: { sampletag: 'helloworld' } };
resourceClient.resourceGroups.createOrUpdate(resourceGroupName, groupParameters, callback);
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);
resourceClient.resources.get(resourceGroupName,
resourceProviderNamespace,
parentResourcePath,
resourceType,
resourceName,
apiVersion,
callback);
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);
resourceClient.resources.deleteMethod(resourceGroupName,
resourceProviderNamespace,
parentResourcePath,
resourceType,
resourceName,
apiVersion,
callback);
Please refer to Azure SDK for Node for more information.