Debian Codegraph @

neutronclient/osc/v2/vpnaas/vpnservice.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.osc import utils as osc_utils
25
26
27 LOG = logging.getLogger(__name__)
28
29 _attr_map = (
30 ('id', 'ID', column_util.LIST_BOTH),
31 ('name', 'Name', column_util.LIST_BOTH),
32 ('router_id', 'Router', column_util.LIST_BOTH),
33 ('subnet_id', 'Subnet', column_util.LIST_BOTH),
34 ('flavor_id', 'Flavor', column_util.LIST_BOTH),
35 ('is_admin_state_up', 'State', column_util.LIST_BOTH),
36 ('status', 'Status', column_util.LIST_BOTH),
37 ('description', 'Description', column_util.LIST_LONG_ONLY),
38 ('project_id', 'Project', column_util.LIST_LONG_ONLY),
39 ('external_v4_ip', 'Ext v4 IP', column_util.LIST_LONG_ONLY),
40 ('external_v6_ip', 'Ext v6 IP', column_util.LIST_LONG_ONLY),
41 )
42
43 _attr_map_dict = {
44 'id': 'ID',
45 'name': 'Name',
46 'router_id': 'Router',
47 'subnet_id': 'Subnet',
48 'flavor_id': 'Flavor',
49 'is_admin_state_up': 'State',
50 'status': 'Status',
51 'description': 'Description',
52 'project_id': 'Project',
53 'external_v4_ip': 'Ext v4 IP',
54 'external_v6_ip': 'Ext v6 IP',
55 }
56
57
58 def _get_common_parser(parser):
59 parser.add_argument(
60 '--description',
61 metavar='<description>',
62 help=_('Description for the VPN service'))
63 parser.add_argument(
64 '--subnet',
65 metavar='<subnet>',
66 help=_('Local private subnet (name or ID)'))
67 parser.add_argument(
68 '--flavor',
69 metavar='<flavor>',
70 help=_('Flavor for the VPN service (name or ID)'))
71 admin_group = parser.add_mutually_exclusive_group()
72 admin_group.add_argument(
73 '--enable',
74 action='store_true',
75 help=_("Enable VPN service")
76 )
77 admin_group.add_argument(
78 '--disable',
79 action='store_true',
80 help=_("Disable VPN service")
81 )
82 return parser
83
84
85 def _get_common_attrs(client_manager, parsed_args, is_create=True):
86 attrs = {}
87 if is_create:
88 if 'project' in parsed_args and parsed_args.project is not None:
89 attrs['project_id'] = osc_utils.find_project(
90 client_manager.identity,
91 parsed_args.project,
92 parsed_args.project_domain,
93 ).id
94 if parsed_args.description:
95 attrs['description'] = str(parsed_args.description)
96 if parsed_args.subnet:
97 _subnet_id = client_manager.network.find_subnet(
98 parsed_args.subnet).id
99 attrs['subnet_id'] = _subnet_id
100 if parsed_args.flavor:
101 _flavor_id = client_manager.network.find_flavor(
102 parsed_args.flavor,
103 ignore_missing=False
104 ).id
105 attrs['flavor_id'] = _flavor_id
106 if parsed_args.enable:
107 attrs['admin_state_up'] = True
108 if parsed_args.disable:
109 attrs['admin_state_up'] = False
110 return attrs
111
112
113 class CreateVPNService(command.ShowOne):
114 _description = _("Create an VPN service")
115
116 def get_parser(self, prog_name):
117 parser = super(CreateVPNService, self).get_parser(prog_name)
118 _get_common_parser(parser)
119 parser.add_argument(
120 'name',
121 metavar='<name>',
122 help=_('Name for the VPN service'))
123 parser.add_argument(
124 '--router',
125 metavar='ROUTER',
126 required=True,
127 help=_('Router for the VPN service (name or ID)'))
128 osc_utils.add_project_owner_option_to_parser(parser)
129 return parser
130
131 def take_action(self, parsed_args):
132 client = self.app.client_manager.network
133 attrs = _get_common_attrs(self.app.client_manager, parsed_args)
134 if parsed_args.name:
135 attrs['name'] = str(parsed_args.name)
136 if parsed_args.router:
137 _router_id = client.find_router(parsed_args.router,
138 ignore_missing=False).id
139 attrs['router_id'] = _router_id
140 obj = client.create_vpn_service(**attrs)
141 display_columns, columns = utils.get_osc_show_columns_for_sdk_resource(
142 obj, _attr_map_dict, ['location', 'tenant_id'])
143 data = utils.get_dict_properties(obj, columns)
144 return display_columns, data
145
146
147 class DeleteVPNService(command.Command):
148 _description = _("Delete VPN service(s)")
149
150 def get_parser(self, prog_name):
151 parser = super(DeleteVPNService, self).get_parser(prog_name)
152 parser.add_argument(
153 'vpnservice',
154 metavar='<vpn-service>',
155 nargs='+',
156 help=_('VPN service to delete (name or ID)'))
157 return parser
158
159 def take_action(self, parsed_args):
160 client = self.app.client_manager.network
161 result = 0
162 for vpn in parsed_args.vpnservice:
163 try:
164 vpn_id = client.find_vpn_service(vpn,
165 ignore_missing=False)['id']
166 client.delete_vpn_service(vpn_id)
167 except Exception as e:
168 result += 1
169 LOG.error(_("Failed to delete VPN service with "
170 "name or ID '%(vpnservice)s': %(e)s"),
171 {'vpnservice': vpn, 'e': e})
172
173 if result > 0:
174 total = len(parsed_args.vpnservice)
175 msg = (_("%(result)s of %(total)s vpn service failed "
176 "to delete.") % {'result': result, 'total': total})
177 raise exceptions.CommandError(msg)
178
179
180 class ListVPNService(command.Lister):
181 _description = _("List VPN services that belong to a given project")
182
183 def get_parser(self, prog_name):
184 parser = super(ListVPNService, self).get_parser(prog_name)
185 parser.add_argument(
186 '--long',
187 action='store_true',
188 default=False,
189 help=_("List additional fields in output")
190 )
191 return parser
192
193 def take_action(self, parsed_args):
194 client = self.app.client_manager.network
195 obj = client.vpn_services()
196 headers, columns = column_util.get_column_definitions(
197 _attr_map, long_listing=parsed_args.long)
198 return (headers, (utils.get_dict_properties(s, columns) for s in obj))
199
200
201 class SetVPNSercice(command.Command):
202 _description = _("Set VPN service properties")
203
204 def get_parser(self, prog_name):
205 parser = super(SetVPNSercice, self).get_parser(prog_name)
206 _get_common_parser(parser)
207 parser.add_argument(
208 '--name',
209 metavar='<name>',
210 help=_('Name for the VPN service'))
211 parser.add_argument(
212 'vpnservice',
213 metavar='<vpn-service>',
214 help=_('VPN service to modify (name or ID)'))
215 return parser
216
217 def take_action(self, parsed_args):
218 client = self.app.client_manager.network
219 attrs = _get_common_attrs(self.app.client_manager,
220 parsed_args, is_create=False)
221 if parsed_args.name:
222 attrs['name'] = str(parsed_args.name)
223 vpn_id = client.find_vpn_service(parsed_args.vpnservice,
224 ignore_missing=False)['id']
225 try:
226 client.update_vpn_service(vpn_id, **attrs)
227 except Exception as e:
228 msg = (_("Failed to set vpn service '%(vpn)s': %(e)s")
229 % {'vpn': parsed_args.vpnservice, 'e': e})
230 raise exceptions.CommandError(msg)
231
232
233 class ShowVPNService(command.ShowOne):
234 _description = _("Display VPN service details")
235
236 def get_parser(self, prog_name):
237 parser = super(ShowVPNService, self).get_parser(prog_name)
238 parser.add_argument(
239 'vpnservice',
240 metavar='<vpn-service>',
241 help=_('VPN service to display (name or ID)'))
242 return parser
243
244 def take_action(self, parsed_args):
245 client = self.app.client_manager.network
246 vpn_id = client.find_vpn_service(parsed_args.vpnservice,
247 ignore_missing=False)['id']
248 obj = client.get_vpn_service(vpn_id)
249 display_columns, columns = utils.get_osc_show_columns_for_sdk_resource(
250 obj, _attr_map_dict, ['location', 'tenant_id'])
251 data = utils.get_dict_properties(obj, columns)
252 return (display_columns, data)