Skip to content

Commit 8d6b29e

Browse files
committed
Neutron: Move dynamic-routing osc client code from neutronclient
Change-Id: I664b291686de5efc37f0cf3ee704eb77512f88fa Signed-off-by: lajoskatona <lajos.katona@est.tech>
1 parent 32cd99b commit 8d6b29e

13 files changed

Lines changed: 1266 additions & 2 deletions

File tree

openstackclient/network/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
API_VERSION_OPTION = 'os_network_api_version'
2626
API_NAME = 'network'
2727
API_VERSIONS = ('2.0', '2')
28-
API_EXTENSIONS = ('bgpvpn', 'fwaas', 'taas')
28+
API_EXTENSIONS = ('bgpvpn', 'fwaas', 'taas', 'dynamic_routing')
2929

3030

3131
def make_client(instance: Any) -> Any:

openstackclient/network/v2/dynamic_routing/__init__.py

Whitespace-only changes.
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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 import utils
15+
16+
from openstackclient import command
17+
from openstackclient.i18n import _
18+
19+
20+
class AddBgpSpeakerToDRAgent(command.Command):
21+
"""Add a BGP speaker to a dynamic routing agent"""
22+
23+
def get_parser(self, prog_name):
24+
parser = super().get_parser(prog_name)
25+
parser.add_argument(
26+
'dragent_id',
27+
metavar='<agent-id>',
28+
help=_("ID of the dynamic routing agent"),
29+
)
30+
parser.add_argument(
31+
'bgp_speaker',
32+
metavar='<bgp-speaker>',
33+
help=_("ID or name of the BGP speaker"),
34+
)
35+
return parser
36+
37+
def take_action(self, parsed_args):
38+
client = self.app.client_manager.network
39+
speaker_id = client.find_bgp_speaker(
40+
parsed_args.bgp_speaker, ignore_missing=False
41+
).id
42+
client.add_bgp_speaker_to_dragent(parsed_args.dragent_id, speaker_id)
43+
44+
45+
class RemoveBgpSpeakerFromDRAgent(command.Command):
46+
"""Removes a BGP speaker from a dynamic routing agent"""
47+
48+
def get_parser(self, prog_name):
49+
parser = super().get_parser(prog_name)
50+
parser.add_argument(
51+
'dragent_id',
52+
metavar='<agent-id>',
53+
help=_("ID of the dynamic routing agent"),
54+
)
55+
parser.add_argument(
56+
'bgp_speaker',
57+
metavar='<bgp-speaker>',
58+
help=_("ID or name of the BGP speaker"),
59+
)
60+
return parser
61+
62+
def take_action(self, parsed_args):
63+
client = self.app.client_manager.network
64+
speaker_id = client.find_bgp_speaker(
65+
parsed_args.bgp_speaker, ignore_missing=False
66+
).id
67+
client.remove_bgp_speaker_from_dragent(
68+
parsed_args.dragent_id, speaker_id
69+
)
70+
71+
72+
class ListDRAgent(command.Lister):
73+
"""List dynamic routing agents"""
74+
75+
resource = 'agent'
76+
list_columns = ['id', 'host', 'admin_state_up', 'alive']
77+
unknown_parts_flag = False
78+
79+
def get_parser(self, prog_name):
80+
parser = super().get_parser(prog_name)
81+
parser.add_argument(
82+
'--bgp-speaker',
83+
metavar='<bgp-speaker>',
84+
help=_(
85+
"List dynamic routing agents hosting a "
86+
"BGP speaker (name or ID)"
87+
),
88+
)
89+
return parser
90+
91+
def take_action(self, parsed_args):
92+
client = self.app.client_manager.network
93+
if parsed_args.bgp_speaker is not None:
94+
speaker_id = client.find_bgp_speaker(
95+
parsed_args.bgp_speaker, ignore_missing=False
96+
).id
97+
data = client.get_bgp_dragents_hosting_speaker(speaker_id)
98+
else:
99+
data = client.agents(agent_type='BGP dynamic routing agent')
100+
columns = (
101+
'id',
102+
'agent_type',
103+
'host',
104+
'availability_zone',
105+
'is_alive',
106+
'is_admin_state_up',
107+
'binary',
108+
)
109+
column_headers = (
110+
'ID',
111+
'Agent Type',
112+
'Host',
113+
'Availability Zone',
114+
'Alive',
115+
'State',
116+
'Binary',
117+
)
118+
return (
119+
column_headers,
120+
(
121+
utils.get_item_properties(
122+
s,
123+
columns,
124+
)
125+
for s in data
126+
),
127+
)
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
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

Comments
 (0)