|
| 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.cli import identity as identity_utils |
| 15 | +from osc_lib import exceptions |
| 16 | +from osc_lib import utils |
| 17 | + |
| 18 | +from openstackclient import command |
| 19 | +from openstackclient.i18n import _ |
| 20 | +from openstackclient.identity import common as identity_common |
| 21 | +from openstackclient.network import utils as network_utils |
| 22 | + |
| 23 | + |
| 24 | +MIN_AS_NUM = 1 |
| 25 | +MAX_AS_NUM = 4294967295 |
| 26 | + |
| 27 | + |
| 28 | +def _get_attrs(client_manager, parsed_args): |
| 29 | + attrs = {} |
| 30 | + |
| 31 | + # Validate password |
| 32 | + if 'auth_type' in parsed_args: |
| 33 | + if parsed_args.auth_type != 'none': |
| 34 | + if 'password' not in parsed_args or parsed_args.password is None: |
| 35 | + raise exceptions.CommandError( |
| 36 | + _('Must provide password if auth-type is specified.') |
| 37 | + ) |
| 38 | + if ( |
| 39 | + parsed_args.auth_type == 'none' |
| 40 | + and parsed_args.password is not None |
| 41 | + ): |
| 42 | + raise exceptions.CommandError( |
| 43 | + _('Must provide auth-type if password is specified.') |
| 44 | + ) |
| 45 | + attrs['auth_type'] = parsed_args.auth_type |
| 46 | + |
| 47 | + if parsed_args.name is not None: |
| 48 | + attrs['name'] = parsed_args.name |
| 49 | + if 'remote_as' in parsed_args: |
| 50 | + attrs['remote_as'] = parsed_args.remote_as |
| 51 | + if 'peer_ip' in parsed_args: |
| 52 | + attrs['peer_ip'] = parsed_args.peer_ip |
| 53 | + if 'password' in parsed_args: |
| 54 | + attrs['password'] = parsed_args.password |
| 55 | + |
| 56 | + if 'project' in parsed_args and parsed_args.project is not None: |
| 57 | + identity_client = client_manager.identity |
| 58 | + project_id = identity_common.find_project( |
| 59 | + identity_client, |
| 60 | + parsed_args.project, |
| 61 | + parsed_args.project_domain, |
| 62 | + ).id |
| 63 | + attrs['tenant_id'] = project_id |
| 64 | + return attrs |
| 65 | + |
| 66 | + |
| 67 | +class CreateBgpPeer(command.ShowOne): |
| 68 | + _description = _("Create a BGP peer") |
| 69 | + |
| 70 | + def get_parser(self, prog_name): |
| 71 | + parser = super().get_parser(prog_name) |
| 72 | + parser.add_argument( |
| 73 | + 'name', metavar='<name>', help=_("Name of the BGP peer to create") |
| 74 | + ) |
| 75 | + parser.add_argument( |
| 76 | + '--peer-ip', |
| 77 | + metavar='<peer-ip-address>', |
| 78 | + required=True, |
| 79 | + help=_("Peer IP address"), |
| 80 | + ) |
| 81 | + parser.add_argument( |
| 82 | + '--remote-as', |
| 83 | + required=True, |
| 84 | + metavar='<peer-remote-as>', |
| 85 | + help=_( |
| 86 | + "Peer AS number. (Integer in [%(min_val)s, %(max_val)s] " |
| 87 | + "is allowed)" |
| 88 | + ) |
| 89 | + % { |
| 90 | + 'min_val': MIN_AS_NUM, |
| 91 | + 'max_val': MAX_AS_NUM, |
| 92 | + }, |
| 93 | + ) |
| 94 | + parser.add_argument( |
| 95 | + '--auth-type', |
| 96 | + metavar='<peer-auth-type>', |
| 97 | + choices=['none', 'md5'], |
| 98 | + type=network_utils.convert_to_lowercase, |
| 99 | + default='none', |
| 100 | + help=_( |
| 101 | + "Authentication algorithm. Supported algorithms: " |
| 102 | + "none (default), md5" |
| 103 | + ), |
| 104 | + ) |
| 105 | + parser.add_argument( |
| 106 | + '--password', |
| 107 | + metavar='<auth-password>', |
| 108 | + help=_("Authentication password"), |
| 109 | + ) |
| 110 | + identity_utils.add_project_owner_option_to_parser(parser) |
| 111 | + return parser |
| 112 | + |
| 113 | + def take_action(self, parsed_args): |
| 114 | + client = self.app.client_manager.network |
| 115 | + attrs = _get_attrs(self.app.client_manager, parsed_args) |
| 116 | + obj = client.create_bgp_peer(**attrs) |
| 117 | + display_columns, columns = utils.get_osc_show_columns_for_sdk_resource( |
| 118 | + obj, {}, ['location', 'tenant_id'] |
| 119 | + ) |
| 120 | + data = utils.get_dict_properties(obj, columns) |
| 121 | + return display_columns, data |
| 122 | + |
| 123 | + |
| 124 | +class DeleteBgpPeer(command.Command): |
| 125 | + _description = _("Delete a BGP peer") |
| 126 | + |
| 127 | + def get_parser(self, prog_name): |
| 128 | + parser = super().get_parser(prog_name) |
| 129 | + parser.add_argument( |
| 130 | + 'bgp_peer', |
| 131 | + metavar="<bgp-peer>", |
| 132 | + help=_("BGP peer to delete (name or ID)"), |
| 133 | + ) |
| 134 | + return parser |
| 135 | + |
| 136 | + def take_action(self, parsed_args): |
| 137 | + client = self.app.client_manager.network |
| 138 | + id = client.find_bgp_peer( |
| 139 | + parsed_args.bgp_peer, ignore_missing=False |
| 140 | + ).id |
| 141 | + client.delete_bgp_peer(id) |
| 142 | + |
| 143 | + |
| 144 | +class ListBgpPeer(command.Lister): |
| 145 | + _description = _("List BGP peers") |
| 146 | + |
| 147 | + def take_action(self, parsed_args): |
| 148 | + data = self.app.client_manager.network.bgp_peers(retrieve_all=True) |
| 149 | + headers = ('ID', 'Name', 'Peer IP', 'Remote AS') |
| 150 | + columns = ('id', 'name', 'peer_ip', 'remote_as') |
| 151 | + return ( |
| 152 | + headers, |
| 153 | + ( |
| 154 | + utils.get_dict_properties( |
| 155 | + s, |
| 156 | + columns, |
| 157 | + ) |
| 158 | + for s in data |
| 159 | + ), |
| 160 | + ) |
| 161 | + |
| 162 | + |
| 163 | +class SetBgpPeer(command.Command): |
| 164 | + _description = _("Update a BGP peer") |
| 165 | + |
| 166 | + def get_parser(self, prog_name): |
| 167 | + parser = super().get_parser(prog_name) |
| 168 | + parser.add_argument('--name', help=_("Updated name of the BGP peer")) |
| 169 | + parser.add_argument( |
| 170 | + '--password', |
| 171 | + metavar='<auth-password>', |
| 172 | + help=_("Updated authentication password"), |
| 173 | + ) |
| 174 | + parser.add_argument( |
| 175 | + 'bgp_peer', |
| 176 | + metavar="<bgp-peer>", |
| 177 | + help=_("BGP peer to update (name or ID)"), |
| 178 | + ) |
| 179 | + return parser |
| 180 | + |
| 181 | + def take_action(self, parsed_args): |
| 182 | + client = self.app.client_manager.network |
| 183 | + id = client.find_bgp_peer( |
| 184 | + parsed_args.bgp_peer, ignore_missing=False |
| 185 | + ).id |
| 186 | + attrs = _get_attrs(self.app.client_manager, parsed_args) |
| 187 | + client.update_bgp_peer(id, **attrs) |
| 188 | + |
| 189 | + |
| 190 | +class ShowBgpPeer(command.ShowOne): |
| 191 | + _description = _("Show information for a BGP peer") |
| 192 | + |
| 193 | + def get_parser(self, prog_name): |
| 194 | + parser = super().get_parser(prog_name) |
| 195 | + parser.add_argument( |
| 196 | + 'bgp_peer', |
| 197 | + metavar="<bgp-peer>", |
| 198 | + help=_("BGP peer to display (name or ID)"), |
| 199 | + ) |
| 200 | + return parser |
| 201 | + |
| 202 | + def take_action(self, parsed_args): |
| 203 | + client = self.app.client_manager.network |
| 204 | + obj = client.find_bgp_peer(parsed_args.bgp_peer, ignore_missing=False) |
| 205 | + display_columns, columns = utils.get_osc_show_columns_for_sdk_resource( |
| 206 | + obj, {}, ['location', 'tenant_id'] |
| 207 | + ) |
| 208 | + data = utils.get_dict_properties(obj, columns) |
| 209 | + return display_columns, data |
0 commit comments