summaryrefslogtreecommitdiff
path: root/novaclient/v2/networks.py
diff options
context:
space:
mode:
Diffstat (limited to 'novaclient/v2/networks.py')
-rw-r--r--novaclient/v2/networks.py156
1 files changed, 156 insertions, 0 deletions
diff --git a/novaclient/v2/networks.py b/novaclient/v2/networks.py
new file mode 100644
index 0000000..06dee27
--- /dev/null
+++ b/novaclient/v2/networks.py
@@ -0,0 +1,156 @@
1# Copyright 2012 OpenStack Foundation
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
16"""
17Network interface.
18"""
19
20from novaclient import base
21from novaclient import exceptions
22from novaclient.i18n import _
23
24
25class Network(base.Resource):
26 """
27 A network.
28 """
29 HUMAN_ID = True
30 NAME_ATTR = "label"
31
32 def __repr__(self):
33 return "<Network: %s>" % self.label
34
35 def delete(self):
36 self.manager.delete(self)
37
38
39class NetworkManager(base.ManagerWithFind):
40 """
41 Manage :class:`Network` resources.
42 """
43 resource_class = Network
44
45 def list(self):
46 """
47 Get a list of all networks.
48
49 :rtype: list of :class:`Network`.
50 """
51 return self._list("/os-networks", "networks")
52
53 def get(self, network):
54 """
55 Get a specific network.
56
57 :param network: The ID of the :class:`Network` to get.
58 :rtype: :class:`Network`
59 """
60 return self._get("/os-networks/%s" % base.getid(network),
61 "network")
62
63 def delete(self, network):
64 """
65 Delete a specific network.
66
67 :param network: The ID of the :class:`Network` to delete.
68 """
69 self._delete("/os-networks/%s" % base.getid(network))
70
71 def create(self, **kwargs):
72 """
73 Create (allocate) a network. The following parameters are
74 optional except for label; cidr or cidr_v6 must be specified, too.
75
76 :param label: str
77 :param bridge: str
78 :param bridge_interface: str
79 :param cidr: str
80 :param cidr_v6: str
81 :param dns1: str
82 :param dns2: str
83 :param fixed_cidr: str
84 :param gateway: str
85 :param gateway_v6: str
86 :param multi_host: str
87 :param priority: str
88 :param project_id: str
89 :param vlan: int
90 :param vlan_start: int
91 :param vpn_start: int
92 :param mtu: int
93 :param enable_dhcp: int
94 :param dhcp_server: str
95 :param share_address: int
96 :param allowed_start: str
97 :param allowed_end: str
98
99 :rtype: object of :class:`Network`
100 """
101 body = {"network": kwargs}
102 return self._create('/os-networks', body, 'network')
103
104 def disassociate(self, network, disassociate_host=True,
105 disassociate_project=True):
106 """
107 Disassociate a specific network from project and/or host.
108
109 :param network: The ID of the :class:`Network`.
110 :param disassociate_host: Whether to disassociate the host
111 :param disassociate_project: Whether to disassociate the project
112 """
113 if disassociate_host and disassociate_project:
114 body = {"disassociate": None}
115 elif disassociate_project:
116 body = {"disassociate_project": None}
117 elif disassociate_host:
118 body = {"disassociate_host": None}
119 else:
120 raise exceptions.CommandError(
121 _("Must disassociate either host or project or both"))
122
123 self.api.client.post("/os-networks/%s/action" %
124 base.getid(network), body=body)
125
126 def associate_host(self, network, host):
127 """
128 Associate a specific network with a host.
129
130 :param network: The ID of the :class:`Network`.
131 :param host: The name of the host to associate the network with
132 """
133 self.api.client.post("/os-networks/%s/action" %
134 base.getid(network),
135 body={"associate_host": host})
136
137 def associate_project(self, network):
138 """
139 Associate a specific network with a project.
140
141 The project is defined by the project authenticated against
142
143 :param network: The ID of the :class:`Network`.
144 """
145 self.api.client.post("/os-networks/add", body={"id": network})
146
147 def add(self, network=None):
148 """
149 Associates the current project with a network. Network can be chosen
150 automatically or provided explicitly.
151
152 :param network: The ID of the :class:`Network` to associate (optional).
153 """
154 self.api.client.post(
155 "/os-networks/add",
156 body={"id": base.getid(network) if network else None})