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