Skip to content

Instantly share code, notes, and snippets.

@yaron-idan
Last active October 16, 2019 20:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yaron-idan/91a1193e40cb0da5ce42a106bf1a91e0 to your computer and use it in GitHub Desktop.
Save yaron-idan/91a1193e40cb0da5ce42a106bf1a91e0 to your computer and use it in GitHub Desktop.
Reproducing Azure Python sdk error

Create a resource group (if there isn't one already -

az group create -l westus -n PythonSDKTest

Create storage account -

az storage account create -n pythonsdktestsa -g PythonSDKTest -l westus --sku Standard_LRS

Create container -

az storage container create -n pythonsdkcontainer --account-name pythonsdktestsa --account-key <REDACTED>

Create nic -

az network nic create \
	--resource-group PythonSDKTest \
	--vnet-name PythonSDKVMVNET \
	--subnet PythonSDKVMSubnet \
	--name PythonSDKVMVMNic

Create VM -

az vm create \
 	--name PythonSDKVM \
 	--admin-username soluto \
 	--admin-password po09QWpo09QW \
 	--resource-group PythonSDKTest \
 	--authentication-type password \
 	--image UbuntuLTS \
 	--storage-account pythonsdktestsa \
 	--storage-container-name pythonsdkcontainer \
 	--use-unmanaged-disk \
 	--nics PythonSDKVMVMNic
print("start experiment")
from azure.common.client_factory import get_client_from_cli_profile
from azure.mgmt.resource.resources import ResourceManagementClient
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.storage import StorageManagementClient
from azure.storage.blob import BlockBlobService
from azure.common import AzureHttpError
print("authenticating")
resource_management_client = get_client_from_cli_profile(ResourceManagementClient)
compute_management_client = get_client_from_cli_profile(ComputeManagementClient)
storage_management_client = get_client_from_cli_profile(StorageManagementClient)
print("collecting values")
rg_name = "PythonSDKTest"
vm_name = "PythonSDKVM"
nic_name = "PythonSDKVMVMNic"
print("collecting vm details")
vm_details = compute_management_client.virtual_machines.get(
rg_name, vm_name, None)
os_disk = vm_details.storage_profile.os_disk
storage_infos = vm_details.storage_profile.os_disk.vhd.uri.split('/')
account_name = storage_infos[2].split('.')[0]
container_name = storage_infos[3]
blob_name = storage_infos[4]
print('Deleting VM for {}'.format(vm_name))
delete_vm_op = resource_management_client.resources.delete(rg_name,
'Microsoft.Compute',
'',
'virtualMachines',
vm_name,
'2016-03-30')
delete_vm_op.wait()
print('Deleting NIC for {}'.format(vm_name))
delete_nic_op = resource_management_client.resources.delete(rg_name,
'Microsoft.Network',
'',
'networkInterfaces',
nic_name,
'2016-03-30')
delete_nic_op.wait()
print('Deleting OS disk for {}'.format(vm_name))
keys = storage_management_client.storage_accounts.list_keys(rg_name, account_name)
key = keys.keys[0].value
for i in range(5):
try:
block_blob_service = BlockBlobService(
account_name="pythonsdktestsa", account_key=key)
block_blob_service.delete_blob(container_name, blob_name)
except AzureHttpError as err:
print(err.message)
continue
break
print("end experiment")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment