def process_nw_test_connectivity_namespace(namespace): from azure.cli.core.commands.arm import parse_resource_id compute_client = get_mgmt_service_client(ResourceType.MGMT_COMPUTE).virtual_machines vm_name = parse_resource_id(namespace.source_resource)['name'] rg = namespace.resource_group_name or parse_resource_id(namespace.source_resource).get('resource_group', None) if not rg: raise CLIError('usage error: --source-resource ID | --source-resource NAME --resource-group NAME') vm = compute_client.get(rg, vm_name) namespace.location = vm.location # pylint: disable=no-member get_network_watcher_from_location(remove=True)(namespace) if namespace.source_resource and not is_valid_resource_id(namespace.source_resource): namespace.source_resource = resource_id( subscription=get_subscription_id(), resource_group=rg, namespace='Microsoft.Compute', type='virtualMachines', name=namespace.source_resource) if namespace.dest_resource and not is_valid_resource_id(namespace.dest_resource): namespace.dest_resource = resource_id( subscription=get_subscription_id(), resource_group=namespace.resource_group_name, namespace='Microsoft.Compute', type='virtualMachines', name=namespace.dest_resource)
def process_nw_packet_capture_create_namespace(namespace): get_network_watcher_from_vm(namespace) storage_usage = CLIError('usage error: --storage-account NAME_OR_ID [--storage-path ' 'PATH] [--file-path PATH] | --file-path PATH') if not namespace.storage_account and not namespace.file_path: raise storage_usage if namespace.storage_path and not namespace.storage_account: raise storage_usage if not is_valid_resource_id(namespace.vm): namespace.vm = resource_id( subscription=get_subscription_id(), resource_group=namespace.resource_group_name, namespace='Microsoft.Compute', type='virtualMachines', name=namespace.vm) if namespace.storage_account and not is_valid_resource_id(namespace.storage_account): namespace.storage_account = resource_id( subscription=get_subscription_id(), resource_group=namespace.resource_group_name, namespace='Microsoft.Storage', type='storageAccounts', name=namespace.storage_account) if namespace.file_path: file_path = namespace.file_path if not file_path.endswith('.cap'): raise CLIError("usage error: --file-path PATH must end with the '*.cap' extension") file_path = file_path.replace('/', '\\') namespace.file_path = file_path
def process_nw_test_connectivity_namespace(namespace): from azure.cli.core.commands.arm import parse_resource_id compute_client = get_mgmt_service_client(ResourceType.MGMT_COMPUTE).virtual_machines vm_name = parse_resource_id(namespace.source_resource)['name'] rg = namespace.resource_group_name or \ parse_resource_id(namespace.source_resource).get('resource_group', None) if not rg: raise CLIError('usage error: --source-resource ID | ' '--source-resource NAME --resource-group NAME') vm = compute_client.get(rg, vm_name) namespace.location = vm.location # pylint: disable=no-member get_network_watcher_from_location(remove=True)(namespace) if namespace.source_resource and not is_valid_resource_id(namespace.source_resource): namespace.source_resource = resource_id( subscription=get_subscription_id(), resource_group=rg, namespace='Microsoft.Compute', type='virtualMachines', name=namespace.source_resource) if namespace.dest_resource and not is_valid_resource_id(namespace.dest_resource): namespace.dest_resource = resource_id( subscription=get_subscription_id(), resource_group=namespace.resource_group_name, namespace='Microsoft.Compute', type='virtualMachines', name=namespace.dest_resource)
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 none_flag_value: 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.core.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: if not parent_name_val and base_required: raise CLIError( "Must specify '{}' when specifying '{}' name.".format( parent_option_flag or parent_name, base_name)) 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))) elif not new_flag_value: raise CLIError( 'Referenced resource {} does not exist. Please create the required ' 'resource and try again.'.format( resource_id(**resource_id_parts))) else: setattr(namespace, type_field, new_flag_value)
def validate_diagnostic_settings(namespace): from azure.cli.core.commands.client_factory import get_subscription_id resource_group_error = "--resource-group is required when name is provided for "\ "storage account or workspace or service bus namespace and rule. " if namespace.namespace or namespace.rule_name: if namespace.namespace is None: raise CLIError(resource_group_error) if namespace.rule_name is None: raise CLIError(resource_group_error) if namespace.resource_group is None: raise CLIError(resource_group_error) if not is_valid_resource_id(namespace.namespace): namespace.service_bus_rule_id = resource_id( subscription=get_subscription_id(), resource_group=namespace.resource_group, namespace='microsoft.ServiceBus', type='namespaces', name=namespace.namespace, child_type='AuthorizationRules', child_name=namespace.rule_name) else: resource_dict = parse_resource_id(namespace.namespace) namespace.service_bus_rule_id = resource_id( subscription=resource_dict['subscription'], resource_group=resource_dict['resource_group'], namespace=resource_dict['namespace'], type=resource_dict['type'], name=resource_dict['name'], child_type='AuthorizationRules', child_name=namespace.rule_name) if namespace.storage_account and not is_valid_resource_id( namespace.storage_account): if namespace.resource_group is None: raise CLIError(resource_group_error) namespace.storage_account = resource_id( subscription=get_subscription_id(), resource_group=namespace.resource_group, namespace='microsoft.Storage', type='storageAccounts', name=namespace.storage_account) if namespace.workspace and not is_valid_resource_id(namespace.workspace): if namespace.resource_group is None: raise CLIError(resource_group_error) namespace.workspace = resource_id( subscription=get_subscription_id(), resource_group=namespace.resource_group, namespace='microsoft.OperationalInsights', type='workspaces', name=namespace.workspace) _validate_tags(namespace)
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 none_flag_value: 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.core.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: if not parent_name_val and base_required: raise CLIError("Must specify '{}' when specifying '{}' name.".format( parent_option_flag or parent_name, base_name)) 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))) elif not new_flag_value: raise CLIError('Referenced resource {} does not exist. Please create the required ' 'resource and try again.'.format(resource_id(**resource_id_parts))) else: setattr(namespace, type_field, new_flag_value)
def validate_diagnostic_settings(namespace): from azure.cli.core.commands.client_factory import get_subscription_id resource_group_error = "--resource-group is required when name is provided for "\ "storage account or workspace or service bus namespace and rule. " if namespace.namespace or namespace.rule_name: if namespace.namespace is None: raise CLIError(resource_group_error) if namespace.rule_name is None: raise CLIError(resource_group_error) if namespace.resource_group is None: raise CLIError(resource_group_error) if not is_valid_resource_id(namespace.namespace): namespace.service_bus_rule_id = resource_id(subscription=get_subscription_id(), resource_group=namespace.resource_group, namespace='microsoft.ServiceBus', type='namespaces', name=namespace.namespace, child_type='AuthorizationRules', child_name=namespace.rule_name) else: resource_dict = parse_resource_id(namespace.namespace) namespace.service_bus_rule_id = resource_id(subscription=resource_dict['subscription'], resource_group=resource_dict['resource_group'], namespace=resource_dict['namespace'], type=resource_dict['type'], name=resource_dict['name'], child_type='AuthorizationRules', child_name=namespace.rule_name) if namespace.storage_account and not is_valid_resource_id(namespace.storage_account): if namespace.resource_group is None: raise CLIError(resource_group_error) namespace.storage_account = resource_id(subscription=get_subscription_id(), resource_group=namespace.resource_group, namespace='microsoft.Storage', type='storageAccounts', name=namespace.storage_account) if namespace.workspace and not is_valid_resource_id(namespace.workspace): if namespace.resource_group is None: raise CLIError(resource_group_error) namespace.workspace = resource_id(subscription=get_subscription_id(), resource_group=namespace.resource_group, namespace='microsoft.OperationalInsights', type='workspaces', name=namespace.workspace) _validate_tags(namespace)
def simple_validator(namespace): if namespace.virtual_network_name is None and namespace.subnet is None: return if namespace.subnet == '': return usage_error = ValueError( 'incorrect usage: ( --subnet ID | --subnet NAME --vnet-name NAME)') # error if vnet-name is provided without subnet if namespace.virtual_network_name and not namespace.subnet: raise usage_error # 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 usage_error elif not is_id and not namespace.virtual_network_name: raise usage_error 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 _get_resource_id(val, resource_group, resource_type, resource_namespace): from azure.cli.core.commands.client_factory import get_subscription_id if is_valid_resource_id(val): return val return resource_id(name=val, resource_group=resource_group, namespace=resource_namespace, type=resource_type, subscription=get_subscription_id())
def validate_nsg_name(namespace): from azure.cli.core.commands.client_factory import get_subscription_id vm_id = resource_id(name=namespace.vm_name, resource_group=namespace.resource_group_name, namespace='Microsoft.Compute', type='virtualMachines', subscription=get_subscription_id()) namespace.network_security_group_name = namespace.network_security_group_name \ or '{}_NSG_{}'.format(namespace.vm_name, hash_string(vm_id, length=8))
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 validate_vm_nics(namespace): from azure.cli.core.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 _validate_vm_create_nics(namespace): nics_value = namespace.nics nics = [] if not nics_value: namespace.nic_type = 'new' return 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.nics = nics namespace.nic_type = 'existing' namespace.public_ip_type = None
def simple_validator(namespace): if namespace.virtual_network_name is None and namespace.subnet is None: return if namespace.subnet == '': return usage_error = ValueError('incorrect usage: ( --subnet ID | --subnet NAME --vnet-name NAME)') # error if vnet-name is provided without subnet if namespace.virtual_network_name and not namespace.subnet: raise usage_error # 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 usage_error elif not is_id and not namespace.virtual_network_name: raise usage_error 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 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 _validate_vm_create_nics(namespace): from azure.cli.core.commands.client_factory import get_subscription_id nics_value = namespace.nics nics = [] if not nics_value: namespace.nic_type = 'new' logger.debug('new NIC will be created') return 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.nics = nics namespace.nic_type = 'existing' namespace.public_ip_type = None logger.debug('existing NIC(s) will be used')
def _validate_vm_create_nics(namespace): nics_value = namespace.nics nics = [] if not nics_value: namespace.nic_type = 'new' return 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.nics = nics namespace.nic_type = 'existing' namespace.public_ip_type = None
def _handle_vm_nics(namespace): from azure.cli.core.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 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.core.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)
def _validate_vm_create_nics(namespace): from azure.cli.core.commands.client_factory import get_subscription_id nics_value = namespace.nics nics = [] if not nics_value: namespace.nic_type = 'new' logger.debug('new NIC will be created') return 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.nics = nics namespace.nic_type = 'existing' namespace.public_ip_type = None logger.debug('existing NIC(s) will be used')
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 _validate_name_or_id(public_ip): # determine if public_ip_address is name or ID is_id = is_valid_resource_id(public_ip) return public_ip if is_id else resource_id( subscription=get_subscription_id(), resource_group=namespace.resource_group_name, namespace='Microsoft.Network', type='publicIPAddresses', name=public_ip)
def validate_route_filter(namespace): if namespace.route_filter: if not is_valid_resource_id(namespace.route_filter): namespace.route_filter = resource_id( subscription=get_subscription_id(), resource_group=namespace.resource_group_name, namespace='Microsoft.Network', type='routeFilters', name=namespace.route_filter)
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_artifacts(namespace): if namespace.artifacts: from azure.cli.core.commands.client_factory import get_subscription_id lab_resource_id = resource_id(subscription=get_subscription_id(), resource_group=namespace.resource_group, namespace='Microsoft.DevTestLab', type='labs', name=namespace.lab_name) namespace.artifacts = _update_artifacts(namespace.artifacts, lab_resource_id)
def _get_resource_id(val, resource_group, resource_type, resource_namespace): if is_valid_resource_id(val): return val else: return resource_id(name=val, resource_group=resource_group, namespace=resource_namespace, type=resource_type, subscription=get_subscription_id())
def validate_target_listener(namespace): if namespace.target_listener and not is_valid_resource_id(namespace.target_listener): namespace.target_listener = resource_id( subscription=get_subscription_id(), resource_group=namespace.resource_group_name, name=namespace.application_gateway_name, namespace='Microsoft.Network', type='applicationGateways', child_type='httpListeners', child_name=namespace.target_listener)
def process_nw_flow_log_set_namespace(namespace): if namespace.storage_account and not is_valid_resource_id(namespace.storage_account): namespace.storage_account = resource_id( subscription=get_subscription_id(), resource_group=namespace.resource_group_name, namespace='Microsoft.Storage', type='storageAccounts', name=namespace.storage_account) process_nw_flow_log_show_namespace(namespace)
def _validate_name_or_id(namespace, value, resource_type): if not is_valid_resource_id(value): subscription = getattr(namespace, 'subscription', get_subscription_id()) return resource_id(subscription=subscription, resource_group=namespace.resource_group_name, namespace='Microsoft.Network', type=resource_type, name=value) return value
def _get_nic_id(val, resource_group, subscription): if is_valid_resource_id(val): return val else: return resource_id( name=val, resource_group=resource_group, namespace='Microsoft.Network', type='networkInterfaces', subscription=subscription)
def _get_resource_id(val, resource_group, resource_type, resource_namespace): if is_valid_resource_id(val): return val else: return resource_id( name=val, resource_group=resource_group, namespace=resource_namespace, type=resource_type, subscription=get_subscription_id())
def _get_nic_id(val, resource_group, subscription): from azure.cli.core.commands.arm import is_valid_resource_id if is_valid_resource_id(val): return val else: return resource_id(name=val, resource_group=resource_group, namespace='Microsoft.Network', type='networkInterfaces', subscription=subscription)
def simple_validator(namespace): # determine if vnet is name or ID is_id = is_valid_resource_id(namespace.virtual_network) if not is_id: namespace.virtual_network = resource_id( subscription=get_subscription_id(), resource_group=namespace.resource_group_name, namespace='Microsoft.Network', type='virtualNetworks', name=namespace.virtual_network)
def _validate_name_or_id(namespace, value, resource_type): if not is_valid_resource_id(value): subscription = getattr(namespace, 'subscription', get_subscription_id()) return resource_id( subscription=subscription, resource_group=namespace.resource_group_name, namespace='Microsoft.Network', type=resource_type, name=value) return value
def validator(namespace): type_field_name = '{}_type'.format(property_name) property_val = getattr(namespace, property_name, None) parent_val = getattr(namespace, parent_name, None) if parent_name else None # Check for the different scenarios (order matters) # 1) provided value indicates None (pair of empty quotes) if property_val in ('', '""', "''") or (property_val is None and default_none): if not allow_none: raise CLIError('{} cannot be None.'.format(property_option)) setattr(namespace, type_field_name, 'none') setattr(namespace, property_name, None) if parent_name and parent_val: logger = azlogging.get_az_logger(__name__) logger.warning('Ignoring: %s %s', parent_option, parent_val) setattr(namespace, parent_name, None) return # SUCCESS # Create a resource ID we can check for existence. (resource_id_parts, value_was_id) = _validate_name_or_id( namespace.resource_group_name, property_val, property_type, parent_val, parent_type) # 2) resource exists if resource_exists(**resource_id_parts): setattr(namespace, type_field_name, 'existingId') setattr(namespace, property_name, resource_id(**resource_id_parts)) if parent_val: if value_was_id: logger = azlogging.get_az_logger(__name__) logger.warning('Ignoring: %s %s', parent_option, parent_val) setattr(namespace, parent_name, None) return # SUCCESS # if a parent name was required but not specified, raise a usage error if has_parent and not value_was_id and not parent_val and not allow_new: raise ValueError('incorrect usage: {0} ID | {0} NAME {1} NAME'.format( property_option, parent_option)) # if non-existent ID was supplied, throw error depending on whether a new resource can # be created. if value_was_id: usage_message = '{} NAME'.format(property_option) if not has_parent \ else '{} NAME [{} NAME]'.format(property_option, parent_option) action_message = 'Specify ( {} ) to create a new resource.'.format(usage_message) if \ allow_new else 'Create the required resource and try again.' raise CLIError('{} {} does not exist. {}'.format( property_name, property_val, action_message)) # 3) try to create new resource if allow_new: setattr(namespace, type_field_name, 'new') else: raise CLIError( '{} {} does not exist. Create the required resource and try again.'.format( property_name, property_val))
def simple_validator(namespace): if namespace.virtual_network: # determine if vnet is name or ID is_id = is_valid_resource_id(namespace.virtual_network) if not is_id: namespace.virtual_network = resource_id( subscription=get_subscription_id(), resource_group=namespace.resource_group_name, namespace='Microsoft.Network', type='virtualNetworks', name=namespace.virtual_network)
def simple_validator(namespace): 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 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)
def simple_validator(namespace): 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 _get_nic_id(val, resource_group, subscription): from azure.cli.core.commands.arm import is_valid_resource_id if is_valid_resource_id(val): return val else: return resource_id( name=val, resource_group=resource_group, namespace='Microsoft.Network', type='networkInterfaces', subscription=subscription)
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 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 process_nw_flow_log_set_namespace(namespace): from azure.cli.core.commands.arm import parse_resource_id if namespace.storage_account and not is_valid_resource_id(namespace.storage_account): namespace.storage_account = resource_id( subscription=get_subscription_id(), resource_group=namespace.resource_group_name, namespace='Microsoft.Storage', type='storageAccounts', name=namespace.storage_account) process_nw_flow_log_show_namespace(namespace)
def test_resource_id_with_child(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)
def process_nw_troubleshooting_start_namespace(namespace): storage_usage = CLIError('usage error: --storage-account NAME_OR_ID [--storage-path PATH]') if namespace.storage_path and not namespace.storage_account: raise storage_usage if not is_valid_resource_id(namespace.storage_account): namespace.storage_account = resource_id( subscription=get_subscription_id(), resource_group=namespace.resource_group_name, namespace='Microsoft.Storage', type='storageAccounts', name=namespace.storage_account) process_nw_troubleshooting_show_namespace(namespace)