summaryrefslogtreecommitdiffstats
path: root/nova/api/openstack/compute/contrib/instance_actions.py
blob: 3b15de2bac686c6c370476e4a5d7acf1c0619fbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# 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 exception

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()
        self.action_api = compute.InstanceActionAPI()

    def _format_action(self, action_raw):
        action = {}
        for key in ACTION_KEYS:
            action[key] = action_raw.get(key)
        return action

    def _format_event(self, event_raw):
        event = {}
        for key in EVENT_KEYS:
            event[key] = event_raw.get(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"]
        try:
            instance = self.compute_api.get(context, server_id)
        except exception.InstanceNotFound as err:
            raise exc.HTTPNotFound(explanation=err.format_message())
        authorize_actions(context, target=instance)
        actions_raw = self.action_api.actions_get(context, instance)
        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 = self.action_api.action_get_by_request_id(context, instance,
                                                          id)
        if action is None:
            raise exc.HTTPNotFound()

        action_id = action['id']
        action = self._format_action(action)
        if authorize_events(context):
            events_raw = self.action_api.action_events_get(context, instance,
                                                           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]