summaryrefslogtreecommitdiff
path: root/nova/api/openstack/compute/plugins/v3/consoles.py
blob: 7258681b99475407fef64465b8e267009b45ef37 (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
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2010 OpenStack Foundation
# 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.

import webob
from webob import exc

from nova.api.openstack import extensions
from nova.api.openstack import wsgi
from nova.console import api as console_api
from nova import exception


def _translate_keys(cons):
    """Coerces a console instance into proper dictionary format."""
    pool = cons['pool']
    info = {'id': cons['id'],
            'console_type': pool['console_type']}
    return info


def _translate_detail_keys(cons):
    """Coerces a console instance into proper dictionary format with detail."""
    pool = cons['pool']
    info = {'id': cons['id'],
            'console_type': pool['console_type'],
            'password': cons['password'],
            'instance_name': cons['instance_name'],
            'port': cons['port'],
            'host': pool['public_hostname']}
    return dict(console=info)


class ConsolesController(object):
    """The Consoles controller for the OpenStack API."""

    def __init__(self):
        self.console_api = console_api.API()

    @extensions.expected_errors(404)
    def index(self, req, server_id):
        """Returns a list of consoles for this instance."""
        try:
            consoles = self.console_api.get_consoles(
                req.environ['nova.context'], server_id)
        except exception.InstanceNotFound as e:
            raise exc.HTTPNotFound(explanation=e.format_message())
        return dict(consoles=[_translate_keys(console)
                              for console in consoles])

    @extensions.expected_errors(404)
    @wsgi.response(201)
    def create(self, req, server_id, body):
        """Creates a new console."""
        try:
            self.console_api.create_console(
                req.environ['nova.context'], server_id)
        except exception.InstanceNotFound as e:
            raise exc.HTTPNotFound(explanation=e.format_message())

    @extensions.expected_errors(404)
    def show(self, req, server_id, id):
        """Shows in-depth information on a specific console."""
        try:
            console = self.console_api.get_console(
                                        req.environ['nova.context'],
                                        server_id,
                                        int(id))
        except exception.ConsoleNotFound as e:
            raise exc.HTTPNotFound(explanation=e.format_message())
        return _translate_detail_keys(console)

    @extensions.expected_errors(404)
    def delete(self, req, server_id, id):
        """Deletes a console."""
        try:
            self.console_api.delete_console(req.environ['nova.context'],
                                            server_id,
                                            int(id))
        except exception.ConsoleNotFound as e:
            raise exc.HTTPNotFound(explanation=e.format_message())
        return webob.Response(status_int=202)


class Consoles(extensions.V3APIExtensionBase):
    """Consoles."""

    name = "Consoles"
    alias = "consoles"
    version = 1

    def get_resources(self):
        parent = {'member_name': 'server',
                  'collection_name': 'servers'}
        resources = [
            extensions.ResourceExtension(
                'consoles', ConsolesController(), parent=parent,
                member_name='console')]

        return resources

    def get_controller_extensions(self):
        return []