Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / commands / network.py @ 00b1248e

History | View | Annotate | Download (15.8 kB)

1 d18e6439 Stavros Sachtouris
# Copyright 2011-2013 GRNET S.A. All rights reserved.
2 d18e6439 Stavros Sachtouris
#
3 d18e6439 Stavros Sachtouris
# Redistribution and use in source and binary forms, with or
4 d18e6439 Stavros Sachtouris
# without modification, are permitted provided that the following
5 d18e6439 Stavros Sachtouris
# conditions are met:
6 d18e6439 Stavros Sachtouris
#
7 d18e6439 Stavros Sachtouris
#   1. Redistributions of source code must retain the above
8 d18e6439 Stavros Sachtouris
#      copyright notice, this list of conditions and the following
9 d18e6439 Stavros Sachtouris
#      disclaimer.
10 d18e6439 Stavros Sachtouris
#
11 d18e6439 Stavros Sachtouris
#   2. Redistributions in binary form must reproduce the above
12 d18e6439 Stavros Sachtouris
#      copyright notice, this list of conditions and the following
13 d18e6439 Stavros Sachtouris
#      disclaimer in the documentation and/or other materials
14 d18e6439 Stavros Sachtouris
#      provided with the distribution.
15 d18e6439 Stavros Sachtouris
#
16 d18e6439 Stavros Sachtouris
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 d18e6439 Stavros Sachtouris
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 d18e6439 Stavros Sachtouris
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 d18e6439 Stavros Sachtouris
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 d18e6439 Stavros Sachtouris
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 d18e6439 Stavros Sachtouris
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 d18e6439 Stavros Sachtouris
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 d18e6439 Stavros Sachtouris
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 d18e6439 Stavros Sachtouris
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 d18e6439 Stavros Sachtouris
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 d18e6439 Stavros Sachtouris
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 d18e6439 Stavros Sachtouris
# POSSIBILITY OF SUCH DAMAGE.
28 d18e6439 Stavros Sachtouris
#
29 d18e6439 Stavros Sachtouris
# The views and conclusions contained in the software and
30 d18e6439 Stavros Sachtouris
# documentation are those of the authors and should not be
31 d18e6439 Stavros Sachtouris
# interpreted as representing official policies, either expressed
32 d18e6439 Stavros Sachtouris
# or implied, of GRNET S.A.
33 d18e6439 Stavros Sachtouris
34 d18e6439 Stavros Sachtouris
from io import StringIO
35 d18e6439 Stavros Sachtouris
from pydoc import pager
36 d18e6439 Stavros Sachtouris
37 d18e6439 Stavros Sachtouris
from kamaki.cli import command
38 d18e6439 Stavros Sachtouris
from kamaki.cli.command_tree import CommandTree
39 62c6652f Stavros Sachtouris
from kamaki.cli.errors import (
40 62c6652f Stavros Sachtouris
    CLISyntaxError, CLIBaseUrlError, CLIInvalidArgument)
41 cd0927f7 Stavros Sachtouris
from kamaki.clients.cyclades import CycladesNetworkClient
42 62c6652f Stavros Sachtouris
from kamaki.cli.argument import FlagArgument, ValueArgument, RepeatableArgument
43 d18e6439 Stavros Sachtouris
from kamaki.cli.commands import _command_init, errors, addLogSettings
44 d18e6439 Stavros Sachtouris
from kamaki.cli.commands import (
45 d18e6439 Stavros Sachtouris
    _optional_output_cmd, _optional_json, _name_filter, _id_filter)
46 7e57e590 Stavros Sachtouris
from kamaki.cli.utils import filter_dicts_by_dict
47 d18e6439 Stavros Sachtouris
48 d18e6439 Stavros Sachtouris
49 d18e6439 Stavros Sachtouris
network_cmds = CommandTree('network', 'Networking API network commands')
50 d18e6439 Stavros Sachtouris
port_cmds = CommandTree('port', 'Networking API network commands')
51 d18e6439 Stavros Sachtouris
subnet_cmds = CommandTree('subnet', 'Networking API network commands')
52 d18e6439 Stavros Sachtouris
_commands = [network_cmds, port_cmds, subnet_cmds]
53 d18e6439 Stavros Sachtouris
54 d18e6439 Stavros Sachtouris
55 d18e6439 Stavros Sachtouris
about_authentication = '\nUser Authentication:\
56 d18e6439 Stavros Sachtouris
    \n* to check authentication: /user authenticate\
57 d18e6439 Stavros Sachtouris
    \n* to set authentication token: /config set cloud.<cloud>.token <token>'
