|
| 1 | +# Copyright 2017 FUJITSU 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 | + |
| 17 | +from osc_lib.cli import identity as identity_utils |
| 18 | +from osc_lib import exceptions |
| 19 | +from osc_lib import utils |
| 20 | +from osc_lib.utils import columns as column_util |
| 21 | + |
| 22 | +from openstackclient import command |
| 23 | +from openstackclient.i18n import _ |
| 24 | +from openstackclient.identity import common as identity_common |
| 25 | + |
| 26 | + |
| 27 | +_attr_map = [ |
| 28 | + ('id', 'ID', column_util.LIST_BOTH), |
| 29 | + ('name', 'Name', column_util.LIST_BOTH), |
| 30 | + ('type', 'Type', column_util.LIST_BOTH), |
| 31 | + ('endpoints', 'Endpoints', column_util.LIST_BOTH), |
| 32 | + ('description', 'Description', column_util.LIST_LONG_ONLY), |
| 33 | + ('project_id', 'Project', column_util.LIST_LONG_ONLY), |
| 34 | +] |
| 35 | + |
| 36 | +_attr_map_dict = { |
| 37 | + 'id': 'ID', |
| 38 | + 'name': 'Name', |
| 39 | + 'type': 'Type', |
| 40 | + 'endpoints': 'Endpoints', |
| 41 | + 'description': 'Description', |
| 42 | + 'project_id': 'Project', |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +class CreateEndpointGroup(command.ShowOne): |
| 47 | + _description = _("Create an endpoint group") |
| 48 | + |
| 49 | + def get_parser(self, prog_name): |
| 50 | + parser = super().get_parser(prog_name) |
| 51 | + parser.add_argument( |
| 52 | + '--description', |
| 53 | + metavar='<description>', |
| 54 | + help=_('Description for the endpoint group'), |
| 55 | + ) |
| 56 | + parser.add_argument( |
| 57 | + 'name', metavar='<name>', help=_('Name for the endpoint group') |
| 58 | + ) |
| 59 | + parser.add_argument( |
| 60 | + '--type', |
| 61 | + required=True, |
| 62 | + choices=['subnet', 'cidr'], |
| 63 | + help=_( |
| 64 | + 'Type of endpoints in group (e.g. subnet, cidr, network, ' |
| 65 | + 'router). Currently only subnet and cidr are supported.' |
| 66 | + ), |
| 67 | + ) |
| 68 | + parser.add_argument( |
| 69 | + '--value', |
| 70 | + action='append', |
| 71 | + dest='endpoints', |
| 72 | + required=True, |
| 73 | + help=_( |
| 74 | + 'Endpoint(s) for the group. Must all be of the same type. ' |
| 75 | + '(--value) option can be repeated' |
| 76 | + ), |
| 77 | + ) |
| 78 | + identity_utils.add_project_owner_option_to_parser(parser) |
| 79 | + return parser |
| 80 | + |
| 81 | + def take_action(self, parsed_args): |
| 82 | + client = self.app.client_manager.network |
| 83 | + attrs = {} |
| 84 | + if parsed_args.project is not None: |
| 85 | + attrs['project_id'] = identity_common.find_project( |
| 86 | + self.app.client_manager.identity, |
| 87 | + parsed_args.project, |
| 88 | + parsed_args.project_domain, |
| 89 | + ).id |
| 90 | + if parsed_args.description: |
| 91 | + attrs['description'] = parsed_args.description |
| 92 | + |
| 93 | + if parsed_args.name: |
| 94 | + attrs['name'] = str(parsed_args.name) |
| 95 | + attrs['type'] = parsed_args.type |
| 96 | + if parsed_args.type == 'subnet': |
| 97 | + _subnet_ids = [ |
| 98 | + client.find_subnet(endpoint, ignore_missing=False)['id'] |
| 99 | + for endpoint in parsed_args.endpoints |
| 100 | + ] |
| 101 | + attrs['endpoints'] = _subnet_ids |
| 102 | + else: |
| 103 | + attrs['endpoints'] = parsed_args.endpoints |
| 104 | + obj = client.create_vpn_endpoint_group(**attrs) |
| 105 | + display_columns, columns = utils.get_osc_show_columns_for_sdk_resource( |
| 106 | + obj, _attr_map_dict, ['location', 'tenant_id'] |
| 107 | + ) |
| 108 | + data = utils.get_dict_properties(obj, columns) |
| 109 | + return display_columns, data |
| 110 | + |
| 111 | + |
| 112 | +class DeleteEndpointGroup(command.Command): |
| 113 | + _description = _("Delete endpoint group(s)") |
| 114 | + |
| 115 | + def get_parser(self, prog_name): |
| 116 | + parser = super().get_parser(prog_name) |
| 117 | + parser.add_argument( |
| 118 | + 'endpoint_group', |
| 119 | + metavar='<endpoint-group>', |
| 120 | + nargs='+', |
| 121 | + help=_('Endpoint group(s) to delete (name or ID)'), |
| 122 | + ) |
| 123 | + return parser |
| 124 | + |
| 125 | + def take_action(self, parsed_args): |
| 126 | + client = self.app.client_manager.network |
| 127 | + result = 0 |
| 128 | + for endpoint in parsed_args.endpoint_group: |
| 129 | + try: |
| 130 | + endpoint_id = client.find_vpn_endpoint_group( |
| 131 | + endpoint, ignore_missing=False |
| 132 | + ).id |
| 133 | + client.delete_vpn_endpoint_group(endpoint_id) |
| 134 | + except Exception as e: |
| 135 | + result += 1 |
| 136 | + print( |
| 137 | + f"Failed to delete endpoint group with " |
| 138 | + f"name or ID {endpoint}: {e}" |
| 139 | + ) |
| 140 | + |
| 141 | + if result > 0: |
| 142 | + total = len(parsed_args.endpoint_group) |
| 143 | + msg = _( |
| 144 | + "%(result)s of %(total)s endpoint group failed to delete." |
| 145 | + ) % {'result': result, 'total': total} |
| 146 | + raise exceptions.CommandError(msg) |
| 147 | + |
| 148 | + |
| 149 | +class ListEndpointGroup(command.Lister): |
| 150 | + _description = _("List endpoint groups that belong to a given project") |
| 151 | + |
| 152 | + def get_parser(self, prog_name): |
| 153 | + parser = super().get_parser(prog_name) |
| 154 | + parser.add_argument( |
| 155 | + '--long', |
| 156 | + action='store_true', |
| 157 | + default=False, |
| 158 | + help=_("List additional fields in output"), |
| 159 | + ) |
| 160 | + return parser |
| 161 | + |
| 162 | + def take_action(self, parsed_args): |
| 163 | + client = self.app.client_manager.network |
| 164 | + obj = client.vpn_endpoint_groups() |
| 165 | + headers, columns = column_util.get_column_definitions( |
| 166 | + _attr_map, long_listing=parsed_args.long |
| 167 | + ) |
| 168 | + return (headers, (utils.get_dict_properties(s, columns) for s in obj)) |
| 169 | + |
| 170 | + |
| 171 | +class SetEndpointGroup(command.Command): |
| 172 | + _description = _("Set endpoint group properties") |
| 173 | + |
| 174 | + def get_parser(self, prog_name): |
| 175 | + parser = super().get_parser(prog_name) |
| 176 | + parser.add_argument( |
| 177 | + '--description', |
| 178 | + metavar='<description>', |
| 179 | + help=_('Description for the endpoint group'), |
| 180 | + ) |
| 181 | + parser.add_argument( |
| 182 | + '--name', |
| 183 | + metavar='<name>', |
| 184 | + help=_('Set a name for the endpoint group'), |
| 185 | + ) |
| 186 | + parser.add_argument( |
| 187 | + 'endpoint_group', |
| 188 | + metavar='<endpoint-group>', |
| 189 | + help=_('Endpoint group to set (name or ID)'), |
| 190 | + ) |
| 191 | + return parser |
| 192 | + |
| 193 | + def take_action(self, parsed_args): |
| 194 | + client = self.app.client_manager.network |
| 195 | + attrs = {} |
| 196 | + if parsed_args.description: |
| 197 | + attrs['description'] = parsed_args.description |
| 198 | + if parsed_args.name: |
| 199 | + attrs['name'] = str(parsed_args.name) |
| 200 | + endpoint_id = client.find_vpn_endpoint_group( |
| 201 | + parsed_args.endpoint_group, ignore_missing=False |
| 202 | + )['id'] |
| 203 | + try: |
| 204 | + client.update_vpn_endpoint_group(endpoint_id, **attrs) |
| 205 | + except Exception as e: |
| 206 | + msg = _( |
| 207 | + "Failed to set endpoint group %(endpoint_group)s: %(e)s" |
| 208 | + ) % {'endpoint_group': parsed_args.endpoint_group, 'e': e} |
| 209 | + raise exceptions.CommandError(msg) |
| 210 | + |
| 211 | + |
| 212 | +class ShowEndpointGroup(command.ShowOne): |
| 213 | + _description = _("Display endpoint group details") |
| 214 | + |
| 215 | + def get_parser(self, prog_name): |
| 216 | + parser = super().get_parser(prog_name) |
| 217 | + parser.add_argument( |
| 218 | + 'endpoint_group', |
| 219 | + metavar='<endpoint-group>', |
| 220 | + help=_('Endpoint group to display (name or ID)'), |
| 221 | + ) |
| 222 | + return parser |
| 223 | + |
| 224 | + def take_action(self, parsed_args): |
| 225 | + client = self.app.client_manager.network |
| 226 | + obj = client.find_vpn_endpoint_group( |
| 227 | + parsed_args.endpoint_group, ignore_missing=False |
| 228 | + ) |
| 229 | + display_columns, columns = utils.get_osc_show_columns_for_sdk_resource( |
| 230 | + obj, _attr_map_dict, ['location', 'tenant_id'] |
| 231 | + ) |
| 232 | + data = utils.get_dict_properties(obj, columns) |
| 233 | + return (display_columns, data) |
0 commit comments