How to use the msrestazure.azure_exceptions function in msrestazure

To help you get started, we’ve selected a few msrestazure examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github juju / juju / acceptancetests / jujupy / k8s_provider / aks.py View on Github external
def get_poller_result(poller, wait=5):
    try:
        delay = wait
        n = 0
        while not poller.done():
            n += 1
            print(
                "\r\t=> Current status: {}, waiting for {} sec{}".format(poller.status(), delay, n * '.'),
                end='', flush=True,
            )
            poller.wait(timeout=delay)
        print()
        return poller.result()
    except azure_exceptions.CloudError as e:
        logger.error(str(e))
        raise e
github Azure / batch-shipyard / convoy / monitor.py View on Github external
raise RuntimeError('Role Id not found for Reader')
    # sometimes the sp created is not added to the directory in time for
    # the following call, allow some retries before giving up
    attempts = 0
    while attempts < 90:
        try:
            role_assign = auth_client.role_assignments.create(
                scope=sub_scope,
                role_assignment_name=uuid.uuid4(),
                parameters=authmodels.RoleAssignmentCreateParameters(
                    role_definition_id=cont_role,
                    principal_id=vms[0].identity.principal_id
                ),
            )
            break
        except msrestazure.azure_exceptions.CloudError:
            time.sleep(2)
            attempts += 1
            if attempts == 90:
                raise
    del attempts
    if settings.verbose(config):
        logger.debug('reader role assignment: {}'.format(role_assign))
    cont_role = None
    for role in auth_client.role_definitions.list(
            sub_scope, filter='roleName eq \'Reader and Data Access\''):
        cont_role = role.id
        break
    if cont_role is None:
        raise RuntimeError('Role Id not found for Reader and Data Access')
    role_assign = auth_client.role_assignments.create(
        scope=sub_scope,
github Azure / batch-shipyard / convoy / federation.py View on Github external
fs = settings.federation_settings(config)
    # get subscription id for msi
    sub_id = settings.credentials_management(config).subscription_id
    if util.is_none_or_empty(sub_id):
        raise ValueError('Management subscription id not specified')
    # check if cluster already exists
    logger.debug('checking if federation proxy exists')
    try:
        vm = compute_client.virtual_machines.get(
            resource_group_name=fs.resource_group,
            vm_name=settings.generate_virtual_machine_name(fs, 0)
        )
        raise RuntimeError(
            'Existing virtual machine {} found for federation proxy'.format(
                vm.id))
    except msrestazure.azure_exceptions.CloudError as e:
        if e.status_code == 404:
            pass
        else:
            raise
    # confirm before proceeding
    if not util.confirm_action(config, 'create federation proxy'):
        return
    # create resource group if it doesn't exist
    resource.create_resource_group(
        resource_client, fs.resource_group, fs.location)
    # create storage containers
    storage.create_storage_containers_nonbatch(
        blob_client, table_client, queue_client, 'federation')
    # create file share for log persistence
    bs = settings.batch_shipyard_settings(config)
    storage.create_file_share_saskey(
github Azure / batch-shipyard / convoy / monitor.py View on Github external
async_delete.result()
                logger.info('resource group {} deleted'.format(
                    ms.resource_group))
        return
    if not util.confirm_action(config, 'delete monitoring resource'):
        return
    # get vms and cache for concurent async ops
    resources = {}
    i = 0
    vm_name = settings.generate_virtual_machine_name(ms, i)
    try:
        vm = compute_client.virtual_machines.get(
            resource_group_name=ms.resource_group,
            vm_name=vm_name,
        )
    except msrestazure.azure_exceptions.CloudError as e:
        if e.status_code == 404:
            logger.warning('virtual machine {} not found'.format(vm_name))
            if generate_from_prefix:
                logger.warning(
                    'OS and data disks for this virtual machine will not '
                    'be deleted, please use "fs disks del" to delete '
                    'those resources if desired')
                resources[i] = {
                    'vm': settings.generate_virtual_machine_name(ms, i),
                    'as': None,
                    'nic': settings.generate_network_interface_name(ms, i),
                    'pip': settings.generate_public_ip_name(ms, i),
                    'subnet': None,
                    'nsg': settings.generate_network_security_group_name(ms),
                    'vnet': None,
                    'os_disk': None,
github Azure / batch-shipyard / convoy / slurm.py View on Github external
if i in role_assign_done:
                    continue
                role_assign = auth_client.role_assignments.create(
                    scope=sub_scope,
                    role_assignment_name=uuid.uuid4(),
                    parameters=authmodels.RoleAssignmentCreateParameters(
                        role_definition_id=cont_role,
                        principal_id=vms[i].identity.principal_id
                    ),
                )
                role_assign_done.add(i)
                if settings.verbose(config):
                    logger.debug('reader role assignment: {}'.format(
                        role_assign))
            break
        except msrestazure.azure_exceptions.CloudError:
            time.sleep(2)
            attempts += 1
            if attempts == 90:
                raise
    del attempts
    cont_role = None
    for role in auth_client.role_definitions.list(
            sub_scope, filter='roleName eq \'Reader and Data Access\''):
        cont_role = role.id
        break
    if cont_role is None:
        raise RuntimeError('Role Id not found for Reader and Data Access')
    attempts = 0
    role_assign_done = set()
    while attempts < 30:
        try:
github Azure / batch-shipyard / convoy / resource.py View on Github external
"""Wait on async operation result
        :param AsyncOperation self: this
        :rtype: object
        :return: result of async wait
        """
        alloc_failures = 0
        while True:
            last_status_code = None
            last_error_message = None
            if self._noop:
                return self._op  # will return None
            self._invoke()
            try:
                return self._op.result()
            except (msrest.exceptions.ClientException,
                    msrestazure.azure_exceptions.CloudError) as e:
                if e.status_code >= 400 and e.status_code < 500:
                    if not (e.status_code == 409 and self._retry_conflict):
                        logger.error('not retrying status_code={}'.format(
                            e.status_code))
                        raise
                if e.status_code == 200 and 'Allocation failed' in e.message:
                    alloc_failures += 1
                    if alloc_failures > 10:
                        raise
                self._retry_count += 1
                if (self._max_retries >= 0 and
                        self._retry_count > self._max_retries):
                    logger.error(
                        ('Ran out of retry attempts invoking {}(args={} '
                         'kwargs={}) status_code={}').format(
                             self._partial.func.__name__, self._partial.args,
github Azure / batch-shipyard / convoy / resource.py View on Github external
"""Create a network security group
    :param azure.mgmt.network.NetworkManagementClient network_client:
        network client
    :param settings.VmResource vm_resource: VM Resource
    :rtype: msrestazure.azure_operation.AzureOperationPoller
    :return: async op poller
    """
    nsg_name = settings.generate_network_security_group_name(vm_resource)
    # check and fail if nsg exists
    try:
        network_client.network_security_groups.get(
            resource_group_name=vm_resource.resource_group,
            network_security_group_name=nsg_name,
        )
        raise RuntimeError('network security group {} exists'.format(nsg_name))
    except msrestazure.azure_exceptions.CloudError as e:
        if e.status_code == 404:
            pass
        else:
            raise
    # create security rules as found in settings
    priority = 100
    security_rules = []
    for nsi in vm_resource.network_security.inbound:
        i = 0
        ir = vm_resource.network_security.inbound[nsi]
        for sap in ir.source_address_prefix:
            proto = ir.protocol.lower()
            if proto == 'tcp':
                proto = networkmodels.SecurityRuleProtocol.tcp
            elif proto == 'udp':
                proto = networkmodels.SecurityRuleProtocol.udp
github Azure / batch-shipyard / convoy / remotefs.py View on Github external
# retrieve remotefs settings
    if util.is_none_or_empty(sc_id):
        raise ValueError('storage cluster id not specified')
    rfs = settings.remotefs_settings(config, sc_id)
    # retrieve specific vm
    if cardinal is not None:
        vm_name = settings.generate_virtual_machine_name(
            rfs.storage_cluster, cardinal)
    else:
        vm_name = hostname
    try:
        vm = compute_client.virtual_machines.get(
            resource_group_name=rfs.storage_cluster.resource_group,
            vm_name=vm_name,
        )
    except msrestazure.azure_exceptions.CloudError as e:
        if e.status_code == 404:
            raise RuntimeError('virtual machine {} not found'.format(vm_name))
        else:
            raise
    # get connection ip
    if rfs.storage_cluster.public_ip.enabled:
        # get pip connected to vm
        if pip is None:
            _, pip = resource.get_nic_and_pip_from_virtual_machine(
                network_client, rfs.storage_cluster.resource_group, vm)
        ip_address = pip.ip_address
    else:
        if nic is None:
            nic, _ = resource.get_nic_and_pip_from_virtual_machine(
                network_client, rfs.storage_cluster.resource_group, vm)
        ip_address = nic.ip_configurations[0].private_ip_address
github Azure / batch-shipyard / convoy / slurm.py View on Github external
async_delete.result()
                logger.info('resource group {} deleted'.format(
                    ss[0].resource_group))
        return
    if not util.confirm_action(config, 'delete slurm controller'):
        return
    # get vms and cache for concurent async ops
    resources = {}
    for i in range(0, len(ss)):
        vm_name = settings.generate_virtual_machine_name(ss[i], i)
        try:
            vm = compute_client.virtual_machines.get(
                resource_group_name=ss[i].resource_group,
                vm_name=vm_name,
            )
        except msrestazure.azure_exceptions.CloudError as e:
            if e.status_code == 404:
                logger.warning('virtual machine {} not found'.format(vm_name))
                if generate_from_prefix:
                    logger.warning(
                        'OS and data disks for this virtual machine will not '
                        'be deleted, please use "fs disks del" to delete '
                        'those resources if desired')
                    resources[i] = {
                        'vm': settings.generate_virtual_machine_name(
                            ss[i], i),
                        'as': None,
                        'nic': settings.generate_network_interface_name(
                            ss[i], i),
                        'pip': settings.generate_public_ip_name(ss[i], i),
                        'subnet': None,
                        'nsg': settings.generate_network_security_group_name(
github Azure / batch-shipyard / convoy / federation.py View on Github external
async_delete.result()
                logger.info('resource group {} deleted'.format(
                    fs.resource_group))
        return
    if not util.confirm_action(config, 'delete federation proxy'):
        return
    # get vms and cache for concurent async ops
    resources = {}
    i = 0
    vm_name = settings.generate_virtual_machine_name(fs, i)
    try:
        vm = compute_client.virtual_machines.get(
            resource_group_name=fs.resource_group,
            vm_name=vm_name,
        )
    except msrestazure.azure_exceptions.CloudError as e:
        if e.status_code == 404:
            logger.warning('virtual machine {} not found'.format(vm_name))
            if generate_from_prefix:
                logger.warning(
                    'OS and data disks for this virtual machine will not '
                    'be deleted, please use "fs disks del" to delete '
                    'those resources if desired')
                resources[i] = {
                    'vm': settings.generate_virtual_machine_name(fs, i),
                    'as': None,
                    'nic': settings.generate_network_interface_name(fs, i),
                    'pip': settings.generate_public_ip_name(fs, i),
                    'subnet': None,
                    'nsg': settings.generate_network_security_group_name(fs),
                    'vnet': None,
                    'os_disk': None,