| 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 |
from osc_lib.command import command |
| 15 |
from osc_lib import utils |
| 16 |
|
| 17 |
from neutronclient._i18n import _ |
| 18 |
from neutronclient.osc import utils as nc_osc_utils |
| 19 |
from neutronclient.osc.v2.dynamic_routing import constants |
| 20 |
|
| 21 |
|
| 22 |
def _get_attrs(client_manager, parsed_args): |
| 23 |
attrs = {} |
| 24 |
if parsed_args.name is not None: |
| 25 |
attrs['name'] = str(parsed_args.name) |
| 26 |
if 'local_as' in parsed_args: |
| 27 |
attrs['local_as'] = parsed_args.local_as |
| 28 |
if 'ip_version' in parsed_args: |
| 29 |
attrs['ip_version'] = parsed_args.ip_version |
| 30 |
if parsed_args.advertise_tenant_networks: |
| 31 |
attrs['advertise_tenant_networks'] = True |
| 32 |
if parsed_args.no_advertise_tenant_networks: |
| 33 |
attrs['advertise_tenant_networks'] = False |
| 34 |
if parsed_args.advertise_floating_ip_host_routes: |
| 35 |
attrs['advertise_floating_ip_host_routes'] = True |
| 36 |
if parsed_args.no_advertise_floating_ip_host_routes: |
| 37 |
attrs['advertise_floating_ip_host_routes'] = False |
| 38 |
|
| 39 |
if 'project' in parsed_args and parsed_args.project is not None: |
| 40 |
identity_client = client_manager.identity |
| 41 |
project_id = nc_osc_utils.find_project( |
| 42 |
identity_client, |
| 43 |
parsed_args.project, |
| 44 |
parsed_args.project_domain, |
| 45 |
).id |
| 46 |
attrs['tenant_id'] = project_id |
| 47 |
return attrs |
| 48 |
|
| 49 |
|
| 50 |
def add_common_arguments(parser): |
| 51 |
parser.add_argument( |
| 52 |
'--advertise-floating-ip-host-routes', |
| 53 |
action='store_true', |
| 54 |
help=_("Enable the advertisement of floating IP host routes " |
| 55 |
"by the BGP speaker. (default)")) |
| 56 |
parser.add_argument( |
| 57 |
'--no-advertise-floating-ip-host-routes', |
| 58 |
action='store_true', |
| 59 |
help=_("Disable the advertisement of floating IP host routes " |
| 60 |
"by the BGP speaker.")) |
| 61 |
parser.add_argument( |
| 62 |
'--advertise-tenant-networks', |
| 63 |
action='store_true', |
| 64 |
help=_("Enable the advertisement of tenant network routes " |
| 65 |
"by the BGP speaker. (default)")) |
| 66 |
parser.add_argument( |
| 67 |
'--no-advertise-tenant-networks', |
| 68 |
action='store_true', |
| 69 |
help=_("Disable the advertisement of tenant network routes " |
| 70 |
"by the BGP speaker.")) |
| 71 |
|
| 72 |
|
| 73 |
class AddNetworkToSpeaker(command.Command): |
| 74 |
_description = _("Add a network to a BGP speaker") |
| 75 |
|
| 76 |
def get_parser(self, prog_name): |
| 77 |
parser = super(AddNetworkToSpeaker, self).get_parser(prog_name) |
| 78 |
parser.add_argument( |
| 79 |
'bgp_speaker', |
| 80 |
metavar='<bgp-speaker>', |
| 81 |
help=_("BGP speaker (name or ID)")) |
| 82 |
parser.add_argument( |
| 83 |
'network', |
| 84 |
metavar='<network>', |
| 85 |
help=_("Network to add (name or ID)")) |
| 86 |
return parser |
| 87 |
|
| 88 |
def take_action(self, parsed_args): |
| 89 |
client = self.app.client_manager.network |
| 90 |
speaker_id = client.find_bgp_speaker(parsed_args.bgp_speaker, |
| 91 |
ignore_missing=False).id |
| 92 |
net_id = client.find_network(parsed_args.network, |
| 93 |
ignore_missing=False).id |
| 94 |
client.add_gateway_network_to_speaker(speaker_id, net_id) |
| 95 |
|
| 96 |
|
| 97 |
class AddPeerToSpeaker(command.Command): |
| 98 |
_description = _("Add a peer to a BGP speaker") |
| 99 |
|
| 100 |
def get_parser(self, prog_name): |
| 101 |
parser = super(AddPeerToSpeaker, self).get_parser(prog_name) |
| 102 |
parser.add_argument( |
| 103 |
'bgp_speaker', |
| 104 |
metavar='<bgp-speaker>', |
| 105 |
help=_("BGP speaker (name or ID)")) |
| 106 |
parser.add_argument( |
| 107 |
'bgp_peer', |
| 108 |
metavar='<bgp-peer>', |
| 109 |
help=_("BGP Peer to add (name or ID)")) |
| 110 |
return parser |
| 111 |
|
| 112 |
def take_action(self, parsed_args): |
| 113 |
client = self.app.client_manager.network |
| 114 |
speaker_id = client.find_bgp_speaker(parsed_args.bgp_speaker)['id'] |
| 115 |
peer_id = client.find_bgp_peer(parsed_args.bgp_peer)['id'] |
| 116 |
client.add_bgp_peer_to_speaker(speaker_id, peer_id) |
| 117 |
|
| 118 |
|
| 119 |
class CreateBgpSpeaker(command.ShowOne): |
| 120 |
_description = _("Create a BGP speaker") |
| 121 |
|
| 122 |
def get_parser(self, prog_name): |
| 123 |
parser = super(CreateBgpSpeaker, self).get_parser(prog_name) |
| 124 |
parser.add_argument( |
| 125 |
'name', |
| 126 |
metavar='<name>', |
| 127 |
help=_("Name of the BGP speaker to create")) |
| 128 |
parser.add_argument( |
| 129 |
'--local-as', |
| 130 |
metavar='<local-as>', |
| 131 |
required=True, |
| 132 |
help=_("Local AS number. (Integer in [%(min_val)s, %(max_val)s] " |
| 133 |
"is allowed.)") % {'min_val': constants.MIN_AS_NUM, |
| 134 |
'max_val': constants.MAX_AS_NUM}) |
| 135 |
parser.add_argument( |
| 136 |
'--ip-version', |
| 137 |
type=int, choices=[4, 6], |
| 138 |
default=4, |
| 139 |
help=_("IP version for the BGP speaker (default is 4)")) |
| 140 |
add_common_arguments(parser) |
| 141 |
nc_osc_utils.add_project_owner_option_to_parser(parser) |
| 142 |
return parser |
| 143 |
|
| 144 |
def take_action(self, parsed_args): |
| 145 |
client = self.app.client_manager.network |
| 146 |
attrs = _get_attrs(self.app.client_manager, parsed_args) |
| 147 |
obj = client.create_bgp_speaker(**attrs) |
| 148 |
display_columns, columns = nc_osc_utils._get_columns(obj) |
| 149 |
data = utils.get_dict_properties(obj, columns) |
| 150 |
return display_columns, data |
| 151 |
|
| 152 |
|
| 153 |
class DeleteBgpSpeaker(command.Command): |
| 154 |
_description = _("Delete a BGP speaker") |
| 155 |
|
| 156 |
def get_parser(self, prog_name): |
| 157 |
parser = super(DeleteBgpSpeaker, self).get_parser(prog_name) |
| 158 |
parser.add_argument( |
| 159 |
'bgp_speaker', |
| 160 |
metavar="<bgp-speaker>", |
| 161 |
help=_("BGP speaker to delete (name or ID)") |
| 162 |
) |
| 163 |
return parser |
| 164 |
|
| 165 |
def take_action(self, parsed_args): |
| 166 |
client = self.app.client_manager.network |
| 167 |
id = client.find_bgp_speaker(parsed_args.bgp_speaker)['id'] |
| 168 |
client.delete_bgp_speaker(id) |
| 169 |
|
| 170 |
|
| 171 |
class ListBgpSpeaker(command.Lister): |
| 172 |
_description = _("List BGP speakers") |
| 173 |
|
| 174 |
def get_parser(self, prog_name): |
| 175 |
parser = super(ListBgpSpeaker, self).get_parser(prog_name) |
| 176 |
parser.add_argument( |
| 177 |
'--agent', |
| 178 |
metavar='<agent-id>', |
| 179 |
help=_("List BGP speakers hosted by an agent (ID only)")) |
| 180 |
return parser |
| 181 |
|
| 182 |
def take_action(self, parsed_args): |
| 183 |
client = self.app.client_manager.network |
| 184 |
if parsed_args.agent is not None: |
| 185 |
data = client.get_bgp_speakers_hosted_by_dragent(parsed_args.agent) |
| 186 |
else: |
| 187 |
data = client.bgp_speakers(retrieve_all=True) |
| 188 |
|
| 189 |
headers = ('ID', 'Name', 'Local AS', 'IP Version') |
| 190 |
columns = ('id', 'name', 'local_as', 'ip_version') |
| 191 |
return (headers, (utils.get_dict_properties(s, columns) |
| 192 |
for s in data)) |
| 193 |
|
| 194 |
|
| 195 |
class ListRoutesAdvertisedBySpeaker(command.Lister): |
| 196 |
_description = _("List routes advertised") |
| 197 |
|
| 198 |
def get_parser(self, prog_name): |
| 199 |
parser = super(ListRoutesAdvertisedBySpeaker, |
| 200 |
self).get_parser(prog_name) |
| 201 |
parser.add_argument( |
| 202 |
'bgp_speaker', |
| 203 |
metavar='<bgp-speaker>', |
| 204 |
help=_("BGP speaker (name or ID)")) |
| 205 |
return parser |
| 206 |
|
| 207 |
def take_action(self, parsed_args): |
| 208 |
client = self.app.client_manager.network |
| 209 |
speaker_id = client.find_bgp_speaker(parsed_args.bgp_speaker)['id'] |
| 210 |
data = client.get_advertised_routes_of_speaker(speaker_id) |
| 211 |
headers = ('Destination', 'Nexthop') |
| 212 |
columns = ('destination', 'next_hop') |
| 213 |
return (headers, (utils.get_dict_properties(s, columns) |
| 214 |
for s in data['advertised_routes'])) |
| 215 |
|
| 216 |
|
| 217 |
class RemoveNetworkFromSpeaker(command.Command): |
| 218 |
_description = _("Remove a network from a BGP speaker") |
| 219 |
|
| 220 |
def get_parser(self, prog_name): |
| 221 |
parser = super(RemoveNetworkFromSpeaker, self).get_parser(prog_name) |
| 222 |
parser.add_argument( |
| 223 |
'bgp_speaker', |
| 224 |
metavar='<bgp-speaker>', |
| 225 |
help=_("BGP speaker (name or ID)")) |
| 226 |
parser.add_argument( |
| 227 |
'network', |
| 228 |
metavar='<network>', |
| 229 |
help=_("Network to remove (name or ID)")) |
| 230 |
return parser |
| 231 |
|
| 232 |
def take_action(self, parsed_args): |
| 233 |
client = self.app.client_manager.network |
| 234 |
speaker_id = client.find_bgp_speaker(parsed_args.bgp_speaker)['id'] |
| 235 |
net_id = client.find_network(parsed_args.network)['id'] |
| 236 |
client.remove_gateway_network_from_speaker(speaker_id, net_id) |
| 237 |
|
| 238 |
|
| 239 |
class RemovePeerFromSpeaker(command.Command): |
| 240 |
_description = _("Remove a peer from a BGP speaker") |
| 241 |
|
| 242 |
def get_parser(self, prog_name): |
| 243 |
parser = super(RemovePeerFromSpeaker, self).get_parser(prog_name) |
| 244 |
parser.add_argument( |
| 245 |
'bgp_speaker', |
| 246 |
metavar='<bgp-speaker>', |
| 247 |
help=_("BGP speaker (name or ID)")) |
| 248 |
parser.add_argument( |
| 249 |
'bgp_peer', |
| 250 |
metavar='<bgp-peer>', |
| 251 |
help=_("BGP Peer to remove (name or ID)")) |
| 252 |
return parser |
| 253 |
|
| 254 |
def take_action(self, parsed_args): |
| 255 |
client = self.app.client_manager.network |
| 256 |
speaker_id = client.find_bgp_speaker(parsed_args.bgp_speaker)['id'] |
| 257 |
peer_id = client.find_bgp_peer(parsed_args.bgp_peer)['id'] |
| 258 |
client.remove_bgp_peer_from_speaker(speaker_id, peer_id) |
| 259 |
|
| 260 |
|
| 261 |
class SetBgpSpeaker(command.Command): |
| 262 |
_description = _("Set BGP speaker properties") |
| 263 |
|
| 264 |
resource = constants.BGP_SPEAKER |
| 265 |
|
| 266 |
def get_parser(self, prog_name): |
| 267 |
parser = super(SetBgpSpeaker, self).get_parser(prog_name) |
| 268 |
parser.add_argument( |
| 269 |
'bgp_speaker', |
| 270 |
metavar="<bgp-speaker>", |
| 271 |
help=_("BGP speaker to update (name or ID)") |
| 272 |
) |
| 273 |
parser.add_argument( |
| 274 |
'--name', |
| 275 |
help=_("New name for the BGP speaker")) |
| 276 |
add_common_arguments(parser) |
| 277 |
return parser |
| 278 |
|
| 279 |
def take_action(self, parsed_args): |
| 280 |
client = self.app.client_manager.network |
| 281 |
id = client.find_bgp_speaker(parsed_args.bgp_speaker)['id'] |
| 282 |
attrs = _get_attrs(self.app.client_manager, parsed_args) |
| 283 |
client.update_bgp_speaker(id, **attrs) |
| 284 |
|
| 285 |
|
| 286 |
class ShowBgpSpeaker(command.ShowOne): |
| 287 |
_description = _("Show a BGP speaker") |
| 288 |
|
| 289 |
def get_parser(self, prog_name): |
| 290 |
parser = super(ShowBgpSpeaker, self).get_parser(prog_name) |
| 291 |
parser.add_argument( |
| 292 |
'bgp_speaker', |
| 293 |
metavar="<bgp-speaker>", |
| 294 |
help=_("BGP speaker to display (name or ID)") |
| 295 |
) |
| 296 |
return parser |
| 297 |
|
| 298 |
def take_action(self, parsed_args): |
| 299 |
client = self.app.client_manager.network |
| 300 |
id = client.find_bgp_speaker(parsed_args.bgp_speaker, |
| 301 |
ignore_missing=False).id |
| 302 |
obj = client.get_bgp_speaker(id) |
| 303 |
display_columns, columns = nc_osc_utils._get_columns(obj) |
| 304 |
data = utils.get_dict_properties(obj, columns) |
| 305 |
return display_columns, data |