summaryrefslogtreecommitdiffstats
path: root/nova/api
diff options
context:
space:
mode:
authorAndrew Laski <andrew.laski@rackspace.com>2012-12-12 10:35:11 -0500
committerAndrew Laski <andrew.laski@rackspace.com>2013-02-13 14:53:43 -0500
commit9ed8d398c2232722c44ca06ea545f679d4514d43 (patch)
treede4c904d49f537ce2f8982b296461b342479b84b /nova/api
parent787e334a105c47bb387dc51c8798b4ac8e1d7bc0 (diff)
downloadnova-9ed8d398c2232722c44ca06ea545f679d4514d43.tar.gz
nova-9ed8d398c2232722c44ca06ea545f679d4514d43.tar.xz
nova-9ed8d398c2232722c44ca06ea545f679d4514d43.zip
API extension for accessing instance_actions
Adds a new API extension for accessing the recorded actions and events on an instance. Usage is documented with api samples. Additionally it modified the db api to retrieve actions by request_id since the api does not return the db id. This extension is the first consumer of that method so there's no issue of changing behaviour elsewhere. Blueprint instance-actions DocImpact Change-Id: I74109586cc762a7f51d2b114896cf071ee0671cb
Diffstat (limited to 'nova/api')
-rw-r--r--nova/api/openstack/compute/contrib/instance_actions.py128
1 files changed, 128 insertions, 0 deletions
diff --git a/nova/api/openstack/compute/contrib/instance_actions.py b/nova/api/openstack/compute/contrib/instance_actions.py
new file mode 100644
index 000000000..4ab32ad4c
--- /dev/null
+++ b/nova/api/openstack/compute/contrib/instance_actions.py
@@ -0,0 +1,128 @@
+# Copyright 2013 Rackspace Hosting
+# 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.
+
+from webob import exc
+
+from nova.api.openstack import extensions
+from nova.api.openstack import wsgi
+from nova.api.openstack import xmlutil
+from nova import compute
+from nova import db
+
+authorize_actions = extensions.extension_authorizer('compute',
+ 'instance_actions')
+authorize_events = extensions.soft_extension_authorizer('compute',
+ 'instance_actions:events')
+
+ACTION_KEYS = ['action', 'instance_uuid', 'request_id', 'user_id',
+ 'project_id', 'start_time', 'message']
+EVENT_KEYS = ['event', 'start_time', 'finish_time', 'result', 'traceback']
+
+
+def make_actions(elem):
+ for key in ACTION_KEYS:
+ elem.set(key)
+
+
+def make_action(elem):
+ for key in ACTION_KEYS:
+ elem.set(key)
+ event = xmlutil.TemplateElement('events', selector='events')
+ for key in EVENT_KEYS:
+ event.set(key)
+ elem.append(event)
+
+
+class InstanceActionsTemplate(xmlutil.TemplateBuilder):
+ def construct(self):
+ root = xmlutil.TemplateElement('instanceActions')
+ elem = xmlutil.SubTemplateElement(root, 'instanceAction',
+ selector='instanceActions')
+ make_actions(elem)
+ return xmlutil.MasterTemplate(root, 1)
+
+
+class InstanceActionTemplate(xmlutil.TemplateBuilder):
+ def construct(self):
+ root = xmlutil.TemplateElement('instanceAction',
+ selector='instanceAction')
+ make_action(root)
+ return xmlutil.MasterTemplate(root, 1)
+
+
+class InstanceActionsController(wsgi.Controller):
+
+ def __init__(self):
+ super(InstanceActionsController, self).__init__()
+ self.compute_api = compute.API()
+
+ def _format_action(self, action_raw):
+ action = {}
+ for key in ACTION_KEYS:
+ if key in action_raw:
+ action[key] = action_raw[key]
+ return action
+
+ def _format_event(self, event_raw):
+ event = {}
+ for key in EVENT_KEYS:
+ if key in event_raw:
+ event[key] = event_raw[key]
+ return event
+
+ @wsgi.serializers(xml=InstanceActionsTemplate)
+ def index(self, req, server_id):
+ """Returns the list of actions recorded for a given instance."""
+ context = req.environ["nova.context"]
+ instance = self.compute_api.get(context, server_id)
+ authorize_actions(context, target=instance)
+ actions_raw = db.actions_get(context, server_id)
+ actions = [self._format_action(action) for action in actions_raw]
+ return {'instanceActions': actions}
+
+ @wsgi.serializers(xml=InstanceActionTemplate)
+ def show(self, req, server_id, id):
+ """Return data about the given instance action."""
+ context = req.environ['nova.context']
+ instance = self.compute_api.get(context, server_id)
+ authorize_actions(context, target=instance)
+ action = db.action_get_by_request_id(context, server_id, id)
+ if action is None:
+ raise exc.HTTPNotFound()
+
+ action_id = action['id']
+ action = self._format_action(action)
+ if authorize_events(context):
+ events_raw = db.action_events_get(context, action_id)
+ action['events'] = [self._format_event(evt) for evt in events_raw]
+ return {'instanceAction': action}
+
+
+class Instance_actions(extensions.ExtensionDescriptor):
+ """View a log of actions and events taken on an instance."""
+
+ name = "InstanceActions"
+ alias = "os-instance-actions"
+ namespace = ("http://docs.openstack.org/compute/ext/"
+ "instance-actions/api/v1.1")
+ updated = "2013-02-08T00:00:00+00:00"
+
+ def get_resources(self):
+ ext = extensions.ResourceExtension('os-instance-actions',
+ InstanceActionsController(),
+ parent=dict(
+ member_name='server',
+ collection_name='servers'))
+ return [ext]