Skip to content

Commit f5cf1fb

Browse files
committed
Migrate 'endpoint group' commands to SDK
Also add functional tests. Change-Id: I86a7142d3014f1052d961a52c8bb222e9b97c6b0 Signed-off-by: 0weng <oweng@osuosl.org>
1 parent 6ef4d93 commit f5cf1fb

5 files changed

Lines changed: 392 additions & 182 deletions

File tree

openstackclient/identity/v3/endpoint_group.py

Lines changed: 88 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
"""Identity v3 Endpoint Group action implementations"""
1515

1616
import argparse
17-
from collections.abc import Iterable, Sequence
17+
from collections.abc import Generator, Iterable, Sequence
1818
import json
1919
import logging
2020
from typing import Any
2121

22+
from openstack.identity.v3 import endpoint_group as _endpoint_group
23+
from openstack import utils as sdk_utils
2224
from osc_lib import exceptions
2325
from osc_lib import utils
2426

@@ -30,6 +32,14 @@
3032
LOG = logging.getLogger(__name__)
3133

3234

35+
def _format_endpoint_group(
36+
endpoint_group: _endpoint_group.EndpointGroup,
37+
) -> tuple[tuple[str, ...], tuple[str, ...]]:
38+
columns = ('description', 'filters', 'id', 'name')
39+
column_headers = ('description', 'filters', 'id', 'name')
40+
return (column_headers, utils.get_item_properties(endpoint_group, columns))
41+
42+
3343
class _FiltersReader:
3444
_description = _("Helper class capable of reading filters from files")
3545

@@ -81,17 +91,26 @@ def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
8191
return parser
8292

