示例#1
0
def datadog_monitor_create(cmd,
                           client,
                           resource_group_name,
                           monitor_name,
                           tags=None,
                           location=None,
                           type_=None,
                           datadog_organization_properties=None,
                           user_info=None,
                           sku_name=None,
                           no_wait=False):
    body = {}
    body['tags'] = tags
    body['location'] = location
    body['identity'] = {}
    body['identity']['type'] = type_
    body['properties'] = {}
    body['properties']['monitoring_status'] = "Enabled"
    body['properties']['datadog_organization_properties'] = datadog_organization_properties
    body['properties']['user_info'] = user_info
    body['sku'] = {}
    body['sku']['name'] = sku_name
    poller = sdk_no_wait(no_wait,
                         client.begin_create,
                         resource_group_name=resource_group_name,
                         monitor_name=monitor_name,
                         body=body)
    result = poller.result()
    if result and result.identity and result.identity.principal_id:
        scrope = '/subscriptions/' + result.id.split('/')[2]
        create_role_assignment(cmd, role='43d0d8ad-25c7-4714-9337-8ba259a9fe05',
                               assignee_object_id=result.identity.principal_id,
                               scope=scrope, assignee_principal_type='ServicePrincipal')
    return poller
def _create_role_assignment(cmd, quantum_workspace):
    from azure.cli.command_modules.role.custom import create_role_assignment
    retry_attempts = 0
    while retry_attempts < MAX_RETRIES_ROLE_ASSIGNMENT:
        try:
            create_role_assignment(
                cmd,
                role="Contributor",
                scope=quantum_workspace.storage_account,
                assignee=quantum_workspace.identity.principal_id)
            break
        except (CloudError, AzureInternalError) as e:
            error = str(e.args).lower()
            if (("does not exist" in error) or ("cannot find" in error)):
                print('.', end='', flush=True)
                time.sleep(POLLING_TIME_DURATION)
                retry_attempts += 1
                continue
            raise e
        except Exception as x:
            raise AzureInternalError(
                f"Role assignment encountered exception ({type(x).__name__}): {x}"
            ) from x
    if retry_attempts > 0:
        print()  # To end the line of the waiting indicators.
    if retry_attempts == MAX_RETRIES_ROLE_ASSIGNMENT:
        max_time_in_seconds = MAX_RETRIES_ROLE_ASSIGNMENT * POLLING_TIME_DURATION
        raise AzureInternalError(
            f"Role assignment could not be added to storage account {quantum_workspace.storage_account} within {max_time_in_seconds} seconds."
        )
    return quantum_workspace
示例#3
0
def _assign_owner_role_in_target_scope(
    cmd,
    role_scope,
    spn_object_id,
):
    from azure.cli.command_modules.role.custom import list_role_assignments, create_role_assignment
    role_assignments = list_role_assignments(cmd,
                                             assignee=spn_object_id,
                                             role='Owner',
                                             scope=role_scope)
    if not role_assignments:
        create_role_assignment(cmd,
                               role='Owner',
                               assignee_object_id=spn_object_id,
                               scope=role_scope,
                               assignee_principal_type='ServicePrincipal')
示例#4
0
def _add_role_assignment(role, service_principal):
    # AAD can have delays in propogating data, so sleep and retry
    sys.stdout.write('waiting for AAD role to propogate.')
    for x in range(0, 10):
        from azure.cli.command_modules.role.custom import create_role_assignment
        try:
            # TODO: break this out into a shared utility library
            create_role_assignment(role, service_principal)
            break
        except CloudError as ex:
            if ex.message == 'The role assignment already exists.':
                break
            sys.stdout.write('.')
            logger.info('%s', ex.message)
            time.sleep(2 + 2 * x)
        except:  #pylint: disable=bare-except
            sys.stdout.write('.')
            time.sleep(2 + 2 * x)
    print('done')
示例#5
0
文件: custom.py 项目: Azure/azure-cli
def _add_role_assignment(role, service_principal):
    # AAD can have delays in propogating data, so sleep and retry
    sys.stdout.write("waiting for AAD role to propogate.")
    for x in range(0, 10):
        from azure.cli.command_modules.role.custom import create_role_assignment

        try:
            # TODO: break this out into a shared utility library
            create_role_assignment(role, service_principal)
            break
        except CloudError as ex:
            if ex.message == "The role assignment already exists.":
                break
            sys.stdout.write(".")
            logger.info("%s", ex.message)
            time.sleep(2 + 2 * x)
        except:  # pylint: disable=bare-except
            sys.stdout.write(".")
            time.sleep(2 + 2 * x)
    print("done")
示例#6
0
def datadog_monitor_create(cmd,
                           client,
                           resource_group_name,
                           monitor_name,
                           location,
                           tags=None,
                           identity_type=None,
                           provisioning_state=None,
                           monitoring_status=None,
                           marketplace_subscription_status=None,
                           datadog_organization_properties=None,
                           user_info=None,
                           sku_name=None,
                           no_wait=False):
    poller = sdk_no_wait(
        no_wait,
        client.begin_create,
        resource_group_name=resource_group_name,
        monitor_name=monitor_name,
        tags=tags,
        location=location,
        type=identity_type,
        provisioning_state=provisioning_state,
        monitoring_status=monitoring_status,
        marketplace_subscription_status=marketplace_subscription_status,
        datadog_organization_properties=datadog_organization_properties,
        user_info=user_info,
        name=sku_name)
    result = poller.result()
    if result and result.principal_id:
        scrope = '/subscriptions/' + result.id.split('/')[2]
        create_role_assignment(cmd,
                               role='43d0d8ad-25c7-4714-9337-8ba259a9fe05',
                               assignee_object_id=result.principal_id,
                               scope=scrope,
                               assignee_principal_type='ServicePrincipal')
    return poller
示例#7
0
def _add_role_assignment(role, service_principal, delay=2, output=True):
    # AAD can have delays in propagating data, so sleep and retry
    if output:
        sys.stdout.write('waiting for AAD role to propagate.')
    for x in range(0, 10):
        from azure.cli.command_modules.role.custom import create_role_assignment
        try:
            # TODO: break this out into a shared utility library
            create_role_assignment(role, service_principal)
            break
        except CloudError as ex:
            if ex.message == 'The role assignment already exists.':
                break
            logger.info('%s', ex.message)
        except: #pylint: disable=bare-except
            pass
        if output:
            sys.stdout.write('.')
            time.sleep(delay + delay * x)
    else:
        return False
    if output:
        print('done')
    return True