Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions openshift/dynamic/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import json
import hashlib
import tempfile
from collections import defaultdict
from functools import partial
from six import PY2, PY3

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

resources = {}
resources = defaultdict(list)
subresources = {}

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

resources[resource['kind']] = Resource(
resourceobj = Resource(
prefix=prefix,
group=group,
api_version=version,
Expand All @@ -213,7 +214,8 @@ def get_resources_for_api_version(self, prefix, group, version, preferred):
subresources=subresources.get(resource['name']),
**resource
)
resources['{}List'.format(resource['kind'])] = ResourceList(resources[resource['kind']])
resources[resource['kind']].append(resourceobj)
resources['{}List'.format(resource['kind'])].append(ResourceList(resourceobj))
return resources

def ensure_namespace(self, resource, namespace, body):
Expand Down Expand Up @@ -668,13 +670,13 @@ def __search(self, parts, resources):
if isinstance(resources.get(part), dict):
return self.__search(parts[1:], resources[part])
else:
resource = resources.get(part)
if parts[1] != '*' and isinstance(parts[1], dict):
for term, value in parts[1].items():
if getattr(resource, term) == value:
return [resource]
for resource in resources.get(part):
for term, value in parts[1].items():
if getattr(resource, term) == value:
return [resource]
else:
return [resource]
return resources.get(part)
elif part == '*':
matches = []
for key in resources.keys():
Expand Down
41 changes: 38 additions & 3 deletions test/unit/test_resource_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,33 @@

from openshift.dynamic import DynamicClient, Resource, ResourceList

@pytest.fixture(scope='module')
def mock_templates():
return Resource(
api_version='v1',
kind='Template',
name='templates',
namespaced=True,
preferred=True,
prefix='api',
shorter_names=[],
shortNames=[],
verbs=['create', 'delete', 'get', 'list', 'patch', 'update', 'watch']
)

@pytest.fixture(scope='module')
def mock_processedtemplates():
return Resource(
api_version='v1',
kind='Template',
name='processedtemplates',
namespaced=True,
preferred=True,
prefix='api',
shorter_names=[],
shortNames=[],
verbs=['create', 'delete', 'get', 'list', 'patch', 'update', 'watch']
)

@pytest.fixture(scope='module')
def mock_namespace():
Expand All @@ -26,7 +53,7 @@ def mock_namespace_list(mock_namespace):
return ResourceList(mock_namespace)

@pytest.fixture(scope='function', autouse=True)
def setup_client_monkeypatch(monkeypatch, mock_namespace, mock_namespace_list):
def setup_client_monkeypatch(monkeypatch, mock_namespace, mock_namespace_list, mock_templates, mock_processedtemplates):

def mock_load_server_info(self):
self.__version = {'kubernetes': 'mock-k8s-version'}
Expand All @@ -36,8 +63,9 @@ def mock_parse_api_groups(self):
'api': {
'': {
'v1': {
'Namespace': mock_namespace,
'NamespaceList': mock_namespace_list
'Namespace': [mock_namespace],
'NamespaceList': [mock_namespace_list],
'Template': [mock_templates, mock_processedtemplates],
}
}
}
Expand Down Expand Up @@ -80,3 +108,10 @@ def test_get_namespace_list_kind(client, mock_namespace_list):
resource = client.resources.get(api_version='v1', kind='NamespaceList')

assert resource == mock_namespace_list

def test_search_multiple_resources_for_template(client, mock_templates, mock_processedtemplates):
resources = client.resources.search(api_version='v1', kind='Template')

assert len(resources) == 2
assert mock_templates in resources
assert mock_processedtemplates in resources