/
osbornray
/
PythonHacks
Обзор
Документация
Войти
/
osbornray
/
PythonHacks
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
AzureAutomationWithPython/provision_rg.py
52 строки
2 KB
RekhuGopal
Azure Automation With Python
20 авг 2022, 15:10
20 авг 2022, 15:10
5acfefd
Код
Авторство
О чём код?
import os, random from azure.identity import AzureCliCredential from azure.mgmt.resource import ResourceManagementClient from azure.mgmt.storage import StorageManagementClient credential = AzureCliCredential() subscription_id = "285a9b29-43df-4ebf-85b1-61bbf7929871" resource_client = ResourceManagementClient(credential, subscription_id) RESOURCE_GROUP_NAME = "PythonAzureExample-Storage-rg" LOCATION = "centralus" # Step 1: Provision the resource group. rg_result = resource_client.resource_groups.create_or_update(RESOURCE_GROUP_NAME, { "location": LOCATION }) print(f"Provisioned resource group {rg_result.name}") # Step 2: Provision the storage account, starting with a management object. storage_client = StorageManagementClient(credential, subscription_id) STORAGE_ACCOUNT_NAME = f"pythonazurestorage{random.randint(1,100000):05}" availability_result = storage_client.storage_accounts.check_name_availability({ "name": STORAGE_ACCOUNT_NAME }) if not availability_result.name_available: print(f"Storage name {STORAGE_ACCOUNT_NAME} is already in use. Try another name.") exit() poller = storage_client.storage_accounts.begin_create(RESOURCE_GROUP_NAME, STORAGE_ACCOUNT_NAME, { "location" : LOCATION, "kind": "StorageV2", "sku": {"name": "Standard_LRS"} } ) account_result = poller.result() print(f"Provisioned storage account {account_result.name}") # Step 3: Retrieve the account's primary access key and generate a connection string. keys = storage_client.storage_accounts.list_keys(RESOURCE_GROUP_NAME, STORAGE_ACCOUNT_NAME) print(f"Primary key for storage account: {keys.keys[0].value}") conn_string = f"DefaultEndpointsProtocol=https;EndpointSuffix=core.windows.net;AccountName={STORAGE_ACCOUNT_NAME};AccountKey={keys.keys[0].value}" print(f"Connection string: {conn_string}") # Step 4: Provision the blob container in the account (this call is synchronous) CONTAINER_NAME = "cloudquicklabs" container = storage_client.blob_containers.create(RESOURCE_GROUP_NAME, STORAGE_ACCOUNT_NAME, CONTAINER_NAME, {}) print(f"Provisioned blob container {container.name}")