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)
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'
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)
def _generate_lb_id_list_from_names_or_ids(namespace, prop, child_type): raw = getattr(namespace, prop) if not raw: return raw = raw if isinstance(raw, list) else [raw] result = [] subscription = get_subscription_id() lb_name = namespace.load_balancer_name for item in raw: if is_valid_resource_id(item): result.append({'id': item}) else: if not lb_name: raise CLIError( 'Unable to process {}. Please supply a well-formed ID or ' '--lb-name.'.format(item)) else: result.append({ 'id': _generate_lb_subproperty_id( subscription=subscription, resource_group=namespace.resource_group_name, load_balancer_name=lb_name, child_type=child_type, child_name=item) }) setattr(namespace, prop, result)
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)
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'
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)
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)
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)
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)
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)
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)
def _generate_lb_id_list_from_names_or_ids(namespace, prop, child_type): raw = getattr(namespace, prop) if not raw: return raw = raw if isinstance(raw, list) else [raw] result = [] subscription = get_subscription_id() lb_name = namespace.load_balancer_name for item in raw: if is_valid_resource_id(item): result.append({'id': item}) else: if not lb_name: raise CLIError('Unable to process {}. Please supply a well-formed ID or ' '--lb-name.'.format(item)) else: result.append({'id': _generate_lb_subproperty_id( subscription=subscription, resource_group=namespace.resource_group_name, load_balancer_name=lb_name, child_type=child_type, child_name=item)}) setattr(namespace, prop, result)