| 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 format_columns |
| 18 |
from osc_lib.command import command |
| 19 |
from osc_lib import exceptions |
| 20 |
from osc_lib import utils |
| 21 |
from osc_lib.utils import columns as column_util |
| 22 |
from oslo_log import log as logging |
| 23 |
|
| 24 |
from neutronclient._i18n import _ |
| 25 |
from neutronclient.common import utils as nc_utils |
| 26 |
from neutronclient.osc import utils as osc_utils |
| 27 |
from neutronclient.osc.v2.vpnaas import utils as vpn_utils |
| 28 |
|
| 29 |
|
| 30 |
LOG = logging.getLogger(__name__) |
| 31 |
|
| 32 |
|
| 33 |
_formatters = { |
| 34 |
'peer_cidrs': format_columns.ListColumn |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
_attr_map = ( |
| 39 |
('id', 'ID', column_util.LIST_BOTH), |
| 40 |
('name', 'Name', column_util.LIST_BOTH), |
| 41 |
('peer_address', 'Peer Address', column_util.LIST_BOTH), |
| 42 |
('auth_mode', 'Authentication Algorithm', column_util.LIST_BOTH), |
| 43 |
('status', 'Status', column_util.LIST_BOTH), |
| 44 |
('project_id', 'Project', column_util.LIST_LONG_ONLY), |
| 45 |
('peer_cidrs', 'Peer CIDRs', column_util.LIST_LONG_ONLY), |
| 46 |
('vpnservice_id', 'VPN Service', column_util.LIST_LONG_ONLY), |
| 47 |
('ipsecpolicy_id', 'IPSec Policy', column_util.LIST_LONG_ONLY), |
| 48 |
('ikepolicy_id', 'IKE Policy', column_util.LIST_LONG_ONLY), |
| 49 |
('mtu', 'MTU', column_util.LIST_LONG_ONLY), |
| 50 |
('initiator', 'Initiator', column_util.LIST_LONG_ONLY), |
| 51 |
('is_admin_state_up', 'State', column_util.LIST_LONG_ONLY), |
| 52 |
('description', 'Description', column_util.LIST_LONG_ONLY), |
| 53 |
('psk', 'Pre-shared Key', column_util.LIST_LONG_ONLY), |
| 54 |
('route_mode', 'Route Mode', column_util.LIST_LONG_ONLY), |
| 55 |
('local_id', 'Local ID', column_util.LIST_LONG_ONLY), |
| 56 |
('peer_id', 'Peer ID', column_util.LIST_LONG_ONLY), |
| 57 |
('local_ep_group_id', 'Local Endpoint Group ID', |
| 58 |
column_util.LIST_LONG_ONLY), |
| 59 |
('peer_ep_group_id', 'Peer Endpoint Group ID', column_util.LIST_LONG_ONLY), |
| 60 |
('dpd', 'DPD', column_util.LIST_LONG_ONLY), |
| 61 |
) |
| 62 |
|
| 63 |
_attr_map_dict = { |
| 64 |
'id': 'ID', |
| 65 |
'name': 'Name', |
| 66 |
'peer_address': 'Peer Address', |
| 67 |
'auth_mode': 'Authentication Algorithm', |
| 68 |
'status': 'Status', |
| 69 |
'peer_cidrs': 'Peer CIDRs', |
| 70 |
'vpnservice_id': 'VPN Service', |
| 71 |
'ipsecpolicy_id': 'IPSec Policy', |
| 72 |
'ikepolicy_id': 'IKE Policy', |
| 73 |
'mtu': 'MTU', |
| 74 |
'initiator': 'Initiator', |
| 75 |
'is_admin_state_up': 'State', |
| 76 |
'psk': 'Pre-shared Key', |
| 77 |
'route_mode': 'Route Mode', |
| 78 |
'local_id': 'Local ID', |
| 79 |
'peer_id': 'Peer ID', |
| 80 |
'local_ep_group_id': 'Local Endpoint Group ID', |
| 81 |
'peer_ep_group_id': 'Peer Endpoint Group ID', |
| 82 |
'description': 'Description', |
| 83 |
'project_id': 'Project', |
| 84 |
'dpd': 'DPD', |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
def _convert_to_lowercase(string): |
| 89 |
return string.lower() |
| 90 |
|
| 91 |
|
| 92 |
def _get_common_parser(parser, is_create=True): |
| 93 |
parser.add_argument( |
| 94 |
'--description', |
| 95 |
metavar='<description>', |
| 96 |
help=_('Description for the connection')) |
| 97 |
parser.add_argument( |
| 98 |
'--dpd', |
| 99 |
metavar="action=ACTION,interval=INTERVAL,timeout=TIMEOUT", |
| 100 |
type=nc_utils.str2dict_type( |
| 101 |
optional_keys=['action', 'interval', 'timeout']), |
| 102 |
help=vpn_utils.dpd_help("IPsec connection")) |
| 103 |
parser.add_argument( |
| 104 |
'--mtu', |
| 105 |
help=_('MTU size for the connection')) |
| 106 |
parser.add_argument( |
| 107 |
'--initiator', |
| 108 |
choices=['bi-directional', 'response-only'], |
| 109 |
type=_convert_to_lowercase, |
| 110 |
help=_('Initiator state')) |
| 111 |
peer_group = parser.add_mutually_exclusive_group() |
| 112 |
peer_group.add_argument( |
| 113 |
'--peer-cidr', |
| 114 |
dest='peer_cidrs', |
| 115 |
help=_('Remote subnet(s) in CIDR format. ' |
| 116 |
'Cannot be specified when using endpoint groups. Only ' |
| 117 |
'applicable, if subnet provided for VPN service.') |
| 118 |
) |
| 119 |
peer_group.add_argument( |
| 120 |
'--local-endpoint-group', |
| 121 |
help=_('Local endpoint group (name or ID) with subnet(s) ' |
| 122 |
'for IPsec connection') |
| 123 |
) |
| 124 |
parser.add_argument( |
| 125 |
'--peer-endpoint-group', |
| 126 |
help=_('Peer endpoint group (name or ID) with CIDR(s) for ' |
| 127 |
'IPSec connection')) |
| 128 |
admin_group = parser.add_mutually_exclusive_group() |
| 129 |
admin_group.add_argument( |
| 130 |
'--enable', |
| 131 |
action='store_true', |
| 132 |
help=_("Enable IPSec site connection") |
| 133 |
) |
| 134 |
admin_group.add_argument( |
| 135 |
'--disable', |
| 136 |
action='store_true', |
| 137 |
help=_("Disable IPSec site connection") |
| 138 |
) |
| 139 |
parser.add_argument( |
| 140 |
'--local-id', |
| 141 |
help=_('An ID to be used instead of the external IP ' |
| 142 |
'address for a virtual router')) |
| 143 |
return parser |
| 144 |
|
| 145 |
|
| 146 |
def _get_common_attrs(client_manager, parsed_args, is_create=True): |
| 147 |
attrs = {} |
| 148 |
if is_create: |
| 149 |
if 'project' in parsed_args and parsed_args.project is not None: |
| 150 |
attrs['project_id'] = osc_utils.find_project( |
| 151 |
client_manager.identity, |
| 152 |
parsed_args.project, |
| 153 |
parsed_args.project_domain, |
| 154 |
).id |
| 155 |
if parsed_args.description: |
| 156 |
attrs['description'] = str(parsed_args.description) |
| 157 |
if parsed_args.mtu: |
| 158 |
attrs['mtu'] = parsed_args.mtu |
| 159 |
if parsed_args.enable: |
| 160 |
attrs['admin_state_up'] = True |
| 161 |
if parsed_args.disable: |
| 162 |
attrs['admin_state_up'] = False |
| 163 |
if parsed_args.initiator: |
| 164 |
attrs['initiator'] = parsed_args.initiator |
| 165 |
if parsed_args.dpd: |
| 166 |
vpn_utils.validate_dpd_dict(parsed_args.dpd) |
| 167 |
attrs['dpd'] = parsed_args.dpd |
| 168 |
if parsed_args.local_endpoint_group: |
| 169 |
_local_epg = client_manager.network.find_vpn_endpoint_group( |
| 170 |
parsed_args.local_endpoint_group)['id'] |
| 171 |
attrs['local_ep_group_id'] = _local_epg |
| 172 |
if parsed_args.peer_endpoint_group: |
| 173 |
_peer_epg = client_manager.network.find_vpn_endpoint_group( |
| 174 |
parsed_args.peer_endpoint_group)['id'] |
| 175 |
attrs['peer_ep_group_id'] = _peer_epg |
| 176 |
if parsed_args.peer_cidrs: |
| 177 |
attrs['peer_cidrs'] = parsed_args.peer_cidrs |
| 178 |
if parsed_args.local_id: |
| 179 |
attrs['local_id'] = parsed_args.local_id |
| 180 |
return attrs |
| 181 |
|
| 182 |
|
| 183 |
class CreateIPsecSiteConnection(command.ShowOne): |
| 184 |
_description = _("Create an IPsec site connection") |
| 185 |
|
| 186 |
def get_parser(self, prog_name): |
| 187 |
parser = super(CreateIPsecSiteConnection, self).get_parser(prog_name) |
| 188 |
_get_common_parser(parser) |
| 189 |
parser.add_argument( |
| 190 |
'--peer-id', |
| 191 |
required=True, |
| 192 |
help=_('Peer router identity for authentication. Can be ' |
| 193 |
'IPv4/IPv6 address, e-mail address, key id, or FQDN')) |
| 194 |
parser.add_argument( |
| 195 |
'--peer-address', |
| 196 |
required=True, |
| 197 |
help=_('Peer gateway public IPv4/IPv6 address or FQDN')) |
| 198 |
parser.add_argument( |
| 199 |
'--psk', |
| 200 |
required=True, |
| 201 |
help=_('Pre-shared key string.')) |
| 202 |
parser.add_argument( |
| 203 |
'--vpnservice', |
| 204 |
metavar='VPNSERVICE', |
| 205 |
required=True, |
| 206 |
help=_('VPN service instance associated with this ' |
| 207 |
'connection (name or ID)')) |
| 208 |
parser.add_argument( |
| 209 |
'--ikepolicy', |
| 210 |
metavar='IKEPOLICY', |
| 211 |
required=True, |
| 212 |
help=_('IKE policy associated with this connection (name or ID)')) |
| 213 |
parser.add_argument( |
| 214 |
'--ipsecpolicy', |
| 215 |
metavar='IPSECPOLICY', |
| 216 |
required=True, |
| 217 |
help=_('IPsec policy associated with this connection ' |
| 218 |
'(name or ID)')) |
| 219 |
parser.add_argument( |
| 220 |
'name', |
| 221 |
metavar='<name>', |
| 222 |
help=_('Set friendly name for the connection')) |
| 223 |
osc_utils.add_project_owner_option_to_parser(parser) |
| 224 |
return parser |
| 225 |
|
| 226 |
def take_action(self, parsed_args): |
| 227 |
client = self.app.client_manager.network |
| 228 |
attrs = _get_common_attrs(self.app.client_manager, parsed_args) |
| 229 |
if parsed_args.vpnservice: |
| 230 |
_vpnservice_id = client.find_vpn_service( |
| 231 |
parsed_args.vpnservice, ignore_missing=False)['id'] |
| 232 |
attrs['vpnservice_id'] = _vpnservice_id |
| 233 |
if parsed_args.ikepolicy: |
| 234 |
_ikepolicy_id = client.find_vpn_ike_policy( |
| 235 |
parsed_args.ikepolicy, ignore_missing=False)['id'] |
| 236 |
attrs['ikepolicy_id'] = _ikepolicy_id |
| 237 |
if parsed_args.ipsecpolicy: |
| 238 |
_ipsecpolicy_id = client.find_vpn_ipsec_policy( |
| 239 |
parsed_args.ipsecpolicy, ignore_missing=False)['id'] |
| 240 |
attrs['ipsecpolicy_id'] = _ipsecpolicy_id |
| 241 |
if parsed_args.peer_id: |
| 242 |
attrs['peer_id'] = parsed_args.peer_id |
| 243 |
if parsed_args.peer_address: |
| 244 |
attrs['peer_address'] = parsed_args.peer_address |
| 245 |
if parsed_args.psk: |
| 246 |
attrs['psk'] = parsed_args.psk |
| 247 |
if parsed_args.name: |
| 248 |
attrs['name'] = parsed_args.name |
| 249 |
if (bool(parsed_args.local_endpoint_group) != |
| 250 |
bool(parsed_args.peer_endpoint_group)): |
| 251 |
message = _("You must specify both local and peer endpoint " |
| 252 |
"groups") |
| 253 |
raise exceptions.CommandError(message) |
| 254 |
if not parsed_args.peer_cidrs and not parsed_args.local_endpoint_group: |
| 255 |
message = _("You must specify endpoint groups or peer CIDR(s)") |
| 256 |
raise exceptions.CommandError(message) |
| 257 |
obj = client.create_vpn_ipsec_site_connection(**attrs) |
| 258 |
display_columns, columns = utils.get_osc_show_columns_for_sdk_resource( |
| 259 |
obj, _attr_map_dict, ['location', 'tenant_id', 'action', |
| 260 |
'timeout', 'interval']) |
| 261 |
data = utils.get_dict_properties(obj, columns, formatters=_formatters) |
| 262 |
return display_columns, data |
| 263 |
|
| 264 |
|
| 265 |
class DeleteIPsecSiteConnection(command.Command): |
| 266 |
_description = _("Delete IPsec site connection(s)") |
| 267 |
|
| 268 |
def get_parser(self, prog_name): |
| 269 |
parser = super(DeleteIPsecSiteConnection, self).get_parser(prog_name) |
| 270 |
parser.add_argument( |
| 271 |
'ipsec_site_connection', |
| 272 |
metavar='<ipsec-site-connection>', |
| 273 |
nargs='+', |
| 274 |
help=_('IPsec site connection to delete (name or ID)')) |
| 275 |
return parser |
| 276 |
|
| 277 |
def take_action(self, parsed_args): |
| 278 |
client = self.app.client_manager.network |
| 279 |
result = 0 |
| 280 |
for ipsec_conn in parsed_args.ipsec_site_connection: |
| 281 |
try: |
| 282 |
ipsec_con_id = client.find_vpn_ipsec_site_connection( |
| 283 |
ipsec_conn, ignore_missing=False)['id'] |
| 284 |
client.delete_vpn_ipsec_site_connection(ipsec_con_id) |
| 285 |
except Exception as e: |
| 286 |
result += 1 |
| 287 |
LOG.error(_("Failed to delete IPsec site connection with " |
| 288 |
"name or ID '%(ipsec_site_conn)s': %(e)s"), |
| 289 |
{'ipsec_site_conn': ipsec_conn, 'e': e}) |
| 290 |
|
| 291 |
if result > 0: |
| 292 |
total = len(parsed_args.ipsec_site_connection) |
| 293 |
msg = (_("%(result)s of %(total)s IPsec site connection failed " |
| 294 |
"to delete.") % {'result': result, 'total': total}) |
| 295 |
raise exceptions.CommandError(msg) |
| 296 |
|
| 297 |
|
| 298 |
class ListIPsecSiteConnection(command.Lister): |
| 299 |
_description = _("List IPsec site connections " |
| 300 |
"that belong to a given project") |
| 301 |
|
| 302 |
def get_parser(self, prog_name): |
| 303 |
parser = super(ListIPsecSiteConnection, self).get_parser(prog_name) |
| 304 |
parser.add_argument( |
| 305 |
'--long', |
| 306 |
action='store_true', |
| 307 |
default=False, |
| 308 |
help=_("List additional fields in output") |
| 309 |
) |
| 310 |
return parser |
| 311 |
|
| 312 |
def take_action(self, parsed_args): |
| 313 |
client = self.app.client_manager.network |
| 314 |
obj = client.vpn_ipsec_site_connections() |
| 315 |
headers, columns = column_util.get_column_definitions( |
| 316 |
_attr_map, long_listing=parsed_args.long) |
| 317 |
return (headers, (utils.get_dict_properties( |
| 318 |
s, columns, formatters=_formatters) for s in obj)) |
| 319 |
|
| 320 |
|
| 321 |
class SetIPsecSiteConnection(command.Command): |
| 322 |
_description = _("Set IPsec site connection properties") |
| 323 |
|
| 324 |
def get_parser(self, prog_name): |
| 325 |
parser = super(SetIPsecSiteConnection, self).get_parser(prog_name) |
| 326 |
_get_common_parser(parser) |
| 327 |
parser.add_argument( |
| 328 |
'--peer-id', |
| 329 |
help=_('Peer router identity for authentication. Can be ' |
| 330 |
'IPv4/IPv6 address, e-mail address, key id, or FQDN')) |
| 331 |
parser.add_argument( |
| 332 |
'--peer-address', |
| 333 |
help=_('Peer gateway public IPv4/IPv6 address or FQDN')) |
| 334 |
parser.add_argument( |
| 335 |
'--name', |
| 336 |
metavar='<name>', |
| 337 |
help=_('Set friendly name for the connection')) |
| 338 |
parser.add_argument( |
| 339 |
'ipsec_site_connection', |
| 340 |
metavar='<ipsec-site-connection>', |
| 341 |
help=_('IPsec site connection to set (name or ID)')) |
| 342 |
return parser |
| 343 |
|
| 344 |
def take_action(self, parsed_args): |
| 345 |
client = self.app.client_manager.network |
| 346 |
attrs = _get_common_attrs(self.app.client_manager, |
| 347 |
parsed_args, is_create=False) |
| 348 |
if parsed_args.peer_id: |
| 349 |
attrs['peer_id'] = parsed_args.peer_id |
| 350 |
if parsed_args.peer_address: |
| 351 |
attrs['peer_address'] = parsed_args.peer_address |
| 352 |
if parsed_args.name: |
| 353 |
attrs['name'] = parsed_args.name |
| 354 |
ipsec_conn_id = client.find_vpn_ipsec_site_connection( |
| 355 |
parsed_args.ipsec_site_connection, ignore_missing=False)['id'] |
| 356 |
try: |
| 357 |
client.update_vpn_ipsec_site_connection(ipsec_conn_id, **attrs) |
| 358 |
except Exception as e: |
| 359 |
msg = (_("Failed to set IPsec site " |
| 360 |
"connection '%(ipsec_conn)s': %(e)s") |
| 361 |
% {'ipsec_conn': parsed_args.ipsec_site_connection, 'e': e}) |
| 362 |
raise exceptions.CommandError(msg) |
| 363 |
|
| 364 |
|
| 365 |
class ShowIPsecSiteConnection(command.ShowOne): |
| 366 |
_description = _("Show information of a given IPsec site connection") |
| 367 |
|
| 368 |
def get_parser(self, prog_name): |
| 369 |
parser = super(ShowIPsecSiteConnection, self).get_parser(prog_name) |
| 370 |
parser.add_argument( |
| 371 |
'ipsec_site_connection', |
| 372 |
metavar='<ipsec-site-connection>', |
| 373 |
help=_('IPsec site connection to display (name or ID)')) |
| 374 |
return parser |
| 375 |
|
| 376 |
def take_action(self, parsed_args): |
| 377 |
client = self.app.client_manager.network |
| 378 |
ipsec_site_id = client.find_vpn_ipsec_site_connection( |
| 379 |
parsed_args.ipsec_site_connection, ignore_missing=False)['id'] |
| 380 |
obj = client.get_vpn_ipsec_site_connection(ipsec_site_id) |
| 381 |
display_columns, columns = utils.get_osc_show_columns_for_sdk_resource( |
| 382 |
obj, _attr_map_dict, ['location', 'tenant_id', 'action', |
| 383 |
'timeout', 'interval']) |
| 384 |
data = utils.get_dict_properties(obj, columns, formatters=_formatters) |
| 385 |
return (display_columns, data) |