Debian Codegraph @

neutronclient/neutron/v2_0/vpn/ipsec_site_connection.py

raw
1 # (c) Copyright 2013 Hewlett-Packard Development Company, L.P.
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 import argparse
18
19 from oslo_serialization import jsonutils
20
21 from neutronclient._i18n import _
22 from neutronclient.common import exceptions
23 from neutronclient.common import utils
24 from neutronclient.neutron import v2_0 as neutronv20
25 from neutronclient.neutron.v2_0.vpn import utils as vpn_utils
26
27
28 def _format_peer_cidrs(ipsec_site_connection):
29 try:
30 return '\n'.join([jsonutils.dumps(cidrs) for cidrs in
31 ipsec_site_connection['peer_cidrs']])
32 except (TypeError, KeyError):
33 return ''
34
35
36 class ListIPsecSiteConnection(neutronv20.ListCommand):
37 """List IPsec site connections that belong to a given tenant."""
38
39 resource = 'ipsec_site_connection'
40 _formatters = {'peer_cidrs': _format_peer_cidrs}
41 list_columns = [
42 'id', 'name', 'peer_address', 'auth_mode', 'status']
43 pagination_support = True
44 sorting_support = True
45
46
47 class ShowIPsecSiteConnection(neutronv20.ShowCommand):
48 """Show information of a given IPsec site connection."""
49
50 resource = 'ipsec_site_connection'
51 help_resource = 'IPsec site connection'
52
53
54 class IPsecSiteConnectionMixin(object):
55
56 def add_known_arguments(self, parser, is_create=True):
57 parser.add_argument(
58 '--name',
59 help=_('Set friendly name for the connection.'))
60 parser.add_argument(
61 '--description',
62 help=_('Set a description for the connection.'))
63 parser.add_argument(
64 '--dpd',
65 metavar="action=ACTION,interval=INTERVAL,timeout=TIMEOUT",
66 type=utils.str2dict_type(
67 optional_keys=['action', 'interval', 'timeout']),
68 help=vpn_utils.dpd_help("IPsec connection."))
69 parser.add_argument(
70 '--local-ep-group',
71 help=_('Local endpoint group ID/name with subnet(s) for '
72 'IPSec connection.'))
73 parser.add_argument(
74 '--peer-ep-group',
75 help=_('Peer endpoint group ID/name with CIDR(s) for '
76 'IPSec connection.'))
77 parser.add_argument(
78 '--peer-cidr',
79 action='append', dest='peer_cidrs',
80 help=_('[DEPRECATED in Mitaka] Remote subnet(s) in CIDR format. '
81 'Cannot be specified when using endpoint groups. Only '
82 'applicable, if subnet provided for VPN service.'))
83 parser.add_argument(
84 '--peer-id',
85 required=is_create,
86 help=_('Peer router identity for authentication. Can be '
87 'IPv4/IPv6 address, e-mail address, key id, or FQDN.'))
88 parser.add_argument(
89 '--peer-address',
90 required=is_create,
91 help=_('Peer gateway public IPv4/IPv6 address or FQDN.'))
92 parser.add_argument(
93 '--psk',
94 required=is_create,
95 help=_('Pre-shared key string.'))
96 parser.add_argument(
97 '--mtu',
98 default='1500' if is_create else argparse.SUPPRESS,
99 help=_('MTU size for the connection, default:1500.'))
100 parser.add_argument(
101 '--initiator',
102 default='bi-directional' if is_create else argparse.SUPPRESS,
103 choices=['bi-directional', 'response-only'],
104 help=_('Initiator state in lowercase, default:bi-directional'))
105
106 def args2body(self, parsed_args, body=None):
107 """Add in conditional args and then return all conn info."""
108
109 if body is None:
110 body = {}
111 if parsed_args.dpd:
112 vpn_utils.validate_dpd_dict(parsed_args.dpd)
113 body['dpd'] = parsed_args.dpd
114 if parsed_args.local_ep_group:
115 _local_epg = neutronv20.find_resourceid_by_name_or_id(
116 self.get_client(), 'endpoint_group',
117 parsed_args.local_ep_group)
118 body['local_ep_group_id'] = _local_epg
119 if parsed_args.peer_ep_group:
120 _peer_epg = neutronv20.find_resourceid_by_name_or_id(
121 self.get_client(), 'endpoint_group',
122 parsed_args.peer_ep_group)
123 body['peer_ep_group_id'] = _peer_epg
124 if hasattr(parsed_args, 'mtu') and int(parsed_args.mtu) < 68:
125 message = _("Invalid MTU value: MTU must be "
126 "greater than or equal to 68.")
127 raise exceptions.CommandError(message)
128 # ToDo (Reedip) : Remove below check when peer-cidr is removed
129 if parsed_args.peer_cidrs and parsed_args.local_ep_group:
130 message = _("You cannot specify both endpoint groups and peer "
131 "CIDR(s).")
132 raise exceptions.CommandError(message)
133 neutronv20.update_dict(parsed_args, body,
134 ['peer_id', 'mtu', 'initiator', 'psk',
135 'peer_address', 'name', 'description',
136 'peer_cidrs'])
137 return {self.resource: body}
138
139
140 class CreateIPsecSiteConnection(IPsecSiteConnectionMixin,
141 neutronv20.CreateCommand):
142 """Create an IPsec site connection."""
143 resource = 'ipsec_site_connection'
144
145 def add_known_arguments(self, parser):
146 parser.add_argument(
147 '--admin-state-down',
148 default=True, action='store_false',
149 help=_('Set admin state up to false.'))
150 parser.add_argument(
151 '--vpnservice-id', metavar='VPNSERVICE',
152 required=True,
153 help=_('VPN service instance ID associated with this connection.'))
154 parser.add_argument(
155 '--ikepolicy-id', metavar='IKEPOLICY',
156 required=True,
157 help=_('IKE policy ID associated with this connection.'))
158 parser.add_argument(
159 '--ipsecpolicy-id', metavar='IPSECPOLICY',
160 required=True,
161 help=_('IPsec policy ID associated with this connection.'))
162 super(CreateIPsecSiteConnection, self).add_known_arguments(parser)
163
164 def args2body(self, parsed_args):
165 _vpnservice_id = neutronv20.find_resourceid_by_name_or_id(
166 self.get_client(), 'vpnservice',
167 parsed_args.vpnservice_id)
168 _ikepolicy_id = neutronv20.find_resourceid_by_name_or_id(
169 self.get_client(), 'ikepolicy',
170 parsed_args.ikepolicy_id)
171 _ipsecpolicy_id = neutronv20.find_resourceid_by_name_or_id(
172 self.get_client(), 'ipsecpolicy',
173 parsed_args.ipsecpolicy_id)
174 body = {
175 'vpnservice_id': _vpnservice_id,
176 'ikepolicy_id': _ikepolicy_id,
177 'ipsecpolicy_id': _ipsecpolicy_id,
178 'admin_state_up': parsed_args.admin_state_down,
179 }
180 if parsed_args.tenant_id:
181 body['tenant_id'] = parsed_args.tenant_id
182
183 if (bool(parsed_args.local_ep_group) !=
184 bool(parsed_args.peer_ep_group)):
185 message = _("You must specify both local and peer endpoint "
186 "groups.")
187 raise exceptions.CommandError(message)
188 if not parsed_args.peer_cidrs and not parsed_args.local_ep_group:
189 message = _("You must specify endpoint groups or peer CIDR(s).")
190 raise exceptions.CommandError(message)
191 return super(CreateIPsecSiteConnection, self).args2body(parsed_args,
192 body)
193
194
195 class UpdateIPsecSiteConnection(IPsecSiteConnectionMixin,
196 neutronv20.UpdateCommand):
197 """Update a given IPsec site connection."""
198
199 resource = 'ipsec_site_connection'
200 help_resource = 'IPsec site connection'
201
202 def add_known_arguments(self, parser):
203 utils.add_boolean_argument(
204 parser, '--admin-state-up',
205 help=_('Update the administrative state. (True meaning "Up")'))
206 super(UpdateIPsecSiteConnection, self).add_known_arguments(parser,
207 False)
208
209 def args2body(self, parsed_args):
210 body = {}
211 neutronv20.update_dict(parsed_args, body, ['admin_state_up'])
212 return super(UpdateIPsecSiteConnection, self).args2body(parsed_args,
213 body)
214
215
216 class DeleteIPsecSiteConnection(neutronv20.DeleteCommand):
217 """Delete a given IPsec site connection."""
218
219 resource = 'ipsec_site_connection'
220 help_resource = 'IPsec site connection'