Debian Codegraph @

neutronclient/osc/v2/vpnaas/endpoint_group.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 ('type', 'Type', column_util.LIST_BOTH),
33 ('endpoints', 'Endpoints', column_util.LIST_BOTH),
34 ('description', 'Description', column_util.LIST_LONG_ONLY),
35 ('project_id', 'Project', column_util.LIST_LONG_ONLY),
36 )
37
38 _attr_map_dict = {
39 'id': 'ID',
40 'name': 'Name',
41 'type': 'Type',
42 'endpoints': 'Endpoints',
43 'description': 'Description',
44 'tenant_id': 'Project',
45 'project_id': 'Project',
46 }
47
48
49 def _get_common_parser(parser):
50 parser.add_argument(
51 '--description',
52 metavar='<description>',
53 help=_('Description for the endpoint group'))
54 return parser
55
56
57 def _get_common_attrs(client_manager, parsed_args, is_create=True):
58 attrs = {}
59 if is_create:
60 if parsed_args.project is not None:
61 attrs['tenant_id'] = osc_utils.find_project(
62 client_manager.identity,
63 parsed_args.project,
64 parsed_args.project_domain,
65 ).id
66 if parsed_args.description:
67 attrs['description'] = parsed_args.description
68 return attrs
69
70
71 class CreateEndpointGroup(command.ShowOne):
72 _description = _("Create an endpoint group")
73
74 def get_parser(self, prog_name):
75 parser = super(CreateEndpointGroup, self).get_parser(prog_name)
76 _get_common_parser(parser)
77 parser.add_argument(
78 'name',
79 metavar='<name>',
80 help=_('Name for the endpoint group'))
81 parser.add_argument(
82 '--type',
83 required=True,
84 help=_('Type of endpoints in group (e.g. subnet, cidr)'))
85 parser.add_argument(
86 '--value',
87 action='append',
88 dest='endpoints',
89 required=True,
90 help=_('Endpoint(s) for the group. Must all be of the same type. '
91 '(--value) option can be repeated'))
92 osc_utils.add_project_owner_option_to_parser(parser)
93 return parser
94
95 def take_action(self, parsed_args):
96 client = self.app.client_manager.network
97 attrs = _get_common_attrs(self.app.client_manager, parsed_args)
98 if parsed_args.name:
99 attrs['name'] = str(parsed_args.name)
100 attrs['type'] = parsed_args.type
101 if parsed_args.type == 'subnet':
102 _subnet_ids = [client.find_subnet(
103 endpoint,
104 ignore_missing=False)['id']
105 for endpoint in parsed_args.endpoints]
106 attrs['endpoints'] = _subnet_ids
107 else:
108 attrs['endpoints'] = parsed_args.endpoints
109 obj = client.create_vpn_endpoint_group(**attrs)
110 display_columns, columns = utils.get_osc_show_columns_for_sdk_resource(
111 obj, _attr_map_dict, ['location', 'tenant_id'])
112 data = utils.get_dict_properties(obj, columns)
113 return display_columns, data
114
115
116 class DeleteEndpointGroup(command.Command):
117 _description = _("Delete endpoint group(s)")
118
119 def get_parser(self, prog_name):
120 parser = super(DeleteEndpointGroup, self).get_parser(prog_name)
121 parser.add_argument(
122 'endpoint_group',
123 metavar='<endpoint-group>',
124 nargs='+',
125 help=_('Endpoint group(s) to delete (name or ID)'))
126 return parser
127
128 def take_action(self, parsed_args):
129 client = self.app.client_manager.network
130 result = 0
131 for endpoint in parsed_args.endpoint_group:
132 try:
133 endpoint_id = client.find_vpn_endpoint_group(
134 endpoint, ignore_missing=False)['id']
135 client.delete_vpn_endpoint_group(endpoint_id)
136 except Exception as e:
137 result += 1
138 LOG.error(_("Failed to delete endpoint group with "
139 "name or ID '%(endpoint_group)s': %(e)s"),
140 {'endpoint_group': endpoint, 'e': e})
141
142 if result > 0:
143 total = len(parsed_args.endpoint_group)
144 msg = (_("%(result)s of %(total)s endpoint group failed "
145 "to delete.") % {'result': result, 'total': total})
146 raise exceptions.CommandError(msg)
147
148
149 class ListEndpointGroup(command.Lister):
150 _description = _("List endpoint groups that belong to a given project")
151
152 def get_parser(self, prog_name):
153 parser = super(ListEndpointGroup, self).get_parser(prog_name)
154 parser.add_argument(
155 '--long',
156 action='store_true',
157 default=False,
158 help=_("List additional fields in output")
159 )
160 return parser
161
162 def take_action(self, parsed_args):
163 client = self.app.client_manager.network
164 obj = client.vpn_endpoint_groups()
165 headers, columns = column_util.get_column_definitions(
166 _attr_map, long_listing=parsed_args.long)
167 return (headers, (utils.get_dict_properties(s, columns) for s in obj))
168
169
170 class SetEndpointGroup(command.Command):
171 _description = _("Set endpoint group properties")
172
173 def get_parser(self, prog_name):
174 parser = super(SetEndpointGroup, self).get_parser(prog_name)
175 _get_common_parser(parser)
176 parser.add_argument(
177 '--name',
178 metavar='<name>',
179 help=_('Set a name for the endpoint group'))
180 parser.add_argument(
181 'endpoint_group',
182 metavar='<endpoint-group>',
183 help=_('Endpoint group to set (name or ID)'))
184 return parser
185
186 def take_action(self, parsed_args):
187 client = self.app.client_manager.network
188 attrs = _get_common_attrs(self.app.client_manager,
189 parsed_args, is_create=False)
190 if parsed_args.name:
191 attrs['name'] = str(parsed_args.name)
192 endpoint_id = client.find_vpn_endpoint_group(
193 parsed_args.endpoint_group, ignore_missing=False)['id']
194 try:
195 client.update_vpn_endpoint_group(endpoint_id, **attrs)
196 except Exception as e:
197 msg = (_("Failed to set endpoint group "
198 "%(endpoint_group)s: %(e)s")
199 % {'endpoint_group': parsed_args.endpoint_group, 'e': e})
200 raise exceptions.CommandError(msg)
201
202
203 class ShowEndpointGroup(command.ShowOne):
204 _description = _("Display endpoint group details")
205
206 def get_parser(self, prog_name):
207 parser = super(ShowEndpointGroup, self).get_parser(prog_name)
208 parser.add_argument(
209 'endpoint_group',
210 metavar='<endpoint-group>',
211 help=_('Endpoint group to display (name or ID)'))
212 return parser
213
214 def take_action(self, parsed_args):
215 client = self.app.client_manager.network
216 endpoint_id = client.find_vpn_endpoint_group(
217 parsed_args.endpoint_group, ignore_missing=False)['id']
218 obj = client.get_vpn_endpoint_group(endpoint_id)
219 display_columns, columns = utils.get_osc_show_columns_for_sdk_resource(
220 obj, _attr_map_dict, ['location', 'tenant_id'])
221 data = utils.get_dict_properties(obj, columns)
222 return (display_columns, data)