|
1 |
# Copyright 2013 GRNET S.A. All rights reserved.
|
|
2 |
#
|
|
3 |
# Redistribution and use in source and binary forms, with or
|
|
4 |
# without modification, are permitted provided that the following
|
|
5 |
# conditions are met:
|
|
6 |
#
|
|
7 |
# 1. Redistributions of source code must retain the above
|
|
8 |
# copyright notice, this list of conditions and the following
|
|
9 |
# disclaimer.
|
|
10 |
#
|
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
|
12 |
# copyright notice, this list of conditions and the following
|
|
13 |
# disclaimer in the documentation and/or other materials
|
|
14 |
# provided with the distribution.
|
|
15 |
#
|
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
|
28 |
#
|
|
29 |
# The views and conclusions contained in the software and
|
|
30 |
# documentation are those of the authors and should not be
|
|
31 |
# interpreted as representing official policies, either expressed
|
|
32 |
# or implied, of GRNET S.A.
|
|
33 |
|
|
34 |
from kamaki.clients import Client, ClientError
|
|
35 |
from kamaki.clients.utils import path4url
|
|
36 |
from json import dumps
|
|
37 |
|
|
38 |
|
|
39 |
class NetworkRestClient(Client):
|
|
40 |
|
|
41 |
def networks_get(self, network_id=None, **kwargs):
|
|
42 |
path = path4url('networks', network_id) if (
|
|
43 |
network_id) else path4url('networks')
|
|
44 |
return self.get(path, **kwargs)
|
|
45 |
|
|
46 |
def networks_post(self, json_data=None, shared=None, **kwargs):
|
|
47 |
path = path4url('networks')
|
|
48 |
self.set_param(shared, bool(shared), iff=shared)
|
|
49 |
return self.post(
|
|
50 |
path, data=dumps(json_data) if json_data else None, **kwargs)
|
|
51 |
|
|
52 |
def networks_put(
|
|
53 |
self, network_id,
|
|
54 |
json_data=None, admin_state_up=None, shared=None, **kwargs):
|
|
55 |
path = path4url('networks', network_id)
|
|
56 |
|
|
57 |
self.set_param(
|
|
58 |
'admin_state_up', bool(admin_state_up), iff=admin_state_up)
|
|
59 |
self.set_param(shared, bool(shared), iff=shared)
|
|
60 |
|
|
61 |
return self.put(
|
|
62 |
path, data=dumps(json_data) if json_data else None, **kwargs)
|
|
63 |
|
|
64 |
def networks_delete(self, network_id, **kwargs):
|
|
65 |
return self.delete(path4url('networks', network_id), **kwargs)
|
|
66 |
|
|
67 |
def subnets_get(self, json_data=None, subnet_id=None, **kwargs):
|
|
68 |
if subnet_id:
|
|
69 |
return self.get(path4url('subnets', subnet_id), **kwargs)
|
|
70 |
elif json_data:
|
|
71 |
return self.get(
|
|
72 |
path4url('subnets'), data=dumps(json_data), **kwargs)
|
|
73 |
else:
|
|
74 |
raise ClientError('No subnet_id or json_data in GET subnets')
|
|
75 |
|
|
76 |
def subnets_post(self, **kwargs):
|
|
77 |
return self.post(path4url('subnets'), **kwargs)
|
|
78 |
|
|
79 |
def subnets_put(self, subnet_id, **kwargs):
|
|
80 |
return self.put(path4url('subnets', subnet_id), **kwargs)
|
|
81 |
|
|
82 |
def subnets_delete(self, subnet_id, **kwargs):
|
|
83 |
return self.delete(path4url('subnets', subnet_id), **kwargs)
|
|
84 |
|
|
85 |
def ports_get(self, port_id=None, **kwargs):
|
|
86 |
path = path4url('ports', port_id) if port_id else path4url('ports')
|
|
87 |
return self.get(path, **kwargs)
|
|
88 |
|
|
89 |
def ports_post(
|
|
90 |
self,
|
|
91 |
json_data=None,
|
|
92 |
name=None, mac_address=None, fixed_ips=None, security_groups=None,
|
|
93 |
**kwargs):
|
|
94 |
self.set_param('name', name, iff=name)
|
|
95 |
self.set_param('mac_address', mac_address, iff=mac_address)
|
|
96 |
self.set_param('fixed_ips', fixed_ips, iff=fixed_ips)
|
|
97 |
self.set_param('security_groups', security_groups, iff=security_groups)
|
|
98 |
data = dumps(json_data) if json_data else None
|
|
99 |
self.post(path4url('ports'), data=data, **kwargs)
|
|
100 |
|
|
101 |
def ports_put(
|
|
102 |
self, port_id,
|
|
103 |
json_data=None,
|
|
104 |
name=None, mac_address=None, fixed_ips=None, security_groups=None,
|
|
105 |
**kwargs):
|
|
106 |
self.set_param('name', name, iff=name)
|
|
107 |
self.set_param('mac_address', mac_address, iff=mac_address)
|
|
108 |
self.set_param('fixed_ips', fixed_ips, iff=fixed_ips)
|
|
109 |
self.set_param('security_groups', security_groups, iff=security_groups)
|
|
110 |
data = dumps(json_data) if json_data else None
|
|
111 |
self.put(path4url('ports', port_id), data=data, **kwargs)
|
|
112 |
|
|
113 |
def ports_delete(self, port_id, **kwargs):
|
|
114 |
return self.delete(path4url('ports', port_id), **kwargs)
|