58 d18e6439 Stavros Sachtouris
59 d18e6439 Stavros Sachtouris
60 5c433331 Stavros Sachtouris
class _init_network(_command_init):
61 bc4662d8 Stavros Sachtouris
    @errors.generic.all
62 bc4662d8 Stavros Sachtouris
    @addLogSettings
63 bc4662d8 Stavros Sachtouris
    def _run(self, service='network'):
64 bc4662d8 Stavros Sachtouris
        if getattr(self, 'cloud', None):
65 bc4662d8 Stavros Sachtouris
            base_url = self._custom_url(service) or self._custom_url(
66 bc4662d8 Stavros Sachtouris
                'compute')
67 bc4662d8 Stavros Sachtouris
            if base_url:
68 bc4662d8 Stavros Sachtouris
                token = self._custom_token(service) or self._custom_token(
69 bc4662d8 Stavros Sachtouris
                    'compute') or self.config.get_cloud('token')
70 cd0927f7 Stavros Sachtouris
                self.client = CycladesNetworkClient(
71 bc4662d8 Stavros Sachtouris
                  base_url=base_url, token=token)
72 bc4662d8 Stavros Sachtouris
                return
73 bc4662d8 Stavros Sachtouris
        else:
74 bc4662d8 Stavros Sachtouris
            self.cloud = 'default'
75 bc4662d8 Stavros Sachtouris
        if getattr(self, 'auth_base', False):
76 bc4662d8 Stavros Sachtouris
            cyclades_endpoints = self.auth_base.get_service_endpoints(
77 bc4662d8 Stavros Sachtouris
                self._custom_type('compute') or 'compute',
78 bc4662d8 Stavros Sachtouris
                self._custom_version('compute') or '')
79 bc4662d8 Stavros Sachtouris
            base_url = cyclades_endpoints['publicURL']
80 bc4662d8 Stavros Sachtouris
            token = self.auth_base.token
81 cd0927f7 Stavros Sachtouris
            self.client = CycladesNetworkClient(base_url=base_url, token=token)
82 bc4662d8 Stavros Sachtouris
        else:
83 bc4662d8 Stavros Sachtouris
            raise CLIBaseUrlError(service='network')
84 bc4662d8 Stavros Sachtouris
85 bc4662d8 Stavros Sachtouris
    def main(self):
86 bc4662d8 Stavros Sachtouris
        self._run()
87 d18e6439 Stavros Sachtouris
88 d18e6439 Stavros Sachtouris
89 d18e6439 Stavros Sachtouris
@command(network_cmds)
90 5c433331 Stavros Sachtouris
class network_list(_init_network, _optional_json, _name_filter, _id_filter):
91 d18e6439 Stavros Sachtouris
    """List networks
92 d18e6439 Stavros Sachtouris
    Use filtering arguments (e.g., --name-like) to manage long server lists
93 d18e6439 Stavros Sachtouris
    """
94 d18e6439 Stavros Sachtouris
95 d18e6439 Stavros Sachtouris
    arguments = dict(
96 d18e6439 Stavros Sachtouris
        detail=FlagArgument('show detailed output', ('-l', '--details')),
97 cd40143a Stavros Sachtouris
        more=FlagArgument(
98 cd40143a Stavros Sachtouris
            'output results in pages (-n to set items per page, default 10)',
99 cd40143a Stavros Sachtouris
            '--more'),
100 7e57e590 Stavros Sachtouris
        user_id=ValueArgument(
101 7e57e590 Stavros Sachtouris
            'show only networks belonging to user with this id', '--user-id')
102 d18e6439 Stavros Sachtouris
    )
103 d18e6439 Stavros Sachtouris
104 7e57e590 Stavros Sachtouris
    def _filter_by_user_id(self, nets):
105 7e57e590 Stavros Sachtouris
        return filter_dicts_by_dict(nets, dict(user_id=self['user_id'])) if (
106 7e57e590 Stavros Sachtouris
            self['user_id']) else nets
107 7e57e590 Stavros Sachtouris
108 d18e6439 Stavros Sachtouris
    @errors.generic.all
109 d18e6439 Stavros Sachtouris
    @errors.cyclades.connection
110 d18e6439 Stavros Sachtouris
    def _run(self):
111 7e57e590 Stavros Sachtouris
        detail = self['detail'] or self['user_id']
112 7e57e590 Stavros Sachtouris
        nets = self.client.list_networks(detail=detail)
113 7e57e590 Stavros Sachtouris
        nets = self._filter_by_user_id(nets)
