root / kamaki / clients / network / __init__.py @ f1e45161
History | View | Annotate | Download (14.8 kB)
1 | 25d2ff8e | Stavros Sachtouris | # Copyright 2013 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | 25d2ff8e | Stavros Sachtouris | #
|
3 | 25d2ff8e | Stavros Sachtouris | # Redistribution and use in source and binary forms, with or
|
4 | 25d2ff8e | Stavros Sachtouris | # without modification, are permitted provided that the following
|
5 | 25d2ff8e | Stavros Sachtouris | # conditions are met:
|
6 | 25d2ff8e | Stavros Sachtouris | #
|
7 | 25d2ff8e | Stavros Sachtouris | # 1. Redistributions of source code must retain the above
|
8 | 25d2ff8e | Stavros Sachtouris | # copyright notice, this list of conditions and the following
|
9 | 25d2ff8e | Stavros Sachtouris | # disclaimer.
|
10 | 25d2ff8e | Stavros Sachtouris | #
|
11 | 25d2ff8e | Stavros Sachtouris | # 2. Redistributions in binary form must reproduce the above
|
12 | 25d2ff8e | Stavros Sachtouris | # copyright notice, this list of conditions and the following
|
13 | 25d2ff8e | Stavros Sachtouris | # disclaimer in the documentation and/or other materials
|
14 | 25d2ff8e | Stavros Sachtouris | # provided with the distribution.
|
15 | 25d2ff8e | Stavros Sachtouris | #
|
16 | 25d2ff8e | Stavros Sachtouris | # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 | 25d2ff8e | Stavros Sachtouris | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 | 25d2ff8e | Stavros Sachtouris | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 | 25d2ff8e | Stavros Sachtouris | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 | 25d2ff8e | Stavros Sachtouris | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 | 25d2ff8e | Stavros Sachtouris | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 | 25d2ff8e | Stavros Sachtouris | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 | 25d2ff8e | Stavros Sachtouris | # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 | 25d2ff8e | Stavros Sachtouris | # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 | 25d2ff8e | Stavros Sachtouris | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 | 25d2ff8e | Stavros Sachtouris | # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 | 25d2ff8e | Stavros Sachtouris | # POSSIBILITY OF SUCH DAMAGE.
|
28 | 25d2ff8e | Stavros Sachtouris | #
|
29 | 25d2ff8e | Stavros Sachtouris | # The views and conclusions contained in the software and
|
30 | 25d2ff8e | Stavros Sachtouris | # documentation are those of the authors and should not be
|
31 | 25d2ff8e | Stavros Sachtouris | # interpreted as representing official policies, either expressed
|
32 | 25d2ff8e | Stavros Sachtouris | # or implied, of GRNET S.A.
|
33 | c6ebe715 | Stavros Sachtouris | |
34 | eb647cfe | Stavros Sachtouris | from kamaki.clients import ClientError, Waiter |
35 | 5c433331 | Stavros Sachtouris | from kamaki.clients.network.rest_api import NetworkRestClient |
36 | c6ebe715 | Stavros Sachtouris | |
37 | c6ebe715 | Stavros Sachtouris | |
38 | eb647cfe | Stavros Sachtouris | class NetworkClient(NetworkRestClient, Waiter): |
39 | c6ebe715 | Stavros Sachtouris | """OpenStack Network API 2.0 client"""
|
40 | 058ee9a8 | Stavros Sachtouris | |
41 | 058ee9a8 | Stavros Sachtouris | def list_networks(self): |
42 | 058ee9a8 | Stavros Sachtouris | r = self.networks_get(success=200) |
43 | 058ee9a8 | Stavros Sachtouris | return r.json['networks'] |
44 | 058ee9a8 | Stavros Sachtouris | |
45 | afba96a7 | Stavros Sachtouris | def create_network(self, name, admin_state_up=None, shared=None): |
46 | 058ee9a8 | Stavros Sachtouris | req = dict(network=dict( |
47 | ba1ab65d | Stavros Sachtouris | name=name, admin_state_up=bool(admin_state_up)))
|
48 | ba1ab65d | Stavros Sachtouris | if shared not in (None, ): |
49 | ba1ab65d | Stavros Sachtouris | req['network']['shared'] = bool(shared) |
50 | ba1ab65d | Stavros Sachtouris | r = self.networks_post(json_data=req, success=201) |
51 | 058ee9a8 | Stavros Sachtouris | return r.json['network'] |
52 | afba96a7 | Stavros Sachtouris | |
53 | ba1ab65d | Stavros Sachtouris | def create_networks(self, networks): |
54 | ba1ab65d | Stavros Sachtouris | """Atomic operation for batch network creation (all or nothing)
|
55 | de329b4c | Stavros Sachtouris |
|
56 | ba1ab65d | Stavros Sachtouris | :param networks: (list) [
|
57 | ba1ab65d | Stavros Sachtouris | {name: ..(str).., admin_state_up: ..(bool).., shared: ..(bool)..},
|
58 | ba1ab65d | Stavros Sachtouris | {name: ..(str).., admin_state_up: ..(bool).., shared: ..(bool)..}]
|
59 | afba96a7 | Stavros Sachtouris | name is mandatory, the rest is optional
|
60 | ba1ab65d | Stavros Sachtouris | e.g., create_networks([
|
61 | de329b4c | Stavros Sachtouris | {name: 'net1', admin_state_up: True},
|
62 | de329b4c | Stavros Sachtouris | {name: 'net2'}])
|
63 | ba1ab65d | Stavros Sachtouris | :returns: (list of dicts) created networks details
|
64 | afba96a7 | Stavros Sachtouris | :raises ValueError: if networks is misformated
|
65 | afba96a7 | Stavros Sachtouris | :raises ClientError: if the request failed or didn't return 201
|
66 | afba96a7 | Stavros Sachtouris | """
|
67 | afba96a7 | Stavros Sachtouris | try:
|
68 | afba96a7 | Stavros Sachtouris | msg = 'The networks parameter must be list or tuple'
|
69 | afba96a7 | Stavros Sachtouris | assert (
|
70 | afba96a7 | Stavros Sachtouris | isinstance(networks, list) or isinstance(networks, tuple)), msg |
71 | afba96a7 | Stavros Sachtouris | for network in networks: |
72 | afba96a7 | Stavros Sachtouris | msg = 'Network specification %s is not a dict' % network
|
73 | afba96a7 | Stavros Sachtouris | assert isinstance(network, dict), msg |
74 | ba1ab65d | Stavros Sachtouris | err = set(network).difference(
|
75 | ba1ab65d | Stavros Sachtouris | ('name', 'admin_state_up', 'shared')) |
76 | afba96a7 | Stavros Sachtouris | if err:
|
77 | afba96a7 | Stavros Sachtouris | raise ValueError( |
78 | afba96a7 | Stavros Sachtouris | 'Invalid key(s): %s in network specification %s' % (
|
79 | afba96a7 | Stavros Sachtouris | err, network)) |
80 | afba96a7 | Stavros Sachtouris | msg = 'Name is missing in network specification: %s' % network
|
81 | afba96a7 | Stavros Sachtouris | assert network.get('name', None), msg |
82 | afba96a7 | Stavros Sachtouris | network.setdefault('admin_state_up', False) |
83 | afba96a7 | Stavros Sachtouris | except AssertionError as ae: |
84 | afba96a7 | Stavros Sachtouris | raise ValueError('%s' % ae) |
85 | afba96a7 | Stavros Sachtouris | |
86 | afba96a7 | Stavros Sachtouris | req = dict(networks=list(networks)) |
87 | ba1ab65d | Stavros Sachtouris | r = self.networks_post(json_data=req, success=201) |
88 | afba96a7 | Stavros Sachtouris | return r.json['networks'] |
89 | afba96a7 | Stavros Sachtouris | |
90 | afba96a7 | Stavros Sachtouris | def get_network_details(self, network_id): |
91 | afba96a7 | Stavros Sachtouris | r = self.networks_get(network_id, success=200) |
92 | afba96a7 | Stavros Sachtouris | return r.json['network'] |
93 | afba96a7 | Stavros Sachtouris | |
94 | afba96a7 | Stavros Sachtouris | def update_network( |
95 | afba96a7 | Stavros Sachtouris | self, network_id, name=None, admin_state_up=None, shared=None): |
96 | afba96a7 | Stavros Sachtouris | network = dict()
|
97 | afba96a7 | Stavros Sachtouris | if name:
|
98 | afba96a7 | Stavros Sachtouris | network['name'] = name
|
99 | afba96a7 | Stavros Sachtouris | if admin_state_up not in (None, ): |
100 | afba96a7 | Stavros Sachtouris | network['admin_state_up'] = admin_state_up
|
101 | ba1ab65d | Stavros Sachtouris | if shared not in (None, ): |
102 | ba1ab65d | Stavros Sachtouris | network['shared'] = shared
|
103 | afba96a7 | Stavros Sachtouris | network = dict(network=network)
|
104 | ba1ab65d | Stavros Sachtouris | r = self.networks_put(network_id, json_data=network, success=200) |
105 | afba96a7 | Stavros Sachtouris | return r.json['network'] |
106 | afba96a7 | Stavros Sachtouris | |
107 | afba96a7 | Stavros Sachtouris | def delete_network(self, network_id): |
108 | afba96a7 | Stavros Sachtouris | r = self.networks_delete(network_id, success=204) |
109 | afba96a7 | Stavros Sachtouris | return r.headers
|
110 | ba1ab65d | Stavros Sachtouris | |
111 | ba1ab65d | Stavros Sachtouris | def list_subnets(self): |
112 | ba1ab65d | Stavros Sachtouris | r = self.subnets_get(success=200) |
113 | ba1ab65d | Stavros Sachtouris | return r.json['subnets'] |
114 | ba1ab65d | Stavros Sachtouris | |
115 | ba1ab65d | Stavros Sachtouris | def create_subnet( |
116 | ba1ab65d | Stavros Sachtouris | self, network_id, cidr,
|
117 | 25d2ff8e | Stavros Sachtouris | name=None, allocation_pools=None, gateway_ip=None, subnet_id=None, |
118 | 25d2ff8e | Stavros Sachtouris | ipv6=None, enable_dhcp=None): |
119 | ba1ab65d | Stavros Sachtouris | """
|
120 | ba1ab65d | Stavros Sachtouris | :param network_id: (str)
|
121 | ba1ab65d | Stavros Sachtouris | :param cidr: (str)
|
122 | ba1ab65d | Stavros Sachtouris |
|
123 | ba1ab65d | Stavros Sachtouris | :param name: (str) The subnet name
|
124 | ba1ab65d | Stavros Sachtouris | :param allocation_pools: (list of dicts) start/end addresses of
|
125 | ba1ab65d | Stavros Sachtouris | allocation pools: [{'start': ..., 'end': ...}, ...]
|
126 | ba1ab65d | Stavros Sachtouris | :param gateway_ip: (str)
|
127 | ba1ab65d | Stavros Sachtouris | :param subnet_id: (str)
|
128 | ba1ab65d | Stavros Sachtouris | :param ipv6: (bool) ip_version == 6 if true else 4 (default)
|
129 | ba1ab65d | Stavros Sachtouris | :param enable_dhcp: (bool)
|
130 | ba1ab65d | Stavros Sachtouris | """
|
131 | ba1ab65d | Stavros Sachtouris | subnet = dict(
|
132 | ba1ab65d | Stavros Sachtouris | network_id=network_id, cidr=cidr, ip_version=6 if ipv6 else 4) |
133 | ba1ab65d | Stavros Sachtouris | if name:
|
134 | ba1ab65d | Stavros Sachtouris | subnet['name'] = name
|
135 | ba1ab65d | Stavros Sachtouris | if allocation_pools:
|
136 | ba1ab65d | Stavros Sachtouris | subnet['allocation_pools'] = allocation_pools
|
137 | ba1ab65d | Stavros Sachtouris | if gateway_ip:
|
138 | ba1ab65d | Stavros Sachtouris | subnet['gateway_ip'] = gateway_ip
|
139 | ba1ab65d | Stavros Sachtouris | if subnet_id:
|
140 | ba1ab65d | Stavros Sachtouris | subnet['id'] = subnet_id
|
141 | ba1ab65d | Stavros Sachtouris | if enable_dhcp not in (None, ): |
142 | ba1ab65d | Stavros Sachtouris | subnet['enable_dhcp'] = bool(enable_dhcp) |
143 | ba1ab65d | Stavros Sachtouris | r = self.subnets_post(json_data=dict(subnet=subnet), success=201) |
144 | ba1ab65d | Stavros Sachtouris | return r.json['subnet'] |
145 | ba1ab65d | Stavros Sachtouris | |
146 | ba1ab65d | Stavros Sachtouris | def create_subnets(self, subnets): |
147 | ba1ab65d | Stavros Sachtouris | """Atomic operation for batch subnet creation (all or nothing)
|
148 | de329b4c | Stavros Sachtouris |
|
149 | ba1ab65d | Stavros Sachtouris | :param subnets: (list of dicts) {key: ...} with all parameters in the
|
150 | ba1ab65d | Stavros Sachtouris | method create_subnet, where method mandatory / optional paramteres
|
151 | ba1ab65d | Stavros Sachtouris | respond to mandatory / optional paramters in subnets items
|
152 | ba1ab65d | Stavros Sachtouris | :returns: (list of dicts) created subnetss details
|
153 | ba1ab65d | Stavros Sachtouris | :raises ValueError: if subnets parameter is incorrectly formated
|
154 | ba1ab65d | Stavros Sachtouris | :raises ClientError: if the request failed or didn't return 201
|
155 | ba1ab65d | Stavros Sachtouris | """
|
156 | ba1ab65d | Stavros Sachtouris | try:
|
157 | ba1ab65d | Stavros Sachtouris | msg = 'The subnets parameter must be list or tuple'
|
158 | ba1ab65d | Stavros Sachtouris | assert (
|
159 | ba1ab65d | Stavros Sachtouris | isinstance(subnets, list) or isinstance(subnets, tuple)), msg |
160 | ba1ab65d | Stavros Sachtouris | for subnet in subnets: |
161 | ba1ab65d | Stavros Sachtouris | msg = 'Subnet specification %s is not a dict' % subnet
|
162 | ba1ab65d | Stavros Sachtouris | assert isinstance(subnet, dict), msg |
163 | ba1ab65d | Stavros Sachtouris | err = set(subnet).difference((
|
164 | ba1ab65d | Stavros Sachtouris | 'network_id', 'cidr', 'name', 'allocation_pools', |
165 | 25d2ff8e | Stavros Sachtouris | 'gateway_ip', 'subnet_id', 'ipv6', 'enable_dhcp')) |
166 | ba1ab65d | Stavros Sachtouris | if err:
|
167 | ba1ab65d | Stavros Sachtouris | raise ValueError( |
168 | ba1ab65d | Stavros Sachtouris | 'Invalid key(s): %s in subnet specification %s' % (
|
169 | ba1ab65d | Stavros Sachtouris | err, subnet)) |
170 | ba1ab65d | Stavros Sachtouris | msg = 'network_id is missing in subnet spec: %s' % subnet
|
171 | ba1ab65d | Stavros Sachtouris | assert subnet.get('network_id', None), msg |
172 | ba1ab65d | Stavros Sachtouris | msg = 'cidr is missing in subnet spec: %s' % subnet
|
173 | ba1ab65d | Stavros Sachtouris | assert subnet.get('cidr', None), msg |
174 | ba1ab65d | Stavros Sachtouris | subnet['ip_version'] = 6 if subnet.pop('ipv6', None) else 4 |
175 | 25d2ff8e | Stavros Sachtouris | if 'subnet_id' in subnet: |
176 | 25d2ff8e | Stavros Sachtouris | subnet['id'] = subnet.pop('subnet_id') |
177 | ba1ab65d | Stavros Sachtouris | except AssertionError as ae: |
178 | ba1ab65d | Stavros Sachtouris | raise ValueError('%s' % ae) |
179 | ba1ab65d | Stavros Sachtouris | |
180 | 25d2ff8e | Stavros Sachtouris | r = self.subnets_post(
|
181 | 25d2ff8e | Stavros Sachtouris | json_data=dict(subnets=list(subnets)), success=201) |
182 | ba1ab65d | Stavros Sachtouris | return r.json['subnets'] |
183 | 25d2ff8e | Stavros Sachtouris | |
184 | 25d2ff8e | Stavros Sachtouris | def get_subnet_details(self, subnet_id): |
185 | c0b795f8 | Stavros Sachtouris | r = self.subnets_get(subnet_id, success=200) |
186 | 25d2ff8e | Stavros Sachtouris | return r.json
|
187 | 25d2ff8e | Stavros Sachtouris | |
188 | 25d2ff8e | Stavros Sachtouris | def update_subnet( |
189 | 2da652e2 | Stavros Sachtouris | self, subnet_id,
|
190 | 2da652e2 | Stavros Sachtouris | name=None, allocation_pools=None, gateway_ip=None, ipv6=None, |
191 | 2da652e2 | Stavros Sachtouris | enable_dhcp=None):
|
192 | 25d2ff8e | Stavros Sachtouris | """
|
193 | 2da652e2 | Stavros Sachtouris | :param subnet_id: (str)
|
194 | 25d2ff8e | Stavros Sachtouris |
|
195 | 25d2ff8e | Stavros Sachtouris | :param name: (str) The subnet name
|
196 | 25d2ff8e | Stavros Sachtouris | :param allocation_pools: (list of dicts) start/end addresses of
|
197 | 25d2ff8e | Stavros Sachtouris | allocation pools: [{'start': ..., 'end': ...}, ...]
|
198 | 25d2ff8e | Stavros Sachtouris | :param gateway_ip: (str)
|
199 | 25d2ff8e | Stavros Sachtouris | :param ipv6: (bool) ip_version == 6 if true, 4 if false, used as filter
|
200 | 25d2ff8e | Stavros Sachtouris | :param enable_dhcp: (bool)
|
201 | 25d2ff8e | Stavros Sachtouris | """
|
202 | 2da652e2 | Stavros Sachtouris | subnet = dict()
|
203 | 25d2ff8e | Stavros Sachtouris | if name not in (None, ): |
204 | 25d2ff8e | Stavros Sachtouris | subnet['name'] = name
|
205 | 25d2ff8e | Stavros Sachtouris | if allocation_pools not in (None, ): |
206 | 25d2ff8e | Stavros Sachtouris | subnet['allocation_pools'] = allocation_pools
|
207 | 25d2ff8e | Stavros Sachtouris | if gateway_ip not in (None, ): |
208 | 25d2ff8e | Stavros Sachtouris | subnet['gateway_ip'] = gateway_ip
|
209 | 25d2ff8e | Stavros Sachtouris | if ipv6 not in (None, ): |
210 | 25d2ff8e | Stavros Sachtouris | subnet['ip_version'] = 6 if ipv6 else 4 |
211 | 25d2ff8e | Stavros Sachtouris | if enable_dhcp not in (None, ): |
212 | 25d2ff8e | Stavros Sachtouris | subnet['enable_dhcp'] = enable_dhcp
|
213 | 2da652e2 | Stavros Sachtouris | r = self.subnets_put(
|
214 | 2da652e2 | Stavros Sachtouris | subnet_id, json=dict(subnet=subnet), success=200) |
215 | 25d2ff8e | Stavros Sachtouris | return r.json['subnet'] |
216 | 25d2ff8e | Stavros Sachtouris | |
217 | 25d2ff8e | Stavros Sachtouris | def delete_subnet(self, subnet_id): |
218 | 25d2ff8e | Stavros Sachtouris | r = self.subnets_delete(subnet_id, success=204) |
219 | 25d2ff8e | Stavros Sachtouris | return r.headers
|
220 | bac9c8ac | Stavros Sachtouris | |
221 | bac9c8ac | Stavros Sachtouris | def list_ports(self): |
222 | bac9c8ac | Stavros Sachtouris | r = self.ports_get(success=200) |
223 | 45cb7f88 | Stavros Sachtouris | return r.json['ports'] |
224 | bac9c8ac | Stavros Sachtouris | |
225 | bac9c8ac | Stavros Sachtouris | def create_port( |
226 | bac9c8ac | Stavros Sachtouris | self, network_id,
|
227 | bac9c8ac | Stavros Sachtouris | name=None, status=None, admin_state_up=None, mac_address=None, |
228 | bac9c8ac | Stavros Sachtouris | fixed_ips=None, security_groups=None): |
229 | bac9c8ac | Stavros Sachtouris | """
|
230 | bac9c8ac | Stavros Sachtouris | :param network_id: (str)
|
231 | bac9c8ac | Stavros Sachtouris |
|
232 | bac9c8ac | Stavros Sachtouris | :param name: (str)
|
233 | bac9c8ac | Stavros Sachtouris | :param status: (str)
|
234 | bac9c8ac | Stavros Sachtouris | :param admin_state_up: (bool) Router administrative status (UP / DOWN)
|
235 | bac9c8ac | Stavros Sachtouris | :param mac_address: (str)
|
236 | bac9c8ac | Stavros Sachtouris | :param fixed_ips: (str)
|
237 | bac9c8ac | Stavros Sachtouris | :param security_groups: (list)
|
238 | bac9c8ac | Stavros Sachtouris | """
|
239 | bac9c8ac | Stavros Sachtouris | port = dict(network_id=network_id)
|
240 | bac9c8ac | Stavros Sachtouris | if name:
|
241 | bac9c8ac | Stavros Sachtouris | port['name'] = name
|
242 | bac9c8ac | Stavros Sachtouris | if status:
|
243 | bac9c8ac | Stavros Sachtouris | port['status'] = status
|
244 | bac9c8ac | Stavros Sachtouris | if admin_state_up not in (None, ): |
245 | bac9c8ac | Stavros Sachtouris | port['admin_state_up'] = bool(admin_state_up) |
246 | bac9c8ac | Stavros Sachtouris | if mac_address:
|
247 | bac9c8ac | Stavros Sachtouris | port['mac_address'] = mac_address
|
248 | bac9c8ac | Stavros Sachtouris | if fixed_ips:
|
249 | bac9c8ac | Stavros Sachtouris | port['fixed_ips'] = fixed_ips
|
250 | bac9c8ac | Stavros Sachtouris | if security_groups:
|
251 | bac9c8ac | Stavros Sachtouris | port['security_groups'] = security_groups
|
252 | e8ba3e9f | Stavros Sachtouris | r = self.ports_post(json_data=dict(port=port), success=201) |
253 | bac9c8ac | Stavros Sachtouris | return r.json['port'] |
254 | bac9c8ac | Stavros Sachtouris | |
255 | bac9c8ac | Stavros Sachtouris | def create_ports(self, ports): |
256 | bac9c8ac | Stavros Sachtouris | """Atomic operation for batch port creation (all or nothing)
|
257 | de329b4c | Stavros Sachtouris |
|
258 | bac9c8ac | Stavros Sachtouris | :param ports: (list of dicts) {key: ...} with all parameters in the
|
259 | bac9c8ac | Stavros Sachtouris | method create_port, where method mandatory / optional paramteres
|
260 | bac9c8ac | Stavros Sachtouris | respond to mandatory / optional paramters in ports items
|
261 | bac9c8ac | Stavros Sachtouris | :returns: (list of dicts) created portss details
|
262 | bac9c8ac | Stavros Sachtouris | :raises ValueError: if ports parameter is incorrectly formated
|
263 | bac9c8ac | Stavros Sachtouris | :raises ClientError: if the request failed or didn't return 201
|
264 | bac9c8ac | Stavros Sachtouris | """
|
265 | bac9c8ac | Stavros Sachtouris | try:
|
266 | bac9c8ac | Stavros Sachtouris | msg = 'The ports parameter must be list or tuple'
|
267 | bac9c8ac | Stavros Sachtouris | assert (
|
268 | bac9c8ac | Stavros Sachtouris | isinstance(ports, list) or isinstance(ports, tuple)), msg |
269 | bac9c8ac | Stavros Sachtouris | for port in ports: |
270 | bac9c8ac | Stavros Sachtouris | msg = 'Subnet specification %s is not a dict' % port
|
271 | bac9c8ac | Stavros Sachtouris | assert isinstance(port, dict), msg |
272 | bac9c8ac | Stavros Sachtouris | err = set(port).difference((
|
273 | bac9c8ac | Stavros Sachtouris | 'network_id', 'status', 'name', 'admin_state_up', |
274 | bac9c8ac | Stavros Sachtouris | 'mac_address', 'fixed_ips', 'security_groups')) |
275 | bac9c8ac | Stavros Sachtouris | if err:
|
276 | bac9c8ac | Stavros Sachtouris | raise ValueError( |
277 | bac9c8ac | Stavros Sachtouris | 'Invalid key(s): %s in port specification %s' % (
|
278 | bac9c8ac | Stavros Sachtouris | err, port)) |
279 | bac9c8ac | Stavros Sachtouris | msg = 'network_id is missing in port spec: %s' % port
|
280 | bac9c8ac | Stavros Sachtouris | assert port.get('network_id', None), msg |
281 | bac9c8ac | Stavros Sachtouris | except AssertionError as ae: |
282 | bac9c8ac | Stavros Sachtouris | raise ValueError('%s' % ae) |
283 | e8ba3e9f | Stavros Sachtouris | r = self.ports_post(json_data=dict(ports=list(ports)), success=201) |
284 | bac9c8ac | Stavros Sachtouris | return r.json['ports'] |
285 | bac9c8ac | Stavros Sachtouris | |
286 | bac9c8ac | Stavros Sachtouris | def get_port_details(self, port_id): |
287 | 8ac8898f | Buildbot | r = self.ports_get(port_id, success=200) |
288 | 1d565254 | Stavros Sachtouris | return r.json['port'] |
289 | bac9c8ac | Stavros Sachtouris | |
290 | bac9c8ac | Stavros Sachtouris | def delete_port(self, port_id): |
291 | bac9c8ac | Stavros Sachtouris | r = self.ports_delete(port_id, success=204) |
292 | bac9c8ac | Stavros Sachtouris | return r.headers
|
293 | 447365fe | Stavros Sachtouris | |
294 | 447365fe | Stavros Sachtouris | def update_port( |
295 | 447365fe | Stavros Sachtouris | self, port_id, network_id,
|
296 | 447365fe | Stavros Sachtouris | name=None, status=None, admin_state_up=None, mac_address=None, |
297 | 447365fe | Stavros Sachtouris | fixed_ips=None, security_groups=None): |
298 | 447365fe | Stavros Sachtouris | """
|
299 | 447365fe | Stavros Sachtouris | :param network_id: (str)
|
300 | 447365fe | Stavros Sachtouris |
|
301 | 447365fe | Stavros Sachtouris | :param name: (str)
|
302 | 447365fe | Stavros Sachtouris | :param status: (str)
|
303 | 447365fe | Stavros Sachtouris | :param admin_state_up: (bool) Router administrative status (UP / DOWN)
|
304 | 447365fe | Stavros Sachtouris | :param mac_address: (str)
|
305 | 447365fe | Stavros Sachtouris | :param fixed_ips: (str)
|
306 | 447365fe | Stavros Sachtouris | :param security_groups: (list)
|
307 | 447365fe | Stavros Sachtouris | """
|
308 | 447365fe | Stavros Sachtouris | port = dict(network_id=network_id)
|
309 | 447365fe | Stavros Sachtouris | if name:
|
310 | 447365fe | Stavros Sachtouris | port['name'] = name
|
311 | 447365fe | Stavros Sachtouris | if status:
|
312 | 447365fe | Stavros Sachtouris | port['status'] = status
|
313 | 447365fe | Stavros Sachtouris | if admin_state_up not in (None, ): |
314 | 447365fe | Stavros Sachtouris | port['admin_state_up'] = bool(admin_state_up) |
315 | 447365fe | Stavros Sachtouris | if mac_address:
|
316 | 447365fe | Stavros Sachtouris | port['mac_address'] = mac_address
|
317 | 447365fe | Stavros Sachtouris | if fixed_ips:
|
318 | 447365fe | Stavros Sachtouris | port['fixed_ips'] = fixed_ips
|
319 | 447365fe | Stavros Sachtouris | if security_groups:
|
320 | 447365fe | Stavros Sachtouris | port['security_groups'] = security_groups
|
321 | 8ac8898f | Buildbot | r = self.ports_put(port_id, json_data=dict(port=port), success=200) |
322 | 447365fe | Stavros Sachtouris | return r.json['port'] |
323 | 8fd99602 | Stavros Sachtouris | |
324 | d1bced10 | Stavros Sachtouris | def list_floatingips(self): |
325 | 8fd99602 | Stavros Sachtouris | r = self.floatingips_get(success=200) |
326 | 74b7c6dc | Stavros Sachtouris | return r.json['floatingips'] |
327 | 8fd99602 | Stavros Sachtouris | |
328 | 8fd99602 | Stavros Sachtouris | def get_floatingip_details(self, floatingip_id): |
329 | 74b7c6dc | Stavros Sachtouris | r = self.floatingips_get(floatingip_id, success=200) |
330 | 74b7c6dc | Stavros Sachtouris | return r.json['floatingip'] |
331 | 8fd99602 | Stavros Sachtouris | |
332 | 8fd99602 | Stavros Sachtouris | def create_floatingip( |
333 | 8fd99602 | Stavros Sachtouris | self, floating_network_id,
|
334 | f1e45161 | Stavros Sachtouris | floating_ip_address='', port_id='', fixed_ip_address=''): |
335 | 8fd99602 | Stavros Sachtouris | floatingip = dict(floating_network_id=floating_network_id)
|
336 | d1bced10 | Stavros Sachtouris | if floating_ip_address:
|
337 | 8fd99602 | Stavros Sachtouris | floatingip['floating_ip_address'] = floating_ip_address
|
338 | d1bced10 | Stavros Sachtouris | if port_id:
|
339 | 8fd99602 | Stavros Sachtouris | floatingip['port_id'] = port_id
|
340 | d1bced10 | Stavros Sachtouris | if fixed_ip_address:
|
341 | 8fd99602 | Stavros Sachtouris | floatingip['fixed_ip_address'] = fixed_ip_address
|
342 | 8fd99602 | Stavros Sachtouris | r = self.floatingips_post(
|
343 | 74b7c6dc | Stavros Sachtouris | json_data=dict(floatingip=floatingip), success=200) |
344 | 8fd99602 | Stavros Sachtouris | return r.json['floatingip'] |
345 | 8fd99602 | Stavros Sachtouris | |
346 | 8fd99602 | Stavros Sachtouris | def update_floatingip( |
347 | 8fd99602 | Stavros Sachtouris | self, floatingip_id,
|
348 | 8fd99602 | Stavros Sachtouris | floating_ip_address='', port_id='', fixed_ip_address=''): |
349 | 8fd99602 | Stavros Sachtouris | """To nullify something optional, use None"""
|
350 | 8fd99602 | Stavros Sachtouris | floatingip = dict()
|
351 | 8fd99602 | Stavros Sachtouris | if floating_ip_address != '': |
352 | 8fd99602 | Stavros Sachtouris | floatingip['floating_ip_address'] = floating_ip_address
|
353 | 8fd99602 | Stavros Sachtouris | if port_id != '': |
354 | 8fd99602 | Stavros Sachtouris | floatingip['port_id'] = port_id
|
355 | 8fd99602 | Stavros Sachtouris | if fixed_ip_address != '': |
356 | 8fd99602 | Stavros Sachtouris | floatingip['fixed_ip_address'] = fixed_ip_address
|
357 | 8fd99602 | Stavros Sachtouris | r = self.floatingips_put(
|
358 | 74b7c6dc | Stavros Sachtouris | floatingip_id, json_data=dict(floatingip=floatingip), success=200) |
359 | a6d2ad78 | Stavros Sachtouris | return r.json['floatingip'] |
360 | 8fd99602 | Stavros Sachtouris | |
361 | 8fd99602 | Stavros Sachtouris | def delete_floatingip(self, floatingip_id): |
362 | 8fd99602 | Stavros Sachtouris | r = self.floatingips_delete(floatingip_id, success=204) |
363 | 8fd99602 | Stavros Sachtouris | return r.headers
|
364 | eb647cfe | Stavros Sachtouris | |
365 | eb647cfe | Stavros Sachtouris | # Wait methods
|
366 | eb647cfe | Stavros Sachtouris | |
367 | eb647cfe | Stavros Sachtouris | def wait_port( |
368 | eb647cfe | Stavros Sachtouris | self, port_id,
|
369 | 67dfe4a2 | Stavros Sachtouris | current_status='BUILD', delay=1, max_wait=100, wait_cb=None): |
370 | eb647cfe | Stavros Sachtouris | |
371 | eb647cfe | Stavros Sachtouris | def get_status(self, net_id): |
372 | eb647cfe | Stavros Sachtouris | r = self.get_port_details(port_id)
|
373 | eb647cfe | Stavros Sachtouris | return r['status'], None |
374 | eb647cfe | Stavros Sachtouris | |
375 | eb647cfe | Stavros Sachtouris | return self._wait( |
376 | eb647cfe | Stavros Sachtouris | port_id, current_status, get_status, delay, max_wait, wait_cb) |