Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
Fix #61 - get_cloud_from_metadata_endpoint and verify/cert
Browse files Browse the repository at this point in the history
  • Loading branch information
lmazuel committed Dec 1, 2017
1 parent 0d9cc2d commit de102cd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 5 additions & 3 deletions msrestazure/azure_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,15 @@ def __str__(self):
sql_server_hostname='.database.cloudapi.de'))


def _populate_from_metadata_endpoint(cloud, arm_endpoint):
def _populate_from_metadata_endpoint(cloud, arm_endpoint, session=None):
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
session = requests.Session() if session is None else session
metadata_endpoint = arm_endpoint + METADATA_ENDPOINT_SUFFIX
response = requests.get(metadata_endpoint)
if response.status_code == 200:
Expand All @@ -225,7 +226,7 @@ def _populate_from_metadata_endpoint(cloud, arm_endpoint):
msg = 'Response body does not contain valid json. Error detail: {}'.format(str(err))
raise MetadataEndpointError(error_msg_fmt.format(msg))

def get_cloud_from_metadata_endpoint(arm_endpoint, name=None):
def get_cloud_from_metadata_endpoint(arm_endpoint, name=None, session=None):
"""Get a Cloud object from an ARM endpoint.
.. versionadded:: 0.4.11
Expand All @@ -238,12 +239,13 @@ def get_cloud_from_metadata_endpoint(arm_endpoint, name=None):
:param str arm_endpoint: The ARM management endpoint
:param str name: An optional name for the Cloud object. Otherwise it's the ARM endpoint
:params requests.Session session: A requests session object if you need to configure proxy, cert, etc.
:rtype Cloud:
:returns: a Cloud object
:raises: MetadataEndpointError if unable to build the Cloud object
"""
cloud = Cloud(name or arm_endpoint)
cloud.endpoints.management = arm_endpoint
cloud.endpoints.resource_manager = arm_endpoint
_populate_from_metadata_endpoint(cloud, arm_endpoint)
_populate_from_metadata_endpoint(cloud, arm_endpoint, session)
return cloud
4 changes: 3 additions & 1 deletion tests/test_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import unittest
import json
import httpretty
import requests

from msrestazure import azure_cloud

Expand Down Expand Up @@ -58,7 +59,8 @@ def test_get_cloud_from_endpoint(self):
self.assertEqual("https://graph.windows.net/", cloud.endpoints.active_directory_graph_resource_id)
self.assertEqual("https://login.windows.net", cloud.endpoints.active_directory)

cloud = azure_cloud.get_cloud_from_metadata_endpoint("https://management.azure.com", "Public Azure")
session = requests.Session()
cloud = azure_cloud.get_cloud_from_metadata_endpoint("https://management.azure.com", "Public Azure", session)
self.assertEqual("Public Azure", cloud.name)
self.assertEqual("https://management.azure.com", cloud.endpoints.management)
self.assertEqual("https://management.azure.com", cloud.endpoints.resource_manager)
Expand Down

0 comments on commit de102cd

Please sign in to comment.