Skip to content

Commit

Permalink
Sync server_external_events v2 to v2.1 Part 1
Browse files Browse the repository at this point in the history
This syncs commit I18062b81e50c722ec96b4296ac39384493683ae3 which
was made to the v2 version of os-server-external-events but not
the v2.1 version.

Partially implements blueprint v2-on-v3-api

Change-Id: If0657744fac021f635ce292142c82239751ae2f2
  • Loading branch information
Chris Yeoh authored and Eli Qiao committed Nov 20, 2014
1 parent aebcf02 commit 8f08719
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 26 deletions.
42 changes: 29 additions & 13 deletions nova/api/openstack/compute/plugins/v3/server_external_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def create(self, req, body):
context = req.environ['nova.context']
authorize(context, action='create')

events = []
response_events = []
accepted = []
instances = {}
result = 200
Expand Down Expand Up @@ -74,8 +74,8 @@ def create(self, req, body):
raise webob.exc.HTTPBadRequest(
_('Invalid event status `%s\'') % event.status)

events.append(_event)
if event.instance_uuid not in instances:
instance = instances.get(event.instance_uuid)
if not instance:
try:
instance = objects.Instance.get_by_uuid(
context, event.instance_uuid)
Expand All @@ -88,24 +88,40 @@ def create(self, req, body):
_event['code'] = 404
result = 207

if event.instance_uuid in instances:
accepted.append(event)
_event['code'] = 200
LOG.audit(_('Create event %(name)s:%(tag)s for instance '
'%(instance_uuid)s'),
dict(event.iteritems()))
# NOTE: before accepting the event, make sure the instance
# for which the event is sent is assigned to a host; otherwise
# it will not be possible to dispatch the event
if instance:
if instance.host:
accepted.append(event)
LOG.audit(_('Creating event %(name)s:%(tag)s for instance '
'%(instance_uuid)s'),
dict(event.iteritems()))
# NOTE: as the event is processed asynchronously verify
# whether 202 is a more suitable response code than 200
_event['status'] = 'completed'
_event['code'] = 200
else:
LOG.debug("Unable to find a host for instance "
"%(instance)s. Dropping event %(event)s",
{'instance': event.instance_uuid,
'event': event.name})
_event['status'] = 'failed'
_event['code'] = 422
result = 207

response_events.append(_event)

if accepted:
self.compute_api.external_instance_event(context,
instances.values(),
accepted)
self.compute_api.external_instance_event(
context, instances.values(), accepted)
else:
msg = _('No instances found for any event')
raise webob.exc.HTTPNotFound(explanation=msg)

# FIXME(cyeoh): This needs some infrastructure support so that
# we have a general way to do this
robj = wsgi.ResponseObject({'events': events})
robj = wsgi.ResponseObject({'events': response_events})
robj._code = result
return robj

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
uuid='00000000-0000-0000-0000-000000000002', host='host1'),
'00000000-0000-0000-0000-000000000003': objects.Instance(
uuid='00000000-0000-0000-0000-000000000003', host='host2'),
'00000000-0000-0000-0000-000000000004': objects.Instance(
uuid='00000000-0000-0000-0000-000000000004', host=None),
}
fake_instance_uuids = sorted(fake_instances.keys())
MISSING_UUID = '00000000-0000-0000-0000-000000000004'
MISSING_UUID = '00000000-0000-0000-0000-000000000005'


@classmethod
Expand All @@ -48,17 +50,20 @@ def setUp(self):
super(ServerExternalEventsTest, self).setUp()
self.api = server_external_events.ServerExternalEventsController()
self.context = context.get_admin_context()
self.default_body = {
'events': [
{'name': 'network-vif-plugged',
'tag': 'foo',
'status': 'completed',
'server_uuid': fake_instance_uuids[0]},
{'name': 'network-changed',
'status': 'completed',
'server_uuid': fake_instance_uuids[1]},
]
}
self.event_1 = {'name': 'network-vif-plugged',
'tag': 'foo',
'server_uuid': fake_instance_uuids[0]}
self.event_2 = {'name': 'network-changed',
'server_uuid': fake_instance_uuids[1]}
self.default_body = {'events': [self.event_1, self.event_2]}
self.resp_event_1 = dict(self.event_1)
self.resp_event_1['code'] = 200
self.resp_event_1['status'] = 'completed'
self.resp_event_2 = dict(self.event_2)
self.resp_event_2['code'] = 200
self.resp_event_2['status'] = 'completed'
self.default_resp_body = {'events': [self.resp_event_1,
self.resp_event_2]}

def _create_req(self, body):
req = webob.Request.blank('/v2/fake/os-server-external-events')
Expand Down Expand Up @@ -91,7 +96,7 @@ def test_create(self):
fake_instance_uuids[:2],
['network-vif-plugged',
'network-changed'])
self.assertEqual(self.default_body, result)
self.assertEqual(self.default_resp_body, result)
self.assertEqual(200, code)

def test_create_one_bad_instance(self):
Expand All @@ -105,6 +110,19 @@ def test_create_one_bad_instance(self):
self.assertEqual(404, result['events'][1]['code'])
self.assertEqual(207, code)

def test_create_event_instance_has_no_host(self):
body = self.default_body
body['events'][0]['server_uuid'] = fake_instance_uuids[-1]
req = self._create_req(body)
result, code = self._assert_call(req, body,
[fake_instance_uuids[1],
fake_instance_uuids[-1]],
['network-changed'])
self.assertEqual(422, result['events'][0]['code'])
self.assertEqual('failed', result['events'][0]['status'])
self.assertEqual(200, result['events'][1]['code'])
self.assertEqual(207, code)

def test_create_no_good_instances(self):
body = self.default_body
body['events'][0]['server_uuid'] = MISSING_UUID
Expand Down

0 comments on commit 8f08719

Please sign in to comment.