~ubuntu-branches/ubuntu/saucy/nova/saucy-proposed

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
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright (c) 2011 X.commerce, a business unit of eBay Inc.
#
#    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 nova.api.openstack import extensions
from nova.api.openstack import wsgi
from nova.api.openstack import xmlutil
from nova import network
from nova.openstack.common import log as logging


LOG = logging.getLogger(__name__)
authorize = extensions.extension_authorizer('compute', 'floating_ip_pools')


def _translate_floating_ip_view(pool):
    return {
        'name': pool['name'],
    }


def _translate_floating_ip_pools_view(pools):
    return {
        'floating_ip_pools': [_translate_floating_ip_view(pool)
                              for pool in pools]
    }


def make_float_ip(elem):
    elem.set('name')


class FloatingIPPoolTemplate(xmlutil.TemplateBuilder):
    def construct(self):
        root = xmlutil.TemplateElement('floating_ip_pool',
                                       selector='floating_ip_pool')
        make_float_ip(root)
        return xmlutil.MasterTemplate(root, 1)


class FloatingIPPoolsTemplate(xmlutil.TemplateBuilder):
    def construct(self):
        root = xmlutil.TemplateElement('floating_ip_pools')
        elem = xmlutil.SubTemplateElement(root, 'floating_ip_pool',
                                          selector='floating_ip_pools')
        make_float_ip(elem)
        return xmlutil.MasterTemplate(root, 1)


class FloatingIPPoolsController(object):
    """The Floating IP Pool API controller for the OpenStack API."""

    def __init__(self):
        self.network_api = network.API()
        super(FloatingIPPoolsController, self).__init__()

    @wsgi.serializers(xml=FloatingIPPoolsTemplate)
    def index(self, req):
        """Return a list of pools."""
        context = req.environ['nova.context']
        authorize(context)
        pools = self.network_api.get_floating_ip_pools(context)
        return _translate_floating_ip_pools_view(pools)


class Floating_ip_pools(extensions.ExtensionDescriptor):
    """Floating IPs support."""

    name = "FloatingIpPools"
    alias = "os-floating-ip-pools"
    namespace = ("http://docs.openstack.org/compute/ext/"
                 "floating_ip_pools/api/v1.1")
    updated = "2012-01-04T00:00:00+00:00"

    def get_resources(self):
        resources = []

        res = extensions.ResourceExtension('os-floating-ip-pools',
                         FloatingIPPoolsController(),
                         member_actions={})
        resources.append(res)

        return resources