Revision 963bd664

b/kamaki/clients/network/rest_api.py
31 31
# interpreted as representing official policies, either expressed
32 32
# or implied, of GRNET S.A.
33 33

  
34
from kamaki.clients import Client, ClientError
34
from kamaki.clients import Client
35 35
from kamaki.clients.utils import path4url
36 36
from json import dumps
37 37

  
......
39 39
class NetworkRestClient(Client):
40 40

  
41 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)
42
        if network_id:
43
            return self.get(path4url('networks', network_id), **kwargs)
44
        return self.get(path4url('networks'), **kwargs)
45 45

  
46 46
    def networks_post(self, json_data=None, shared=None, **kwargs):
47 47
        path = path4url('networks')
48
        self.set_param(shared, bool(shared), iff=shared)
48
        self.set_param('shared', bool(shared), iff=shared)
49 49
        return self.post(
50 50
            path, data=dumps(json_data) if json_data else None, **kwargs)
51 51

  
......
56 56

  
57 57
        self.set_param(
58 58
            'admin_state_up', bool(admin_state_up), iff=admin_state_up)
59
        self.set_param(shared, bool(shared), iff=shared)
59
        self.set_param('shared', bool(shared), iff=shared)
60 60

  
61 61
        return self.put(
62 62
            path, data=dumps(json_data) if json_data else None, **kwargs)
......
64 64
    def networks_delete(self, network_id, **kwargs):
65 65
        return self.delete(path4url('networks', network_id), **kwargs)
66 66

  
67
    def subnets_get(self, json_data=None, subnet_id=None, **kwargs):
67
    def subnets_get(self, subnet_id=None, **kwargs):
68 68
        if subnet_id:
69 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')
70
        return self.get(path4url('subnets'), **kwargs)
75 71

  
76 72
    def subnets_post(self, **kwargs):
77 73
        return self.post(path4url('subnets'), **kwargs)
......
83 79
        return self.delete(path4url('subnets', subnet_id), **kwargs)
84 80

  
85 81
    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)
82
        if port_id:
83
            return self.get(path4url('ports', port_id), **kwargs)
84
        return self.get(path4url('ports'), **kwargs)
