Debian Codegraph @

neutronclient/osc/v2/sfc/sfc_port_pair_group.py

raw
1 # Copyright (c) 2017 Huawei Technologies India Pvt.Limited.
2 # All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16 import logging
17
18 from osc_lib.cli import parseractions
19 from osc_lib.command import command
20 from osc_lib import exceptions
21 from osc_lib import utils
22 from osc_lib.utils import columns as column_util
23
24 from neutronclient._i18n import _
25
26 LOG = logging.getLogger(__name__)
27
28 resource = 'port_pair_group'
29
30 _attr_map = (
31 ('id', 'ID', column_util.LIST_BOTH),
32 ('name', 'Name', column_util.LIST_BOTH),
33 ('port_pairs', 'Port Pair', column_util.LIST_BOTH),
34 ('port_pair_group_parameters', 'Port Pair Group Parameters',
35 column_util.LIST_BOTH),
36 ('description', 'Description', column_util.LIST_LONG_ONLY),
37 ('project_id', 'Project', column_util.LIST_LONG_ONLY),
38 ('is_tap_enabled', 'Tap Enabled', column_util.LIST_BOTH)
39 )
40
41 _attr_map_dict = {
42 'id': 'ID',
43 'name': 'Name',
44 'description': 'Description',
45 'port_pairs': 'Port Pair',
46 'port_pair_group_parameters': 'Port Pair Group Parameters',
47 'is_tap_enabled': 'Tap Enabled',
48 'tenant_id': 'Project',
49 'project_id': 'Project',
50 }
51
52
53 class CreateSfcPortPairGroup(command.ShowOne):
54 _description = _("Create a port pair group")
55
56 def get_parser(self, prog_name):
57 parser = super(CreateSfcPortPairGroup, self).get_parser(prog_name)
58 parser.add_argument(
59 'name',
60 metavar='<name>',
61 help=_('Name of the port pair group'))
62 parser.add_argument(
63 '--description',
64 metavar='<description>',
65 help=_('Description for the port pair group'))
66 parser.add_argument(
67 '--port-pair',
68 metavar='<port-pair>',
69 dest='port_pairs',
70 default=[],
71 action='append',
72 help=_('Port pair (name or ID). '
73 'This option can be repeated.'))
74 tap_enable = parser.add_mutually_exclusive_group()
75 tap_enable.add_argument(
76 '--enable-tap',
77 action='store_true',
78 help=_('Port pairs of this port pair group are deployed as '
79 'passive tap service function')
80 )
81 tap_enable.add_argument(
82 '--disable-tap',
83 action='store_true',
84 help=_('Port pairs of this port pair group are deployed as l3 '
85 'service function (default)')
86 )
87 parser.add_argument(
88 '--port-pair-group-parameters',
89 metavar='lb-fields=<lb-fields>',
90 action=parseractions.KeyValueAction,
91 help=_('Dictionary of port pair group parameters. '
92 'Currently only one parameter lb-fields is supported. '
93 '<lb-fields> is a & separated list of load-balancing '
94 'fields.'))
95 return parser
96
97 def take_action(self, parsed_args):
98 client = self.app.client_manager.network
99 attrs = _get_common_attrs(self.app.client_manager, parsed_args)
100 obj = client.create_sfc_port_pair_group(**attrs)
101 display_columns, columns = utils.get_osc_show_columns_for_sdk_resource(
102 obj, _attr_map_dict, ['location', 'tenant_id'])
103 data = utils.get_dict_properties(obj, columns)
104 return display_columns, data
105
106
107 class DeleteSfcPortPairGroup(command.Command):
108 _description = _("Delete a given port pair group")
109
110 def get_parser(self, prog_name):
111 parser = super(DeleteSfcPortPairGroup, self).get_parser(prog_name)
112 parser.add_argument(
113 'port_pair_group',
114 metavar='<port-pair-group>',
115 nargs='+',
116 help=_("Port pair group(s) to delete (name or ID)")
117 )
118 return parser
119
120 def take_action(self, parsed_args):
121 client = self.app.client_manager.network
122 result = 0
123 for ppg in parsed_args.port_pair_group:
124 try:
125 ppg_id = client.find_sfc_port_pair_group(
126 ppg, ignore_missing=False)['id']
127 client.delete_sfc_port_pair_group(ppg_id)
128 except Exception as e:
129 result += 1
130 LOG.error(_("Failed to delete port pair group with name "
131 "or ID '%(ppg)s': %(e)s"), {'ppg': ppg, 'e': e})
132 if result > 0:
133 total = len(parsed_args.port_pair_group)
134 msg = (_("%(result)s of %(total)s port pair group(s) "
135 "failed to delete.") % {'result': result,
136 'total': total})
137 raise exceptions.CommandError(msg)
138
139
140 class ListSfcPortPairGroup(command.Lister):
141 _description = _("List port pair group")
142
143 def get_parser(self, prog_name):
144 parser = super(ListSfcPortPairGroup, self).get_parser(prog_name)
145 parser.add_argument(
146 '--long',
147 action='store_true',
148 default=False,
149 help=_("List additional fields in output")
150 )
151 return parser
152
153 def take_action(self, parsed_args):
154 client = self.app.client_manager.network
155 data = client.sfc_port_pair_groups()
156 headers, columns = column_util.get_column_definitions(
157 _attr_map, long_listing=parsed_args.long)
158 return (headers,
159 (utils.get_dict_properties(
160 s, columns,
161 ) for s in data))
162
163
164 class SetSfcPortPairGroup(command.Command):
165 _description = _("Set port pair group properties")
166
167 def get_parser(self, prog_name):
168 parser = super(SetSfcPortPairGroup, self).get_parser(prog_name)
169 parser.add_argument(
170 'port_pair_group',
171 metavar='<port-pair-group>',
172 help=_("Port pair group to modify (name or ID)"))
173 parser.add_argument(
174 '--name',
175 metavar='<name>',
176 help=_('Name of the port pair group'))
177 parser.add_argument(
178 '--description',
179 metavar='<description>',
180 help=_('Description for the port pair group'))
181 parser.add_argument(
182 '--port-pair',
183 metavar='<port-pair>',
184 dest='port_pairs',
185 default=[],
186 action='append',
187 help=_('Port pair (name or ID). '
188 'This option can be repeated.'))
189 parser.add_argument(
190 '--no-port-pair',
191 action='store_true',
192 help=_('Remove all port pair from port pair group'))
193 return parser
194
195 def take_action(self, parsed_args):
196 client = self.app.client_manager.network
197 ppg_id = client.find_sfc_port_pair_group(
198 parsed_args.port_pair_group)['id']
199 attrs = _get_common_attrs(self.app.client_manager, parsed_args,
200 is_create=False)
201 if parsed_args.no_port_pair:
202 attrs['port_pairs'] = []
203 if parsed_args.port_pairs:
204 added = [client.find_sfc_port_pair(pp,
205 ignore_missing=False)['id']
206 for pp in parsed_args.port_pairs]
207 if parsed_args.no_port_pair:
208 existing = []
209 else:
210 existing = client.find_sfc_port_pair_group(
211 parsed_args.port_pair_group,
212 ignore_missing=False)['port_pairs']
213 attrs['port_pairs'] = sorted(list(set(existing) | set(added)))
214 try:
215 client.update_sfc_port_pair_group(ppg_id, **attrs)
216 except Exception as e:
217 msg = (_("Failed to update port pair group '%(ppg)s': %(e)s")
218 % {'ppg': parsed_args.port_pair_group, 'e': e})
219 raise exceptions.CommandError(msg)
220
221
222 class ShowSfcPortPairGroup(command.ShowOne):
223 _description = _("Display port pair group details")
224
225 def get_parser(self, prog_name):
226 parser = super(ShowSfcPortPairGroup, self).get_parser(prog_name)
227 parser.add_argument(
228 'port_pair_group',
229 metavar='<port-pair-group>',
230 help=_("Port pair group to display (name or ID)")
231 )
232 return parser
233
234 def take_action(self, parsed_args):
235 client = self.app.client_manager.network
236 ppg_id = client.find_sfc_port_pair_group(
237 parsed_args.port_pair_group, ignore_missing=False)['id']
238 obj = client.get_sfc_port_pair_group(ppg_id)
239 display_columns, columns = utils.get_osc_show_columns_for_sdk_resource(
240 obj, _attr_map_dict, ['location', 'tenant_id'])
241 data = utils.get_dict_properties(obj, columns)
242 return display_columns, data
243
244
245 class UnsetSfcPortPairGroup(command.Command):
246 _description = _("Unset port pairs from port pair group")
247
248 def get_parser(self, prog_name):
249 parser = super(UnsetSfcPortPairGroup, self).get_parser(prog_name)
250 parser.add_argument(
251 'port_pair_group',
252 metavar='<port-pair-group>',
253 help=_("Port pair group to unset (name or ID)"))
254 port_pair_group = parser.add_mutually_exclusive_group()
255 port_pair_group.add_argument(
256 '--port-pair',
257 action='append',
258 metavar='<port-pair>',
259 dest='port_pairs',
260 help=_('Remove port pair(s) from the port pair group '
261 '(name or ID). This option can be repeated.'))
262 port_pair_group.add_argument(
263 '--all-port-pair',
264 action='store_true',
265 help=_('Remove all port pairs from the port pair group'))
266 return parser
267
268 def take_action(self, parsed_args):
269 client = self.app.client_manager.network
270 ppg_id = client.find_sfc_port_pair_group(
271 parsed_args.port_pair_group, ignore_missing=False)['id']
272 attrs = {}
273 if parsed_args.port_pairs:
274 existing = client.find_sfc_port_pair_group(
275 parsed_args.port_pair_group,
276 ignore_missing=False)['port_pairs']
277 removed = [client.find_sfc_port_pair(pp,
278 ignore_missing=False)['id']
279 for pp in parsed_args.port_pairs]
280 attrs['port_pairs'] = list(set(existing) - set(removed))
281 if parsed_args.all_port_pair:
282 attrs['port_pairs'] = []
283 try:
284 client.update_sfc_port_pair_group(ppg_id, **attrs)
285 except Exception as e:
286 msg = (_("Failed to unset port pair group '%(ppg)s': %(e)s")
287 % {'ppg': parsed_args.port_pair_group, 'e': e})
288 raise exceptions.CommandError(msg)
289
290
291 def _get_ppg_param(attrs, ppg):
292 attrs['port_pair_group_parameters'] = {}
293 for key, value in ppg.items():
294 if key == 'lb-fields':
295 attrs['port_pair_group_parameters']['lb_fields'] = ([
296 field for field in value.split('&') if field])
297 else:
298 attrs['port_pair_group_parameters'][key] = value
299 return attrs['port_pair_group_parameters']
300
301
302 def _get_common_attrs(client_manager, parsed_args, is_create=True):
303 client = client_manager.network
304 attrs = {}
305 if parsed_args.name is not None:
306 attrs['name'] = parsed_args.name
307 if parsed_args.description is not None:
308 attrs['description'] = parsed_args.description
309 if parsed_args.port_pairs:
310 attrs['port_pairs'] = [client.find_sfc_port_pair(
311 pp, ignore_missing=False)['id']
312 for pp in parsed_args.port_pairs]
313
314 if is_create:
315 _get_attrs(attrs, parsed_args)
316 return attrs
317
318
319 def _get_attrs(attrs, parsed_args):
320 if parsed_args.port_pair_group_parameters is not None:
321 attrs['port_pair_group_parameters'] = (
322 _get_ppg_param(attrs, parsed_args.port_pair_group_parameters))
323 if parsed_args.enable_tap:
324 attrs['tap_enabled'] = True
325 if parsed_args.disable_tap:
326 attrs['tap_enabled'] = False