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