88 85

  
89 86
    def ports_post(
90 87
            self,
......
96 93
        self.set_param('fixed_ips', fixed_ips, iff=fixed_ips)
97 94
        self.set_param('security_groups', security_groups, iff=security_groups)
98 95
        data = dumps(json_data) if json_data else None
99
        self.post(path4url('ports'), data=data, **kwargs)
96
        return self.post(path4url('ports'), data=data, **kwargs)
100 97

  
101 98
    def ports_put(
102 99
            self, port_id,
......
108 105
        self.set_param('fixed_ips', fixed_ips, iff=fixed_ips)
109 106
        self.set_param('security_groups', security_groups, iff=security_groups)
110 107
        data = dumps(json_data) if json_data else None
111
        self.put(path4url('ports', port_id), data=data, **kwargs)
108
        return self.put(path4url('ports', port_id), data=data, **kwargs)
112 109

  
113 110
    def ports_delete(self, port_id, **kwargs):
114 111
        return self.delete(path4url('ports', port_id), **kwargs)
b/kamaki/clients/network/test.py
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 mock import patch, call
35
from unittest import TestCase
36
from itertools import product
37
from json import dumps
38

  
39
from kamaki.clients import ClientError, network
40

  
41

  
42
class NetworkRestClient(TestCase):
43

  
44
    """Set up a ComputesRest thorough test"""
45
    def setUp(self):
46
        self.url = 'http://network.example.com'
47
        self.token = 'n2tw0rk70k3n'
48
        self.client = network.NetworkRestClient(self.url, self.token)
49

  
50
    def tearDown(self):
51
        pass
52

  
53
    def _assert(self, method_call, path, set_param=None, params=(), **kwargs):
54
        """Assert the REST method call is called as expected"""
55
        x0 = - len(params)
56
        for i, (k, v, c) in enumerate(params):
57
            self.assertEqual(set_param.mock_calls[x0 + i], call(k, v, iff=c))
58

  
59
        self.assertEqual(method_call.mock_calls[-1], call(path, **kwargs))
60

  
61
    @patch('kamaki.clients.Client.get', return_value='ret val')
62
    def test_networks_get(self, get):
63
        for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
64
            self.assertEqual(self.client.networks_get(**kwargs), 'ret val')
65
            self._assert(get, '/networks', **kwargs)
66

  
67
            netid = 'netid'
68
            self.assertEqual(
69
                self.client.networks_get(network_id=netid, **kwargs),
70
                'ret val')
71
            self._assert(get, '/networks/%s' % netid, **kwargs)
72

  
73
    @patch('kamaki.clients.Client.set_param')
74
    @patch('kamaki.clients.Client.post', return_value='ret val')
75
    def test_networks_post(self, post, set_param):
76
        for params, kwargs in product(
77
                (
78
                    (('shared', False, None), ),
79
                    (('shared', True, True), )),
80
                (dict(), dict(k1='v1'), dict(k2='v2', k3='v3'))):
81

  
82
            callargs = dict()
83
            for p in params:
84
                callargs[p[0]] = p[2]
85
            callargs.update(kwargs)
86

  
87
            self.assertEqual(self.client.networks_post(**callargs), 'ret val')
88
            self._assert(
89
                post, '/networks', set_param,
90
                params=params, data=None, **kwargs)
91

  
92
            json_data = dict(id='some id', other_param='other val')
93
            callargs['json_data'] = json_data
94
            self.assertEqual(self.client.networks_post(**callargs), 'ret val')
95
            self._assert(
96
                post, '/networks', set_param, params,
97
                data=dumps(json_data), **kwargs)
98

  
99
    @patch('kamaki.clients.Client.set_param')
100
    @patch('kamaki.clients.Client.put', return_value='ret val')
101
    def test_networks_put(self, put, set_param):
102
        netid = 'netid'
103
        for params, kwargs in product(
104
                [p for p in product(
105
                    (
106
                        ('admin_state_up', False, None),
107
                        ('admin_state_up', True, True)),
108
                    (('shared', False, None), ('shared', True, True)),
109
                )],
110
                (dict(), dict(k1='v1'), dict(k2='v2', k3='v3'))):
111

  
112
            callargs = dict()
113
            for p in params:
114
                callargs[p[0]] = p[2]
115
            callargs.update(kwargs)
116

  
117
            self.assertEqual(
118
                self.client.networks_put(netid, **callargs), 'ret val')
119
            self._assert(
120
                put, '/networks/%s' % netid, set_param, params,
121
                data=None, **kwargs)
122

  
123
            json_data = dict(id='some id', other_param='other val')
124
            callargs['json_data'] = json_data
125
            self.assertEqual(
126
                self.client.networks_put(netid, **callargs), 'ret val')
127
            self._assert(
128
                put, '/networks/%s' % netid, set_param, params,
129
                data=dumps(json_data), **kwargs)
130

  
131
    @patch('kamaki.clients.Client.delete', return_value='ret val')
132
    def test_networks_delete(self, delete):
133
        netid = 'netid'
134
        for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
135
            self.assertEqual(
136
                self.client.networks_delete(netid, **kwargs), 'ret val')
137
            self._assert(delete, '/networks/%s' % netid, **kwargs)
138

  
139
    @patch('kamaki.clients.Client.get', return_value='ret val')
140
    def test_subnets_get(self, get):
141
        for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
142
            self.assertEqual(self.client.subnets_get(**kwargs), 'ret val')
143
            self._assert(get, '/subnets', **kwargs)
144

  
145
            subnet_id = 'subnet id'
146
            self.assertEqual(
147
                self.client.subnets_get(subnet_id=subnet_id, **kwargs),
148
                'ret val')
149
            self._assert(get, '/subnets/%s' % subnet_id, **kwargs)
150

  
151
    @patch('kamaki.clients.Client.post', return_value='ret val')
152
    def test_subnets_post(self, post):
153
        for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
154
            self.assertEqual(self.client.subnets_post(**kwargs), 'ret val')
155
            self._assert(post, '/subnets', **kwargs)
156

  
157
    @patch('kamaki.clients.Client.put', return_value='ret val')
158
    def test_subnets_put(self, put):
159
        subnet_id = 'subid'
160
        for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
161
            self.assertEqual(
162
                self.client.subnets_put(subnet_id, **kwargs), 'ret val')
163
            self._assert(put, '/subnets/%s' % subnet_id, **kwargs)
164

  
165
    @patch('kamaki.clients.Client.delete', return_value='ret val')
166
    def test_subnets_delete(self, delete):
167
        netid = 'netid'
168
        for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
169
            self.assertEqual(
170
                self.client.subnets_delete(netid, **kwargs), 'ret val')
171
            self._assert(delete, '/subnets/%s' % netid, **kwargs)
172

  
173
    @patch('kamaki.clients.Client.get', return_value='ret val')
174
    def test_ports_get(self, get):
175
        for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
176
            self.assertEqual(self.client.ports_get(**kwargs), 'ret val')
177
            self._assert(get, '/ports', **kwargs)
178

  
179
            port_id = 'port id'
180
            self.assertEqual(
181
                self.client.ports_get(port_id=port_id, **kwargs),
182
                'ret val')
183
            self._assert(get, '/ports/%s' % port_id, **kwargs)
184

  
185
    @patch('kamaki.clients.Client.set_param')
186
    @patch('kamaki.clients.Client.post', return_value='ret val')
187
    def test_ports_post(self, post, set_param):
188
        for params, kwargs in product(
189
                [p for p in product(
190
                    (
191
                        ('name', 'port name', 'port name'),
192
                        ('name', None, None)),
193
                    (
194
                        ('mac_address', 'max address', 'max address'),
195
                        ('mac_address', None, None)),
196
                    (
197
                        ('fixed_ips', 'fixed ip', 'fixed ip'),
198
                        ('fixed_ips', None, None)),
199
                    (
200
                        ('security_groups', 'sec groups', 'sec groups'),
201
                        ('security_groups', None, None))
202
                )],
203
                (dict(), dict(k1='v1'), dict(k2='v2', k3='v3'))):
204

  
205
            callargs = dict()
206
            for p in params:
207
                callargs[p[0]] = p[2]
208
            callargs.update(kwargs)
209

  
210
            self.assertEqual(self.client.ports_post(**callargs), 'ret val')
211
            self._assert(
212
                post, '/ports', set_param,
213
                params=params, data=None, **kwargs)
214

  
215
            json_data = dict(id='some id', other_param='other val')
216
            callargs['json_data'] = json_data
217
            self.assertEqual(self.client.ports_post(**callargs), 'ret val')
218
            self._assert(
219
                post, '/ports', set_param, params,
220
                data=dumps(json_data), **kwargs)
221

  
222
    @patch('kamaki.clients.Client.set_param')
223
    @patch('kamaki.clients.Client.put', return_value='ret val')
224
    def test_ports_put(self, put, set_param):
225
        port_id = 'portid'
226
        for params, kwargs in product(
227
                [p for p in product(
228
                    (
229
                        ('name', 'port name', 'port name'),
230
                        ('name', None, None)),
231
                    (
232
                        ('mac_address', 'max address', 'max address'),
233
                        ('mac_address', None, None)),
234
                    (
235
                        ('fixed_ips', 'fixed ip', 'fixed ip'),
236
                        ('fixed_ips', None, None)),
237
                    (
238
                        ('security_groups', 'sec groups', 'sec groups'),
239
                        ('security_groups', None, None))
240
                )],
241
                (dict(), dict(k1='v1'), dict(k2='v2', k3='v3'))):
242

  
243
            callargs = dict()
244
            for p in params:
245
                callargs[p[0]] = p[2]
246
            callargs.update(kwargs)
247

  
248
            self.assertEqual(
249
                self.client.ports_put(port_id, **callargs), 'ret val')
250
            self._assert(
251
                put, '/ports/%s' % port_id, set_param,
252
                params=params, data=None, **kwargs)
253

  
254
            json_data = dict(id='some id', other_param='other val')
255
            callargs['json_data'] = json_data
256
            self.assertEqual(
257
                self.client.ports_put(port_id, **callargs), 'ret val')
258
            self._assert(
259
                put, '/ports/%s' % port_id, set_param, params,
260
                data=dumps(json_data), **kwargs)
261

  
262

  
263
if __name__ == '__main__':
264
    from sys import argv
265
    from kamaki.clients.test import runTestCase
266
    not_found = True
267
    #if not argv[1:] or argv[1] == 'NetworkClient':
268
    #    not_found = False
269
    #    runTestCase(NetworkClient, 'Network Client', argv[2:])
270
    if not argv[1:] or argv[1] == 'NetworkRest':
271
        not_found = False
272
        runTestCase(NetworkRestClient, 'NetworkRest Client', argv[2:])
273
    if not_found:
274
        print('TestCase %s not found' % argv[1])
b/kamaki/clients/test.py
41 41
from kamaki.clients.utils.test import Utils
42 42
from kamaki.clients.astakos.test import AstakosClient
43 43
from kamaki.clients.compute.test import ComputeClient, ComputeRestClient
44
from kamaki.clients.network.test import NetworkRestClient
44 45
from kamaki.clients.cyclades.test import CycladesClient
45 46
from kamaki.clients.cyclades.test import CycladesRestClient
46 47
from kamaki.clients.image.test import ImageClient

Also available in: Unified diff