|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | +# not use this file except in compliance with the License. You may obtain |
| 3 | +# a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations |
| 11 | +# under the License. |
| 12 | +# |
| 13 | + |
| 14 | +"""L3 Conntrack Helper action implementations""" |
| 15 | + |
| 16 | +import logging |
| 17 | + |
| 18 | +from osc_lib.command import command |
| 19 | +from osc_lib import exceptions |
| 20 | +from osc_lib import utils |
| 21 | + |
| 22 | +from openstackclient.i18n import _ |
| 23 | +from openstackclient.network import sdk_utils |
| 24 | + |
| 25 | + |
| 26 | +LOG = logging.getLogger(__name__) |
| 27 | + |
| 28 | + |
| 29 | +def _get_columns(item): |
| 30 | + column_map = {} |
| 31 | + return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map) |
| 32 | + |
| 33 | + |
| 34 | +def _get_attrs(client, parsed_args): |
| 35 | + router = client.find_router(parsed_args.router, ignore_missing=False) |
| 36 | + attrs = {'router_id': router.id} |
| 37 | + if parsed_args.helper: |
| 38 | + attrs['helper'] = parsed_args.helper |
| 39 | + if parsed_args.protocol: |
| 40 | + attrs['protocol'] = parsed_args.protocol |
| 41 | + if parsed_args.port: |
| 42 | + attrs['port'] = parsed_args.port |
| 43 | + |
| 44 | + return attrs |
| 45 | + |
| 46 | + |
| 47 | +class CreateConntrackHelper(command.ShowOne): |
| 48 | + _description = _("Create a new L3 conntrack helper") |
| 49 | + |
| 50 | + def get_parser(self, prog_name): |
| 51 | + parser = super(CreateConntrackHelper, self).get_parser(prog_name) |
| 52 | + parser.add_argument( |
| 53 | + 'router', |
| 54 | + metavar='<router>', |
| 55 | + help=_('Router for which conntrack helper will be created') |
| 56 | + ) |
| 57 | + parser.add_argument( |
| 58 | + '--helper', |
| 59 | + required=True, |
| 60 | + metavar='<helper>', |
| 61 | + help=_('The netfilter conntrack helper module') |
| 62 | + ) |
| 63 | + parser.add_argument( |
| 64 | + '--protocol', |
| 65 | + required=True, |
| 66 | + metavar='<protocol>', |
| 67 | + help=_('The network protocol for the netfilter conntrack target ' |
| 68 | + 'rule') |
| 69 | + ) |
| 70 | + parser.add_argument( |
| 71 | + '--port', |
| 72 | + required=True, |
| 73 | + metavar='<port>', |
| 74 | + type=int, |
| 75 | + help=_('The network port for the netfilter conntrack target rule') |
| 76 | + ) |
| 77 | + |
| 78 | + return parser |
| 79 | + |
| 80 | + def take_action(self, parsed_args): |
| 81 | + client = self.app.client_manager.network |
| 82 | + |
| 83 | + attrs = _get_attrs(client, parsed_args) |
| 84 | + obj = client.create_conntrack_helper(attrs.pop('router_id'), **attrs) |
| 85 | + display_columns, columns = _get_columns(obj) |
| 86 | + data = utils.get_item_properties(obj, columns, formatters={}) |
| 87 | + |
| 88 | + return (display_columns, data) |
| 89 | + |
| 90 | + |
| 91 | +class DeleteConntrackHelper(command.Command): |
| 92 | + _description = _("Delete L3 conntrack helper") |
| 93 | + |
| 94 | + def get_parser(self, prog_name): |
| 95 | + parser = super(DeleteConntrackHelper, self).get_parser(prog_name) |
| 96 | + parser.add_argument( |
| 97 | + 'router', |
| 98 | + metavar='<router>', |
| 99 | + help=_('Router that the conntrack helper belong to') |
| 100 | + ) |
| 101 | + parser.add_argument( |
| 102 | + 'conntrack_helper_ids', |
| 103 | + metavar='<conntrack-helper-ids>', |
| 104 | + nargs='+', |
| 105 | + help=_('The ID of the conntrack helper(s) to delete') |
| 106 | + ) |
| 107 | + |
| 108 | + return parser |
| 109 | + |
| 110 | + def take_action(self, parsed_args): |
| 111 | + client = self.app.client_manager.network |
| 112 | + result = 0 |
| 113 | + |
| 114 | + router = client.find_router(parsed_args.router, ignore_missing=False) |
| 115 | + for ct_helper in parsed_args.conntrack_helper_ids: |
| 116 | + try: |
| 117 | + client.delete_conntrack_helper( |
| 118 | + ct_helper, router.id, ignore_missing=False) |
| 119 | + except Exception as e: |
| 120 | + result += 1 |
| 121 | + LOG.error(_("Failed to delete L3 conntrack helper with " |
| 122 | + "ID '%(ct_helper)s': %(e)s"), |
| 123 | + {'ct_helper': ct_helper, 'e': e}) |
| 124 | + |
| 125 | + if result > 0: |
| 126 | + total = len(parsed_args.conntrack_helper_ids) |
| 127 | + msg = (_("%(result)s of %(total)s L3 conntrack helpers failed " |
| 128 | + "to delete.") % {'result': result, 'total': total}) |
| 129 | + raise exceptions.CommandError(msg) |
| 130 | + |
| 131 | + |
| 132 | +class ListConntrackHelper(command.Lister): |
| 133 | + _description = _("List L3 conntrack helpers") |
| 134 | + |
| 135 | + def get_parser(self, prog_name): |
| 136 | + parser = super(ListConntrackHelper, self).get_parser(prog_name) |
| 137 | + parser.add_argument( |
| 138 | + 'router', |
| 139 | + metavar='<router>', |
| 140 | + help=_('Router that the conntrack helper belong to') |
| 141 | + ) |
| 142 | + parser.add_argument( |
| 143 | + '--helper', |
| 144 | + metavar='<helper>', |
| 145 | + help=_('The netfilter conntrack helper module') |
| 146 | + ) |
| 147 | + parser.add_argument( |
| 148 | + '--protocol', |
| 149 | + metavar='<protocol>', |
| 150 | + help=_('The network protocol for the netfilter conntrack target ' |
| 151 | + 'rule') |
| 152 | + ) |
| 153 | + parser.add_argument( |
| 154 | + '--port', |
| 155 | + metavar='<port>', |
| 156 | + help=_('The network port for the netfilter conntrack target rule') |
| 157 | + ) |
| 158 | + |
| 159 | + return parser |
| 160 | + |
| 161 | + def take_action(self, parsed_args): |
| 162 | + client = self.app.client_manager.network |
| 163 | + columns = ( |
| 164 | + 'id', |
| 165 | + 'router_id', |
| 166 | + 'helper', |
| 167 | + 'protocol', |
| 168 | + 'port', |
| 169 | + ) |
| 170 | + column_headers = ( |
| 171 | + 'ID', |
| 172 | + 'Router ID', |
| 173 | + 'Helper', |
| 174 | + 'Protocol', |
| 175 | + 'Port', |
| 176 | + ) |
| 177 | + attrs = _get_attrs(client, parsed_args) |
| 178 | + data = client.conntrack_helpers(attrs.pop('router_id'), **attrs) |
| 179 | + |
| 180 | + return (column_headers, |
| 181 | + (utils.get_item_properties( |
| 182 | + s, columns, formatters={}, |
| 183 | + ) for s in data)) |
| 184 | + |
| 185 | + |
| 186 | +class SetConntrackHelper(command.Command): |
| 187 | + _description = _("Set L3 conntrack helper properties") |
| 188 | + |
| 189 | + def get_parser(self, prog_name): |
| 190 | + parser = super(SetConntrackHelper, self).get_parser(prog_name) |
| 191 | + parser.add_argument( |
| 192 | + 'router', |
| 193 | + metavar='<router>', |
| 194 | + help=_('Router that the conntrack helper belong to') |
| 195 | + ) |
| 196 | + parser.add_argument( |
| 197 | + 'conntrack_helper_id', |
| 198 | + metavar='<conntrack-helper-id>', |
| 199 | + help=_('The ID of the conntrack helper(s)') |
| 200 | + ) |
| 201 | + parser.add_argument( |
| 202 | + '--helper', |
| 203 | + metavar='<helper>', |
| 204 | + help=_('The netfilter conntrack helper module') |
| 205 | + ) |
| 206 | + parser.add_argument( |
| 207 | + '--protocol', |
| 208 | + metavar='<protocol>', |
| 209 | + help=_('The network protocol for the netfilter conntrack target ' |
| 210 | + 'rule') |
| 211 | + ) |
| 212 | + parser.add_argument( |
| 213 | + '--port', |
| 214 | + metavar='<port>', |
| 215 | + type=int, |
| 216 | + help=_('The network port for the netfilter conntrack target rule') |
| 217 | + ) |
| 218 | + return parser |
| 219 | + |
| 220 | + def take_action(self, parsed_args): |
| 221 | + client = self.app.client_manager.network |
| 222 | + attrs = _get_attrs(client, parsed_args) |
| 223 | + if attrs: |
| 224 | + client.update_conntrack_helper( |
| 225 | + parsed_args.conntrack_helper_id, attrs.pop('router_id'), |
| 226 | + **attrs) |
| 227 | + |
| 228 | + |
| 229 | +class ShowConntrackHelper(command.ShowOne): |
| 230 | + _description = _("Display L3 conntrack helper details") |
| 231 | + |
| 232 | + def get_parser(self, prog_name): |
| 233 | + parser = super(ShowConntrackHelper, self).get_parser(prog_name) |
| 234 | + parser.add_argument( |
| 235 | + 'router', |
| 236 | + metavar='<router>', |
| 237 | + help=_('Router that the conntrack helper belong to') |
| 238 | + ) |
| 239 | + parser.add_argument( |
| 240 | + 'conntrack_helper_id', |
| 241 | + metavar='<conntrack-helper-id>', |
| 242 | + help=_('The ID of the conntrack helper') |
| 243 | + ) |
| 244 | + |
| 245 | + return parser |
| 246 | + |
| 247 | + def take_action(self, parsed_args): |
| 248 | + client = self.app.client_manager.network |
| 249 | + router = client.find_router(parsed_args.router, ignore_missing=False) |
| 250 | + obj = client.get_conntrack_helper( |
| 251 | + parsed_args.conntrack_helper_id, router.id) |
| 252 | + display_columns, columns = _get_columns(obj) |
| 253 | + data = utils.get_item_properties(obj, columns, formatters={}) |
| 254 | + |
| 255 | + return (display_columns, data) |
0 commit comments