114 77bf75fe Stavros Sachtouris
        nets = self._filter_by_name(nets)
115 77bf75fe Stavros Sachtouris
        nets = self._filter_by_id(nets)
116 7e57e590 Stavros Sachtouris
        if detail and not self['detail']:
117 7e57e590 Stavros Sachtouris
            nets = [dict(
118 7e57e590 Stavros Sachtouris
                id=n['id'], name=n['name'], links=n['links']) for n in nets]
119 cd40143a Stavros Sachtouris
        kwargs = dict()
120 cd40143a Stavros Sachtouris
        if self['more']:
121 cd40143a Stavros Sachtouris
            kwargs['out'] = StringIO()
122 cd40143a Stavros Sachtouris
            kwargs['title'] = ()
123 cd40143a Stavros Sachtouris
        self._print(nets, **kwargs)
124 cd40143a Stavros Sachtouris
        if self['more']:
125 cd40143a Stavros Sachtouris
            pager(kwargs['out'].getvalue())
126 d18e6439 Stavros Sachtouris
127 d18e6439 Stavros Sachtouris
    def main(self):
128 d18e6439 Stavros Sachtouris
        super(self.__class__, self)._run()
129 d18e6439 Stavros Sachtouris
        self._run()
130 cd40143a Stavros Sachtouris
131 cd40143a Stavros Sachtouris
132 cd40143a Stavros Sachtouris
@command(network_cmds)
133 5c433331 Stavros Sachtouris
class network_info(_init_network, _optional_json):
134 cd40143a Stavros Sachtouris
    """Get details about a network"""
135 cd40143a Stavros Sachtouris
136 cd40143a Stavros Sachtouris
    @errors.generic.all
137 cd40143a Stavros Sachtouris
    @errors.cyclades.connection
138 cd40143a Stavros Sachtouris
    @errors.cyclades.network_id
139 cd40143a Stavros Sachtouris
    def _run(self, network_id):
140 cd40143a Stavros Sachtouris
        net = self.client.get_network_details(network_id)
141 cd40143a Stavros Sachtouris
        self._print(net, self.print_dict)
142 cd40143a Stavros Sachtouris
143 cd40143a Stavros Sachtouris
    def main(self, network_id):
144 cd40143a Stavros Sachtouris
        super(self.__class__, self)._run()
145 cd40143a Stavros Sachtouris
        self._run(network_id=network_id)
146 49413939 Stavros Sachtouris
147 49413939 Stavros Sachtouris
148 49413939 Stavros Sachtouris
@command(network_cmds)
149 5c433331 Stavros Sachtouris
class network_create(_init_network, _optional_json):
150 cd0927f7 Stavros Sachtouris
    """Create a new network
151 0e27687b Stavros Sachtouris
    Valid network types: CUSTOM MAC_FILTERED IP_LESS_ROUTED PHYSICAL_VLAN
152 cd0927f7 Stavros Sachtouris
    """
153 49413939 Stavros Sachtouris
154 cd0927f7 Stavros Sachtouris
    arguments = dict(
155 cd0927f7 Stavros Sachtouris
        name=ValueArgument('Network name', '--name'),
156 cd0927f7 Stavros Sachtouris
        shared=FlagArgument(
157 cd0927f7 Stavros Sachtouris
            'Make network shared (special privileges required)', '--shared')
158 49413939 Stavros Sachtouris
    )
159 49413939 Stavros Sachtouris
160 49413939 Stavros Sachtouris
    @errors.generic.all
161 49413939 Stavros Sachtouris
    @errors.cyclades.connection
162 cd0927f7 Stavros Sachtouris
    @errors.cyclades.network_type
163 cd0927f7 Stavros Sachtouris
    def _run(self, network_type):
164 49413939 Stavros Sachtouris
        net = self.client.create_network(
165 cd0927f7 Stavros Sachtouris
            network_type, name=self['name'], shared=self['shared'])
166 49413939 Stavros Sachtouris
        self._print(net, self.print_dict)
167 49413939 Stavros Sachtouris
168 cd0927f7 Stavros Sachtouris
    def main(self, network_type):
169 49413939 Stavros Sachtouris
        super(self.__class__, self)._run()
170 cd0927f7 Stavros Sachtouris
        self._run(network_type=network_type)
171 aef3fa1f Stavros Sachtouris
172 aef3fa1f Stavros Sachtouris
173 aef3fa1f Stavros Sachtouris
@command(network_cmds)
174 5c433331 Stavros Sachtouris
class network_delete(_init_network, _optional_output_cmd):
175 aef3fa1f Stavros Sachtouris
    """Delete a network"""
