Include project_id in instance metadata.

Instances sometimes need to know what project they're
in in order to manage permissions or naming.  This
change allows an instance to grab its project id without
needing keystone auth or other work-arounds.

Implements blueprint project-id-in-metadata
DocImpact: Update release notes and the metadata-service doc page.
Closes-Bug: #1470179
Change-Id: Idf2ea1473a545a51e1a4e50579da585d8f674f74
This commit is contained in:
andrewbogott 2015-06-30 01:07:55 -05:00
parent a0923fc4e2
commit 9260ea1493
2 changed files with 18 additions and 0 deletions

View File

@ -330,6 +330,9 @@ class InstanceMetadata(object):
if self._check_os_version(GRIZZLY, version):
metadata['random_seed'] = base64.b64encode(os.urandom(512))
if self._check_os_version(LIBERTY, version):
metadata['project_id'] = self.instance.project_id
self.set_mimetype(MIME_TYPE_APPLICATION_JSON)
return jsonutils.dumps(metadata)

View File

@ -528,6 +528,21 @@ class OpenStackMetadataTestCase(test.TestCase):
mdjson = mdinst.lookup("/openstack/2012-08-10/meta_data.json")
self.assertNotIn("random_seed", jsonutils.loads(mdjson))
def test_project_id(self):
fakes.stub_out_key_pair_funcs(self.stubs)
mdinst = fake_InstanceMetadata(self.stubs, self.instance)
# verify that 2015-10-15 has the 'project_id' field
mdjson = mdinst.lookup("/openstack/2015-10-15/meta_data.json")
mddict = jsonutils.loads(mdjson)
self.assertIn("project_id", mddict)
self.assertEqual(mddict["project_id"], self.instance.project_id)
# verify that older version do not have it
mdjson = mdinst.lookup("/openstack/2013-10-17/meta_data.json")
self.assertNotIn("project_id", jsonutils.loads(mdjson))
def test_no_dashes_in_metadata(self):
# top level entries in meta_data should not contain '-' in their name
fakes.stub_out_key_pair_funcs(self.stubs)