Debian Codegraph @

neutronclient/osc/v2/dynamic_routing/bgp_peer.py

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