Пример #1
0
    def handle_folding(namespace):
        base_name_val = getattr(namespace, base_name)
        type_field_val = getattr(namespace, type_field)
        parent_name_val = getattr(namespace, parent_name) if parent_name else None

        if base_name_val is None or type_field_val is not None:
            # Either no name was specified, or the user specified the type of resource
            # (i.e. new/existing/none)
            pass
        elif base_name_val in ('', '""', "''"):
            # An empty name specified - that means that we are neither referencing an existing
            # field, or the name is set to an empty string.  We check for all types of quotes
            # so scripts can run cross-platform.
            if not allow_none:
                raise CLIError('Field {} cannot be none.'.format(make_camel_case(base_name)))
            setattr(namespace, type_field, none_flag_value)
            setattr(namespace, base_name, None)
        else:
            from azure.cli.commands.client_factory import get_subscription_id
            has_parent = parent_name is not None and parent_type is not None
            if is_valid_resource_id(base_name_val):
                resource_id_parts = parse_resource_id(base_name_val)
            elif has_parent:
                resource_id_parts = dict(
                    name=parent_name_val,
                    resource_group=namespace.resource_group_name,
                    namespace=parent_type.split('/')[0],
                    type=parent_type.split('/')[1],
                    subscription=get_subscription_id(),
                    child_name=base_name_val,
                    child_type=resource_type)
            else:
                resource_id_parts = dict(
                    name=base_name_val,
                    resource_group=namespace.resource_group_name,
                    namespace=resource_type.split('/')[0],
                    type=resource_type.split('/')[1],
                    subscription=get_subscription_id())

            if resource_exists(**resource_id_parts):
                setattr(namespace, type_field, existing_id_flag_value)
                setattr(namespace, base_name, resource_id(**resource_id_parts))
            elif is_valid_resource_id(base_name_val):
                raise CLIError('ID {} does not exist. Please specify '
                               'a name to create a new resource.'.format(
                                   resource_id(**resource_id_parts)))
            else:
                setattr(namespace, type_field, new_flag_value)
Пример #2
0
def move_resource(ids, destination_group, destination_subscription_id=None):
    '''Moves resources from one resource group to another(can be under different subscription)

    :param ids: the space separated resource ids to be moved
    :param destination_group: the destination resource group name
    :param destination_subscription_id: the destination subscription identifier
    '''
    from azure.cli.commands.arm import parse_resource_id, is_valid_resource_id, resource_id

    #verify all resource ids are valid and under the same group
    resources = []
    for i in ids:
        if is_valid_resource_id(i):
            resources.append(parse_resource_id(i))
        else:
            raise CLIError('Invalid id "{}", as it has no group or subscription field'.format(i))

    if len(set([r['subscription'] for r in resources])) > 1:
        raise CLIError('All resources should be under the same subscription')
    if len(set([r['resource_group'] for r in resources])) > 1:
        raise CLIError('All resources should be under the same group')

    rcf = _resource_client_factory()
    target = resource_id(subscription=(destination_subscription_id or rcf.config.subscription_id),
                         resource_group=destination_group)

    return rcf.resources.move_resources(resources[0]['resource_group'], ids, target)
Пример #3
0
def validate_subnet_name_or_id(namespace):
    """ Validates a subnet ID or, if a name is provided, formats it as an ID. """
    if namespace.virtual_network_name is None and namespace.subnet is None:
        return
    if namespace.subnet == '':
        return

    # error if vnet-name is provided without subnet
    if namespace.virtual_network_name and not namespace.subnet:
        raise CLIError(
            'You must specify --subnet name when using --vnet-name.')

    # determine if subnet is name or ID
    is_id = is_valid_resource_id(namespace.subnet)

    # error if vnet-name is provided along with a subnet ID
    if is_id and namespace.virtual_network_name:
        raise argparse.ArgumentError(
            None, 'Please omit --vnet-name when specifying a subnet ID')
    elif not is_id and not namespace.virtual_network_name:
        raise argparse.ArgumentError(
            None, 'Please specify --vnet-name when specifying a subnet name')
    if not is_id:
        namespace.subnet = resource_id(
            subscription=get_subscription_id(),
            resource_group=namespace.resource_group_name,
            namespace='Microsoft.Network',
            type='virtualNetworks',
            name=namespace.virtual_network_name,
            child_type='subnets',
            child_name=namespace.subnet)
