本文整理汇总了Python中msrestazure.tools.parse_resource_id函数的典型用法代码示例。如果您正苦于以下问题:Python parse_resource_id函数的具体用法?Python parse_resource_id怎么用?Python parse_resource_id使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parse_resource_id函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: transform_effective_nsg
def transform_effective_nsg(result):
from msrestazure.tools import parse_resource_id
transformed = []
for item in result['value']:
association = item['association']
try:
nic = parse_resource_id(association['networkInterface']['id'])['name']
except TypeError:
nic = '-'
try:
subnet = parse_resource_id(association['subnet']['id'])['name']
except TypeError:
subnet = '-'
nsg = parse_resource_id(item['networkSecurityGroup']['id'])['name']
print_names = True
for rule in item['effectiveSecurityRules']:
transformed.append(OrderedDict([
('NIC', nic if print_names else ' '),
('Subnet', subnet if print_names else ' '),
('NSG', nsg if print_names else ' '),
('Rule Name', rule['name']),
('Protocol', rule['protocol']),
('Direction', rule['direction']),
('Access', rule['access'])
]))
print_names = False
return transformed
示例2: process_nw_test_connectivity_namespace
def process_nw_test_connectivity_namespace(cmd, namespace):
from msrestazure.tools import is_valid_resource_id, resource_id, parse_resource_id
compute_client = get_mgmt_service_client(cmd.cli_ctx, 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)(cmd, namespace)
if namespace.source_resource and not is_valid_resource_id(namespace.source_resource):
namespace.source_resource = resource_id(
subscription=get_subscription_id(cmd.cli_ctx),
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(cmd.cli_ctx),
resource_group=namespace.resource_group_name,
namespace='Microsoft.Compute',
type='virtualMachines',
name=namespace.dest_resource)
示例3: validate_diagnostic_settings
def validate_diagnostic_settings(cmd, namespace):
from azure.cli.core.commands.client_factory import get_subscription_id
from msrestazure.tools import is_valid_resource_id, resource_id, parse_resource_id
from knack.util import CLIError
get_target_resource_validator('resource_uri', required=True, preserve_resource_group_parameter=True)(cmd, namespace)
if not namespace.resource_group_name:
namespace.resource_group_name = parse_resource_id(namespace.resource_uri)['resource_group']
if namespace.storage_account and not is_valid_resource_id(namespace.storage_account):
namespace.storage_account = resource_id(subscription=get_subscription_id(cmd.cli_ctx),
resource_group=namespace.resource_group_name,
namespace='microsoft.Storage',
type='storageAccounts',
name=namespace.storage_account)
if namespace.workspace and not is_valid_resource_id(namespace.workspace):
namespace.workspace = resource_id(subscription=get_subscription_id(cmd.cli_ctx),
resource_group=namespace.resource_group_name,
namespace='microsoft.OperationalInsights',
type='workspaces',
name=namespace.workspace)
if namespace.event_hub and is_valid_resource_id(namespace.event_hub):
namespace.event_hub = parse_resource_id(namespace.event_hub)['name']
if namespace.event_hub_rule:
if not is_valid_resource_id(namespace.event_hub_rule):
if not namespace.event_hub:
raise CLIError('usage error: --event-hub-rule ID | --event-hub-rule NAME --event-hub NAME')
# use value from --event-hub if the rule is a name
namespace.event_hub_rule = resource_id(
subscription=get_subscription_id(cmd.cli_ctx),
resource_group=namespace.resource_group_name,
namespace='Microsoft.EventHub',
type='namespaces',
name=namespace.event_hub,
child_type_1='AuthorizationRules',
child_name_1=namespace.event_hub_rule)
elif not namespace.event_hub:
# extract the event hub name from `--event-hub-rule` if provided as an ID
namespace.event_hub = parse_resource_id(namespace.event_hub_rule)['name']
if not any([namespace.storage_account, namespace.workspace, namespace.event_hub]):
raise CLIError(
'usage error - expected one or more: --storage-account NAME_OR_ID | --workspace NAME_OR_ID '
'| --event-hub NAME_OR_ID | --event-hub-rule ID')
try:
del namespace.resource_group_name
except AttributeError:
pass
示例4: get_storage_account_endpoint
def get_storage_account_endpoint(cmd, storage_account, is_wasb):
from ._client_factory import cf_storage
from msrestazure.tools import parse_resource_id, is_valid_resource_id
host = None
if is_valid_resource_id(storage_account):
parsed_storage_account = parse_resource_id(storage_account)
resource_group_name = parsed_storage_account['resource_group']
storage_account_name = parsed_storage_account['resource_name']
storage_client = cf_storage(cmd.cli_ctx)
storage_account = storage_client.storage_accounts.get_properties(
resource_group_name=resource_group_name,
account_name=storage_account_name)
def extract_endpoint(storage_account, is_wasb):
if not storage_account:
return None
return storage_account.primary_endpoints.dfs if not is_wasb else storage_account.primary_endpoints.blob
def extract_host(uri):
import re
return uri and re.search('//(.*)/', uri).groups()[0]
host = extract_host(extract_endpoint(storage_account, is_wasb))
return host
示例5: __call__
def __call__(self, parser, namespace, values, option_string=None):
''' The SplitAction will take the given ID parameter and spread the parsed
parts of the id into the individual backing fields.
Since the id value is expected to be of type `IterateValue`, all the backing
(dest) fields will also be of type `IterateValue`
'''
from msrestazure.tools import parse_resource_id
import os
if isinstance(values, str):
values = [values]
expanded_values = []
for val in values:
try:
# support piping values from JSON. Does not require use of --query
json_vals = json.loads(val)
if not isinstance(json_vals, list):
json_vals = [json_vals]
for json_val in json_vals:
if 'id' in json_val:
expanded_values += [json_val['id']]
except ValueError:
# supports piping of --ids to the command when using TSV. Requires use of --query
expanded_values = expanded_values + val.split(os.linesep)
try:
for value in expanded_values:
parts = parse_resource_id(value)
for arg in [arg for arg in arguments.values() if arg.type.settings.get('id_part')]:
self.set_argument_value(namespace, arg, parts)
except Exception as ex:
raise ValueError(ex)
示例6: _replica_create
def _replica_create(cmd, client, resource_group_name, server_name, source_server, no_wait=False, **kwargs):
provider = 'Microsoft.DBForMySQL' if isinstance(client, MySqlServersOperations) else 'Microsoft.DBforPostgreSQL'
# set source server id
if not is_valid_resource_id(source_server):
if len(source_server.split('/')) == 1:
source_server = resource_id(subscription=get_subscription_id(cmd.cli_ctx),
resource_group=resource_group_name,
namespace=provider,
type='servers',
name=source_server)
else:
raise CLIError('The provided source-server {} is invalid.'.format(source_server))
source_server_id_parts = parse_resource_id(source_server)
try:
source_server_object = client.get(source_server_id_parts['resource_group'], source_server_id_parts['name'])
except CloudError as e:
raise CLIError('Unable to get source server: {}.'.format(str(e)))
parameters = None
if provider == 'Microsoft.DBForMySQL':
from azure.mgmt.rdbms import mysql
parameters = mysql.models.ServerForCreate(
sku=mysql.models.Sku(name=source_server_object.sku.name),
properties=mysql.models.ServerPropertiesForReplica(source_server_id=source_server),
location=source_server_object.location)
return sdk_no_wait(no_wait, client.create, resource_group_name, server_name, parameters)
示例7: check_existence
def check_existence(cli_ctx, value, resource_group, provider_namespace, resource_type,
parent_name=None, parent_type=None):
# check for name or ID and set the type flags
from azure.cli.core.commands.client_factory import get_mgmt_service_client
from msrestazure.azure_exceptions import CloudError
from msrestazure.tools import parse_resource_id
from azure.cli.core.profiles import ResourceType
resource_client = get_mgmt_service_client(cli_ctx, ResourceType.MGMT_RESOURCE_RESOURCES).resources
id_parts = parse_resource_id(value)
rg = id_parts.get('resource_group', resource_group)
ns = id_parts.get('namespace', provider_namespace)
if parent_name and parent_type:
parent_path = '{}/{}'.format(parent_type, parent_name)
resource_name = id_parts.get('child_name_1', value)
resource_type = id_parts.get('child_type_1', resource_type)
else:
parent_path = ''
resource_name = id_parts['name']
resource_type = id_parts.get('type', resource_type)
api_version = _resolve_api_version(cli_ctx, provider_namespace, resource_type, parent_path)
try:
resource_client.get(rg, ns, parent_path, resource_type, resource_name, api_version)
return True
except CloudError:
return False
示例8: build_msi_role_assignment
def build_msi_role_assignment(vm_vmss_name, vm_vmss_resource_id, role_definition_id,
role_assignment_guid, identity_scope, is_vm=True):
from msrestazure.tools import parse_resource_id
result = parse_resource_id(identity_scope)
if result.get('type'): # is a resource id?
name = '{}/Microsoft.Authorization/{}'.format(result['name'], role_assignment_guid)
assignment_type = '{}/{}/providers/roleAssignments'.format(result['namespace'], result['type'])
else:
name = role_assignment_guid
assignment_type = 'Microsoft.Authorization/roleAssignments'
# pylint: disable=line-too-long
msi_rp_api_version = '2015-08-31-PREVIEW'
return {
'name': name,
'type': assignment_type,
'apiVersion': '2015-07-01', # the minimum api-version to create the assignment
'dependsOn': [
'Microsoft.Compute/{}/{}'.format('virtualMachines' if is_vm else 'virtualMachineScaleSets', vm_vmss_name)
],
'properties': {
'roleDefinitionId': role_definition_id,
'principalId': "[reference('{}/providers/Microsoft.ManagedIdentity/Identities/default', '{}').principalId]".format(
vm_vmss_resource_id, msi_rp_api_version),
'scope': identity_scope
}
}
示例9: transform_sqlvm_output
def transform_sqlvm_output(result):
'''
Transforms the result of SQL virtual machine group to eliminate unnecessary parameters.
'''
from collections import OrderedDict
from msrestazure.tools import parse_resource_id
try:
resource_group = getattr(result, 'resource_group', None) or parse_resource_id(result.id)['resource_group']
# Create a dictionary with the relevant parameters
output = OrderedDict([('id', result.id),
('location', result.location),
('name', result.name),
('provisioningState', result.provisioning_state),
('sqlImageOffer', result.sql_image_offer),
('sqlImageSku', result.sql_image_sku),
('resourceGroup', resource_group),
('sqlServerLicenseType', result.sql_server_license_type),
('virtualMachineResourceId', result.virtual_machine_resource_id),
('tags', result.tags)])
# Note, wsfcDomainCredentials will not display
if result.sql_virtual_machine_group_resource_id is not None:
output['sqlVirtualMachineGroupResourceId'] = result.sql_virtual_machine_group_resource_id
if result.auto_patching_settings is not None:
output['autoPatchingSettings'] = format_auto_patching_settings(result.auto_patching_settings)
if result.auto_backup_settings is not None:
output['autoBackupSettings'] = format_auto_backup_settings(result.auto_backup_settings)
if result.server_configurations_management_settings is not None:
output['serverConfigurationManagementSettings'] = format_server_configuration_management_settings(result.server_configurations_management_settings)
return output
except AttributeError:
from msrest.pipeline import ClientRawResponse
# Return the response object if the formating fails
return None if isinstance(result, ClientRawResponse) else result
示例10: _validate_name_or_id
def _validate_name_or_id(
cli_ctx, resource_group_name, property_value, property_type, parent_value, parent_type):
from azure.cli.core.commands.client_factory import get_subscription_id
from msrestazure.tools import parse_resource_id, is_valid_resource_id
has_parent = parent_type is not None
if is_valid_resource_id(property_value):
resource_id_parts = parse_resource_id(property_value)
value_supplied_was_id = True
elif has_parent:
resource_id_parts = dict(
name=parent_value,
resource_group=resource_group_name,
namespace=parent_type.split('/')[0],
type=parent_type.split('/')[1],
subscription=get_subscription_id(cli_ctx),
child_name_1=property_value,
child_type_1=property_type)
value_supplied_was_id = False
else:
resource_id_parts = dict(
name=property_value,
resource_group=resource_group_name,
namespace=property_type.split('/')[0],
type=property_type.split('/')[1],
subscription=get_subscription_id(cli_ctx))
value_supplied_was_id = False
return (resource_id_parts, value_supplied_was_id)
示例11: resolve_storage_source
def resolve_storage_source(self, source):
blob_uri = None
disk = None
snapshot = None
if source.lower().endswith('.vhd'):
blob_uri = source
return (blob_uri, disk, snapshot)
tokenize = parse_resource_id(source)
if tokenize.get('type') == 'disks':
disk = source
return (blob_uri, disk, snapshot)
if tokenize.get('type') == 'snapshots':
snapshot = source
return (blob_uri, disk, snapshot)
# not a disk or snapshots
if 'type' in tokenize:
return (blob_uri, disk, snapshot)
# source can be name of snapshot or disk
snapshot_instance = self.get_snapshot(source)
if snapshot_instance:
snapshot = snapshot_instance.id
return (blob_uri, disk, snapshot)
disk_instance = self.get_disk(source)
if disk_instance:
disk = disk_instance.id
return (blob_uri, disk, snapshot)
示例12: parse_domain_name
def parse_domain_name(domain):
from msrestazure.tools import parse_resource_id, is_valid_resource_id
domain_name = None
if is_valid_resource_id(domain):
parsed_domain_id = parse_resource_id(domain)
domain_name = parsed_domain_id['resource_name']
return domain_name
示例13: process_autoscale_create_namespace
def process_autoscale_create_namespace(cmd, namespace):
from msrestazure.tools import parse_resource_id
validate_tags(namespace)
get_target_resource_validator('resource', required=True, preserve_resource_group_parameter=True)(cmd, namespace)
if not namespace.resource_group_name:
namespace.resource_group_name = parse_resource_id(namespace.resource).get('resource_group', None)
get_default_location_from_resource_group(cmd, namespace)
示例14: get_resource_type
def get_resource_type(resource_id):
parsed = parse_resource_id(resource_id)
# parse_resource_id returns dictionary with "child_type_#" to represent
# types sequence. "type" stores root type.
child_type_keys = [k for k in parsed.keys() if k.find("child_type_") != -1]
types = [parsed.get(k) for k in sorted(child_type_keys)]
types.insert(0, parsed.get('type'))
return '/'.join(types)
示例15: get_source_vm
def get_source_vm(self):
vm_resource_id = format_resource_id(self.source,
self.subscription_id,
'Microsoft.Compute',
'virtualMachines',
self.resource_group)
resource = parse_resource_id(vm_resource_id)
return self.get_vm(resource['resource_group'], resource['name']) if resource['type'] == 'virtualMachines' else None