Debian Codegraph @

networking_l2gw/l2gatewayclient/osc/l2gw.py

raw
1 # All Rights Reserved 2018
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15 import logging
16
17 from osc_lib.cli import format_columns
18 from osc_lib.cli import identity as identity_utils
19 from osc_lib.command import command
20 from osc_lib import exceptions
21 from osc_lib import utils as osc_utils
22 from osc_lib.utils import columns as column_util
23
24 from neutronclient.common import utils
25 from neutronclient.osc import utils as nc_osc_utils
26
27 from networking_l2gw._i18n import _
28 from networking_l2gw.l2gatewayclient.osc import sdk_objects
29
30
31 LOG = logging.getLogger(__name__)
32
33 INTERFACE_DELIMITER = ";"
34 SEGMENTATION_ID_DELIMITER = "#"
35 INTERFACE_SEG_ID_DELIMITER = "|"
36
37 L2_GATEWAY = 'l2_gateway'
38 L2_GATEWAYS = '%ss' % L2_GATEWAY
39
40 path = 'l2-gateways'
41 object_path = '/%s' % path
42 resource_path = '/%s/%%s' % path
43
44 _attr_map = (
45 ('id', 'ID', column_util.LIST_BOTH),
46 ('tenant_id', 'Tenant', column_util.LIST_LONG_ONLY),
47 ('name', 'Name', column_util.LIST_BOTH),
48 ('devices', 'Devices', column_util.LIST_BOTH),
49 )
50 _formatters = {
51 'devices': format_columns.ListDictColumn,
52 }
53
54
55 def _get_common_parser(parser):
56 """Adds to parser arguments common to create and update commands.
57
58 :params ArgumentParser parser: argparse object contains all command's
59 arguments
60 """
61 parser.add_argument(
62 '--device',
63 metavar='name=name,interface_names=INTERFACE-DETAILS',
64 action='append', dest='devices', type=utils.str2dict,
65 help=_('Device name and Interface-names of l2gateway. '
66 'INTERFACE-DETAILS is of form '
67 '\"<interface_name1>;[<interface_name2>]'
68 '[|<seg_id1>[#<seg_id2>]]\" '
69 '(--device option can be repeated)'))
70
71
72 def get_interface(interfaces):
73 interface_dict = []
74 for interface in interfaces:
75 if INTERFACE_SEG_ID_DELIMITER in interface:
76 int_name = interface.split(INTERFACE_SEG_ID_DELIMITER)[0]
77 segid = interface.split(INTERFACE_SEG_ID_DELIMITER)[1]
78 if SEGMENTATION_ID_DELIMITER in segid:
79 segid = segid.split(SEGMENTATION_ID_DELIMITER)
80 else:
81 segid = [segid]
82 interface_detail = {'name': int_name, 'segmentation_id': segid}
83 else:
84 interface_detail = {'name': interface}
85 interface_dict.append(interface_detail)
86 return interface_dict
87
88
89 def _args2body(parsed_args, update=False):
90 if parsed_args.devices:
91 devices = parsed_args.devices
92 interfaces = []
93 else:
94 devices = []
95 device_dict = []
96 for device in devices:
97 if 'interface_names' in device.keys():
98 interface = device['interface_names']
99
100 if INTERFACE_DELIMITER in interface:
101 interface_dict = interface.split(INTERFACE_DELIMITER)
102 interfaces = get_interface(interface_dict)
103 else:
104 interfaces = get_interface([interface])
105 if 'name' in device.keys():
106 device = {'device_name': device['name'],
107 'interfaces': interfaces}
108 else:
109 device = {'interfaces': interfaces}
110 device_dict.append(device)
111 if parsed_args.name:
112 l2gw_name = parsed_args.name
113 body = {'name': l2gw_name, 'devices': device_dict}
114 else:
115 body = {'devices': device_dict}
116 return body
117
118
119 class CreateL2gw(command.ShowOne):
120 _description = _("Create l2gateway resource")
121
122 def get_parser(self, prog_name):
123 parser = super().get_parser(prog_name)
124 identity_utils.add_project_owner_option_to_parser(parser)
125 parser.add_argument(
126 'name', metavar='<GATEWAY-NAME>',
127 help=_('Descriptive name for logical gateway.'))
128 _get_common_parser(parser)
129 return parser
130
131 def take_action(self, parsed_args):
132 client = self.app.client_manager.network
133 attrs = {}
134 if parsed_args.name is not None:
135 attrs['name'] = str(parsed_args.name)
136 if parsed_args.devices is not None:
137 attrs['devices'] = str(parsed_args.devices)
138 if 'project' in parsed_args and parsed_args.project is not None:
139 project_id = identity_utils.find_project(
140 self.app.client_manager.identity,
141 parsed_args.project,
142 parsed_args.project_domain,
143 ).id
144 attrs['tenant_id'] = project_id
145 body = _args2body(parsed_args)
146 obj = client._create(sdk_objects.L2gw, **body)
147 display_columns, columns = nc_osc_utils._get_columns(obj)
148 data = osc_utils.get_dict_properties(obj, columns)
149 return display_columns, data
150
151
152 class ListL2gw(command.Lister):
153 _description = _("List l2gateway that belongs to a given tenant")
154
155 def get_parser(self, prog_name):
156 parser = super().get_parser(prog_name)
157 identity_utils.add_project_owner_option_to_parser(parser)
158
159 return parser
160
161 def take_action(self, parsed_args):
162 client = self.app.client_manager.network
163 params = {}
164 if parsed_args.project is not None:
165 project_id = identity_utils.find_project(
166 self.app.client_manager.identity,
167 parsed_args.project,
168 parsed_args.project_domain,
169 ).id
170 params['tenant_id'] = project_id
171 objs = client._list(sdk_objects.L2gw,
172 retrieve_all=True, params=params)
173 headers, columns = column_util.get_column_definitions(
174 _attr_map, long_listing=True)
175 return (headers, (osc_utils.get_dict_properties(
176 s, columns) for s in objs))
177
178
179 class ShowL2gw(command.ShowOne):
180 _description = _("Show information of a given l2gateway")
181
182 def get_parser(self, prog_name):
183 parser = super().get_parser(prog_name)
184 parser.add_argument(
185 L2_GATEWAY,
186 metavar="<L2_GATEWAY>",
187 help=_("ID or name of l2_gateway to look up."),
188 )
189 return parser
190
191 def take_action(self, parsed_args):
192 client = self.app.client_manager.network
193 id = client._find(sdk_objects.L2gw, parsed_args.l2_gateway)
194 obj = client._get(sdk_objects.L2gw, id)
195 display_columns, columns = nc_osc_utils._get_columns(obj)
196 data = osc_utils.get_dict_properties(obj, columns)
197 return display_columns, data
198
199
200 class DeleteL2gw(command.Command):
201 _description = _("Delete a given l2gateway")
202
203 def get_parser(self, prog_name):
204 parser = super().get_parser(prog_name)
205 parser.add_argument(
206 L2_GATEWAYS,
207 metavar="<L2_GATEWAY>",
208 nargs="+",
209 help=_("ID(s) or name(s) of l2_gateway to delete."),
210 )
211 return parser
212
213 def take_action(self, parsed_args):
214 client = self.app.client_manager.network
215 fails = 0
216 for id_or_name in parsed_args.l2_gateways:
217 try:
218 id = client._find(sdk_objects.L2gw, id_or_name).id
219 client._delete(sdk_objects.L2gw, id)
220 LOG.warning("L2 Gateaway %(id)s deleted", {'id': id})
221 except Exception as e:
222 fails += 1
223 LOG.error("Failed to delete L2 Gateway with name or ID "
224 "'%(id_or_name)s': %(e)s",
225 {'id_or_name': id_or_name, 'e': e})
226 if fails > 0:
227 msg = (_("Failed to delete %(fails)s of %(total)s L2 Gateway.") %
228 {'fails': fails, 'total': len(parsed_args.l2_gateways)})
229 raise exceptions.CommandError(msg)
230
231
232 class UpdateL2gw(command.ShowOne):
233 _description = _("Update a given l2gateway")
234
235 def get_parser(self, prog_name):
236 parser = super().get_parser(prog_name)
237 parser.add_argument(
238 L2_GATEWAY,
239 metavar="<L2_GATEWAY>",
240 help=_("ID or name of l2_gateway to update."),
241 )
242 parser.add_argument('--name', metavar='name',
243 help=_('Descriptive name for logical gateway.'))
244 _get_common_parser(parser)
245 return parser
246
247 def take_action(self, parsed_args):
248 client = self.app.client_manager.network
249 id = client._find(sdk_objects.L2gw, parsed_args.l2_gateway).id
250 if parsed_args.devices:
251 body = _args2body(parsed_args)
252 else:
253 body = {'name': parsed_args.name}
254 obj = client._update(sdk_objects.L2gw, id, **body)
255 display_columns, columns = nc_osc_utils._get_columns(obj)
256 data = osc_utils.get_dict_properties(obj, columns)
257 return display_columns, data