Adds security groups to metadata server. Also adds some basic tests for metadata code.
This commit is contained in:
@@ -166,6 +166,9 @@ class CloudController(object):
|
||||
instance_ref['id'])
|
||||
ec2_id = ec2utils.id_to_ec2_id(instance_ref['id'])
|
||||
image_ec2_id = self.image_ec2_id(instance_ref['image_ref'])
|
||||
security_groups = db.security_group_get_by_instance(ctxt,
|
||||
instance_ref['id'])
|
||||
security_groups = [x['name'] for x in security_groups]
|
||||
data = {
|
||||
'user-data': base64.b64decode(instance_ref['user_data']),
|
||||
'meta-data': {
|
||||
@@ -189,7 +192,7 @@ class CloudController(object):
|
||||
'public-ipv4': floating_ip or '',
|
||||
'public-keys': keys,
|
||||
'reservation-id': instance_ref['reservation_id'],
|
||||
'security-groups': '',
|
||||
'security-groups': security_groups,
|
||||
'mpi': mpi}}
|
||||
|
||||
for image_type in ['kernel', 'ramdisk']:
|
||||
|
||||
@@ -35,6 +35,9 @@ FLAGS = flags.FLAGS
|
||||
class MetadataRequestHandler(wsgi.Application):
|
||||
"""Serve metadata from the EC2 API."""
|
||||
|
||||
def __init__(self):
|
||||
self.cc = cloud.CloudController()
|
||||
|
||||
def print_data(self, data):
|
||||
if isinstance(data, dict):
|
||||
output = ''
|
||||
@@ -68,12 +71,11 @@ class MetadataRequestHandler(wsgi.Application):
|
||||
|
||||
@webob.dec.wsgify(RequestClass=wsgi.Request)
|
||||
def __call__(self, req):
|
||||
cc = cloud.CloudController()
|
||||
remote_address = req.remote_addr
|
||||
if FLAGS.use_forwarded_for:
|
||||
remote_address = req.headers.get('X-Forwarded-For', remote_address)
|
||||
try:
|
||||
meta_data = cc.get_metadata(remote_address)
|
||||
meta_data = self.cc.get_metadata(remote_address)
|
||||
except Exception:
|
||||
LOG.exception(_('Failed to get metadata for ip: %s'),
|
||||
remote_address)
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
||||
|
||||
# Copyright 2010 United States Government as represented by the
|
||||
# Administrator of the National Aeronautics and Space Administration.
|
||||
# All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
"""Tests for the testing the metadata code."""
|
||||
|
||||
import base64
|
||||
import httplib
|
||||
|
||||
import webob
|
||||
|
||||
from nova import test
|
||||
from nova import wsgi
|
||||
from nova.api.ec2 import metadatarequesthandler
|
||||
from nova.db.sqlalchemy import api
|
||||
|
||||
|
||||
class MetadataTestCase(test.TestCase):
|
||||
"""Test that metadata is returning proper values."""
|
||||
|
||||
def setUp(self):
|
||||
super(MetadataTestCase, self).setUp()
|
||||
self.instance = ({'id': 1,
|
||||
'project_id': 'test',
|
||||
'key_name': None,
|
||||
'host': 'test',
|
||||
'launch_index': 1,
|
||||
'instance_type': 'm1.tiny',
|
||||
'reservation_id': 'r-xxxxxxxx',
|
||||
'user_data': '',
|
||||
'image_ref': 7,
|
||||
'hostname': 'test'})
|
||||
|
||||
def instance_get(*args, **kwargs):
|
||||
return self.instance
|
||||
|
||||
def floating_get(*args, **kwargs):
|
||||
return '99.99.99.99'
|
||||
|
||||
self.stubs.Set(api, 'instance_get', instance_get)
|
||||
self.stubs.Set(api, 'fixed_ip_get_instance', instance_get)
|
||||
self.stubs.Set(api, 'instance_get_floating_address', floating_get)
|
||||
self.app = metadatarequesthandler.MetadataRequestHandler()
|
||||
|
||||
def request(self, relative_url):
|
||||
request = webob.Request.blank(relative_url)
|
||||
request.remote_addr = "127.0.0.1"
|
||||
return request.get_response(self.app).body
|
||||
|
||||
def test_base(self):
|
||||
self.assertEqual(self.request('/'), 'meta-data/\nuser-data')
|
||||
|
||||
def test_user_data(self):
|
||||
self.instance['user_data'] = base64.b64encode('happy')
|
||||
self.assertEqual(self.request('/user-data'), 'happy')
|
||||
|
||||
def test_security_groups(self):
|
||||
def sg_get(*args, **kwargs):
|
||||
return [{'name': 'default'}, {'name': 'other'}]
|
||||
self.stubs.Set(api, 'security_group_get_by_instance', sg_get)
|
||||
self.assertEqual(self.request('/meta-data/security-groups'),
|
||||
'default\nother')
|
||||
Reference in New Issue
Block a user