Skip to content

Commit

Permalink
Use msrestazure method to autodetect metadata endpoints (#5096)
Browse files Browse the repository at this point in the history
  • Loading branch information
derekbekoe authored Dec 14, 2017
1 parent d314bd4 commit 2ac8521
Showing 1 changed file with 5 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# pylint: disable=unused-argument
from knack.util import CLIError, to_snake_case

from msrestazure.azure_cloud import _populate_from_metadata_endpoint, MetadataEndpointError
from azure.cli.core.cloud import (Cloud,
get_clouds,
get_cloud,
Expand All @@ -20,10 +20,6 @@
CannotUnregisterCloudException)


# The exact API version doesn't matter too much right now. It just has to be YYYY-MM-DD format.
METADATA_ENDPOINT_SUFFIX = '/metadata/endpoints?api-version=2015-01-01'


def list_clouds(cmd):
return get_clouds(cmd.cli_ctx)

Expand All @@ -37,37 +33,6 @@ def show_cloud(cmd, cloud_name=None):
raise CLIError(e)


def _populate_from_metadata_endpoint(cloud, arm_endpoint):
endpoints_in_metadata = ['active_directory_graph_resource_id',
'active_directory_resource_id', 'active_directory']
if not arm_endpoint or all([cloud.endpoints.has_endpoint_set(n) for n in endpoints_in_metadata]):
return
try:
error_msg_fmt = "Unable to get endpoints from the cloud.\n{}"
import requests
metadata_endpoint = arm_endpoint + METADATA_ENDPOINT_SUFFIX
response = requests.get(metadata_endpoint)
if response.status_code == 200:
metadata = response.json()
if not cloud.endpoints.has_endpoint_set('gallery'):
setattr(cloud.endpoints, 'gallery', metadata.get('galleryEndpoint'))
if not cloud.endpoints.has_endpoint_set('active_directory_graph_resource_id'):
setattr(cloud.endpoints, 'active_directory_graph_resource_id', metadata.get('graphEndpoint'))
if not cloud.endpoints.has_endpoint_set('active_directory'):
setattr(cloud.endpoints, 'active_directory', metadata['authentication'].get('loginEndpoint'))
if not cloud.endpoints.has_endpoint_set('active_directory_resource_id'):
setattr(cloud.endpoints, 'active_directory_resource_id', metadata['authentication']['audiences'][0])
else:
msg = 'Server returned status code {} for {}'.format(response.status_code, metadata_endpoint)
raise CLIError(error_msg_fmt.format(msg))
except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError) as err:
msg = 'Please ensure you have network connection. Error detail: {}'.format(str(err))
raise CLIError(error_msg_fmt.format(msg))
except ValueError as err:
msg = 'Response body does not contain valid json. Error detail: {}'.format(str(err))
raise CLIError(error_msg_fmt.format(msg))


def _build_cloud(cli_ctx, cloud_name, cloud_config=None, cloud_args=None):
if cloud_config:
# Using JSON format so convert the keys to snake case
Expand All @@ -82,7 +47,10 @@ def _build_cloud(cli_ctx, cloud_name, cloud_config=None, cloud_args=None):
elif arg.startswith('suffix_') and cloud_args[arg] is not None:
setattr(c.suffixes, arg.replace('suffix_', ''), cloud_args[arg])
arm_endpoint = cloud_args.get('endpoint_resource_manager', None)
_populate_from_metadata_endpoint(c, arm_endpoint)
try:
_populate_from_metadata_endpoint(c, arm_endpoint)
except MetadataEndpointError as err:
raise CLIError(err)
required_endpoints = {'resource_manager': '--endpoint-resource-manager',
'active_directory': '--endpoint-active-directory',
'active_directory_resource_id': '--endpoint-active-directory-resource-id',
Expand Down

0 comments on commit 2ac8521

Please sign in to comment.