Пример #4
0
def validate_subnet_name_or_id(namespace):
    """ Validates a subnet ID or, if a name is provided, formats it as an ID. """
    if namespace.virtual_network_name is None and namespace.subnet is None:
        return
    if namespace.subnet == '':
        return
    # error if vnet-name is provided without subnet
    if namespace.virtual_network_name and not namespace.subnet:
        raise CLIError('You must specify --subnet name when using --vnet-name.')

    # determine if subnet is name or ID
    is_id = is_valid_resource_id(namespace.subnet)

    # error if vnet-name is provided along with a subnet ID
    if is_id and namespace.virtual_network_name:
        raise argparse.ArgumentError(None, 'Please omit --vnet-name when specifying a subnet ID')
    elif not is_id and not namespace.virtual_network_name:
        raise argparse.ArgumentError(None,
                                     'Please specify --vnet-name when specifying a subnet name')
    if not is_id:
        namespace.subnet = resource_id(
            subscription=get_subscription_id(),
            resource_group=namespace.resource_group_name,
            namespace='Microsoft.Network',
            type='virtualNetworks',
            name=namespace.virtual_network_name,
            child_type='subnets',
            child_name=namespace.subnet)
Пример #5
0
def _handle_vm_nics(namespace):
    from azure.cli.commands.client_factory import get_subscription_id
    nics_value = namespace.network_interface_ids
    nics = []

    if not nics_value:
        namespace.network_interface_type = 'new'
        return

    namespace.network_interface_type = 'existing'

    if not isinstance(nics_value, list):
        nics_value = [nics_value]

    for n in nics_value:
        nics.append({
            'id': n if '/' in n else resource_id(name=n,
                                                 resource_group=namespace.resource_group_name,
                                                 namespace='Microsoft.Network',
                                                 type='networkInterfaces',
                                                 subscription=get_subscription_id()),
            'properties': {
                'primary': nics_value[0] == n
            }
        })

    namespace.network_interface_ids = nics
    namespace.network_interface_type = 'existing'
Пример #6
0
def move_resource(ids, destination_group, destination_subscription_id=None):
    '''Moves resources from one resource group to another(can be under different subscription)

    :param ids: the space separated resource ids to be moved
    :param destination_group: the destination resource group name
    :param destination_subscription_id: the destination subscription identifier
    '''
    from azure.cli.commands.arm import resource_id

    #verify all resource ids are valid and under the same group
    resources = []
    for i in ids:
        if is_valid_resource_id(i):
            resources.append(parse_resource_id(i))
        else:
            raise CLIError('Invalid id "{}", as it has no group or subscription field'.format(i))

    if len(set([r['subscription'] for r in resources])) > 1:
        raise CLIError('All resources should be under the same subscription')
    if len(set([r['resource_group'] for r in resources])) > 1:
        raise CLIError('All resources should be under the same group')

    rcf = _resource_client_factory()
    target = resource_id(subscription=(destination_subscription_id or rcf.config.subscription_id),
                         resource_group=destination_group)

    return rcf.resources.move_resources(resources[0]['resource_group'], ids, target)
Пример #7
0
def _handle_vm_nics(namespace):
    from azure.cli.commands.client_factory import get_subscription_id
    nics_value = namespace.network_interface_ids
    nics = []

    if not nics_value:
        namespace.network_interface_type = 'new'
        return

    namespace.network_interface_type = 'existing'

    if not isinstance(nics_value, list):
        nics_value = [nics_value]

    for n in nics_value:
        nics.append({
            'id':
            n if '/' in n else resource_id(
                name=n,
                resource_group=namespace.resource_group_name,
                namespace='Microsoft.Network',
                type='networkInterfaces',
                subscription=get_subscription_id()),
            'properties': {
                'primary': nics_value[0] == n
            }
        })

    namespace.network_interface_ids = nics
    namespace.network_interface_type = 'existing'

    namespace.public_ip_address_type = 'none'
