| 1 |
# Copyright (c) 2016 Juniper Networks Inc. |
| 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 |
import logging |
| 18 |
|
| 19 |
from osc_lib.cli import format_columns |
| 20 |
from osc_lib.cli.parseractions import KeyValueAction |
| 21 |
from osc_lib.command import command |
| 22 |
from osc_lib import exceptions |
| 23 |
from osc_lib import utils as osc_utils |
| 24 |
from osc_lib.utils import columns as column_util |
| 25 |
|
| 26 |
from neutronclient._i18n import _ |
| 27 |
from neutronclient.osc import utils as nc_osc_utils |
| 28 |
|
| 29 |
LOG = logging.getLogger(__name__) |
| 30 |
|
| 31 |
_attr_map = ( |
| 32 |
('id', 'ID', column_util.LIST_BOTH), |
| 33 |
('project_id', 'Project', column_util.LIST_LONG_ONLY), |
| 34 |
('name', 'Name', column_util.LIST_BOTH), |
| 35 |
('type', 'Type', column_util.LIST_BOTH), |
| 36 |
('route_targets', 'Route Targets', column_util.LIST_LONG_ONLY), |
| 37 |
('import_targets', 'Import Targets', column_util.LIST_LONG_ONLY), |
| 38 |
('export_targets', 'Export Targets', column_util.LIST_LONG_ONLY), |
| 39 |
('route_distinguishers', 'Route Distinguishers', |
| 40 |
column_util.LIST_LONG_ONLY), |
| 41 |
('networks', 'Associated Networks', column_util.LIST_LONG_ONLY), |
| 42 |
('routers', 'Associated Routers', column_util.LIST_LONG_ONLY), |
| 43 |
('ports', 'Associated Ports', column_util.LIST_LONG_ONLY), |
| 44 |
('vni', 'VNI', column_util.LIST_LONG_ONLY), |
| 45 |
('local_pref', 'Local Pref', column_util.LIST_LONG_ONLY), |
| 46 |
) |
| 47 |
_formatters = { |
| 48 |
'route_targets': format_columns.ListColumn, |
| 49 |
'import_targets': format_columns.ListColumn, |
| 50 |
'export_targets': format_columns.ListColumn, |
| 51 |
'route_distinguishers': format_columns.ListColumn, |
| 52 |
'networks': format_columns.ListColumn, |
| 53 |
'routers': format_columns.ListColumn, |
| 54 |
'ports': format_columns.ListColumn, |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
def _get_common_parser(parser, update=None): |
| 59 |
"""Adds to parser arguments common to create, set and unset commands. |
| 60 |
|
| 61 |
:params ArgumentParser parser: argparse object contains all command's |
| 62 |
arguments |
| 63 |
:params string update: Determines if it is a create command (value: None), |
| 64 |
it is a set command (value: 'set') or if it is an |
| 65 |
unset command (value: 'unset') |
| 66 |
""" |
| 67 |
ADD_RT = _("Add Route Target to import/export list") |
| 68 |
REMOVE_RT = _("Remove Route Target from import/export list") |
| 69 |
ADD_IMPORT_RT = _("Add Route Target to import list") |
| 70 |
REMOVE_IMPORT_RT = _("Remove Route Target from import list") |
| 71 |
ADD_EXPORT_RT = _("Add Route Target to export list") |
| 72 |
REMOVE_EXPORT_RT = _("Remove Route Target from export list") |
| 73 |
ADD_RD = _("Add Route Distinguisher to the list of Route Distinguishers " |
| 74 |
"from which a Route Distinguishers will be picked from to " |
| 75 |
"advertise a VPN route") |
| 76 |
REMOVE_RD = _("Remove Route Distinguisher from the list of Route " |
| 77 |
"Distinguishers from which a Route Distinguishers will be " |
| 78 |
"picked from to advertise a VPN route") |
| 79 |
REPEAT_RT = _("repeat option for multiple Route Targets") |
| 80 |
REPEAT_RD = _("repeat option for multiple Route Distinguishers") |
| 81 |
|
| 82 |
def is_appended(): |
| 83 |
return update is None or update == 'set' |
| 84 |
|
| 85 |
if update is None or update == 'set': |
| 86 |
parser.add_argument( |
| 87 |
'--name', |
| 88 |
metavar="<name>", |
| 89 |
help=_("Name of the BGP VPN"), |
| 90 |
) |
| 91 |
parser.add_argument( |
| 92 |
'--route-target', |
| 93 |
dest='route_targets', |
| 94 |
action='append', |
| 95 |
metavar="<route-target>", |
| 96 |
help="%s (%s)" % ((ADD_RT if is_appended() else REMOVE_RT), REPEAT_RT), |
| 97 |
) |
| 98 |
if update: |
| 99 |
parser.add_argument( |
| 100 |
'--no-route-target' if update == 'set' else '--all-route-target', |
| 101 |
dest='purge_route_target', |
| 102 |
action='store_true', |
| 103 |
help=_('Empty route target list'), |
| 104 |
) |
| 105 |
parser.add_argument( |
| 106 |
'--import-target', |
| 107 |
dest='import_targets', |
| 108 |
action='append', |
| 109 |
metavar="<import-target>", |
| 110 |
help="%s (%s)" % ((ADD_IMPORT_RT if is_appended() else |
| 111 |
REMOVE_IMPORT_RT), REPEAT_RT), |
| 112 |
) |
| 113 |
if update: |
| 114 |
parser.add_argument( |
| 115 |
'--no-import-target' if update == 'set' else '--all-import-target', |
| 116 |
dest='purge_import_target', |
| 117 |
action='store_true', |
| 118 |
help=_('Empty import route target list'), |
| 119 |
) |
| 120 |
parser.add_argument( |
| 121 |
'--export-target', |
| 122 |
dest='export_targets', |
| 123 |
action='append', |
| 124 |
metavar="<export-target>", |
| 125 |
help="%s (%s)" % ((ADD_EXPORT_RT if is_appended() else |
| 126 |
REMOVE_EXPORT_RT), REPEAT_RT), |
| 127 |
) |
| 128 |
if update: |
| 129 |
parser.add_argument( |
| 130 |
'--no-export-target' if update == 'set' else |
| 131 |
'--all-export-target', |
| 132 |
dest='purge_export_target', |
| 133 |
action='store_true', |
| 134 |
help=_('Empty export route target list'), |
| 135 |
) |
| 136 |
parser.add_argument( |
| 137 |
'--route-distinguisher', |
| 138 |
dest='route_distinguishers', |
| 139 |
action='append', |
| 140 |
metavar="<route-distinguisher>", |
| 141 |
help="%s (%s)" % ((ADD_RD if is_appended() else REMOVE_RD), REPEAT_RD), |
| 142 |
) |
| 143 |
if update: |
| 144 |
parser.add_argument( |
| 145 |
'--no-route-distinguisher' if update == 'set' else |
| 146 |
'--all-route-distinguisher', |
| 147 |
dest='purge_route_distinguisher', |
| 148 |
action='store_true', |
| 149 |
help=_('Empty route distinguisher list'), |
| 150 |
) |
| 151 |
parser.add_argument( |
| 152 |
'--vni', type=int, |
| 153 |
help=_('VXLAN Network Identifier to be used for this BGPVPN ' |
| 154 |
'when a VXLAN encapsulation is used')) |
| 155 |
parser.add_argument( |
| 156 |
'--local-pref', type=int, |
| 157 |
dest='local_pref', |
| 158 |
help=_('Default BGP LOCAL_PREF to use in route advertisements' |
| 159 |
'towards this BGPVPN.')) |
| 160 |
|
| 161 |
|
| 162 |
def _args2body(client_manager, id, action, args): |
| 163 |
|
| 164 |
if (not (args.purge_route_target and args.purge_import_target and |
| 165 |
args.purge_export_target and args.purge_route_distinguisher) and |
| 166 |
(args.route_targets or args.import_targets or |
| 167 |
args.export_targets or args.route_distinguishers)): |
| 168 |
bgpvpn = client_manager.network.get_bgpvpn(id) |
| 169 |
|
| 170 |
attrs = {} |
| 171 |
|
| 172 |
if 'name' in args and args.name is not None: |
| 173 |
attrs['name'] = str(args.name) |
| 174 |
|
| 175 |
if 'vni' in args and args.vni is not None: |
| 176 |
attrs['vni'] = args.vni |
| 177 |
|
| 178 |
if 'local_pref' in args and args.local_pref is not None: |
| 179 |
attrs['local_pref'] = args.local_pref |
| 180 |
|
| 181 |
if args.purge_route_target: |
| 182 |
attrs['route_targets'] = [] |
| 183 |
elif args.route_targets: |
| 184 |
if action == 'set': |
| 185 |
attrs['route_targets'] = list(set(bgpvpn['route_targets']) | |
| 186 |
set(args.route_targets)) |
| 187 |
elif action == 'unset': |
| 188 |
attrs['route_targets'] = list(set(bgpvpn['route_targets']) - |
| 189 |
set(args.route_targets)) |
| 190 |
|
| 191 |
if args.purge_import_target: |
| 192 |
attrs['import_targets'] = [] |
| 193 |
elif args.import_targets: |
| 194 |
if action == 'set': |
| 195 |
attrs['import_targets'] = list(set(bgpvpn['import_targets']) | |
| 196 |
set(args.import_targets)) |
| 197 |
elif action == 'unset': |
| 198 |
attrs['import_targets'] = list(set(bgpvpn['import_targets']) - |
| 199 |
set(args.import_targets)) |
| 200 |
|
| 201 |
if args.purge_export_target: |
| 202 |
attrs['export_targets'] = [] |
| 203 |
elif args.export_targets: |
| 204 |
if action == 'set': |
| 205 |
attrs['export_targets'] = list(set(bgpvpn['export_targets']) | |
| 206 |
set(args.export_targets)) |
| 207 |
elif action == 'unset': |
| 208 |
attrs['export_targets'] = list(set(bgpvpn['export_targets']) - |
| 209 |
set(args.export_targets)) |
| 210 |
|
| 211 |
if args.purge_route_distinguisher: |
| 212 |
attrs['route_distinguishers'] = [] |
| 213 |
elif args.route_distinguishers: |
| 214 |
if action == 'set': |
| 215 |
attrs['route_distinguishers'] = list( |
| 216 |
set(bgpvpn['route_distinguishers']) | |
| 217 |
set(args.route_distinguishers)) |
| 218 |
elif action == 'unset': |
| 219 |
attrs['route_distinguishers'] = list( |
| 220 |
set(bgpvpn['route_distinguishers']) - |
| 221 |
set(args.route_distinguishers)) |
| 222 |
|
| 223 |
return attrs |
| 224 |
|
| 225 |
|
| 226 |
class CreateBgpvpn(command.ShowOne): |
| 227 |
_description = _("Create BGP VPN resource") |
| 228 |
|
| 229 |
def get_parser(self, prog_name): |
| 230 |
parser = super(CreateBgpvpn, self).get_parser(prog_name) |
| 231 |
nc_osc_utils.add_project_owner_option_to_parser(parser) |
| 232 |
_get_common_parser(parser) |
| 233 |
parser.add_argument( |
| 234 |
'--type', |
| 235 |
default='l3', |
| 236 |
choices=['l2', 'l3'], |
| 237 |
help=_("BGP VPN type selection between IP VPN (l3) and Ethernet " |
| 238 |
"VPN (l2) (default: l3)"), |
| 239 |
) |
| 240 |
return parser |
| 241 |
|
| 242 |
def take_action(self, parsed_args): |
| 243 |
client = self.app.client_manager.network |
| 244 |
attrs = {} |
| 245 |
if parsed_args.name is not None: |
| 246 |
attrs['name'] = str(parsed_args.name) |
| 247 |
if parsed_args.type is not None: |
| 248 |
attrs['type'] = parsed_args.type |
| 249 |
if parsed_args.route_targets is not None: |
| 250 |
attrs['route_targets'] = parsed_args.route_targets |
| 251 |
if parsed_args.import_targets is not None: |
| 252 |
attrs['import_targets'] = parsed_args.import_targets |
| 253 |
if parsed_args.export_targets is not None: |
| 254 |
attrs['export_targets'] = parsed_args.export_targets |
| 255 |
if parsed_args.route_distinguishers is not None: |
| 256 |
attrs['route_distinguishers'] = parsed_args.route_distinguishers |
| 257 |
if parsed_args.vni is not None: |
| 258 |
attrs['vni'] = parsed_args.vni |
| 259 |
if parsed_args.local_pref is not None: |
| 260 |
attrs['local_pref'] = parsed_args.local_pref |
| 261 |
if 'project' in parsed_args and parsed_args.project is not None: |
| 262 |
project_id = nc_osc_utils.find_project( |
| 263 |
self.app.client_manager.identity, |
| 264 |
parsed_args.project, |
| 265 |
parsed_args.project_domain, |
| 266 |
).id |
| 267 |
attrs['tenant_id'] = project_id |
| 268 |
obj = client.create_bgpvpn(**attrs) |
| 269 |
display_columns, columns = nc_osc_utils._get_columns(obj) |
| 270 |
data = osc_utils.get_dict_properties(obj, columns, |
| 271 |
formatters=_formatters) |
| 272 |
return display_columns, data |
| 273 |
|
| 274 |
|
| 275 |
class SetBgpvpn(command.Command): |
| 276 |
_description = _("Set BGP VPN properties") |
| 277 |
|
| 278 |
def get_parser(self, prog_name): |
| 279 |
parser = super(SetBgpvpn, self).get_parser(prog_name) |
| 280 |
parser.add_argument( |
| 281 |
'bgpvpn', |
| 282 |
metavar="<bgpvpn>", |
| 283 |
help=_("BGP VPN to update (name or ID)"), |
| 284 |
) |
| 285 |
_get_common_parser(parser, update='set') |
| 286 |
return parser |
| 287 |
|
| 288 |
def take_action(self, parsed_args): |
| 289 |
client = self.app.client_manager.network |
| 290 |
id = client.find_bgpvpn(parsed_args.bgpvpn)['id'] |
| 291 |
body = _args2body(self.app.client_manager, id, 'set', parsed_args) |
| 292 |
client.update_bgpvpn(id, **body) |
| 293 |
|
| 294 |
|
| 295 |
class UnsetBgpvpn(command.Command): |
| 296 |
_description = _("Unset BGP VPN properties") |
| 297 |
|
| 298 |
def get_parser(self, prog_name): |
| 299 |
parser = super(UnsetBgpvpn, self).get_parser(prog_name) |
| 300 |
parser.add_argument( |
| 301 |
'bgpvpn', |
| 302 |
metavar="<bgpvpn>", |
| 303 |
help=_("BGP VPN to update (name or ID)"), |
| 304 |
) |
| 305 |
_get_common_parser(parser, update='unset') |
| 306 |
return parser |
| 307 |
|
| 308 |
def take_action(self, parsed_args): |
| 309 |
client = self.app.client_manager.network |
| 310 |
id = client.find_bgpvpn(parsed_args.bgpvpn)['id'] |
| 311 |
body = _args2body(self.app.client_manager, id, 'unset', parsed_args) |
| 312 |
client.update_bgpvpn(id, **body) |
| 313 |
|
| 314 |
|
| 315 |
class DeleteBgpvpn(command.Command): |
| 316 |
_description = _("Delete BGP VPN resource(s)") |
| 317 |
|
| 318 |
def get_parser(self, prog_name): |
| 319 |
parser = super(DeleteBgpvpn, self).get_parser(prog_name) |
| 320 |
parser.add_argument( |
| 321 |
'bgpvpns', |
| 322 |
metavar="<bgpvpn>", |
| 323 |
nargs="+", |
| 324 |
help=_("BGP VPN(s) to delete (name or ID)"), |
| 325 |
) |
| 326 |
return parser |
| 327 |
|
| 328 |
def take_action(self, parsed_args): |
| 329 |
client = self.app.client_manager.network |
| 330 |
fails = 0 |
| 331 |
for id_or_name in parsed_args.bgpvpns: |
| 332 |
try: |
| 333 |
id = client.find_bgpvpn(id_or_name)['id'] |
| 334 |
client.delete_bgpvpn(id) |
| 335 |
LOG.warning("BGP VPN %(id)s deleted", {'id': id}) |
| 336 |
except Exception as e: |
| 337 |
fails += 1 |
| 338 |
LOG.error("Failed to delete BGP VPN with name or ID " |
| 339 |
"'%(id_or_name)s': %(e)s", |
| 340 |
{'id_or_name': id_or_name, 'e': e}) |
| 341 |
if fails > 0: |
| 342 |
msg = (_("Failed to delete %(fails)s of %(total)s BGP VPN.") % |
| 343 |
{'fails': fails, 'total': len(parsed_args.bgpvpns)}) |
| 344 |
raise exceptions.CommandError(msg) |
| 345 |
|
| 346 |
|
| 347 |
class ListBgpvpn(command.Lister): |
| 348 |
_description = _("List BGP VPN resources") |
| 349 |
|
| 350 |
def get_parser(self, prog_name): |
| 351 |
parser = super(ListBgpvpn, self).get_parser(prog_name) |
| 352 |
nc_osc_utils.add_project_owner_option_to_parser(parser) |
| 353 |
parser.add_argument( |
| 354 |
'--long', |
| 355 |
action='store_true', |
| 356 |
help=_("List additional fields in output"), |
| 357 |
) |
| 358 |
parser.add_argument( |
| 359 |
'--property', |
| 360 |
metavar="<key=value>", |
| 361 |
default=dict(), |
| 362 |
help=_("Filter property to apply on returned BGP VPNs (repeat to " |
| 363 |
"filter on multiple properties)"), |
| 364 |
action=KeyValueAction, |
| 365 |
) |
| 366 |
return parser |
| 367 |
|
| 368 |
def take_action(self, parsed_args): |
| 369 |
client = self.app.client_manager.network |
| 370 |
params = {} |
| 371 |
if parsed_args.project is not None: |
| 372 |
project_id = nc_osc_utils.find_project( |
| 373 |
self.app.client_manager.identity, |
| 374 |
parsed_args.project, |
| 375 |
parsed_args.project_domain, |
| 376 |
).id |
| 377 |
params['tenant_id'] = project_id |
| 378 |
if parsed_args.property: |
| 379 |
params.update(parsed_args.property) |
| 380 |
objs = client.bgpvpns(**params) |
| 381 |
headers, columns = column_util.get_column_definitions( |
| 382 |
_attr_map, long_listing=parsed_args.long) |
| 383 |
return (headers, (osc_utils.get_dict_properties( |
| 384 |
s, columns, formatters=_formatters) for s in objs)) |
| 385 |
|
| 386 |
|
| 387 |
class ShowBgpvpn(command.ShowOne): |
| 388 |
_description = _("Show information of a given BGP VPN") |
| 389 |
|
| 390 |
def get_parser(self, prog_name): |
| 391 |
parser = super(ShowBgpvpn, self).get_parser(prog_name) |
| 392 |
parser.add_argument( |
| 393 |
'bgpvpn', |
| 394 |
metavar="<bgpvpn>", |
| 395 |
help=_("BGP VPN to display (name or ID)"), |
| 396 |
) |
| 397 |
return parser |
| 398 |
|
| 399 |
def take_action(self, parsed_args): |
| 400 |
client = self.app.client_manager.network |
| 401 |
id = client.find_bgpvpn(parsed_args.bgpvpn)['id'] |
| 402 |
obj = client.get_bgpvpn(id) |
| 403 |
display_columns, columns = nc_osc_utils._get_columns(obj) |
| 404 |
data = osc_utils.get_dict_properties(obj, columns, |
| 405 |
formatters=_formatters) |
| 406 |
return display_columns, data |