def delete_current_vnet(start_retry_interval=30, **kwargs): if constants.USE_EXTERNAL_RESOURCE in ctx.node.properties and ctx.node.properties[ constants.USE_EXTERNAL_RESOURCE]: ctx.logger.info( "An existing VNET was used, so there's no need to delete") return ctx.instance.runtime_properties[constants.RESOURCE_NOT_DELETED] = True resource_group_name = ctx.instance.runtime_properties[ constants.RESOURCE_GROUP_KEY] vnet_name = ctx.instance.runtime_properties[constants.VNET_KEY] headers, location, subscription_id = auth.get_credentials() try: ctx.logger.info("Deleting the virtual network: {0}".format(vnet_name)) vnet_url = constants.azure_url + '/subscriptions/' + subscription_id + '/resourceGroups/' + resource_group_name + '/providers/microsoft.network/virtualNetworks/' + vnet_name + '?api-version=' + constants.api_version_network response_vnet = requests.delete(url=vnet_url, headers=headers) return azurerequests.check_delete_response(response_vnet, start_retry_interval, 'delete_current_vnet', vnet_name, 'VNET') except: ctx.logger.info( "Virtual Network {0} could not be deleted.".format(vnet_name)) raise NonRecoverableError( "Virtual Network {0} could not be created.".format(vnet_name))
def delete_current_storage_account(start_retry_interval=30, **kwargs): if constants.USE_EXTERNAL_RESOURCE in ctx.node.properties and ctx.node.properties[ constants.USE_EXTERNAL_RESOURCE]: ctx.logger.info( "An existing storage_account was used, so there's no need to delete" ) return constants.ACCEPTED_STATUS_CODE ctx.instance.runtime_properties[constants.RESOURCE_NOT_DELETED] = True resource_group_name = ctx.instance.runtime_properties[ constants.RESOURCE_GROUP_KEY] headers, location, subscription_id = auth.get_credentials() storage_account_name = ctx.instance.runtime_properties[ constants.STORAGE_ACCOUNT_KEY] ctx.logger.info( "Deleting Storage Account {0}".format(storage_account_name)) try: storage_account_url = constants.azure_url + '/subscriptions/' + subscription_id + '/resourceGroups/' + resource_group_name + '/providers/Microsoft.Storage/storageAccounts/' + storage_account_name + '?api-version=' + constants.api_version response_sa = requests.delete(url=storage_account_url, headers=headers) return azurerequests.check_delete_response( response_sa, start_retry_interval, 'delete_current_storage_account', storage_account_name, 'storage_account') except: ctx.logger.info("Storage Account {0} could not be deleted.".format( storage_account_name)) return constants.FAILURE_CODE
def delete_current_nic(start_retry_interval=30, **kwargs): if constants.USE_EXTERNAL_RESOURCE in ctx.node.properties and ctx.node.properties[ constants.USE_EXTERNAL_RESOURCE]: ctx.logger.info( "An existing NIC was used, so there's no need to delete") return ctx.instance.runtime_properties[constants.RESOURCE_NOT_DELETED] = True headers, location, subscription_id = auth.get_credentials() resource_group_name = ctx.instance.runtime_properties[ constants.RESOURCE_GROUP_KEY] nic_name = utils.key_in_runtime(constants.NIC_KEY, ends_with_key=False, starts_with_key=True, return_value=True) try: ctx.logger.info("Deleting NIC {0}".format(nic_name)) nic_url = constants.azure_url + "/subscriptions/" + subscription_id + "/resourceGroups/" + resource_group_name + "/providers/microsoft.network/networkInterfaces/" + nic_name + "?api-version=" + constants.api_version response_nic = requests.delete(url=nic_url, headers=headers) return azurerequests.check_delete_response(response_nic, start_retry_interval, 'delete_current_nic', nic_name, 'NIC') except: ctx.logger.info( "Network Interface Card {0} could not be deleted.".format( nic_name))
def _delete_os_disk(start_retry_interval): headers, location, subscription_id = auth.get_credentials() os_disk_name = ctx.instance.runtime_properties[constants.OS_DISK_NAME] delete_os_disk_url = ctx.instance.runtime_properties[constants.OS_DISK_URI] ctx.instance.runtime_properties[constants.RESOURCE_NOT_DELETED] = True try: ctx.logger.info("Deleting the OS Disk {0}: {1}".format(os_disk_name, delete_os_disk_url)) response_delete_os_disk = requests.delete(url=delete_os_disk_url, headers=headers) return azurerequests.check_delete_response(response_delete_os_disk, start_retry_interval, '_delete_os_disk', os_disk_name, constants.OS_DISK) except: ctx.logger.info("OS disk {0} could not be deleted".format(delete_os_disk_url))
def delete_current_virtual_machine(start_retry_interval=30, **kwargs): resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY] headers, location, subscription_id = auth.get_credentials() vm_name = ctx.instance.runtime_properties[constants.VM_KEY] ctx.instance.runtime_properties[constants.RESOURCE_NOT_DELETED] = True try: ctx.logger.info("Deleting the virtual machine: {0}".format(vm_name)) vm_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'/providers/Microsoft.Compute/virtualMachines/'+vm_name+'?validating=true&api-version='+constants.api_version response_vm = requests.delete(url=vm_url, headers=headers) return azurerequests.check_delete_response(response_vm, start_retry_interval, 'delete_current_virtual_machine', vm_name, 'VM') except: ctx.logger.info("Virtual Machine {0} could not be deleted".format(vm_name))
def _delete_data_disks(start_retry_interval): headers, location, subscription_id = auth.get_credentials() storage_account_name = ctx.instance.runtime_properties[constants.STORAGE_ACCOUNT_KEY] for curr_key in ctx.instance.runtime_properties: if curr_key.startswith(constants.DATA_DISK_KEY): disk_name = ctx.instance.runtime_properties[curr_key] ctx.logger.info("_delete_data_disks : disk_name is {0}".format(disk_name)) delete_data_disk_url = "https://{0}.blob.core.windows.net/vhds/{1}.vhd".format(storage_account_name, disk_name) ctx.instance.runtime_properties[constants.RESOURCE_NOT_DELETED] = True try: ctx.logger.info("Deleting the data Disk {0}: {1}".format(disk_name, delete_data_disk_url)) response_delete_data_disk = requests.delete(url=delete_data_disk_url, headers=headers) return azurerequests.check_delete_response(response_delete_data_disk, start_retry_interval, '_delete_data_disks', disk_name, constants.OS_DISK) except: ctx.logger.info("Data disk {0} could not be deleted".format(delete_data_disk_url))
def delete_current_virtual_machine(start_retry_interval=30, **kwargs): resource_group_name = ctx.instance.runtime_properties[ constants.RESOURCE_GROUP_KEY] headers, location, subscription_id = auth.get_credentials() vm_name = ctx.instance.runtime_properties[constants.VM_KEY] ctx.instance.runtime_properties[constants.RESOURCE_NOT_DELETED] = True try: ctx.logger.info("Deleting the virtual machine: {0}".format(vm_name)) vm_url = constants.azure_url + '/subscriptions/' + subscription_id + '/resourceGroups/' + resource_group_name + '/providers/Microsoft.Compute/virtualMachines/' + vm_name + '?validating=true&api-version=' + constants.api_version response_vm = requests.delete(url=vm_url, headers=headers) return azurerequests.check_delete_response( response_vm, start_retry_interval, 'delete_current_virtual_machine', vm_name, 'VM') except: ctx.logger.info( "Virtual Machine {0} could not be deleted".format(vm_name))
def delete_current_nic(start_retry_interval=30, **kwargs): if constants.USE_EXTERNAL_RESOURCE in ctx.node.properties and ctx.node.properties[constants.USE_EXTERNAL_RESOURCE]: ctx.logger.info("An existing NIC was used, so there's no need to delete") return ctx.instance.runtime_properties[constants.RESOURCE_NOT_DELETED] = True headers, location, subscription_id = auth.get_credentials() resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY] nic_name = utils.key_in_runtime(constants.NIC_KEY, ends_with_key=False, starts_with_key=True, return_value=True) try: ctx.logger.info("Deleting NIC {0}".format(nic_name)) nic_url = constants.azure_url+"/subscriptions/"+subscription_id+"/resourceGroups/"+resource_group_name+"/providers/microsoft.network/networkInterfaces/"+nic_name+"?api-version="+constants.api_version response_nic = requests.delete(url=nic_url, headers=headers) return azurerequests.check_delete_response(response_nic, start_retry_interval, 'delete_current_nic', nic_name, 'NIC') except: ctx.logger.info("Network Interface Card {0} could not be deleted.".format(nic_name))
def delete_current_security_group(start_retry_interval=30, **kwargs): if constants.USE_EXTERNAL_RESOURCE in ctx.node.properties and ctx.node.properties[constants.USE_EXTERNAL_RESOURCE]: ctx.logger.info("An existing security group was used, so there's no need to delete") return ctx.instance.runtime_properties[constants.RESOURCE_NOT_DELETED] = True resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY] security_group_name = ctx.instance.runtime_properties[constants.SECURITY_GROUP_KEY] headers, location, subscription_id = auth.get_credentials() try: ctx.logger.info("Deleting Security Group: {0}".format(security_group_name)) security_group_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'/providers/microsoft.network/networkSecurityGroups/'+security_group_name+'?api-version='+constants.api_version_network response_nsg = requests.delete(url=security_group_url, headers=headers) return azurerequests.check_delete_response(response_nsg, start_retry_interval, 'delete_current_security_group', security_group_name, 'security_group') except: ctx.logger.info("Security Group {0} could not be deleted.".format(security_group_name))
def delete_current_vnet(start_retry_interval=30, **kwargs): if constants.USE_EXTERNAL_RESOURCE in ctx.node.properties and ctx.node.properties[constants.USE_EXTERNAL_RESOURCE]: ctx.logger.info("An existing VNET was used, so there's no need to delete") return ctx.instance.runtime_properties[constants.RESOURCE_NOT_DELETED] = True resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY] vnet_name = ctx.instance.runtime_properties[constants.VNET_KEY] headers, location, subscription_id = auth.get_credentials() try: ctx.logger.info("Deleting the virtual network: {0}".format(vnet_name)) vnet_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'/providers/microsoft.network/virtualNetworks/'+vnet_name+'?api-version='+constants.api_version_network response_vnet = requests.delete(url=vnet_url, headers=headers) return azurerequests.check_delete_response(response_vnet, start_retry_interval, 'delete_current_vnet', vnet_name, 'VNET') except: ctx.logger.info("Virtual Network {0} could not be deleted.".format(vnet_name)) raise NonRecoverableError("Virtual Network {0} could not be created.".format(vnet_name))
def delete_current_public_ip(start_retry_interval=30, **kwargs): if constants.USE_EXTERNAL_RESOURCE in ctx.node.properties and ctx.node.properties[constants.USE_EXTERNAL_RESOURCE]: ctx.logger.info("An existing Public IP was used, so there's no need to delete") return ctx.instance.runtime_properties[constants.RESOURCE_NOT_DELETED] = True headers, location, subscription_id = auth.get_credentials() resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY] public_ip_name = ctx.instance.runtime_properties[constants.PUBLIC_IP_KEY] try: ctx.logger.info("Deleting Public IP") public_ip_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'/providers/microsoft.network/publicIPAddresses/'+public_ip_name+'?api-version='+constants.api_version_network response_pip = requests.delete(url=public_ip_url, headers=headers) return azurerequests.check_delete_response(response_pip, start_retry_interval, 'delete_current_public_ip', public_ip_name, 'public_ip') except: ctx.logger.info("Public IP {0} could not be deleted.".format(public_ip_name))
def delete_current_storage_account(start_retry_interval=30, **kwargs): if constants.USE_EXTERNAL_RESOURCE in ctx.node.properties and ctx.node.properties[constants.USE_EXTERNAL_RESOURCE]: ctx.logger.info("An existing storage_account was used, so there's no need to delete") return constants.ACCEPTED_STATUS_CODE ctx.instance.runtime_properties[constants.RESOURCE_NOT_DELETED] = True resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY] headers, location, subscription_id = auth.get_credentials() storage_account_name = ctx.instance.runtime_properties[constants.STORAGE_ACCOUNT_KEY] ctx.logger.info("Deleting Storage Account {0}".format(storage_account_name)) try: storage_account_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'/providers/Microsoft.Storage/storageAccounts/'+storage_account_name+'?api-version='+constants.api_version response_sa = requests.delete(url=storage_account_url, headers=headers) return azurerequests.check_delete_response(response_sa, start_retry_interval, 'delete_current_storage_account', storage_account_name, 'storage_account') except: ctx.logger.info("Storage Account {0} could not be deleted.".format(storage_account_name)) return constants.FAILURE_CODE
def _delete_os_disk(start_retry_interval): headers, location, subscription_id = auth.get_credentials() os_disk_name = ctx.instance.runtime_properties[constants.OS_DISK_NAME] delete_os_disk_url = ctx.instance.runtime_properties[constants.OS_DISK_URI] ctx.instance.runtime_properties[constants.RESOURCE_NOT_DELETED] = True try: ctx.logger.info("Deleting the OS Disk {0}: {1}".format( os_disk_name, delete_os_disk_url)) response_delete_os_disk = requests.delete(url=delete_os_disk_url, headers=headers) return azurerequests.check_delete_response(response_delete_os_disk, start_retry_interval, '_delete_os_disk', os_disk_name, constants.OS_DISK) except: ctx.logger.info( "OS disk {0} could not be deleted".format(delete_os_disk_url))
def delete_current_resource_group(start_retry_interval=30, **kwargs): if constants.USE_EXTERNAL_RESOURCE in ctx.node.properties and ctx.node.properties[constants.USE_EXTERNAL_RESOURCE]: ctx.logger.info("An existing resource group was used, so there's no need to delete") return constants.ACCEPTED_STATUS_CODE ctx.instance.runtime_properties[constants.RESOURCE_NOT_DELETED] = True headers, location, subscription_id = auth.get_credentials() resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY] try: ctx.logger.info("Deleting Resource Group: {0}".format(resource_group_name)) resource_group_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'?api-version='+constants.api_version_resource_group response_rg = requests.delete(url=resource_group_url, headers=headers) return azurerequests.check_delete_response(response_rg, start_retry_interval, 'delete_current_resource_group', resource_group_name, 'resource_group_name') except: ctx.logger.info("Resource Group {0} could not be deleted.".format(resource_group_name)) return constants.FAILURE_CODE
def _delete_data_disks(start_retry_interval): headers, location, subscription_id = auth.get_credentials() storage_account_name = ctx.instance.runtime_properties[ constants.STORAGE_ACCOUNT_KEY] for curr_key in ctx.instance.runtime_properties: if curr_key.startswith(constants.DATA_DISK_KEY): disk_name = ctx.instance.runtime_properties[curr_key] ctx.logger.info( "_delete_data_disks : disk_name is {0}".format(disk_name)) delete_data_disk_url = "https://{0}.blob.core.windows.net/vhds/{1}.vhd".format( storage_account_name, disk_name) ctx.instance.runtime_properties[ constants.RESOURCE_NOT_DELETED] = True try: ctx.logger.info("Deleting the data Disk {0}: {1}".format( disk_name, delete_data_disk_url)) response_delete_data_disk = requests.delete( url=delete_data_disk_url, headers=headers) return azurerequests.check_delete_response( response_delete_data_disk, start_retry_interval, '_delete_data_disks', disk_name, constants.OS_DISK) except: ctx.logger.info("Data disk {0} could not be deleted".format( delete_data_disk_url))