Пример #8
0
    def handle_folding(namespace):
        base_name_val = getattr(namespace, base_name)
        type_field_val = getattr(namespace, type_field)
        parent_name_val = getattr(namespace,
                                  parent_name) if parent_name else None

        if base_name_val is None or type_field_val is not None:
            # Either no name was specified, or the user specified the type of resource
            # (i.e. new/existing/none)
            pass
        elif base_name_val == '':
            # An empty name specified - that means that we are neither referencing an existing
            # field, or the name is set to an empty string
            setattr(namespace, type_field, none_flag_value)
        else:
            from azure.cli.commands.client_factory import get_subscription_id
            has_parent = parent_name is not None and parent_type is not None
            if is_valid_resource_id(base_name_val):
                resource_id_parts = parse_resource_id(base_name_val)
            elif has_parent:
                resource_id_parts = dict(
                    name=parent_name_val,
                    resource_group=namespace.resource_group_name,
                    namespace=parent_type.split('/')[0],
                    type=parent_type.split('/')[1],
                    subscription=get_subscription_id(),
                    child_name=base_name_val,
                    child_type=resource_type)
            else:
                resource_id_parts = dict(
                    name=base_name_val,
                    resource_group=namespace.resource_group_name,
                    namespace=resource_type.split('/')[0],
                    type=resource_type.split('/')[1],
                    subscription=get_subscription_id())

            if resource_exists(**resource_id_parts):
                setattr(namespace, type_field, existing_id_flag_value)
                setattr(namespace, base_name, resource_id(**resource_id_parts))
            elif is_valid_resource_id(base_name_val):
                raise CLIError('ID {} does not exist. Please specify '
                               'a name to create a new resource.'.format(
                                   resource_id(**resource_id_parts)))
            else:
                setattr(namespace, type_field, new_flag_value)
Пример #9
0
def _generate_ag_subproperty_id(namespace, child_type, child_name, subscription=None):
    return resource_id(
        subscription=subscription or get_subscription_id(),
        resource_group=namespace.resource_group_name,
        namespace='Microsoft.Network',
        type='applicationGateways',
        name=namespace.application_gateway_name,
        child_type=child_type,
        child_name=child_name)
Пример #10
0
def _generate_lb_subproperty_id(namespace, child_type, child_name, subscription=None):
    return resource_id(
        subscription=subscription or get_subscription_id(),
        resource_group=namespace.resource_group_name,
        namespace='Microsoft.Network',
        type='loadBalancers',
        name=namespace.load_balancer_name,
        child_type=child_type,
        child_name=child_name)
Пример #11
0
 def test_resource_id_simple(self):
     rg = 'lbrg'
     lb = 'mylb'
     namespace = 'Microsoft.Network'
     rtype = 'loadBalancers'
     sub = '00000000-0000-0000-0000-000000000000'
     result = resource_id(resource_group=rg, name=lb, namespace=namespace, type=rtype, subscription=sub)
     expected = '/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/lbrg/providers/Microsoft.Network/loadBalancers/mylb'
     self.assertTrue(is_valid_resource_id(expected))
     self.assertTrue(is_valid_resource_id(result))
     self.assertEqual(result, expected)
Пример #12
0
def validate_public_ip_name_or_id(namespace):
    """ Validates a public IP ID or, if a name is provided, formats it as an ID. """
    if namespace.public_ip_address:
        # determine if public_ip_address is name or ID
        is_id = is_valid_resource_id(namespace.public_ip_address)
        if not is_id:
            namespace.public_ip_address = resource_id(
                subscription=get_subscription_id(),
                resource_group=namespace.resource_group_name,
                namespace='Microsoft.Network',
                type='publicIPAddresses',
                name=namespace.public_ip_address)
Пример #13
0
def _generate_lb_subproperty_id(resource_group,
                                load_balancer_name,
                                child_type,
                                child_name,
                                subscription=None):
    return resource_id(subscription=subscription or get_subscription_id(),
                       resource_group=resource_group,
                       namespace='Microsoft.Network',
                       type='loadBalancers',
                       name=load_balancer_name,
                       child_type=child_type,
                       child_name=child_name)