8393
def take_action(self, parsed_args: argparse.Namespace) -> None:
84-
client = self.app.client_manager.identity
85-
86-
endpointgroup = utils.find_resource(
87-
client.endpoint_groups, parsed_args.endpointgroup
94+
identity_client = sdk_utils.ensure_service_version(
95+
self.app.client_manager.sdk_connection.identity, '3'
8896
)
8997

90-
project = common.find_project(
91-
client, parsed_args.project, parsed_args.project_domain
98+
endpointgroup = identity_client.find_endpoint_group(
99+
parsed_args.endpointgroup, ignore_missing=False
92100
)
93101

94-
client.endpoint_filter.add_endpoint_group_to_project(
102+
if parsed_args.project_domain:
103+
project = identity_client.find_project(
104+
parsed_args.project,
105+
domain_id=parsed_args.project_domain,
106+
ignore_missing=False,
107+
)
108+
else:
109+
project = identity_client.find_project(
110+
parsed_args.project, ignore_missing=False
111+
)
112+
113+
identity_client.associate_project_with_endpoint_group(
95114
endpoint_group=endpointgroup.id, project=project.id
96115
)
97116

@@ -120,23 +139,21 @@ def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
120139
def take_action(
121140
self, parsed_args: argparse.Namespace
122141
) -> tuple[Sequence[str], Iterable[Any]]:
123-
identity_client = self.app.client_manager.identity
142+
identity_client = sdk_utils.ensure_service_version(
143+
self.app.client_manager.sdk_connection.identity, '3'
144+
)
124145

125146
filters = None
126147
if parsed_args.filters:
127148
filters = self._read_filters(parsed_args.filters)
128149

129-
endpoint_group = identity_client.endpoint_groups.create(
150+
endpoint_group = identity_client.create_endpoint_group(
130151
name=parsed_args.name,
131152
filters=filters,
132153
description=parsed_args.description,
133154
)
134155

135-
info = {}
136-
endpoint_group._info.pop('links')
137-
info.update(endpoint_group._info)
138-
col_headers, col_data = zip(*sorted(info.items()))
139-
return col_headers, col_data
156+
return _format_endpoint_group(endpoint_group)
140157

141158

142159
class DeleteEndpointGroup(command.Command):
@@ -153,14 +170,16 @@ def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
153170
return parser
154171

155172
def take_action(self, parsed_args: argparse.Namespace) -> None:
156-
identity_client = self.app.client_manager.identity
173+
identity_client = sdk_utils.ensure_service_version(
174+
self.app.client_manager.sdk_connection.identity, '3'
175+
)
157176
result = 0
158177
for i in parsed_args.endpointgroup:
159178
try:
160-
endpoint_id = utils.find_resource(
161-
identity_client.endpoint_groups, i
179+
endpointgroup_id = identity_client.find_endpoint_group(
180+
i, ignore_missing=False
162181
).id
163-
identity_client.endpoint_groups.delete(endpoint_id)
182+
identity_client.delete_endpoint_group(endpointgroup_id)
164183
except Exception as e:
165184
result += 1
166185
LOG.error(
@@ -205,33 +224,37 @@ def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
205224
def take_action(
206225
self, parsed_args: argparse.Namespace
207226
) -> tuple[tuple[str, ...], Iterable[tuple[Any, ...]]]:
208-
client = self.app.client_manager.identity
227+
identity_client = sdk_utils.ensure_service_version(
228+
self.app.client_manager.sdk_connection.identity, '3'
229+
)
209230

210-
endpointgroup = None
231+
data: Generator[Any, None, None]
211232
if parsed_args.endpointgroup:
212-
endpointgroup = utils.find_resource(
213-
client.endpoint_groups, parsed_args.endpointgroup
214-
)
215-
project = None
216-
if parsed_args.project:
217-
project = common.find_project(
218-
client, parsed_args.project, parsed_args.domain
233+
endpointgroup = identity_client.find_endpoint_group(
234+
parsed_args.endpointgroup, ignore_missing=False
219235
)
220-
221-
if endpointgroup:
222-
# List projects associated to the endpoint group
236+
# List projects associated with the endpoint group
223237
columns = ('ID', 'Name', 'Description')
224-
data = client.endpoint_filter.list_projects_for_endpoint_group(
238+
data = identity_client.endpoint_group_projects(
225239
endpoint_group=endpointgroup.id
226240
)
227-
elif project:
241+
elif parsed_args.project:
242+
if parsed_args.domain:
243+
project = identity_client.find_project(
244+
parsed_args.project,
245+
domain_id=parsed_args.domain,
246+
ignore_missing=False,
247+
)
248+
else:
249+
project = identity_client.find_project(
250+
parsed_args.project, ignore_missing=False
251+
)
252+
228253
columns = ('ID', 'Name', 'Description')
229-
data = client.endpoint_filter.list_endpoint_groups_for_project(
230-
project=project.id
231-
)
254+
data = identity_client.project_endpoint_groups(project=project.id)
232255
else:
233256
columns = ('ID', 'Name', 'Description')
234-
data = client.endpoint_groups.list()
257+
data = identity_client.endpoint_groups()
235258

236259
return (
237260
columns,
@@ -265,17 +288,26 @@ def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
265288
return parser
266289

267290
def take_action(self, parsed_args: argparse.Namespace) -> None:
268-
client = self.app.client_manager.identity
269-
270-
endpointgroup = utils.find_resource(
271-
client.endpoint_groups, parsed_args.endpointgroup
291+
identity_client = sdk_utils.ensure_service_version(
292+
self.app.client_manager.sdk_connection.identity, '3'
272293
)
273294

274-
project = common.find_project(
275-
client, parsed_args.project, parsed_args.project_domain
295+
endpointgroup = identity_client.find_endpoint_group(
296+
parsed_args.endpointgroup, ignore_missing=False
276297
)
277298

278-
client.endpoint_filter.delete_endpoint_group_from_project(
299+
if parsed_args.project_domain:
300+
project = identity_client.find_project(
301+
parsed_args.project,
302+
domain_id=parsed_args.project_domain,
303+
ignore_missing=False,
304+
)
305+
else:
306+
project = identity_client.find_project(
307+
parsed_args.project, ignore_missing=False
308+
)
309+
310+
identity_client.disassociate_project_from_endpoint_group(
279311
endpoint_group=endpointgroup.id, project=project.id
280312
)
281313

@@ -309,16 +341,18 @@ def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
309341
return parser
310342

311343
def take_action(self, parsed_args: argparse.Namespace) -> None:
312-
identity_client = self.app.client_manager.identity
313-
endpointgroup = utils.find_resource(
314-
identity_client.endpoint_groups, parsed_args.endpointgroup
344+
identity_client = sdk_utils.ensure_service_version(
345+
self.app.client_manager.sdk_connection.identity, '3'
346+
)
347+
endpointgroup = identity_client.find_endpoint_group(
348+
parsed_args.endpointgroup, ignore_missing=False
315349
)
316350

317351
filters = None
318352
if parsed_args.filters:
319353
filters = self._read_filters(parsed_args.filters)
320354

321-
identity_client.endpoint_groups.update(
355+
identity_client.update_endpoint_group(
322356
endpointgroup.id,
323357
name=parsed_args.name,
324358
filters=filters,
@@ -341,13 +375,11 @@ def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
341375
def take_action(
342376
self, parsed_args: argparse.Namespace
343377
) -> tuple[Sequence[str], Iterable[Any]]:
344-
identity_client = self.app.client_manager.identity
345-
endpoint_group = utils.find_resource(
346-
identity_client.endpoint_groups, parsed_args.endpointgroup
378+
identity_client = sdk_utils.ensure_service_version(
379+
self.app.client_manager.sdk_connection.identity, '3'
380+
)
381+
endpoint_group = identity_client.find_endpoint_group(
382+
parsed_args.endpointgroup, ignore_missing=False
347383
)
348384

349-
info = {}
350-
endpoint_group._info.pop('links')
351-
info.update(endpoint_group._info)
352-
col_headers, col_data = zip(*sorted(info.items()))
353-
return col_headers, col_data
385+
return _format_endpoint_group(endpoint_group)

openstackclient/tests/functional/identity/v3/common.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ class IdentityTests(base.TestCase):
6464
'interface',
6565
'url',
6666
]
67+
ENDPOINT_GROUP_FIELDS = [
68+
'description',
69+
'filters',
70+
'id',
71+
'name',
72+
]
6773

6874
REGION_LIST_HEADERS = ['Region', 'Parent Region', 'Description']
6975
ENDPOINT_LIST_HEADERS = [
@@ -76,6 +82,12 @@ class IdentityTests(base.TestCase):
7682
'URL',
7783
]
7884
ENDPOINT_LIST_PROJECT_HEADERS = ['ID', 'Name']
85+
ENDPOINT_GROUP_LIST_HEADERS = [
86+
'ID',
87+
'Name',
88+
'Description',
89+
]
90+
ENDPOINT_GROUP_LIST_PROJECT_HEADERS = ['ID', 'Name', 'Description']
7991

8092
MAPPING_FIELDS = ['id', 'rules', 'schema_version']
8193

@@ -392,6 +404,27 @@ def _create_dummy_endpoint(self, interface='public', add_clean_up=True):
392404
self.assert_show_fields(items, self.ENDPOINT_FIELDS)
393405
return endpoint['id']
394406

407+
def _create_dummy_endpoint_group(
408+
self, filters={'interface': 'public'}, add_clean_up=True
409+
):
410+
endpoint_group = data_utils.rand_name('EndpointGroup')
411+
# Create filters file
412+
with tempfile.NamedTemporaryFile(mode='w+') as f:
413+
f.write(json.dumps(filters))
414+
f.flush()
415+
raw_output = self.openstack(
416+
f'endpoint group create {endpoint_group} {f.name}'
417+
)
418+
endpoint_group = self.parse_show_as_object(raw_output)
419+
if add_clean_up:
420+
self.addCleanup(
421+
self.openstack,
422+
'endpoint group delete {}'.format(endpoint_group['id']),
423+
)
424+
items = self.parse_show(raw_output)
425+
self.assert_show_fields(items, self.ENDPOINT_GROUP_FIELDS)
426+
return endpoint_group['id']
427+
395428
def _create_dummy_mapping(self, add_clean_up=True):
396429
mapping = data_utils.rand_name('Mapping')
397430
# Create rules file

0 commit comments

Comments
 (0)