176 aef3fa1f Stavros Sachtouris
177 aef3fa1f Stavros Sachtouris
    @errors.generic.all
178 aef3fa1f Stavros Sachtouris
    @errors.cyclades.connection
179 58602137 Stavros Sachtouris
    @errors.cyclades.network_id
180 aef3fa1f Stavros Sachtouris
    def _run(self, network_id):
181 aef3fa1f Stavros Sachtouris
        r = self.client.delete_network(network_id)
182 aef3fa1f Stavros Sachtouris
        self._optional_output(r)
183 aef3fa1f Stavros Sachtouris
184 aef3fa1f Stavros Sachtouris
    def main(self, network_id):
185 aef3fa1f Stavros Sachtouris
        super(self.__class__, self)._run()
186 aef3fa1f Stavros Sachtouris
        self._run(network_id=network_id)
187 58602137 Stavros Sachtouris
188 58602137 Stavros Sachtouris
189 58602137 Stavros Sachtouris
@command(network_cmds)
190 5c433331 Stavros Sachtouris
class network_set(_init_network, _optional_json):
191 58602137 Stavros Sachtouris
    """Set an attribute of a network, leave the rest untouched (update)
192 58602137 Stavros Sachtouris
    Only "--name" is supported for now
193 58602137 Stavros Sachtouris
    """
194 58602137 Stavros Sachtouris
195 58602137 Stavros Sachtouris
    arguments = dict(name=ValueArgument('New name of the network', '--name'))
196 58602137 Stavros Sachtouris
197 58602137 Stavros Sachtouris
    @errors.generic.all
198 58602137 Stavros Sachtouris
    @errors.cyclades.connection
199 58602137 Stavros Sachtouris
    @errors.cyclades.network_id
200 58602137 Stavros Sachtouris
    def _run(self, network_id):
201 58602137 Stavros Sachtouris
        if self['name'] in (None, ):
202 58602137 Stavros Sachtouris
            raise CLISyntaxError(
203 cd0927f7 Stavros Sachtouris
                'Missing network attributes to update',
204 58602137 Stavros Sachtouris
                details=[
205 58602137 Stavros Sachtouris
                    'At least one if the following is expected:',
206 58602137 Stavros Sachtouris
                    '  --name=<new name>'])
207 58602137 Stavros Sachtouris
        r = self.client.update_network(network_id, name=self['name'])
208 58602137 Stavros Sachtouris
        self._print(r, self.print_dict)
209 58602137 Stavros Sachtouris
210 58602137 Stavros Sachtouris
    def main(self, network_id):
211 58602137 Stavros Sachtouris
        super(self.__class__, self)._run()
212 58602137 Stavros Sachtouris
        self._run(network_id=network_id)
213 62c6652f Stavros Sachtouris
214 62c6652f Stavros Sachtouris
215 62c6652f Stavros Sachtouris
@command(subnet_cmds)
216 62c6652f Stavros Sachtouris
class subnet_list(_init_network, _optional_json, _name_filter, _id_filter):
217 62c6652f Stavros Sachtouris
    """List subnets
218 62c6652f Stavros Sachtouris
    Use filtering arguments (e.g., --name-like) to manage long server lists
219 62c6652f Stavros Sachtouris
    """
220 62c6652f Stavros Sachtouris
221 62c6652f Stavros Sachtouris
    arguments = dict(
222 62c6652f Stavros Sachtouris
        detail=FlagArgument('show detailed output', ('-l', '--details')),
223 62c6652f Stavros Sachtouris
        more=FlagArgument(
224 62c6652f Stavros Sachtouris
            'output results in pages (-n to set items per page, default 10)',
225 62c6652f Stavros Sachtouris
            '--more'),
226 62c6652f Stavros Sachtouris
        user_id=ValueArgument(
227 62c6652f Stavros Sachtouris
            'show only subnets belonging to user with this id', '--user-id')
228 62c6652f Stavros Sachtouris
    )
229 62c6652f Stavros Sachtouris
230 62c6652f Stavros Sachtouris
    def _filter_by_user_id(self, nets):
231 62c6652f Stavros Sachtouris
        return filter_dicts_by_dict(nets, dict(user_id=self['user_id'])) if (
232 62c6652f Stavros Sachtouris
            self['user_id']) else nets
233 62c6652f Stavros Sachtouris
234 62c6652f Stavros Sachtouris
    @errors.generic.all
235 62c6652f Stavros Sachtouris
    @errors.cyclades.connection