Пример #14
0
def validate_public_ip_name_or_id(namespace):
    """ Validates a public IP ID or, if a name is provided, formats it as an ID. """
    if namespace.public_ip_address:
        # determine if public_ip_address is name or ID
        is_id = is_valid_resource_id(namespace.public_ip_address)
        if not is_id:
            namespace.public_ip_address = resource_id(
                subscription=get_subscription_id(),
                resource_group=namespace.resource_group_name,
                namespace='Microsoft.Network',
                type='publicIPAddresses',
                name=namespace.public_ip_address)
Пример #15
0
def validate_nsg_name_or_id(namespace):
    """ Validates a NSG ID or, if a name is provided, formats it as an ID. """
    if namespace.network_security_group:
        # determine if network_security_group is name or ID
        is_id = is_valid_resource_id(namespace.network_security_group)
        if not is_id:
            namespace.network_security_group = resource_id(
                subscription=get_subscription_id(),
                resource_group=namespace.resource_group_name,
                namespace='Microsoft.Network',
                type='networkSecurityGroups',
                name=namespace.network_security_group)
Пример #16
0
def validate_nsg_name_or_id(namespace):
    """ Validates a NSG ID or, if a name is provided, formats it as an ID. """
    if namespace.network_security_group:
        # determine if network_security_group is name or ID
        is_id = is_valid_resource_id(namespace.network_security_group)
        if not is_id:
            namespace.network_security_group = resource_id(
                subscription=get_subscription_id(),
                resource_group=namespace.resource_group_name,
                namespace='Microsoft.Network',
                type='networkSecurityGroups',
                name=namespace.network_security_group)
Пример #17
0
 def test_resource_id_complex(self):
     rg = 'lbrg'
     lb = 'mylb'
     namespace = 'Microsoft.Network'
     rtype = 'loadBalancers'
     bep = 'mybep'
     bep_type = 'backendAddressPools'
     sub = '00000000-0000-0000-0000-000000000000'
     result = resource_id(name=lb, resource_group=rg, namespace=namespace, type=rtype, subscription=sub, child_type=bep_type, child_name=bep)
     expected = '/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/lbrg/providers/Microsoft.Network/loadBalancers/mylb/backendAddressPools/mybep'
     self.assertTrue(is_valid_resource_id(expected))
     self.assertTrue(is_valid_resource_id(result))
     self.assertEqual(result, expected)
Пример #18
0
 def test_resource_id_simple(self):
     rg = 'lbrg'
     lb = 'mylb'
     namespace = 'Microsoft.Network'
     rtype = 'loadBalancers'
     sub = '00000000-0000-0000-0000-000000000000'
     result = resource_id(resource_group=rg,
                          name=lb,
                          namespace=namespace,
                          type=rtype,
                          subscription=sub)
     expected = '/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/lbrg/providers/Microsoft.Network/loadBalancers/mylb'
     self.assertTrue(is_valid_resource_id(expected))
     self.assertTrue(is_valid_resource_id(result))
     self.assertEqual(result, expected)
Пример #19
0
 def test_resource_id_complex(self):
     rg = 'lbrg'
     lb = 'mylb'
     namespace = 'Microsoft.Network'
     rtype = 'loadBalancers'
     bep = 'mybep'
     bep_type = 'backendAddressPools'
     sub = '00000000-0000-0000-0000-000000000000'
     result = resource_id(name=lb,
                          resource_group=rg,
                          namespace=namespace,
                          type=rtype,
                          subscription=sub,
                          child_type=bep_type,
                          child_name=bep)
     expected = '/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/lbrg/providers/Microsoft.Network/loadBalancers/mylb/backendAddressPools/mybep'
     self.assertTrue(is_valid_resource_id(expected))
     self.assertTrue(is_valid_resource_id(result))
     self.assertEqual(result, expected)
Пример #20
0
 def test_resource_id_with_id(self):
     rid = '/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/lbrg/providers/Microsoft.Network/loadBalancers/mylb/backendAddressPools/mybep'
     result = resource_id(**parse_resource_id(rid))
     self.assertEqual(result, rid)
Пример #21
0
 def test_resource_id_with_id(self):
     rid = '/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/lbrg/providers/Microsoft.Network/loadBalancers/mylb/backendAddressPools/mybep'
     result = resource_id(**parse_resource_id(rid))
     self.assertEqual(result, rid)