Created
May 30, 2018 13:07
-
-
Save podstava/7f177557ae8a870b3916ee9b5e336e3b to your computer and use it in GitHub Desktop.
Python azure sdk cheat sheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'podstava' | |
from azure.common.credentials import ServicePrincipalCredentials | |
from azure.mgmt.compute import ComputeManagementClient | |
from azure.mgmt.dns import DnsManagementClient | |
from azure.mgmt.network import NetworkManagementClient | |
from azure.mgmt.resource import ResourceManagementClient | |
from azure.mgmt.storage import StorageManagementClient | |
from azure.storage.blob import BlockBlobService | |
AZURE_TENANT_ID = '' | |
AZURE_CLIENT_ID = '' | |
AZURE_CLIENT_SECRET = '' | |
AZURE_SUBSCRIPTION_ID = '' | |
def get_credentials(): | |
return ServicePrincipalCredentials( | |
client_id=AZURE_CLIENT_ID, | |
secret=AZURE_CLIENT_SECRET, | |
tenant=AZURE_TENANT_ID | |
) | |
credentials = get_credentials() | |
resource_client = ResourceManagementClient(credentials, AZURE_SUBSCRIPTION_ID) | |
storage_client = StorageManagementClient(credentials, AZURE_SUBSCRIPTION_ID) | |
network_client = NetworkManagementClient(credentials, AZURE_SUBSCRIPTION_ID) | |
compute_client = ComputeManagementClient(credentials, AZURE_SUBSCRIPTION_ID) | |
dns_client = DnsManagementClient(credentials, AZURE_SUBSCRIPTION_ID) | |
def create_or_update_cname_record(dns_rec, cname_value, dns_zone, resource_group): | |
dns_client.record_sets.create_or_update( | |
resource_group, dns_zone, dns_rec, | |
'CNAME', | |
{'ttl': 300, | |
'cname_record': {'cname': cname_value} | |
}) | |
def create_or_update_a_record(dns_rec, ip, dns_zone, resource_group): | |
dns_client.record_sets.create_or_update( | |
resource_group, dns_zone, dns_rec, | |
'CNAME', | |
{'ttl': 300, | |
'arecords': [{'ipv4_address': ip}] | |
}) | |
def run_custom_script_on_vm(resource_group_name, vm_name, script_url, ): | |
vm_command_execution = compute_client.virtual_machine_extensions.create_or_update( | |
resource_group_name, vm_name, 'CustomScriptForLinux', | |
extension_parameters={ | |
"publisher": "Microsoft.OSTCExtensions", | |
"location": "westeurope", # change location if need | |
"type": "CustomScriptForLinux", | |
"typeHandlerVersion": "1.4", | |
"autoUpgradeMinorVersion": True, | |
"settings": { | |
"fileUris": [script_url, ], # url for script to ececute | |
"commandToExecute": "sh <scipt_name>" # as example | |
}, | |
} | |
) | |
vm_command_execution.wait() | |
def list_all_vms(resourse_group_name): | |
i = 0 | |
for v in compute_client.virtual_machines.list(resourse_group_name): | |
print(v.name) | |
i += 1 | |
print(i) | |
def delete_unlocked_vhds(account_name, key): | |
for b in BlockBlobService(account_name=account_name, | |
account_key=key).list_blobs('vhds'): | |
if b.properties.lease.status == 'unlocked': | |
BlockBlobService(account_name=account_name, | |
account_key=key).delete_blob( | |
container_name="vhds", | |
blob_name=b.name) | |
print(b.name, b.properties.lease.status) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment