Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / network / rest_api.py @ c6ebe715

History | View | Annotate | Download (4.7 kB)

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