root / kamaki / cli / commands / network.py @ 5c433331
History | View | Annotate | Download (7.1 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 base64 import b64encode |
35 | d18e6439 | Stavros Sachtouris | from os.path import exists, expanduser |
36 | d18e6439 | Stavros Sachtouris | from io import StringIO |
37 | d18e6439 | Stavros Sachtouris | from pydoc import pager |
38 | d18e6439 | Stavros Sachtouris | |
39 | d18e6439 | Stavros Sachtouris | from kamaki.cli import command |
40 | d18e6439 | Stavros Sachtouris | from kamaki.cli.command_tree import CommandTree |
41 | d18e6439 | Stavros Sachtouris | from kamaki.cli.utils import remove_from_items, filter_dicts_by_dict |
42 | d18e6439 | Stavros Sachtouris | from kamaki.cli.errors import ( |
43 | d18e6439 | Stavros Sachtouris | raiseCLIError, CLISyntaxError, CLIBaseUrlError, CLIInvalidArgument) |
44 | 5c433331 | Stavros Sachtouris | from kamaki.clients.network import NetworkClient, ClientError |
45 | d18e6439 | Stavros Sachtouris | from kamaki.cli.argument import FlagArgument, ValueArgument, KeyValueArgument |
46 | d18e6439 | Stavros Sachtouris | from kamaki.cli.argument import ProgressBarArgument, DateArgument, IntArgument |
47 | d18e6439 | Stavros Sachtouris | from kamaki.cli.commands import _command_init, errors, addLogSettings |
48 | d18e6439 | Stavros Sachtouris | from kamaki.cli.commands import ( |
49 | d18e6439 | Stavros Sachtouris | _optional_output_cmd, _optional_json, _name_filter, _id_filter) |
50 | d18e6439 | Stavros Sachtouris | |
51 | d18e6439 | Stavros Sachtouris | |
52 | d18e6439 | Stavros Sachtouris | network_cmds = CommandTree('network', 'Networking API network commands') |
53 | d18e6439 | Stavros Sachtouris | port_cmds = CommandTree('port', 'Networking API network commands') |
54 | d18e6439 | Stavros Sachtouris | subnet_cmds = CommandTree('subnet', 'Networking API network commands') |
55 | d18e6439 | Stavros Sachtouris | _commands = [network_cmds, port_cmds, subnet_cmds] |
56 | d18e6439 | Stavros Sachtouris | |
57 | d18e6439 | Stavros Sachtouris | |
58 | d18e6439 | Stavros Sachtouris | about_authentication = '\nUser Authentication:\
|
59 | d18e6439 | Stavros Sachtouris | \n* to check authentication: /user authenticate\
|
60 | d18e6439 | Stavros Sachtouris | \n* to set authentication token: /config set cloud.<cloud>.token <token>'
|
61 | d18e6439 | Stavros Sachtouris | |
62 | d18e6439 | Stavros Sachtouris | |
63 | 5c433331 | Stavros Sachtouris | class _init_network(_command_init): |
64 | bc4662d8 | Stavros Sachtouris | @errors.generic.all
|
65 | bc4662d8 | Stavros Sachtouris | @addLogSettings
|
66 | bc4662d8 | Stavros Sachtouris | def _run(self, service='network'): |
67 | bc4662d8 | Stavros Sachtouris | if getattr(self, 'cloud', None): |
68 | bc4662d8 | Stavros Sachtouris | base_url = self._custom_url(service) or self._custom_url( |
69 | bc4662d8 | Stavros Sachtouris | 'compute')
|
70 | bc4662d8 | Stavros Sachtouris | if base_url:
|
71 | bc4662d8 | Stavros Sachtouris | token = self._custom_token(service) or self._custom_token( |
72 | bc4662d8 | Stavros Sachtouris | 'compute') or self.config.get_cloud('token') |
73 | 5c433331 | Stavros Sachtouris | self.client = NetworkClient(
|
74 | bc4662d8 | Stavros Sachtouris | base_url=base_url, token=token) |
75 | bc4662d8 | Stavros Sachtouris | return
|
76 | bc4662d8 | Stavros Sachtouris | else:
|
77 | bc4662d8 | Stavros Sachtouris | self.cloud = 'default' |
78 | bc4662d8 | Stavros Sachtouris | if getattr(self, 'auth_base', False): |
79 | bc4662d8 | Stavros Sachtouris | cyclades_endpoints = self.auth_base.get_service_endpoints(
|
80 | bc4662d8 | Stavros Sachtouris | self._custom_type('compute') or 'compute', |
81 | bc4662d8 | Stavros Sachtouris | self._custom_version('compute') or '') |
82 | bc4662d8 | Stavros Sachtouris | base_url = cyclades_endpoints['publicURL']
|
83 | bc4662d8 | Stavros Sachtouris | token = self.auth_base.token
|
84 | 5c433331 | Stavros Sachtouris | self.client = NetworkClient(base_url=base_url, token=token)
|
85 | bc4662d8 | Stavros Sachtouris | else:
|
86 | bc4662d8 | Stavros Sachtouris | raise CLIBaseUrlError(service='network') |
87 | bc4662d8 | Stavros Sachtouris | |
88 | bc4662d8 | Stavros Sachtouris | def main(self): |
89 | bc4662d8 | Stavros Sachtouris | self._run()
|
90 | d18e6439 | Stavros Sachtouris | |
91 | d18e6439 | Stavros Sachtouris | |
92 | d18e6439 | Stavros Sachtouris | @command(network_cmds)
|
93 | 5c433331 | Stavros Sachtouris | class network_list(_init_network, _optional_json, _name_filter, _id_filter): |
94 | d18e6439 | Stavros Sachtouris | """List networks
|
95 | d18e6439 | Stavros Sachtouris | Use filtering arguments (e.g., --name-like) to manage long server lists
|
96 | d18e6439 | Stavros Sachtouris | """
|
97 | d18e6439 | Stavros Sachtouris | |
98 | d18e6439 | Stavros Sachtouris | arguments = dict(
|
99 | d18e6439 | Stavros Sachtouris | detail=FlagArgument('show detailed output', ('-l', '--details')), |
100 | cd40143a | Stavros Sachtouris | more=FlagArgument( |
101 | cd40143a | Stavros Sachtouris | 'output results in pages (-n to set items per page, default 10)',
|
102 | cd40143a | Stavros Sachtouris | '--more'),
|
103 | d18e6439 | Stavros Sachtouris | ) |
104 | d18e6439 | Stavros Sachtouris | |
105 | d18e6439 | Stavros Sachtouris | @errors.generic.all
|
106 | d18e6439 | Stavros Sachtouris | @errors.cyclades.connection
|
107 | d18e6439 | Stavros Sachtouris | def _run(self): |
108 | d18e6439 | Stavros Sachtouris | nets = self.client.list_networks()
|
109 | 77bf75fe | Stavros Sachtouris | nets = self._filter_by_name(nets)
|
110 | 77bf75fe | Stavros Sachtouris | nets = self._filter_by_id(nets)
|
111 | d18e6439 | Stavros Sachtouris | if not self['detail']: |
112 | 77bf75fe | Stavros Sachtouris | nets = [dict(id=net['id'], name=net['name']) for net in nets] |
113 | cd40143a | Stavros Sachtouris | kwargs = dict()
|
114 | cd40143a | Stavros Sachtouris | if self['more']: |
115 | cd40143a | Stavros Sachtouris | kwargs['out'] = StringIO()
|
116 | cd40143a | Stavros Sachtouris | kwargs['title'] = ()
|
117 | cd40143a | Stavros Sachtouris | self._print(nets, **kwargs)
|
118 | cd40143a | Stavros Sachtouris | if self['more']: |
119 | cd40143a | Stavros Sachtouris | pager(kwargs['out'].getvalue())
|
120 | d18e6439 | Stavros Sachtouris | |
121 | d18e6439 | Stavros Sachtouris | def main(self): |
122 | d18e6439 | Stavros Sachtouris | super(self.__class__, self)._run() |
123 | d18e6439 | Stavros Sachtouris | self._run()
|
124 | cd40143a | Stavros Sachtouris | |
125 | cd40143a | Stavros Sachtouris | |
126 | cd40143a | Stavros Sachtouris | @command(network_cmds)
|
127 | 5c433331 | Stavros Sachtouris | class network_info(_init_network, _optional_json): |
128 | cd40143a | Stavros Sachtouris | """Get details about a network"""
|
129 | cd40143a | Stavros Sachtouris | |
130 | cd40143a | Stavros Sachtouris | @errors.generic.all
|
131 | cd40143a | Stavros Sachtouris | @errors.cyclades.connection
|
132 | cd40143a | Stavros Sachtouris | @errors.cyclades.network_id
|
133 | cd40143a | Stavros Sachtouris | def _run(self, network_id): |
134 | cd40143a | Stavros Sachtouris | net = self.client.get_network_details(network_id)
|
135 | cd40143a | Stavros Sachtouris | self._print(net, self.print_dict) |
136 | cd40143a | Stavros Sachtouris | |
137 | cd40143a | Stavros Sachtouris | def main(self, network_id): |
138 | cd40143a | Stavros Sachtouris | super(self.__class__, self)._run() |
139 | cd40143a | Stavros Sachtouris | self._run(network_id=network_id)
|
140 | 49413939 | Stavros Sachtouris | |
141 | 49413939 | Stavros Sachtouris | |
142 | 49413939 | Stavros Sachtouris | @command(network_cmds)
|
143 | 5c433331 | Stavros Sachtouris | class network_create(_init_network, _optional_json): |
144 | 49413939 | Stavros Sachtouris | """Create a new network"""
|
145 | 49413939 | Stavros Sachtouris | |
146 | 49413939 | Stavros Sachtouris | arguments = dict(shared=FlagArgument(
|
147 | 49413939 | Stavros Sachtouris | 'Network will be shared (special privileges required)', '--shared') |
148 | 49413939 | Stavros Sachtouris | ) |
149 | 49413939 | Stavros Sachtouris | |
150 | 49413939 | Stavros Sachtouris | @errors.generic.all
|
151 | 49413939 | Stavros Sachtouris | @errors.cyclades.connection
|
152 | 49413939 | Stavros Sachtouris | def _run(self, name): |
153 | 49413939 | Stavros Sachtouris | # admin_state_up is not used in Cyclades
|
154 | 49413939 | Stavros Sachtouris | net = self.client.create_network(
|
155 | 58602137 | Stavros Sachtouris | name, shared=self['shared']) |
156 | 49413939 | Stavros Sachtouris | self._print(net, self.print_dict) |
157 | 49413939 | Stavros Sachtouris | |
158 | 49413939 | Stavros Sachtouris | def main(self, name): |
159 | 49413939 | Stavros Sachtouris | super(self.__class__, self)._run() |
160 | 49413939 | Stavros Sachtouris | self._run(name=name)
|
161 | aef3fa1f | Stavros Sachtouris | |
162 | aef3fa1f | Stavros Sachtouris | |
163 | aef3fa1f | Stavros Sachtouris | @command(network_cmds)
|
164 | 5c433331 | Stavros Sachtouris | class network_delete(_init_network, _optional_output_cmd): |
165 | aef3fa1f | Stavros Sachtouris | """Delete a network"""
|
166 | aef3fa1f | Stavros Sachtouris | |
167 | aef3fa1f | Stavros Sachtouris | @errors.generic.all
|
168 | aef3fa1f | Stavros Sachtouris | @errors.cyclades.connection
|
169 | 58602137 | Stavros Sachtouris | @errors.cyclades.network_id
|
170 | aef3fa1f | Stavros Sachtouris | def _run(self, network_id): |
171 | aef3fa1f | Stavros Sachtouris | r = self.client.delete_network(network_id)
|
172 | aef3fa1f | Stavros Sachtouris | self._optional_output(r)
|
173 | aef3fa1f | Stavros Sachtouris | |
174 | aef3fa1f | Stavros Sachtouris | def main(self, network_id): |
175 | aef3fa1f | Stavros Sachtouris | super(self.__class__, self)._run() |
176 | aef3fa1f | Stavros Sachtouris | self._run(network_id=network_id)
|
177 | 58602137 | Stavros Sachtouris | |
178 | 58602137 | Stavros Sachtouris | |
179 | 58602137 | Stavros Sachtouris | @command(network_cmds)
|
180 | 5c433331 | Stavros Sachtouris | class network_set(_init_network, _optional_json): |
181 | 58602137 | Stavros Sachtouris | """Set an attribute of a network, leave the rest untouched (update)
|
182 | 58602137 | Stavros Sachtouris | Only "--name" is supported for now
|
183 | 58602137 | Stavros Sachtouris | """
|
184 | 58602137 | Stavros Sachtouris | |
185 | 58602137 | Stavros Sachtouris | arguments = dict(name=ValueArgument('New name of the network', '--name')) |
186 | 58602137 | Stavros Sachtouris | |
187 | 58602137 | Stavros Sachtouris | @errors.generic.all
|
188 | 58602137 | Stavros Sachtouris | @errors.cyclades.connection
|
189 | 58602137 | Stavros Sachtouris | @errors.cyclades.network_id
|
190 | 58602137 | Stavros Sachtouris | def _run(self, network_id): |
191 | 58602137 | Stavros Sachtouris | if self['name'] in (None, ): |
192 | 58602137 | Stavros Sachtouris | raise CLISyntaxError(
|
193 | 58602137 | Stavros Sachtouris | 'Missing network atrributes to update',
|
194 | 58602137 | Stavros Sachtouris | details=[ |
195 | 58602137 | Stavros Sachtouris | 'At least one if the following is expected:',
|
196 | 58602137 | Stavros Sachtouris | ' --name=<new name>'])
|
197 | 58602137 | Stavros Sachtouris | r = self.client.update_network(network_id, name=self['name']) |
198 | 58602137 | Stavros Sachtouris | self._print(r, self.print_dict) |
199 | 58602137 | Stavros Sachtouris | |
200 | 58602137 | Stavros Sachtouris | def main(self, network_id): |
201 | 58602137 | Stavros Sachtouris | super(self.__class__, self)._run() |
202 | 58602137 | Stavros Sachtouris | self._run(network_id=network_id) |