Skip to content

Commit 46cf660

Browse files
machacekondrafabianvf
authored andcommitted
Make list from kind resource item
1 parent b5a3a7f commit 46cf660

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

openshift/dynamic/client.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import json
77
import hashlib
88
import tempfile
9+
from collections import defaultdict
910
from functools import partial
1011
from six import PY2, PY3
1112

@@ -185,7 +186,7 @@ def parse_api_groups(self):
185186
def get_resources_for_api_version(self, prefix, group, version, preferred):
186187
""" returns a dictionary of resources associated with provided groupVersion"""
187188

188-
resources = {}
189+
resources = defaultdict(list)
189190
subresources = {}
190191

191192
path = '/'.join(filter(None, [prefix, group, version]))
@@ -204,7 +205,7 @@ def get_resources_for_api_version(self, prefix, group, version, preferred):
204205
for key in ('prefix', 'group', 'api_version', 'client', 'preferred'):
205206
resource.pop(key, None)
206207

207-
resources[resource['kind']] = Resource(
208+
resourceobj = Resource(
208209
prefix=prefix,
209210
group=group,
210211
api_version=version,
@@ -213,7 +214,8 @@ def get_resources_for_api_version(self, prefix, group, version, preferred):
213214
subresources=subresources.get(resource['name']),
214215
**resource
215216
)
216-
resources['{}List'.format(resource['kind'])] = ResourceList(resources[resource['kind']])
217+
resources[resource['kind']].append(resourceobj)
218+
resources['{}List'.format(resource['kind'])].append(ResourceList(resourceobj))
217219
return resources
218220

219221
def ensure_namespace(self, resource, namespace, body):
@@ -668,13 +670,13 @@ def __search(self, parts, resources):
668670
if isinstance(resources.get(part), dict):
669671
return self.__search(parts[1:], resources[part])
670672
else:
671-
resource = resources.get(part)
672673
if parts[1] != '*' and isinstance(parts[1], dict):
673-
for term, value in parts[1].items():
674-
if getattr(resource, term) == value:
675-
return [resource]
674+
for resource in resources.get(part):
675+
for term, value in parts[1].items():
676+
if getattr(resource, term) == value:
677+
return [resource]
676678
else:
677-
return [resource]
679+
return resources.get(part)
678680
elif part == '*':
679681
matches = []
680682
for key in resources.keys():

test/unit/test_resource_container.py

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,33 @@
44

55
from openshift.dynamic import DynamicClient, Resource, ResourceList
66

7+
@pytest.fixture(scope='module')
8+
def mock_templates():
9+
return Resource(
10+
api_version='v1',
11+
kind='Template',
12+
name='templates',
13+
namespaced=True,
14+
preferred=True,
15+
prefix='api',
16+
shorter_names=[],
17+
shortNames=[],
18+
verbs=['create', 'delete', 'get', 'list', 'patch', 'update', 'watch']
19+
)
20+
21+
@pytest.fixture(scope='module')
22+
def mock_processedtemplates():
23+
return Resource(
24+
api_version='v1',
25+
kind='Template',
26+
name='processedtemplates',
27+
namespaced=True,
28+
preferred=True,
29+
prefix='api',
30+
shorter_names=[],
31+
shortNames=[],
32+
verbs=['create', 'delete', 'get', 'list', 'patch', 'update', 'watch']
33+
)
734

835
@pytest.fixture(scope='module')
936
def mock_namespace():
@@ -26,7 +53,7 @@ def mock_namespace_list(mock_namespace):
2653
return ResourceList(mock_namespace)
2754

2855
@pytest.fixture(scope='function', autouse=True)
29-
def setup_client_monkeypatch(monkeypatch, mock_namespace, mock_namespace_list):
56+
def setup_client_monkeypatch(monkeypatch, mock_namespace, mock_namespace_list, mock_templates, mock_processedtemplates):
3057

3158
def mock_load_server_info(self):
3259
self.__version = {'kubernetes': 'mock-k8s-version'}
@@ -36,8 +63,9 @@ def mock_parse_api_groups(self):
3663
'api': {
3764
'': {
3865
'v1': {
39-
'Namespace': mock_namespace,
40-
'NamespaceList': mock_namespace_list
66+
'Namespace': [mock_namespace],
67+
'NamespaceList': [mock_namespace_list],
68+
'Template': [mock_templates, mock_processedtemplates],
4169
}
4270
}
4371
}
@@ -80,3 +108,10 @@ def test_get_namespace_list_kind(client, mock_namespace_list):
80108
resource = client.resources.get(api_version='v1', kind='NamespaceList')
81109

82110
assert resource == mock_namespace_list
111+
112+
def test_search_multiple_resources_for_template(client, mock_templates, mock_processedtemplates):
113+
resources = client.resources.search(api_version='v1', kind='Template')
114+
115+
assert len(resources) == 2
116+
assert mock_templates in resources
117+
assert mock_processedtemplates in resources

0 commit comments

Comments
 (0)