示例#1
0
    def test_resource_groups(self):
        # create resource group
        print('Creating resource group: ' + self.rgname)
        response = azurerm.create_resource_group(self.access_token, self.subscription_id, \
            self.rgname, self.location)
        self.assertEqual(response.status_code, 201)

        # Create Cosmos DB account
        print('Creating Cosmos DB account: ' + self.cosmosdbname)

        response = azurerm.create_cosmosdb_account(self.access_token, self.subscription_id, self.rgname, \
            self.cosmosdbname, self.location, cosmosdb_kind='GlobalDocumentDB')
        self.assertEqual(response.status_code, 200)

        # delete resource group
        print('Deleting resource group: ' + self.rgname)
        response = azurerm.delete_resource_group(self.access_token,
                                                 self.subscription_id,
                                                 self.rgname)
        self.assertEqual(response.status_code, 202)
###
# Create the a resource group for our demo
# We need a resource group and a Cosmos DB account. A random name is generated, as each Cosmos DB account name must be globally unique.
###
response = azurerm.create_resource_group(auth_token, subscription_id,
                                         resourcegroup_name, location)
if response.status_code == 200 or response.status_code == 201:
    print('Resource group: ' + resourcegroup_name + ' created successfully.')
else:
    print('Error creating resource group')

# Create a Cosmos DB account for our demo
response = azurerm.create_cosmosdb_account(auth_token,
                                           subscription_id,
                                           resourcegroup_name,
                                           cosmosdb_name,
                                           location,
                                           cosmosdb_kind='GlobalDocumentDB')
if response.status_code == 200:
    print('Cosmos DB account: ' + cosmosdb_name + ' created successfully.')
    print(
        '\nIt can take a couple of minutes to get the Cosmos DB account ready. Waiting...'
    )
    time.sleep(150)
else:
    print('Error creating Cosmos DB account: ' + str(response))

# Each Cosmos DB account has a primary and secondary access key.
# These keys are used by aplications to access data in your Cosmos DB account.
# Obtain the primary access key for use with the rest of the demo
response = azurerm.get_cosmosdb_account_keys(auth_token, subscription_id,
示例#3
0
# Load Azure app defaults
try:
    with open('azurermconfig.json') as config_file:
        CONFIG_DATA = json.load(config_file)
except FileNotFoundError:
    print("Error: Expecting azurermconfig.json in current folder")
    sys.exit()

TENANT_ID = CONFIG_DATA['tenantId']
APP_ID = CONFIG_DATA['appId']
APP_SECRET = CONFIG_DATA['appSecret']
SUB_ID = CONFIG_DATA['subscriptionId']
ACCESS_TOKEN = azurerm.get_access_token(TENANT_ID, APP_ID, APP_SECRET)

print('Enter an existing Azure Resource Group name.')
RG_NAME = input()

# create a Cosmos DB account
print('Enter Cosmos DB account name to create.')
CA_NAME = input()
LOCATION = 'eastus'
RET = azurerm.create_cosmosdb_account(ACCESS_TOKEN, SUB_ID, RG_NAME, CA_NAME, LOCATION, cosmosdb_kind='GlobalDocumentDB')
print(RET)

print('It can take 2-3 minutes to create the Cosmos DB account. The example below is for reference. This likely falls if ran right away.')

# get storage account keys
KEYS = azurerm.get_cosmosdb_account_keys(ACCESS_TOKEN, SUB_ID, RG_NAME, CA_NAME)
print(json.dumps(KEYS.text, sort_keys=False, indent=2, separators=(',', ': ')))