1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2011 Grid Dynamics
4
# Copyright 2011 OpenStack LLC.
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
11
# http://www.apache.org/licenses/LICENSE-2.0
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
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
30
LOG = logging.getLogger('nova.api.openstack.v2.contrib.networks')
33
def network_dict(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',
42
return dict((field, network[field]) for field in fields)
47
class NetworkController(object):
49
def __init__(self, network_api=None):
50
self.network_api = network_api or nova.network.api.API()
52
def action(self, req, id, body):
54
'disassociate': self._disassociate,
57
for action, data in body.iteritems():
59
return _actions[action](req, id, body)
61
msg = _("Network does not have %s action") % action
62
raise exc.HTTPBadRequest(explanation=msg)
64
raise exc.HTTPBadRequest(explanation=_("Invalid request body"))
66
def _disassociate(self, request, network_id, body):
67
context = request.environ['nova.context']
68
LOG.debug(_("Disassociating network with id %s" % network_id))
70
self.network_api.disassociate(context, network_id)
71
except exception.NetworkNotFound:
72
raise exc.HTTPNotFound(_("Network not found"))
73
return exc.HTTPAccepted()
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}
81
def show(self, req, id):
82
context = req.environ['nova.context']
83
LOG.debug(_("Showing network with id %s") % id)
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)}
90
def delete(self, req, id):
91
context = req.environ['nova.context']
92
LOG.info(_("Deleting network with id %s") % id)
94
self.network_api.delete(context, id)
95
except exception.NetworkNotFound:
96
raise exc.HTTPNotFound(_("Network not found"))
97
return exc.HTTPAccepted()
99
def create(self, req, id, body=None):
100
raise exc.HTTPNotImplemented()
103
class Networks(extensions.ExtensionDescriptor):
104
"""Admin-only Network Management Extension"""
107
alias = "os-networks"
108
namespace = "http://docs.openstack.org/compute/ext/networks/api/v1.1"
109
updated = "2011-12-23 00:00:00"
112
def get_resources(self):
113
member_actions = {'action': 'POST'}
114
res = extensions.ResourceExtension('os-networks',
116
member_actions=member_actions)