236 62c6652f Stavros Sachtouris
    def _run(self):
237 62c6652f Stavros Sachtouris
        detail = self['detail'] or self['user_id']
238 62c6652f Stavros Sachtouris
        nets = self.client.list_subnets()
239 62c6652f Stavros Sachtouris
        nets = self._filter_by_user_id(nets)
240 62c6652f Stavros Sachtouris
        nets = self._filter_by_name(nets)
241 62c6652f Stavros Sachtouris
        nets = self._filter_by_id(nets)
242 62c6652f Stavros Sachtouris
        if detail and not self['detail']:
243 62c6652f Stavros Sachtouris
            nets = [dict(
244 62c6652f Stavros Sachtouris
                id=n['id'], name=n['name'], links=n['links']) for n in nets]
245 62c6652f Stavros Sachtouris
        kwargs = dict()
246 62c6652f Stavros Sachtouris
        if self['more']:
247 62c6652f Stavros Sachtouris
            kwargs['out'] = StringIO()
248 62c6652f Stavros Sachtouris
            kwargs['title'] = ()
249 62c6652f Stavros Sachtouris
        self._print(nets, **kwargs)
250 62c6652f Stavros Sachtouris
        if self['more']:
251 62c6652f Stavros Sachtouris
            pager(kwargs['out'].getvalue())
252 62c6652f Stavros Sachtouris
253 62c6652f Stavros Sachtouris
    def main(self):
254 62c6652f Stavros Sachtouris
        super(self.__class__, self)._run()
255 62c6652f Stavros Sachtouris
        self._run()
256 62c6652f Stavros Sachtouris
257 62c6652f Stavros Sachtouris
258 62c6652f Stavros Sachtouris
@command(subnet_cmds)
259 62c6652f Stavros Sachtouris
class subnet_info(_init_network, _optional_json):
260 62c6652f Stavros Sachtouris
    """Get details about a subnet"""
261 62c6652f Stavros Sachtouris
262 62c6652f Stavros Sachtouris
    @errors.generic.all
263 62c6652f Stavros Sachtouris
    @errors.cyclades.connection
264 62c6652f Stavros Sachtouris
    def _run(self, subnet_id):
265 62c6652f Stavros Sachtouris
        net = self.client.get_subnet_details(subnet_id)
266 62c6652f Stavros Sachtouris
        self._print(net, self.print_dict)
267 62c6652f Stavros Sachtouris
268 62c6652f Stavros Sachtouris
    def main(self, subnet_id):
269 62c6652f Stavros Sachtouris
        super(self.__class__, self)._run()
270 62c6652f Stavros Sachtouris
        self._run(subnet_id=subnet_id)
271 62c6652f Stavros Sachtouris
272 62c6652f Stavros Sachtouris
273 62c6652f Stavros Sachtouris
class AllocationPoolArgument(RepeatableArgument):
274 62c6652f Stavros Sachtouris
275 62c6652f Stavros Sachtouris
    @property
276 62c6652f Stavros Sachtouris
    def value(self):
277 62c6652f Stavros Sachtouris
        return super(AllocationPoolArgument, self).value or []
278 62c6652f Stavros Sachtouris
279 62c6652f Stavros Sachtouris
    @value.setter
280 62c6652f Stavros Sachtouris
    def value(self, new_pools):
281 62c6652f Stavros Sachtouris
        new_list = []
282 62c6652f Stavros Sachtouris
        for pool in new_pools:
283 62c6652f Stavros Sachtouris
            start, comma, end = pool.partition(',')
284 62c6652f Stavros Sachtouris
            if not (start and comma and end):
285 62c6652f Stavros Sachtouris
                raise CLIInvalidArgument(
286 62c6652f Stavros Sachtouris
                    'Invalid allocation pool argument %s' % pool, details=[
287 62c6652f Stavros Sachtouris
                    'Allocation values must be of the form:',
288 62c6652f Stavros Sachtouris
                    '  <start address>,<end address>'])
289 62c6652f Stavros Sachtouris
            new_list.append(dict(start=start, end=end))
290 62c6652f Stavros Sachtouris
        self._value = new_list
291 62c6652f Stavros Sachtouris
292 62c6652f Stavros Sachtouris
293 62c6652f Stavros Sachtouris
@command(subnet_cmds)
294 62c6652f Stavros Sachtouris
class subnet_create(_init_network, _optional_json):
295 62c6652f Stavros Sachtouris
    """Create a new subnet
296 62c6652f Stavros Sachtouris
    """
