summaryrefslogtreecommitdiffstats
path: root/nova/api/openstack/compute/contrib/server_password.py
blob: b4b2e04a552f4e0bdd72398ae228b9d753b17c9b (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
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright (c) 2012 Nebula, Inc.
# 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.

"""The server password extension."""

import webob

from nova.api.metadata import password
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 = extensions.extension_authorizer('compute', 'server_password')


class ServerPasswordTemplate(xmlutil.TemplateBuilder):
    def construct(self):
        root = xmlutil.TemplateElement('password', selector='password')
        root.text = unicode
        return xmlutil.MasterTemplate(root, 1)


class ServerPasswordController(object):
    """The flavor access API controller for the OpenStack API."""
    def __init__(self):
        self.compute_api = compute.API()

    def _get_instance(self, context, server_id):
        try:
            return self.compute_api.get(context, server_id)
        except exception.InstanceNotFound as exp:
            raise webob.exc.HTTPNotFound(explanation=unicode(exp))

    @wsgi.serializers(xml=ServerPasswordTemplate)
    def index(self, req, server_id):
        context = req.environ['nova.context']
        authorize(context)
        instance = self._get_instance(context, server_id)

        passw = password.extract_password(instance)
        return {'password': passw or ''}

    @wsgi.response(204)
    def delete(self, req, server_id):
        context = req.environ['nova.context']
        authorize(context)
        instance = self._get_instance(context, server_id)
        password.set_password(context, instance['uuid'], None)


class Server_password(extensions.ExtensionDescriptor):
    """Server password support"""

    name = "ServerPassword"
    alias = "os-server-password"
    namespace = ("http://docs.openstack.org/compute/ext/"
                 "server-password/api/v2")
    updated = "2012-11-29T00:00:00+00:00"

    def get_resources(self):
        resources = []

        res = extensions.ResourceExtension(
            'os-server-password',
            controller=ServerPasswordController(),
            collection_actions={'delete': 'DELETE'},
            parent=dict(member_name='server', collection_name='servers'))
        resources.append(res)

        return resources