Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
  • Loading branch information...
commit 906db3ea0f5b2bd0b37567db50509bd0dfb4d9c8 2 parents 5e6de66 + 302ebc8
@jamesls jamesls authored
View
25 boto/utils.py
@@ -312,8 +312,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
@@ -329,8 +344,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:
@@ -344,7 +359,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)
@@ -367,7 +382,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()
Please sign in to comment.
Something went wrong with that request. Please try again.