| 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 parseractions |
| 20 |
from osc_lib.command import command |
| 21 |
from osc_lib import exceptions |
| 22 |
from osc_lib import utils as osc_utils |
| 23 |
from osc_lib.utils import columns as column_util |
| 24 |
|
| 25 |
from neutronclient._i18n import _ |
| 26 |
from neutronclient.osc import utils as nc_osc_utils |
| 27 |
from neutronclient.osc.v2.networking_bgpvpn import constants |
| 28 |
|
| 29 |
LOG = logging.getLogger(__name__) |
| 30 |
|
| 31 |
|
| 32 |
class CreateBgpvpnResAssoc(command.ShowOne): |
| 33 |
"""Create a BGP VPN resource association""" |
| 34 |
_action = 'create' |
| 35 |
|
| 36 |
def get_parser(self, prog_name): |
| 37 |
parser = super(CreateBgpvpnResAssoc, self).get_parser(prog_name) |
| 38 |
nc_osc_utils.add_project_owner_option_to_parser(parser) |
| 39 |
parser.add_argument( |
| 40 |
'bgpvpn', |
| 41 |
metavar="<bgpvpn>", |
| 42 |
help=(_("BGP VPN to apply the %s association (name or ID)") % |
| 43 |
self._assoc_res_name), |
| 44 |
) |
| 45 |
parser.add_argument( |
| 46 |
'resource', |
| 47 |
metavar="<%s>" % self._assoc_res_name, |
| 48 |
help=(_("%s to associate the BGP VPN (name or ID)") % |
| 49 |
self._assoc_res_name.capitalize()), |
| 50 |
) |
| 51 |
|
| 52 |
get_common_parser = getattr(self, '_get_common_parser', None) |
| 53 |
if callable(get_common_parser): |
| 54 |
get_common_parser(parser) |
| 55 |
|
| 56 |
return parser |
| 57 |
|
| 58 |
def take_action(self, parsed_args): |
| 59 |
client = self.app.client_manager.network |
| 60 |
bgpvpn = client.find_bgpvpn(parsed_args.bgpvpn) |
| 61 |
find_res_method = getattr( |
| 62 |
client, 'find_%s' % self._assoc_res_name) |
| 63 |
assoc_res = find_res_method(parsed_args.resource) |
| 64 |
body = {'%s_id' % self._assoc_res_name: assoc_res['id']} |
| 65 |
if 'project' in parsed_args and parsed_args.project is not None: |
| 66 |
project_id = nc_osc_utils.find_project( |
| 67 |
self.app.client_manager.identity, |
| 68 |
parsed_args.project, |
| 69 |
parsed_args.project_domain, |
| 70 |
).id |
| 71 |
body['tenant_id'] = project_id |
| 72 |
|
| 73 |
arg2body = getattr(self, '_args2body', None) |
| 74 |
if callable(arg2body): |
| 75 |
body.update( |
| 76 |
arg2body(bgpvpn['id'], parsed_args)) |
| 77 |
|
| 78 |
if self._resource == constants.NETWORK_ASSOC: |
| 79 |
obj = client.create_bgpvpn_network_association( |
| 80 |
bgpvpn['id'], **body) |
| 81 |
elif self._resource == constants.PORT_ASSOC: |
| 82 |
obj = client.create_bgpvpn_port_association(bgpvpn['id'], **body) |
| 83 |
else: |
| 84 |
obj = client.create_bgpvpn_router_association( |
| 85 |
bgpvpn['id'], **body) |
| 86 |
transform = getattr(self, '_transform_resource', None) |
| 87 |
if callable(transform): |
| 88 |
transform(obj) |
| 89 |
display_columns, columns = nc_osc_utils._get_columns(obj) |
| 90 |
data = osc_utils.get_dict_properties(obj, columns, |
| 91 |
formatters=self._formatters) |
| 92 |
return display_columns, data |
| 93 |
|
| 94 |
|
| 95 |
class SetBgpvpnResAssoc(command.Command): |
| 96 |
"""Set BGP VPN resource association properties""" |
| 97 |
_action = 'set' |
| 98 |
|
| 99 |
def get_parser(self, prog_name): |
| 100 |
parser = super(SetBgpvpnResAssoc, self).get_parser(prog_name) |
| 101 |
parser.add_argument( |
| 102 |
'resource_association_id', |
| 103 |
metavar="<%s association ID>" % self._assoc_res_name, |
| 104 |
help=(_("%s association ID to update") % |
| 105 |
self._assoc_res_name.capitalize()), |
| 106 |
) |
| 107 |
parser.add_argument( |
| 108 |
'bgpvpn', |
| 109 |
metavar="<bgpvpn>", |
| 110 |
help=(_("BGP VPN the %s association belongs to (name or ID)") % |
| 111 |
self._assoc_res_name), |
| 112 |
) |
| 113 |
|
| 114 |
get_common_parser = getattr(self, '_get_common_parser', None) |
| 115 |
if callable(get_common_parser): |
| 116 |
get_common_parser(parser) |
| 117 |
|
| 118 |
return parser |
| 119 |
|
| 120 |
def take_action(self, parsed_args): |
| 121 |
client = self.app.client_manager.network |
| 122 |
bgpvpn = client.find_bgpvpn(parsed_args.bgpvpn) |
| 123 |
arg2body = getattr(self, '_args2body', None) |
| 124 |
if callable(arg2body): |
| 125 |
body = arg2body(bgpvpn['id'], parsed_args) |
| 126 |
if self._resource == constants.NETWORK_ASSOC: |
| 127 |
client.update_bgpvpn_network_association( |
| 128 |
bgpvpn['id'], parsed_args.resource_association_id, **body) |
| 129 |
elif self._resource == constants.PORT_ASSOC: |
| 130 |
client.update_bgpvpn_port_association( |
| 131 |
bgpvpn['id'], parsed_args.resource_association_id, **body) |
| 132 |
else: |
| 133 |
client.update_bgpvpn_router_association( |
| 134 |
bgpvpn['id'], parsed_args.resource_association_id, **body) |
| 135 |
|
| 136 |
|
| 137 |
class UnsetBgpvpnResAssoc(SetBgpvpnResAssoc): |
| 138 |
"""Unset BGP VPN resource association properties""" |
| 139 |
_action = 'unset' |
| 140 |
|
| 141 |
|
| 142 |
class DeleteBgpvpnResAssoc(command.Command): |
| 143 |
"""Remove a BGP VPN resource association(s) for a given BGP VPN""" |
| 144 |
|
| 145 |
def get_parser(self, prog_name): |
| 146 |
parser = super(DeleteBgpvpnResAssoc, self).get_parser(prog_name) |
| 147 |
parser.add_argument( |
| 148 |
'resource_association_ids', |
| 149 |
metavar="<%s association ID>" % self._assoc_res_name, |
| 150 |
nargs="+", |
| 151 |
help=(_("%s association ID(s) to remove") % |
| 152 |
self._assoc_res_name.capitalize()), |
| 153 |
) |
| 154 |
parser.add_argument( |
| 155 |
'bgpvpn', |
| 156 |
metavar="<bgpvpn>", |
| 157 |
help=(_("BGP VPN the %s association belongs to (name or ID)") % |
| 158 |
self._assoc_res_name), |
| 159 |
) |
| 160 |
return parser |
| 161 |
|
| 162 |
def take_action(self, parsed_args): |
| 163 |
client = self.app.client_manager.network |
| 164 |
bgpvpn = client.find_bgpvpn(parsed_args.bgpvpn) |
| 165 |
fails = 0 |
| 166 |
for id in parsed_args.resource_association_ids: |
| 167 |
try: |
| 168 |
if self._resource == constants.NETWORK_ASSOC: |
| 169 |
client.delete_bgpvpn_network_association(bgpvpn['id'], id) |
| 170 |
elif self._resource == constants.PORT_ASSOC: |
| 171 |
client.delete_bgpvpn_port_association(bgpvpn['id'], id) |
| 172 |
else: |
| 173 |
client.delete_bgpvpn_router_association(bgpvpn['id'], id) |
| 174 |
LOG.warning( |
| 175 |
"%(assoc_res_name)s association %(id)s deleted", |
| 176 |
{'assoc_res_name': self._assoc_res_name.capitalize(), |
| 177 |
'id': id}) |
| 178 |
except Exception as e: |
| 179 |
fails += 1 |
| 180 |
LOG.error("Failed to delete %(assoc_res_name)s " |
| 181 |
"association with ID '%(id)s': %(e)s", |
| 182 |
{'assoc_res_name': self._assoc_res_name, |
| 183 |
'id': id, |
| 184 |
'e': e}) |
| 185 |
if fails > 0: |
| 186 |
msg = (_("Failed to delete %(fails)s of %(total)s " |
| 187 |
"%(assoc_res_name)s BGP VPN association(s).") % |
| 188 |
{'fails': fails, |
| 189 |
'total': len(parsed_args.resource_association_ids), |
| 190 |
'assoc_res_name': self._assoc_res_name}) |
| 191 |
raise exceptions.CommandError(msg) |
| 192 |
|
| 193 |
|
| 194 |
class ListBgpvpnResAssoc(command.Lister): |
| 195 |
"""List BGP VPN resource associations for a given BGP VPN""" |
| 196 |
|
| 197 |
def get_parser(self, prog_name): |
| 198 |
parser = super(ListBgpvpnResAssoc, self).get_parser(prog_name) |
| 199 |
parser.add_argument( |
| 200 |
'bgpvpn', |
| 201 |
metavar="<bgpvpn>", |
| 202 |
help=_("BGP VPN listed associations belong to (name or ID)"), |
| 203 |
) |
| 204 |
parser.add_argument( |
| 205 |
'--long', |
| 206 |
action='store_true', |
| 207 |
help=_("List additional fields in output"), |
| 208 |
) |
| 209 |
parser.add_argument( |
| 210 |
'--property', |
| 211 |
metavar="<key=value>", |
| 212 |
help=_("Filter property to apply on returned BGP VPNs (repeat to " |
| 213 |
"filter on multiple properties)"), |
| 214 |
action=parseractions.KeyValueAction, |
| 215 |
) |
| 216 |
return parser |
| 217 |
|
| 218 |
def take_action(self, parsed_args): |
| 219 |
client = self.app.client_manager.network |
| 220 |
bgpvpn = client.find_bgpvpn(parsed_args.bgpvpn) |
| 221 |
params = {} |
| 222 |
if parsed_args.property: |
| 223 |
params.update(parsed_args.property) |
| 224 |
if self._resource == constants.NETWORK_ASSOC: |
| 225 |
objs = client.bgpvpn_network_associations( |
| 226 |
bgpvpn['id'], retrieve_all=True, **params) |
| 227 |
elif self._resource == constants.PORT_ASSOC: |
| 228 |
objs = client.bgpvpn_port_associations( |
| 229 |
bgpvpn['id'], retrieve_all=True, **params) |
| 230 |
else: |
| 231 |
objs = client.bgpvpn_router_associations( |
| 232 |
bgpvpn['id'], retrieve_all=True, **params) |
| 233 |
transform = getattr(self, '_transform_resource', None) |
| 234 |
transformed_objs = [] |
| 235 |
if callable(transform): |
| 236 |
for obj in objs: |
| 237 |
transformed_objs.append(transform(obj)) |
| 238 |
else: |
| 239 |
transformed_objs = list(objs) |
| 240 |
headers, columns = column_util.get_column_definitions( |
| 241 |
self._attr_map, long_listing=parsed_args.long) |
| 242 |
return (headers, (osc_utils.get_dict_properties( |
| 243 |
s, columns, formatters=self._formatters) |
| 244 |
for s in transformed_objs)) |
| 245 |
|
| 246 |
|
| 247 |
class ShowBgpvpnResAssoc(command.ShowOne): |
| 248 |
"""Show information of a given BGP VPN resource association""" |
| 249 |
|
| 250 |
def get_parser(self, prog_name): |
| 251 |
parser = super(ShowBgpvpnResAssoc, self).get_parser(prog_name) |
| 252 |
parser.add_argument( |
| 253 |
'resource_association_id', |
| 254 |
metavar="<%s association ID>" % self._assoc_res_name, |
| 255 |
help=(_("%s association ID to look up") % |
| 256 |
self._assoc_res_name.capitalize()), |
| 257 |
) |
| 258 |
parser.add_argument( |
| 259 |
'bgpvpn', |
| 260 |
metavar="<bgpvpn>", |
| 261 |
help=_("BGP VPN the association belongs to (name or ID)"), |
| 262 |
) |
| 263 |
return parser |
| 264 |
|
| 265 |
def take_action(self, parsed_args): |
| 266 |
client = self.app.client_manager.network |
| 267 |
bgpvpn = client.find_bgpvpn(parsed_args.bgpvpn) |
| 268 |
if self._resource == constants.NETWORK_ASSOC: |
| 269 |
obj = client.get_bgpvpn_network_association( |
| 270 |
bgpvpn['id'], parsed_args.resource_association_id) |
| 271 |
elif self._resource == constants.PORT_ASSOC: |
| 272 |
obj = client.get_bgpvpn_port_association( |
| 273 |
bgpvpn['id'], parsed_args.resource_association_id) |
| 274 |
else: |
| 275 |
obj = client.get_bgpvpn_router_association( |
| 276 |
bgpvpn['id'], parsed_args.resource_association_id) |
| 277 |
transform = getattr(self, '_transform_resource', None) |
| 278 |
if callable(transform): |
| 279 |
transform(obj) |
| 280 |
display_columns, columns = nc_osc_utils._get_columns(obj) |
| 281 |
data = osc_utils.get_dict_properties(obj, columns, |
| 282 |
formatters=self._formatters) |
| 283 |
return display_columns, data |