Esempio n. 1
0
def get_resource_by_name(cli_ctx, resource_name, resource_type):
    """Returns the ARM resource in the current subscription with resource_name.
    :param str resource_name: The name of resource
    :param str resource_type: The type of resource
    """
    result = get_resources_in_subscription(cli_ctx, resource_type)
    elements = [item for item in result if item.name.lower() ==
                resource_name.lower()]

    if not elements:
        from azure.cli.core._profile import Profile
        profile = Profile(cli_ctx=cli_ctx)
        message = "The resource with name '{}' and type '{}' could not be found".format(
            resource_name, resource_type)
        try:
            subscription = profile.get_subscription(
                cli_ctx.data['subscription_id'])
            raise CLIError(
                "{} in subscription '{} ({})'.".format(message, subscription['name'], subscription['id']))
        except (KeyError, TypeError):
            raise CLIError(
                "{} in the current subscription.".format(message))

    elif len(elements) == 1:
        return elements[0]
    else:
        raise CLIError(
            "More than one resources with type '{}' are found with name '{}'.".format(
                resource_type, resource_name))
Esempio n. 2
0
def _arm_get_resource_by_name(cli_ctx, resource_name, resource_type):
    """Returns the ARM resource in the current subscription with resource_name.
    :param str resource_name: The name of resource
    :param str resource_type: The type of resource
    """
    result = get_resources_in_subscription(cli_ctx, resource_type)
    elements = [item for item in result if item.name.lower() == resource_name.lower()]

    if not elements:
        from azure.cli.core._profile import Profile
        profile = Profile(cli_ctx=cli_ctx)
        message = "The resource with name '{}' and type '{}' could not be found".format(
            resource_name, resource_type)
        try:
            subscription = profile.get_subscription()
            raise CLIError("{} in subscription '{} ({})'.".format(message, subscription['name'], subscription['id']))
        except (KeyError, TypeError):
            raise CLIError("{} in the current subscription.".format(message))

    elif len(elements) == 1:
        return elements[0]
    else:
        raise CLIError(
            "More than one resources with type '{}' are found with name '{}'.".format(
                resource_type, resource_name))
Esempio n. 3
0
def arm_get_registries_in_subscription():
    '''Returns the list of container registries in the current subscription.
    '''
    result = get_resources_in_subscription(RESOURCE_TYPE)
    return [
        Registry(item.id, item.name, item.location, item.tags)
        for item in result
    ]
Esempio n. 4
0
def validate_domain_service(cmd, namespace):
    if namespace.esp and not namespace.domain:
        domain = next(iter(get_resources_in_subscription(cmd.cli_ctx, 'Microsoft.AAD/domainServices')), None)
        if domain:
            namespace.domain = domain.id
    namespace.domain = HDInsightValidator(
        resource_type='Microsoft.AAD/domainServices',
        resource_name=namespace.domain).validate(cmd, namespace)
Esempio n. 5
0
 def worker(cmd, prefix, namespace):  # pylint: disable=unused-argument
     from msrestazure.tools import parse_resource_id
     client = cf_network(cmd.cli_ctx)
     subnets = []
     vnets = get_resources_in_subscription(cmd.cli_ctx, 'Microsoft.Network/virtualNetworks')
     if namespace.vnet_name and vnets:
         vnets = [r for r in vnets if r.name.lower() == namespace.vnet_name.lower()]
         for vnet in vnets:
             vnet_resource_group = parse_resource_id(vnet.id)['resource_group']
             for subnet in client.subnets.list(
                     resource_group_name=vnet_resource_group,
                     virtual_network_name=namespace.vnet_name):
                 subnets.append(subnet.name)
     return subnets
Esempio n. 6
0
def _arm_get_resource_by_name(resource_name, resource_type):
    '''Returns the ARM resource in the current subscription with resource_name.
    :param str resource_name: The name of resource
    :param str resource_type: The type of resource
    '''
    result = get_resources_in_subscription(resource_type)
    elements = [item for item in result if item.name.lower() == resource_name.lower()]

    if len(elements) == 0:
        raise CLIError(
            'No resource with type {} can be found with name: {}'.format(
                resource_type, resource_name))
    elif len(elements) == 1:
        return elements[0]
    else:
        raise CLIError(
            'More than one resources with type {} are found with name: {}'.format(
                resource_type, resource_name))
Esempio n. 7
0
def _arm_get_resource_by_name(resource_name, resource_type):
    """Returns the ARM resource in the current subscription with resource_name.
    :param str resource_name: The name of resource
    :param str resource_type: The type of resource
    """
    result = get_resources_in_subscription(resource_type)
    elements = [item for item in result if item.name.lower() == resource_name.lower()]

    if not elements:
        raise CLIError(
            "No resource with type '{}' can be found with name '{}'.".format(
                resource_type, resource_name))
    elif len(elements) == 1:
        return elements[0]
    else:
        raise CLIError(
            "More than one resources with type '{}' are found with name '{}'.".format(
                resource_type, resource_name))
Esempio n. 8
0
def _arm_get_resource_by_name(resource_name, resource_type):
    '''Returns the ARM resource in the current subscription with resource_name.
    :param str resource_name: The name of resource
    :param str resource_type: The type of resource
    '''
    result = get_resources_in_subscription(resource_type)
    elements = [
        item for item in result if item.name.lower() == resource_name.lower()
    ]

    if len(elements) == 0:
        return None
    elif len(elements) == 1:
        return elements[0]
    else:
        raise CLIError(
            'More than one resources with type {} are found with name: {}'.
            format(resource_type, resource_name))
Esempio n. 9
0
def _get_acr_cred(cli_ctx, registry_name):
    from azure.mgmt.containerregistry import ContainerRegistryManagementClient
    from azure.cli.core.commands.parameters import get_resources_in_subscription
    from azure.cli.core.commands.client_factory import get_mgmt_service_client

    client = get_mgmt_service_client(cli_ctx, ContainerRegistryManagementClient).registries

    result = get_resources_in_subscription(cli_ctx, 'Microsoft.ContainerRegistry/registries')
    result = [item for item in result if item.name.lower() == registry_name]
    if not result or len(result) > 1:
        raise ResourceNotFoundError("No resource or more than one were found with name '{}'.".format(registry_name))
    resource_group_name = parse_resource_id(result[0].id)['resource_group']

    registry = client.get(resource_group_name, registry_name)

    if registry.admin_user_enabled:  # pylint: disable=no-member
        cred = client.list_credentials(resource_group_name, registry_name)
        return cred.username, cred.passwords[0].value, resource_group_name
    raise ResourceNotFoundError("Failed to retrieve container registry credentials. Please either provide the "
                                "credentials or run 'az acr update -n {} --admin-enabled true' to enable "
                                "admin first.".format(registry_name))
Esempio n. 10
0
 def completer(cmd, prefix, namespace, **kwargs):  # pylint: disable=unused-argument
     return [
         r.name
         for r in get_resources_in_subscription(cmd.cli_ctx, resource_type)
     ]