297 62c6652f Stavros Sachtouris
298 62c6652f Stavros Sachtouris
    arguments = dict(
299 62c6652f Stavros Sachtouris
        name=ValueArgument('Subnet name', '--name'),
300 62c6652f Stavros Sachtouris
        allocation_pools=AllocationPoolArgument(
301 62c6652f Stavros Sachtouris
            'start_address,end_address of allocation pool (can be repeated)'
302 62c6652f Stavros Sachtouris
            ' e.g., --alloc-pool=123.45.67.1,123.45.67.8',
303 62c6652f Stavros Sachtouris
            '--alloc-pool'),
304 62c6652f Stavros Sachtouris
        gateway=ValueArgument('Gateway IP', '--gateway'),
305 62c6652f Stavros Sachtouris
        subnet_id=ValueArgument('The id for the subnet', '--id'),
306 62c6652f Stavros Sachtouris
        ipv6=FlagArgument('If set, IP version is set to 6, else 4', '--ipv6'),
307 62c6652f Stavros Sachtouris
        enable_dhcp=FlagArgument('Enable dhcp (default: off)', '--with-dhcp')
308 62c6652f Stavros Sachtouris
    )
309 62c6652f Stavros Sachtouris
310 62c6652f Stavros Sachtouris
    @errors.generic.all
311 62c6652f Stavros Sachtouris
    @errors.cyclades.connection
312 62c6652f Stavros Sachtouris
    @errors.cyclades.network_id
313 62c6652f Stavros Sachtouris
    def _run(self, network_id, cidr):
314 62c6652f Stavros Sachtouris
        net = self.client.create_subnet(
315 62c6652f Stavros Sachtouris
            network_id, cidr,
316 62c6652f Stavros Sachtouris
            self['name'], self['allocation_pools'], self['gateway'],
317 62c6652f Stavros Sachtouris
            self['subnet_id'], self['ipv6'], self['enable_dhcp'])
318 62c6652f Stavros Sachtouris
        self._print(net, self.print_dict)
319 62c6652f Stavros Sachtouris
320 62c6652f Stavros Sachtouris
    def main(self, network_id, cidr):
321 62c6652f Stavros Sachtouris
        super(self.__class__, self)._run()
322 62c6652f Stavros Sachtouris
        self._run(network_id=network_id, cidr=cidr)
323 62c6652f Stavros Sachtouris
324 62c6652f Stavros Sachtouris
325 62c6652f Stavros Sachtouris
# @command(subnet_cmds)
326 62c6652f Stavros Sachtouris
# class subnet_delete(_init_network, _optional_output_cmd):
327 62c6652f Stavros Sachtouris
#     """Delete a subnet"""
328 62c6652f Stavros Sachtouris
329 62c6652f Stavros Sachtouris
#     @errors.generic.all
330 62c6652f Stavros Sachtouris
#     @errors.cyclades.connection
331 62c6652f Stavros Sachtouris
#     def _run(self, subnet_id):
332 62c6652f Stavros Sachtouris
#         r = self.client.delete_subnet(subnet_id)
333 62c6652f Stavros Sachtouris
#         self._optional_output(r)
334 62c6652f Stavros Sachtouris
335 62c6652f Stavros Sachtouris
#     def main(self, subnet_id):
336 62c6652f Stavros Sachtouris
#         super(self.__class__, self)._run()
337 62c6652f Stavros Sachtouris
#         self._run(subnet_id=subnet_id)
338 62c6652f Stavros Sachtouris
339 62c6652f Stavros Sachtouris
340 62c6652f Stavros Sachtouris
@command(subnet_cmds)
341 62c6652f Stavros Sachtouris
class subnet_set(_init_network, _optional_json):
342 62c6652f Stavros Sachtouris
    """Set an attribute of a subnet, leave the rest untouched (update)
343 62c6652f Stavros Sachtouris
    Only "--name" is supported for now
344 62c6652f Stavros Sachtouris
    """
345 62c6652f Stavros Sachtouris
346 62c6652f Stavros Sachtouris
    arguments = dict(name=ValueArgument('New name of the subnet', '--name'))
347 62c6652f Stavros Sachtouris
348 62c6652f Stavros Sachtouris
    @errors.generic.all
349 62c6652f Stavros Sachtouris
    @errors.cyclades.connection
350 62c6652f Stavros Sachtouris
    def _run(self, subnet_id):
351 62c6652f Stavros Sachtouris
        if self['name'] in (None, ):
