Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Abstracted #1383 & added tests. #1406

Closed
wants to merge 3 commits into from

3 participants

@toastdriven

Expands on the work done in #1383, applies it to all the metadata functions (including the one that was hardcoded regardless of the params passed in) & added tests for the new private function.

tests/unit/utils/test_utils.py
@@ -105,5 +106,23 @@ def test_string_with_numbers(self):
self.assertEqual(pythonize_name('HTTPStatus200Ok'), 'http_status_200_ok')
+class TestBuildInstanceMetadataURL(unittest.TestCase):
+ def test_normal(self):
+ # This is the all-defaults case.
+ self.assertEqual(_build_instance_metadata_url('http://169.254.169.254', 'latest', 'meta-data'), 'http://169.254.169.254/latest/meta-data/')
@jamesls Owner
jamesls added a note

pep8 < 80 chars

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jamesls jamesls closed this pull request from a commit
@jamesls jamesls Merge branch 'toastdriven-instance-metadata-urls' into develop
Closes #1406 and #1383.

* toastdriven-instance-metadata-urls:
  Made the tests comply with 80 char lengths per @jamesls's request.
  Abstracted some of #1383 & added tests.
  mod utils.py:get_instance_metadata allowing access to dynamic data
906db3e
@jamesls jamesls closed this in 906db3e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
25 boto/utils.py
@@ -310,8 +310,23 @@ def __repr__(self):
return super(LazyLoadMetadata, self).__repr__()
+def _build_instance_metadata_url(url, version, path):
+ """
+ Builds an EC2 metadata URL for fetching information about an instance.
+
+ Requires the following arguments: a URL, a version and a path.
+
+ Example:
+
+ >>> _build_instance_metadata_url('http://169.254.169.254', 'latest', 'meta-data')
+ http://169.254.169.254/latest/meta-data/
+
+ """
+ return '%s/%s/%s/' % (url, version, path)
+
+
def get_instance_metadata(version='latest', url='http://169.254.169.254',
- timeout=None, num_retries=5):
+ data='meta-data', timeout=None, num_retries=5):
"""
Returns the instance metadata as a nested Python dictionary.
Simple values (e.g. local_hostname, hostname, etc.) will be
@@ -327,8 +342,8 @@ def get_instance_metadata(version='latest', url='http://169.254.169.254',
original = socket.getdefaulttimeout()
socket.setdefaulttimeout(timeout)
try:
- return _get_instance_metadata('%s/%s/meta-data/' % (url, version),
- num_retries=num_retries)
+ metadata_url = _build_instance_metadata_url(url, version, data)
+ return _get_instance_metadata(metadata_url, num_retries=num_retries)
except urllib2.URLError, e:
return None
finally:
@@ -342,7 +357,7 @@ def get_instance_identity(version='latest', url='http://169.254.169.254',
Returns the instance identity as a nested Python dictionary.
"""
iid = {}
- base_url = 'http://169.254.169.254/latest/dynamic/instance-identity'
+ base_url = _build_instance_metadata_url(url, version, 'dynamic/instance-identity')
if timeout is not None:
original = socket.getdefaulttimeout()
socket.setdefaulttimeout(timeout)
@@ -365,7 +380,7 @@ def get_instance_identity(version='latest', url='http://169.254.169.254',
def get_instance_userdata(version='latest', sep=None,
url='http://169.254.169.254'):
- ud_url = '%s/%s/user-data' % (url, version)
+ ud_url = _build_instance_metadata_url(url, version, 'user-data')
user_data = retry_url(ud_url, retry_on_404=False)
if user_data:
if sep:
View
0  tests/unit/utils/__init__.py
No changes.
View
49 tests/unit/utils/test_utils.py
@@ -25,6 +25,7 @@
from boto.utils import Password
from boto.utils import pythonize_name
+from boto.utils import _build_instance_metadata_url
class TestPassword(unittest.TestCase):
@@ -105,5 +106,53 @@ def test_string_with_numbers(self):
self.assertEqual(pythonize_name('HTTPStatus200Ok'), 'http_status_200_ok')
+class TestBuildInstanceMetadataURL(unittest.TestCase):
+ def test_normal(self):
+ # This is the all-defaults case.
+ self.assertEqual(_build_instance_metadata_url(
+ 'http://169.254.169.254',
+ 'latest',
+ 'meta-data'
+ ),
+ 'http://169.254.169.254/latest/meta-data/'
+ )
+
+ def test_custom_path(self):
+ self.assertEqual(_build_instance_metadata_url(
+ 'http://169.254.169.254',
+ 'latest',
+ 'dynamic'
+ ),
+ 'http://169.254.169.254/latest/dynamic/'
+ )
+
+ def test_custom_version(self):
+ self.assertEqual(_build_instance_metadata_url(
+ 'http://169.254.169.254',
+ '1.0',
+ 'meta-data'
+ ),
+ 'http://169.254.169.254/1.0/meta-data/'
+ )
+
+ def test_custom_url(self):
+ self.assertEqual(_build_instance_metadata_url(
+ 'http://10.0.1.5',
+ 'latest',
+ 'meta-data'
+ ),
+ 'http://10.0.1.5/latest/meta-data/'
+ )
+
+ def test_all_custom(self):
+ self.assertEqual(_build_instance_metadata_url(
+ 'http://10.0.1.5',
+ '2013-03-22',
+ 'user-data'
+ ),
+ 'http://10.0.1.5/2013-03-22/user-data/'
+ )
+
+
if __name__ == '__main__':
unittest.main()
Something went wrong with that request. Please try again.