Skip to content

An example illustrating how to use Python to manipulate Azure resources and resource groups

License

Notifications You must be signed in to change notification settings

jijohn14/resource-manager-python-resources-and-groups

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

services platforms author
azure-resource-manager
python
lmazuel

Manage Azure resources and resource groups with Python

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

On this page

Run this sample

  1. If you don't already have it, install Python.

  2. We recommend to use a virtual environnement to run this example, but it's not mandatory. You can initialize a virtualenv this way:

    pip install virtualenv
    virtualenv mytestenv
    cd mytestenv
    source bin/activate
    
  3. Clone the repository.

    git clone https://github.com/Azure-Samples/resource-manager-python-resources-and-groups.git
    
  4. Install the dependencies using pip.

    cd resource-manager-python-resources-and-groups
    pip install -r requirements.txt
    
  5. Create an Azure service principal either through Azure CLI, PowerShell or the portal.

  6. Export these environment variables into your current shell.

    export AZURE_TENANT_ID={your tenant id}
    export AZURE_CLIENT_ID={your client id}
    export AZURE_CLIENT_SECRET={your client secret}
    export AZURE_SUBSCRIPTION_ID={your subscription id}
    
  7. Run the sample.

    python example.py
    

What is example.py doing?

The sample walks you through several resource and resource group management operations. It starts by setting up a ResourceManagementClient object using your subscription and credentials.

import os
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.resource import ResourceManagementClient

subscription_id = os.environ.get(
    'AZURE_SUBSCRIPTION_ID',
    '11111111-1111-1111-1111-111111111111') # your Azure Subscription Id
credentials = ServicePrincipalCredentials(
    client_id=os.environ['AZURE_CLIENT_ID'],
    secret=os.environ['AZURE_CLIENT_SECRET'],
    tenant=os.environ['AZURE_TENANT_ID']
)
client = ResourceManagementClient(credentials, subscription_id)

It also sets up a ResourceGroup object (resource_group_params) to be used as a parameter in some of the API calls.

resource_group_params = {'location':'westus'}

There are a couple of supporting functions (print_item and print_properties) that print a resource group and it's properties. With that set up, the sample lists all resource groups for your subscription, it performs these operations.

List resource groups

List the resource groups in your subscription.

for item in client.resource_groups.list():
    print_item(item)

Create a resource group

client.resource_groups.create_or_update('azure-sample-group', resource_group_params)

Update a resource group

The sample adds a tag to the resource group.

resource_group_params.update(tags={'hello': 'world'})
client.resource_groups.create_or_update('azure-sample-group', resource_group_params)

Create a key vault in the resource group

key_vault_params = {
    'location': 'westus',
    'properties':  {
        'sku': {'family': 'A', 'name': 'standard'},
        'tenantId': os.environ['AZURE_TENANT_ID'],
        'accessPolicies': [],
        'enabledForDeployment': True,
        'enabledForTemplateDeployment': True,
        'enabledForDiskEncryption': True
    }
}
client.resources.create_or_update(GROUP_NAME,
                                  'Microsoft.KeyVault',
                                  '',
                                  'vaults',
                                  'azureSampleVault',
                                  '2015-06-01',
                                  key_vault_params)

List resources within the group

for item in client.resource_groups.list_resources(GROUP_NAME):
    print_item(item)

Export the resource group template

client.resource_groups.export_template(GROUP_NAME, ['*'])

Delete a resource group

delete_async_operation = client.resource_groups.delete('azure-sample-group')
delete_async_operation.wait()

About

An example illustrating how to use Python to manipulate Azure resources and resource groups

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%