~ubuntu-branches/ubuntu/quantal/nova/quantal-proposed

« back to all changes in this revision

Viewing changes to nova/api/openstack/v2/contrib/networks.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-01-20 11:54:15 UTC
  • mto: This revision was merged to the branch mainline in revision 62.
  • Revision ID: package-import@ubuntu.com-20120120115415-h2ujma9o536o1ut6
Tags: upstream-2012.1~e3~20120120.12170
ImportĀ upstreamĀ versionĀ 2012.1~e3~20120120.12170

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
 
 
3
 
# Copyright 2011 Grid Dynamics
4
 
# Copyright 2011 OpenStack LLC.
5
 
# All Rights Reserved.
6
 
#
7
 
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
8
 
#    not use this file except in compliance with the License. You may obtain
9
 
#    a copy of the License at
10
 
#
11
 
#         http://www.apache.org/licenses/LICENSE-2.0
12
 
#
13
 
#    Unless required by applicable law or agreed to in writing, software
14
 
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15
 
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16
 
#    License for the specific language governing permissions and limitations
17
 
#    under the License.
18
 
 
19
 
 
20
 
from webob import exc
21
 
 
22
 
from nova.api.openstack.v2 import extensions
23
 
from nova import exception
24
 
from nova import flags
25
 
from nova import log as logging
26
 
import nova.network.api
27
 
 
28
 
 
29
 
FLAGS = flags.FLAGS
30
 
LOG = logging.getLogger('nova.api.openstack.v2.contrib.networks')
31
 
 
32
 
 
33
 
def network_dict(network):
34
 
    if network:
35
 
        fields = ('bridge', 'vpn_public_port', 'dhcp_start',
36
 
                  'bridge_interface', 'updated_at', 'id', 'cidr_v6',
37
 
                  'deleted_at', 'gateway', 'label', 'project_id',
38
 
                  'vpn_private_address', 'deleted', 'vlan', 'broadcast',
39
 
                  'netmask', 'injected', 'cidr', 'vpn_public_address',
40
 
                  'multi_host', 'dns1', 'host', 'gateway_v6', 'netmask_v6',
41
 
                  'created_at')
42
 
        return dict((field, network[field]) for field in fields)
43
 
    else:
44
 
        return {}
45
 
 
46
 
 
47
 
class NetworkController(object):
48
 
 
49
 
    def __init__(self, network_api=None):
50
 
        self.network_api = network_api or nova.network.api.API()
51
 
 
52
 
    def action(self, req, id, body):
53
 
        _actions = {
54
 
            'disassociate': self._disassociate,
55
 
        }
56
 
 
57
 
        for action, data in body.iteritems():
58
 
            try:
59
 
                return _actions[action](req, id, body)
60
 
            except KeyError:
61
 
                msg = _("Network does not have %s action") % action
62
 
                raise exc.HTTPBadRequest(explanation=msg)
63
 
 
64
 
        raise exc.HTTPBadRequest(explanation=_("Invalid request body"))
65
 
 
66
 
    def _disassociate(self, request, network_id, body):
67
 
        context = request.environ['nova.context']
68
 
        LOG.debug(_("Disassociating network with id %s" % network_id))
69
 
        try:
70
 
            self.network_api.disassociate(context, network_id)
71
 
        except exception.NetworkNotFound:
72
 
            raise exc.HTTPNotFound(_("Network not found"))
73
 
        return exc.HTTPAccepted()
74
 
 
75
 
    def index(self, req):
76
 
        context = req.environ['nova.context']
77
 
        networks = self.network_api.get_all(context)
78
 
        result = [network_dict(net_ref) for net_ref in networks]
79
 
        return  {'networks': result}
80
 
 
81
 
    def show(self, req, id):
82
 
        context = req.environ['nova.context']
83
 
        LOG.debug(_("Showing network with id %s") % id)
84
 
        try:
85
 
            network = self.network_api.get(context, id)
86
 
        except exception.NetworkNotFound:
87
 
            raise exc.HTTPNotFound(_("Network not found"))
88
 
        return {'network': network_dict(network)}
89
 
 
90
 
    def delete(self, req, id):
91
 
        context = req.environ['nova.context']
92
 
        LOG.info(_("Deleting network with id %s") % id)
93
 
        try:
94
 
            self.network_api.delete(context, id)
95
 
        except exception.NetworkNotFound:
96
 
            raise exc.HTTPNotFound(_("Network not found"))
97
 
        return exc.HTTPAccepted()
98
 
 
99
 
    def create(self, req, id, body=None):
100
 
        raise exc.HTTPNotImplemented()
101
 
 
102
 
 
103
 
class Networks(extensions.ExtensionDescriptor):
104
 
    """Admin-only Network Management Extension"""
105
 
 
106
 
    name = "Networks"
107
 
    alias = "os-networks"
108
 
    namespace = "http://docs.openstack.org/compute/ext/networks/api/v1.1"
109
 
    updated = "2011-12-23 00:00:00"
110
 
    admin_only = True
111
 
 
112
 
    def get_resources(self):
113
 
        member_actions = {'action': 'POST'}
114
 
        res = extensions.ResourceExtension('os-networks',
115
 
                                           NetworkController(),
116
 
                                           member_actions=member_actions)
117
 
        return [res]