352 62c6652f Stavros Sachtouris
            raise CLISyntaxError(
353 62c6652f Stavros Sachtouris
                'Missing subnet attributes to update',
354 62c6652f Stavros Sachtouris
                details=[
355 62c6652f Stavros Sachtouris
                    'At least one if the following is expected:',
356 62c6652f Stavros Sachtouris
                    '  --name=<new name>'])
357 62c6652f Stavros Sachtouris
        r = self.client.get_subnet_details(subnet_id)
358 62c6652f Stavros Sachtouris
        r = self.client.update_subnet(
359 62c6652f Stavros Sachtouris
            subnet_id, r['network_id'], name=self['name'])
360 62c6652f Stavros Sachtouris
        self._print(r, self.print_dict)
361 62c6652f Stavros Sachtouris
362 62c6652f Stavros Sachtouris
    def main(self, subnet_id):
363 62c6652f Stavros Sachtouris
        super(self.__class__, self)._run()
364 62c6652f Stavros Sachtouris
        self._run(subnet_id=subnet_id)
365 447365fe Stavros Sachtouris
366 447365fe Stavros Sachtouris
367 447365fe Stavros Sachtouris
@command(port_cmds)
368 11cc86af Stavros Sachtouris
class port_list(_init_network, _optional_json):
369 11cc86af Stavros Sachtouris
    """List all ports"""
370 447365fe Stavros Sachtouris
371 447365fe Stavros Sachtouris
    @errors.generic.all
372 447365fe Stavros Sachtouris
    @errors.cyclades.connection
373 11cc86af Stavros Sachtouris
    def _run(self):
374 11cc86af Stavros Sachtouris
        net = self.client.list_ports()
375 1d565254 Stavros Sachtouris
        self._print(net)
376 447365fe Stavros Sachtouris
377 11cc86af Stavros Sachtouris
    def main(self):
378 447365fe Stavros Sachtouris
        super(self.__class__, self)._run()
379 11cc86af Stavros Sachtouris
        self._run()
380 447365fe Stavros Sachtouris
381 447365fe Stavros Sachtouris
382 447365fe Stavros Sachtouris
@command(port_cmds)
383 447365fe Stavros Sachtouris
class port_info(_init_network, _optional_json):
384 447365fe Stavros Sachtouris
    """Get details about a port"""
385 447365fe Stavros Sachtouris
386 447365fe Stavros Sachtouris
    @errors.generic.all
387 447365fe Stavros Sachtouris
    @errors.cyclades.connection
388 447365fe Stavros Sachtouris
    def _run(self, port_id):
389 447365fe Stavros Sachtouris
        net = self.client.get_port_details(port_id)
390 447365fe Stavros Sachtouris
        self._print(net, self.print_dict)
391 447365fe Stavros Sachtouris
392 447365fe Stavros Sachtouris
    def main(self, port_id):
393 447365fe Stavros Sachtouris
        super(self.__class__, self)._run()
394 447365fe Stavros Sachtouris
        self._run(port_id=port_id)
395 447365fe Stavros Sachtouris
396 447365fe Stavros Sachtouris
397 447365fe Stavros Sachtouris
@command(port_cmds)
398 447365fe Stavros Sachtouris
class port_delete(_init_network, _optional_output_cmd):
399 447365fe Stavros Sachtouris
    """Delete a port"""
400 447365fe Stavros Sachtouris
401 447365fe Stavros Sachtouris
    @errors.generic.all
402 447365fe Stavros Sachtouris
    @errors.cyclades.connection
403 447365fe Stavros Sachtouris
    def _run(self, port_id):
404 447365fe Stavros Sachtouris
        r = self.client.delete_port(port_id)
405 447365fe Stavros Sachtouris
        self._optional_output(r)
406 447365fe Stavros Sachtouris
407 447365fe Stavros Sachtouris
    def main(self, port_id):
408 447365fe Stavros Sachtouris
        super(self.__class__, self)._run()
409 447365fe Stavros Sachtouris
        self._run(port_id=port_id)
410 447365fe Stavros Sachtouris
411 447365fe Stavros Sachtouris
412 447365fe Stavros Sachtouris
@command(port_cmds)
413 447365fe Stavros Sachtouris
class port_set(_init_network, _optional_json):
414 447365fe Stavros Sachtouris
    """Set an attribute of a port, leave the rest untouched (update)
415 447365fe Stavros Sachtouris
    Only "--name" is supported for now
416 447365fe Stavros Sachtouris
    """
417 447365fe Stavros Sachtouris
418 447365fe Stavros Sachtouris
    arguments = dict(name=ValueArgument('New name of the port', '--name'))
