Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

{Network} Update index.json for connection-monitor-preview #1242

Merged
merged 9 commits into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
- name: Create a V2 connection monitor
text: >
az network watcher connection-monitor create
--location westus
--name MyV2ConnectionMonitor
--endpoint-source-name "vm01"
--endpoint-source-resource-id MyVM01ResourceID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,28 @@
from .profiles import CUSTOM_NW_CONNECTION_MONITOR


def _resolve_api_version(rcf, resource_provider_namespace, parent_resource_path, resource_type):
"""
This is copied from src/azure-cli/azure/cli/command_modules/resource/custom.py in Azure/azure-cli
"""
from azure.cli.core.parser import IncorrectUsageError

provider = rcf.providers.get(resource_provider_namespace)

# If available, we will use parent resource's api-version
resource_type_str = (parent_resource_path.split('/')[0] if parent_resource_path else resource_type)

rt = [t for t in provider.resource_types
if t.resource_type.lower() == resource_type_str.lower()]
if not rt:
raise IncorrectUsageError('Resource type {} not found.'.format(resource_type_str))
if len(rt) == 1 and rt[0].api_versions:
npv = [v for v in rt[0].api_versions if 'preview' not in v.lower()]
return npv[0] if npv else rt[0].api_versions[0]
raise IncorrectUsageError(
'API version is required and could not be resolved for resource {}'.format(resource_type))


def get_network_watcher_from_location(remove=False, watcher_name='watcher_name',
rg_name='watcher_rg'):
def _validator(cmd, namespace):
Expand Down Expand Up @@ -70,8 +92,31 @@ def process_nw_cm_v1_create_namespace(cmd, namespace):


def process_nw_cm_v2_create_namespace(cmd, namespace):
if namespace.location is None:
raise CLIError('usage error: --location is required to create a V2 connection monitor')

if namespace.location is None: # location is None only occurs in creating a V2 connection monitor
endpoint_source_resource_id = namespace.endpoint_source_resource_id

from msrestazure.tools import is_valid_resource_id, parse_resource_id
from azure.mgmt.resource import ResourceManagementClient

# parse and verify endpoint_source_resource_id
if endpoint_source_resource_id is None:
raise CLIError('usage error: '
'--location/--endpoint-source-resource-id is required to create a V2 connection monitor')
if is_valid_resource_id(endpoint_source_resource_id) is False:
raise CLIError('usage error: "{}" is not a valid resource id'.format(endpoint_source_resource_id))

resource = parse_resource_id(namespace.endpoint_source_resource_id)
resource_client = get_mgmt_service_client(cmd.cli_ctx, ResourceManagementClient)
resource_api_version = _resolve_api_version(resource_client,
resource['namespace'],
resource['resource_parent'],
resource['resource_type'])
resource = resource_client.resources.get_by_id(namespace.endpoint_source_resource_id, resource_api_version)

namespace.location = resource.location
if namespace.location is None:
raise CLIError("Can not get location from --endpoint-source-resource-id")

v2_required_parameter_set = ['endpoint_source_name', 'endpoint_dest_name', 'test_config_name']
for p in v2_required_parameter_set:
Expand Down
Loading