Debian Codegraph @

neutronclient/osc/v2/vpnaas/ikepolicy.py

raw
1 # Copyright 2017 FUJITSU LIMITED
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 osc_lib.command import command
18 from osc_lib import exceptions
19 from osc_lib import utils
20 from osc_lib.utils import columns as column_util
21 from oslo_log import log as logging
22
23 from neutronclient._i18n import _
24 from neutronclient.common import utils as nc_utils
25 from neutronclient.osc import utils as osc_utils
26 from neutronclient.osc.v2.vpnaas import utils as vpn_utils
27
28
29 LOG = logging.getLogger(__name__)
30
31 _attr_map = (
32 ('id', 'ID', column_util.LIST_BOTH),
33 ('name', 'Name', column_util.LIST_BOTH),
34 ('auth_algorithm', 'Authentication Algorithm', column_util.LIST_BOTH),
35 ('encryption_algorithm', 'Encryption Algorithm', column_util.LIST_BOTH),
36 ('ike_version', 'IKE Version', column_util.LIST_BOTH),
37 ('pfs', 'Perfect Forward Secrecy (PFS)', column_util.LIST_BOTH),
38 ('description', 'Description', column_util.LIST_LONG_ONLY),
39 ('phase1_negotiation_mode', 'Phase1 Negotiation Mode',
40 column_util.LIST_LONG_ONLY),
41 ('project_id', 'Project', column_util.LIST_LONG_ONLY),
42 ('lifetime', 'Lifetime', column_util.LIST_LONG_ONLY),
43 )
44
45 _attr_map_dict = {
46 'id': 'ID',
47 'name': 'Name',
48 'auth_algorithm': 'Authentication Algorithm',
49 'encryption_algorithm': 'Encryption Algorithm',
50 'ike_version': 'IKE Version',
51 'pfs': 'Perfect Forward Secrecy (PFS)',
52 'phase1_negotiation_mode': 'Phase1 Negotiation Mode',
53 'lifetime': 'Lifetime',
54 'description': 'Description',
55 'tenant_id': 'Project',
56 'project_id': 'Project',
57 }
58
59 _auth_algorithms = [
60 'sha1',
61 'sha256',
62 'sha384',
63 'sha512',
64 'aes-xcbc',
65 'aes-cmac',
66 ]
67
68 _encryption_algorithms = [
69 '3des',
70 'aes-128',
71 'aes-192',
72 'aes-256',
73 'aes-128-ccm-8',
74 'aes-192-ccm-8',
75 'aes-256-ccm-8',
76 'aes-128-ccm-12',
77 'aes-192-ccm-12',
78 'aes-256-ccm-12',
79 'aes-128-ccm-16',
80 'aes-192-ccm-16',
81 'aes-256-ccm-16',
82 'aes-128-gcm-8',
83 'aes-192-gcm-8',
84 'aes-256-gcm-8',
85 'aes-128-gcm-12',
86 'aes-192-gcm-12',
87 'aes-256-gcm-12',
88 'aes-128-gcm-16',
89 'aes-192-gcm-16',
90 'aes-256-gcm-16',
91 'aes-128-ctr',
92 'aes-192-ctr',
93 'aes-256-ctr',
94 ]
95
96 _pfs_groups = [
97 'group2',
98 'group5',
99 'group14',
100 'group15',
101 'group16',
102 'group17',
103 'group18',
104 'group19',
105 'group20',
106 'group21',
107 'group22',
108 'group23',
109 'group24',
110 'group25',
111 'group26',
112 'group27',
113 'group28',
114 'group29',
115 'group30',
116 'group31',
117 ]
118
119
120 def _convert_to_lowercase(string):
121 return string.lower()
122
123
124 def _get_common_parser(parser):
125 parser.add_argument(
126 '--description',
127 metavar='<description>',
128 help=_('Description of the IKE policy'))
129 parser.add_argument(
130 '--auth-algorithm',
131 choices=_auth_algorithms,
132 type=_convert_to_lowercase,
133 help=_('Authentication algorithm'))
134 parser.add_argument(
135 '--encryption-algorithm',
136 choices=_encryption_algorithms,
137 type=_convert_to_lowercase,
138 help=_('Encryption algorithm'))
139 parser.add_argument(
140 '--phase1-negotiation-mode',
141 choices=['main', 'aggressive'],
142 type=_convert_to_lowercase,
143 help=_('IKE Phase1 negotiation mode'))
144 parser.add_argument(
145 '--ike-version',
146 choices=['v1', 'v2'],
147 type=_convert_to_lowercase,
148 help=_('IKE version for the policy'))
149 parser.add_argument(
150 '--pfs',
151 choices=_pfs_groups,
152 type=_convert_to_lowercase,
153 help=_('Perfect Forward Secrecy'))
154 parser.add_argument(
155 '--lifetime',
156 metavar="units=UNITS,value=VALUE",
157 type=nc_utils.str2dict_type(optional_keys=['units', 'value']),
158 help=vpn_utils.lifetime_help("IKE"))
159 return parser
160
161
162 def _get_common_attrs(client_manager, parsed_args, is_create=True):
163 attrs = {}
164 if is_create:
165 if 'project' in parsed_args and parsed_args.project is not None:
166 attrs['project_id'] = osc_utils.find_project(
167 client_manager.identity,
168 parsed_args.project,
169 parsed_args.project_domain,
170 ).id
171 if parsed_args.description:
172 attrs['description'] = parsed_args.description
173 if parsed_args.auth_algorithm:
174 attrs['auth_algorithm'] = parsed_args.auth_algorithm
175 if parsed_args.encryption_algorithm:
176 attrs['encryption_algorithm'] = parsed_args.encryption_algorithm
177 if parsed_args.phase1_negotiation_mode:
178 attrs['phase1_negotiation_mode'] = parsed_args.phase1_negotiation_mode
179 if parsed_args.ike_version:
180 attrs['ike_version'] = parsed_args.ike_version
181 if parsed_args.pfs:
182 attrs['pfs'] = parsed_args.pfs
183 if parsed_args.lifetime:
184 vpn_utils.validate_lifetime_dict(parsed_args.lifetime)
185 attrs['lifetime'] = parsed_args.lifetime
186 return attrs
187
188
189 class CreateIKEPolicy(command.ShowOne):
190 _description = _("Create an IKE policy")
191
192 def get_parser(self, prog_name):
193 parser = super(CreateIKEPolicy, self).get_parser(prog_name)
194 _get_common_parser(parser)
195 parser.add_argument(
196 'name',
197 metavar='<name>',
198 help=_('Name of the IKE policy'))
199 osc_utils.add_project_owner_option_to_parser(parser)
200 return parser
201
202 def take_action(self, parsed_args):
203 client = self.app.client_manager.network
204 attrs = _get_common_attrs(self.app.client_manager, parsed_args)
205 if parsed_args.name:
206 attrs['name'] = str(parsed_args.name)
207 obj = client.create_vpn_ike_policy(**attrs)
208 display_columns, columns = utils.get_osc_show_columns_for_sdk_resource(
209 obj, _attr_map_dict, ['location', 'tenant_id', 'units', 'value'])
210 data = utils.get_dict_properties(obj, columns)
211 return display_columns, data
212
213
214 class DeleteIKEPolicy(command.Command):
215 _description = _("Delete IKE policy (policies)")
216
217 def get_parser(self, prog_name):
218 parser = super(DeleteIKEPolicy, self).get_parser(prog_name)
219 parser.add_argument(
220 'ikepolicy',
221 metavar='<ike-policy>',
222 nargs='+',
223 help=_('IKE policy to delete (name or ID)'))
224 return parser
225
226 def take_action(self, parsed_args):
227 client = self.app.client_manager.network
228 result = 0
229 for ike in parsed_args.ikepolicy:
230 try:
231 ike_id = client.find_vpn_ike_policy(ike,
232 ignore_missing=False)['id']
233 client.delete_vpn_ike_policy(ike_id)
234 except Exception as e:
235 result += 1
236 LOG.error(_("Failed to delete IKE policy with "
237 "name or ID '%(ikepolicy)s': %(e)s"),
238 {'ikepolicy': ike, 'e': e})
239
240 if result > 0:
241 total = len(parsed_args.ikepolicy)
242 msg = (_("%(result)s of %(total)s IKE policy failed "
243 "to delete.") % {'result': result, 'total': total})
244 raise exceptions.CommandError(msg)
245
246
247 class ListIKEPolicy(command.Lister):
248 _description = _("List IKE policies that belong to a given project")
249
250 def get_parser(self, prog_name):
251 parser = super(ListIKEPolicy, self).get_parser(prog_name)
252 parser.add_argument(
253 '--long',
254 action='store_true',
255 help=_("List additional fields in output")
256 )
257 return parser
258
259 def take_action(self, parsed_args):
260 client = self.app.client_manager.network
261 obj = client.vpn_ike_policies()
262 headers, columns = column_util.get_column_definitions(
263 _attr_map, long_listing=parsed_args.long)
264 return (headers, (utils.get_dict_properties(s, columns) for s in obj))
265
266
267 class SetIKEPolicy(command.Command):
268 _description = _("Set IKE policy properties")
269
270 def get_parser(self, prog_name):
271 parser = super(SetIKEPolicy, self).get_parser(prog_name)
272 _get_common_parser(parser)
273 parser.add_argument(
274 '--name',
275 metavar='<name>',
276 help=_('Name of the IKE policy'))
277 parser.add_argument(
278 'ikepolicy',
279 metavar='<ike-policy>',
280 help=_('IKE policy to set (name or ID)'))
281 return parser
282
283 def take_action(self, parsed_args):
284 client = self.app.client_manager.network
285 attrs = _get_common_attrs(self.app.client_manager,
286 parsed_args, is_create=False)
287 if parsed_args.name:
288 attrs['name'] = parsed_args.name
289 ike_id = client.find_vpn_ike_policy(parsed_args.ikepolicy,
290 ignore_missing=False)['id']
291 try:
292 client.update_vpn_ike_policy(ike_id, **attrs)
293 except Exception as e:
294 msg = (_("Failed to set IKE policy '%(ike)s': %(e)s")
295 % {'ike': parsed_args.ikepolicy, 'e': e})
296 raise exceptions.CommandError(msg)
297
298
299 class ShowIKEPolicy(command.ShowOne):
300 _description = _("Display IKE policy details")
301
302 def get_parser(self, prog_name):
303 parser = super(ShowIKEPolicy, self).get_parser(prog_name)
304 parser.add_argument(
305 'ikepolicy',
306 metavar='<ike-policy>',
307 help=_('IKE policy to display (name or ID)'))
308 return parser
309
310 def take_action(self, parsed_args):
311 client = self.app.client_manager.network
312 ike_id = client.find_vpn_ike_policy(parsed_args.ikepolicy,
313 ignore_missing=False)['id']
314 obj = client.get_vpn_ike_policy(ike_id)
315 display_columns, columns = utils.get_osc_show_columns_for_sdk_resource(
316 obj, _attr_map_dict, ['location', 'tenant_id', 'units', 'value'])
317 data = utils.get_dict_properties(obj, columns)
318 return (display_columns, data)