419 447365fe Stavros Sachtouris
420 447365fe Stavros Sachtouris
    @errors.generic.all
421 447365fe Stavros Sachtouris
    @errors.cyclades.connection
422 447365fe Stavros Sachtouris
    def _run(self, port_id):
423 447365fe Stavros Sachtouris
        if self['name'] in (None, ):
424 447365fe Stavros Sachtouris
            raise CLISyntaxError(
425 447365fe Stavros Sachtouris
                'Missing port attributes to update',
426 447365fe Stavros Sachtouris
                details=[
427 447365fe Stavros Sachtouris
                    'At least one if the following is expected:',
428 447365fe Stavros Sachtouris
                    '  --name=<new name>'])
429 447365fe Stavros Sachtouris
        r = self.client.get_port_details(port_id)
430 447365fe Stavros Sachtouris
        r = self.client.update_port(
431 447365fe Stavros Sachtouris
            port_id, r['network_id'], name=self['name'])
432 447365fe Stavros Sachtouris
        self._print(r, self.print_dict)
433 447365fe Stavros Sachtouris
434 447365fe Stavros Sachtouris
    def main(self, port_id):
435 447365fe Stavros Sachtouris
        super(self.__class__, self)._run()
436 447365fe Stavros Sachtouris
        self._run(port_id=port_id)
437 447365fe Stavros Sachtouris
438 447365fe Stavros Sachtouris
439 ccdd1b82 Stavros Sachtouris
@command(port_cmds)
440 ccdd1b82 Stavros Sachtouris
class port_create(_init_network, _optional_json):
441 ccdd1b82 Stavros Sachtouris
    """Create a new port"""
442 ccdd1b82 Stavros Sachtouris
443 ccdd1b82 Stavros Sachtouris
    arguments = dict(
444 b7d79306 Stavros Sachtouris
        name=ValueArgument('A human readable name', '--name'),
445 ccdd1b82 Stavros Sachtouris
        security_group_id=RepeatableArgument(
446 ccdd1b82 Stavros Sachtouris
            'Add a security group id (can be repeated)',
447 b7d79306 Stavros Sachtouris
            ('-g', '--security-group')),
448 b7d79306 Stavros Sachtouris
        subnet_id=ValueArgument(
449 b7d79306 Stavros Sachtouris
            'Subnet id for fixed ips (used with --ip-address)',
450 b7d79306 Stavros Sachtouris
            '--subnet-id'),
451 b7d79306 Stavros Sachtouris
        ip_address=ValueArgument(
452 b7d79306 Stavros Sachtouris
            'IP address for subnet id (used with --subnet-id', '--ip-address')
453 ccdd1b82 Stavros Sachtouris
    )
454 ccdd1b82 Stavros Sachtouris
455 ccdd1b82 Stavros Sachtouris
    @errors.generic.all
456 ccdd1b82 Stavros Sachtouris
    @errors.cyclades.connection
457 ccdd1b82 Stavros Sachtouris
    @errors.cyclades.network_id
458 ccdd1b82 Stavros Sachtouris
    def _run(self, network_id, device_id):
459 56d84a4e Stavros Sachtouris
        if bool(self['subnet_id']) != bool(self['ip_address']):
460 b7d79306 Stavros Sachtouris
            raise CLIInvalidArgument('Invalid use of arguments', details=[
461 56d84a4e Stavros Sachtouris
                '--subnet-id and --ip-address should be used together'])
462 56d84a4e Stavros Sachtouris
        fixed_ips = [dict(
463 56d84a4e Stavros Sachtouris
            subnet_id=self['subnet_id'], ip_address=self['ip_address'])] if (
464 b7d79306 Stavros Sachtouris
                self['subnet_id']) else None
465 ccdd1b82 Stavros Sachtouris
        r = self.client.create_port(
466 b7d79306 Stavros Sachtouris
            network_id, device_id,
467 b7d79306 Stavros Sachtouris
            name=self['name'],
468 b7d79306 Stavros Sachtouris
            security_groups=self['security_group_id'],
469 b7d79306 Stavros Sachtouris
            fixed_ips=fixed_ips)
470 ccdd1b82 Stavros Sachtouris
        self._print(r, self.print_dict)
471 ccdd1b82 Stavros Sachtouris
472 ccdd1b82 Stavros Sachtouris
    def main(self, network_id, device_id):
473 ccdd1b82 Stavros Sachtouris
        super(self.__class__, self)._run()
474 ccdd1b82 Stavros Sachtouris
        self._run(network_id=network_id, device_id=device_id)