| 1 |
# Copyright 2016 Huawei Technologies India Pvt. Ltd. |
| 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 neutronclient._i18n import _ |
| 18 |
from neutronclient.common import exceptions |
| 19 |
from neutronclient.common import utils |
| 20 |
from neutronclient.common import validators |
| 21 |
from neutronclient.neutron import v2_0 as neutronv20 |
| 22 |
|
| 23 |
|
| 24 |
def get_bgp_peer_id(client, id_or_name): |
| 25 |
return neutronv20.find_resourceid_by_name_or_id(client, |
| 26 |
'bgp_peer', |
| 27 |
id_or_name) |
| 28 |
|
| 29 |
|
| 30 |
def validate_peer_attributes(parsed_args): |
| 31 |
# Validate AS number |
| 32 |
validators.validate_int_range(parsed_args, 'remote_as', |
| 33 |
neutronv20.bgp.speaker.MIN_AS_NUM, |
| 34 |
neutronv20.bgp.speaker.MAX_AS_NUM) |
| 35 |
# Validate password |
| 36 |
if parsed_args.auth_type != 'none' and parsed_args.password is None: |
| 37 |
raise exceptions.CommandError(_('Must provide password if auth-type ' |
| 38 |
'is specified.')) |
| 39 |
if parsed_args.auth_type == 'none' and parsed_args.password: |
| 40 |
raise exceptions.CommandError(_('Must provide auth-type if password ' |
| 41 |
'is specified.')) |
| 42 |
|
| 43 |
|
| 44 |
class ListPeers(neutronv20.ListCommand): |
| 45 |
"""List BGP peers.""" |
| 46 |
|
| 47 |
resource = 'bgp_peer' |
| 48 |
list_columns = ['id', 'name', 'peer_ip', 'remote_as'] |
| 49 |
pagination_support = True |
| 50 |
sorting_support = True |
| 51 |
|
| 52 |
|
| 53 |
class ShowPeer(neutronv20.ShowCommand): |
| 54 |
"""Show information of a given BGP peer.""" |
| 55 |
|
| 56 |
resource = 'bgp_peer' |
| 57 |
|
| 58 |
|
| 59 |
class CreatePeer(neutronv20.CreateCommand): |
| 60 |
"""Create a BGP Peer.""" |
| 61 |
|
| 62 |
resource = 'bgp_peer' |
| 63 |
|
| 64 |
def add_known_arguments(self, parser): |
| 65 |
parser.add_argument( |
| 66 |
'name', |
| 67 |
metavar='NAME', |
| 68 |
help=_('Name of the BGP peer to create.')) |
| 69 |
parser.add_argument( |
| 70 |
'--peer-ip', |
| 71 |
metavar='PEER_IP_ADDRESS', |
| 72 |
required=True, |
| 73 |
help=_('Peer IP address.')) |
| 74 |
parser.add_argument( |
| 75 |
'--remote-as', |
| 76 |
required=True, |
| 77 |
metavar='PEER_REMOTE_AS', |
| 78 |
help=_('Peer AS number. (Integer in [%(min_val)s, %(max_val)s] ' |
| 79 |
'is allowed.)') % |
| 80 |
{'min_val': neutronv20.bgp.speaker.MIN_AS_NUM, |
| 81 |
'max_val': neutronv20.bgp.speaker.MAX_AS_NUM}) |
| 82 |
parser.add_argument( |
| 83 |
'--auth-type', |
| 84 |
metavar='PEER_AUTH_TYPE', |
| 85 |
choices=['none', 'md5'], |
| 86 |
default='none', |
| 87 |
type=utils.convert_to_lowercase, |
| 88 |
help=_('Authentication algorithm. Supported algorithms: ' |
| 89 |
'none(default), md5')) |
| 90 |
parser.add_argument( |
| 91 |
'--password', |
| 92 |
metavar='AUTH_PASSWORD', |
| 93 |
help=_('Authentication password.')) |
| 94 |
|
| 95 |
def args2body(self, parsed_args): |
| 96 |
body = {} |
| 97 |
validate_peer_attributes(parsed_args) |
| 98 |
neutronv20.update_dict(parsed_args, body, |
| 99 |
['name', 'peer_ip', |
| 100 |
'remote_as', 'auth_type', 'password']) |
| 101 |
return {self.resource: body} |
| 102 |
|
| 103 |
|
| 104 |
class UpdatePeer(neutronv20.UpdateCommand): |
| 105 |
"""Update BGP Peer's information.""" |
| 106 |
|
| 107 |
resource = 'bgp_peer' |
| 108 |
|
| 109 |
def add_known_arguments(self, parser): |
| 110 |
parser.add_argument( |
| 111 |
'--name', |
| 112 |
help=_('Updated name of the BGP peer.')) |
| 113 |
parser.add_argument( |
| 114 |
'--password', |
| 115 |
metavar='AUTH_PASSWORD', |
| 116 |
help=_('Updated authentication password.')) |
| 117 |
|
| 118 |
def args2body(self, parsed_args): |
| 119 |
body = {} |
| 120 |
neutronv20.update_dict(parsed_args, body, ['name', 'password']) |
| 121 |
return {self.resource: body} |
| 122 |
|
| 123 |
|
| 124 |
class DeletePeer(neutronv20.DeleteCommand): |
| 125 |
"""Delete a BGP peer.""" |
| 126 |
|
| 127 |
resource = 'bgp_peer' |