|
| 1 | +# All Rights Reserved 2020 |
| 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 openstackclient.i18n import _ |
| 25 | +from openstackclient.identity import common |
| 26 | +from openstackclient.network.v2.taas import tap_service |
| 27 | + |
| 28 | + |
| 29 | +LOG = logging.getLogger(__name__) |
| 30 | + |
| 31 | +TAP_FLOW = 'tap_flow' |
| 32 | +TAP_FLOWS = f'{TAP_FLOW}s' |
| 33 | + |
| 34 | +_attr_map = ( |
| 35 | + ('id', 'ID', column_util.LIST_BOTH), |
| 36 | + ('tenant_id', 'Tenant', column_util.LIST_LONG_ONLY), |
| 37 | + ('name', 'Name', column_util.LIST_BOTH), |
| 38 | + ('status', 'Status', column_util.LIST_BOTH), |
| 39 | + ('source_port', 'source_port', column_util.LIST_BOTH), |
| 40 | + ('tap_service_id', 'tap_service_id', column_util.LIST_BOTH), |
| 41 | + ('direction', 'Direction', column_util.LIST_BOTH), |
| 42 | +) |
| 43 | + |
| 44 | +_formatters = { |
| 45 | + 'vlan_filter': format_columns.ListColumn, |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +def _add_updatable_args(parser): |
| 50 | + parser.add_argument('--name', help=_('Name of this Tap service.')) |
| 51 | + parser.add_argument( |
| 52 | + '--description', help=_('Description for this Tap service.') |
| 53 | + ) |
| 54 | + |
| 55 | + |
| 56 | +class CreateTapFlow(command.ShowOne): |
| 57 | + _description = _("Create a tap flow") |
| 58 | + |
| 59 | + def get_parser(self, prog_name): |
| 60 | + parser = super().get_parser(prog_name) |
| 61 | + identity_utils.add_project_owner_option_to_parser(parser) |
| 62 | + _add_updatable_args(parser) |
| 63 | + parser.add_argument( |
| 64 | + '--port', |
| 65 | + required=True, |
| 66 | + metavar="SOURCE_PORT", |
| 67 | + help=_('Source port to which the Tap Flow is connected.'), |
| 68 | + ) |
| 69 | + parser.add_argument( |
| 70 | + '--tap-service', |
| 71 | + required=True, |
| 72 | + metavar="TAP_SERVICE", |
| 73 | + help=_('Tap Service to which the Tap Flow belongs.'), |
| 74 | + ) |
| 75 | + parser.add_argument( |
| 76 | + '--direction', |
| 77 | + required=True, |
| 78 | + metavar="DIRECTION", |
| 79 | + choices=['IN', 'OUT', 'BOTH'], |
| 80 | + type=lambda s: s.upper(), |
| 81 | + help=_( |
| 82 | + 'Direction of the Tap flow. Possible options are: ' |
| 83 | + 'IN, OUT, BOTH' |
| 84 | + ), |
| 85 | + ) |
| 86 | + parser.add_argument( |
| 87 | + '--vlan-filter', |
| 88 | + required=False, |
| 89 | + metavar="VLAN_FILTER", |
| 90 | + help=_('VLAN Ids to be mirrored in the form of range string.'), |
| 91 | + ) |
| 92 | + return parser |
| 93 | + |
| 94 | + def take_action(self, parsed_args): |
| 95 | + client = self.app.client_manager.network |
| 96 | + attrs = {} |
| 97 | + if parsed_args.name is not None: |
| 98 | + attrs['name'] = parsed_args.name |
| 99 | + if parsed_args.description is not None: |
| 100 | + attrs['description'] = parsed_args.description |
| 101 | + if parsed_args.port is not None: |
| 102 | + source_port = client.find_port( |
| 103 | + parsed_args.port, ignore_missing=False |
| 104 | + )['id'] |
| 105 | + attrs['source_port'] = source_port |
| 106 | + if parsed_args.tap_service is not None: |
| 107 | + tap_service_id = client.find_tap_service( |
| 108 | + parsed_args.tap_service, ignore_missing=False |
| 109 | + )['id'] |
| 110 | + attrs['tap_service_id'] = tap_service_id |
| 111 | + if parsed_args.direction is not None: |
| 112 | + attrs['direction'] = parsed_args.direction |
| 113 | + if parsed_args.vlan_filter is not None: |
| 114 | + attrs['vlan_filter'] = parsed_args.vlan_filter |
| 115 | + if 'project' in parsed_args and parsed_args.project is not None: |
| 116 | + attrs['project_id'] = common.find_project( |
| 117 | + self.app.client_manager.identity, |
| 118 | + parsed_args.project, |
| 119 | + parsed_args.project_domain, |
| 120 | + ).id |
| 121 | + obj = client.create_tap_flow(**attrs) |
| 122 | + display_columns, columns = tap_service._get_columns(obj) |
| 123 | + data = osc_utils.get_dict_properties(obj, columns) |
| 124 | + return display_columns, data |
| 125 | + |
| 126 | + |
| 127 | +class ListTapFlow(command.Lister): |
| 128 | + _description = _("List tap flows that belong to a given tenant") |
| 129 | + |
| 130 | + def get_parser(self, prog_name): |
| 131 | + parser = super().get_parser(prog_name) |
| 132 | + identity_utils.add_project_owner_option_to_parser(parser) |
| 133 | + |
| 134 | + return parser |
| 135 | + |
| 136 | + def take_action(self, parsed_args): |
| 137 | + client = self.app.client_manager.network |
| 138 | + params = {} |
| 139 | + if parsed_args.project is not None: |
| 140 | + params['project_id'] = common.find_project( |
| 141 | + self.app.client_manager.identity, |
| 142 | + parsed_args.project, |
| 143 | + parsed_args.project_domain, |
| 144 | + ).id |
| 145 | + objs = client.tap_flows(retrieve_all=True, params=params) |
| 146 | + headers, columns = column_util.get_column_definitions( |
| 147 | + _attr_map, long_listing=True |
| 148 | + ) |
| 149 | + return ( |
| 150 | + headers, |
| 151 | + ( |
| 152 | + osc_utils.get_dict_properties( |
| 153 | + s, columns, formatters=_formatters |
| 154 | + ) |
| 155 | + for s in objs |
| 156 | + ), |
| 157 | + ) |
| 158 | + |
| 159 | + |
| 160 | +class ShowTapFlow(command.ShowOne): |
| 161 | + _description = _("Show information of a given tap flow") |
| 162 | + |
| 163 | + def get_parser(self, prog_name): |
| 164 | + parser = super().get_parser(prog_name) |
| 165 | + parser.add_argument( |
| 166 | + TAP_FLOW, |
| 167 | + metavar=f"<{TAP_FLOW}>", |
| 168 | + help=_("ID or name of tap flow to look up."), |
| 169 | + ) |
| 170 | + return parser |
| 171 | + |
| 172 | + def take_action(self, parsed_args): |
| 173 | + client = self.app.client_manager.network |
| 174 | + id = client.find_tap_flow( |
| 175 | + parsed_args.tap_flow, ignore_missing=False |
| 176 | + ).id |
| 177 | + obj = client.get_tap_flow(id) |
| 178 | + display_columns, columns = tap_service._get_columns(obj) |
| 179 | + data = osc_utils.get_dict_properties(obj, columns) |
| 180 | + return display_columns, data |
| 181 | + |
| 182 | + |
| 183 | +class DeleteTapFlow(command.Command): |
| 184 | + _description = _("Delete a tap flow") |
| 185 | + |
| 186 | + def get_parser(self, prog_name): |
| 187 | + parser = super().get_parser(prog_name) |
| 188 | + parser.add_argument( |
| 189 | + TAP_FLOW, |
| 190 | + metavar=f"<{TAP_FLOW}>", |
| 191 | + nargs="+", |
| 192 | + help=_("ID(s) or name(s) of tap flow to delete."), |
| 193 | + ) |
| 194 | + return parser |
| 195 | + |
| 196 | + def take_action(self, parsed_args): |
| 197 | + client = self.app.client_manager.network |
| 198 | + fails = 0 |
| 199 | + for id_or_name in parsed_args.tap_flow: |
| 200 | + try: |
| 201 | + id = client.find_tap_flow(id_or_name, ignore_missing=False).id |
| 202 | + client.delete_tap_flow(id) |
| 203 | + LOG.warning("Tap flow %(id)s deleted", {'id': id}) |
| 204 | + except Exception as e: |
| 205 | + fails += 1 |
| 206 | + LOG.error( |
| 207 | + "Failed to delete tap flow with name or ID " |
| 208 | + "'%(id_or_name)s': %(e)s", |
| 209 | + {'id_or_name': id_or_name, 'e': e}, |
| 210 | + ) |
| 211 | + if fails > 0: |
| 212 | + msg = _("Failed to delete %(fails)s of %(total)s tap flow.") % { |
| 213 | + 'fails': fails, |
| 214 | + 'total': len(parsed_args.tap_flow), |
| 215 | + } |
| 216 | + raise exceptions.CommandError(msg) |
| 217 | + |
| 218 | + |
| 219 | +class UpdateTapFlow(command.ShowOne): |
| 220 | + _description = _("Update a tap flow.") |
| 221 | + |
| 222 | + def get_parser(self, prog_name): |
| 223 | + parser = super().get_parser(prog_name) |
| 224 | + parser.add_argument( |
| 225 | + TAP_FLOW, |
| 226 | + metavar=f"<{TAP_FLOW}>", |
| 227 | + help=_("ID or name of tap flow to update."), |
| 228 | + ) |
| 229 | + _add_updatable_args(parser) |
| 230 | + return parser |
| 231 | + |
| 232 | + def take_action(self, parsed_args): |
| 233 | + client = self.app.client_manager.network |
| 234 | + original_t_f = client.find_tap_flow( |
| 235 | + parsed_args.tap_flow, ignore_missing=False |
| 236 | + ).id |
| 237 | + attrs = {} |
| 238 | + if parsed_args.name is not None: |
| 239 | + attrs['name'] = parsed_args.name |
| 240 | + if parsed_args.description is not None: |
| 241 | + attrs['description'] = parsed_args.description |
| 242 | + obj = client.update_tap_flow(original_t_f, **attrs) |
| 243 | + columns, display_columns = column_util.get_columns(obj, _attr_map) |
| 244 | + data = osc_utils.get_dict_properties(obj, columns) |
| 245 | + return display_columns, data |
0 commit comments