diff --git a/dynamic/resource.py b/dynamic/resource.py index c83ae9f..6dac1d8 100644 --- a/dynamic/resource.py +++ b/dynamic/resource.py @@ -48,7 +48,7 @@ def __init__(self, prefix=None, group=None, api_version=None, kind=None, self.extra_args = kwargs def to_dict(self): - return { + d = { '_type': 'Resource', 'prefix': self.prefix, 'group': self.group, @@ -58,12 +58,13 @@ def to_dict(self): 'verbs': self.verbs, 'name': self.name, 'preferred': self.preferred, - 'singular_name': self.singular_name, - 'short_names': self.short_names, + 'singularName': self.singular_name, + 'shortNames': self.short_names, 'categories': self.categories, 'subresources': {k: sr.to_dict() for k, sr in self.subresources.items()}, - 'extra_args': self.extra_args, } + d.update(self.extra_args) + return d @property def group_version(self): @@ -236,7 +237,7 @@ def __init__(self, parent, **kwargs): self.api_version = parent.api_version self.kind = kwargs.pop('kind') self.name = kwargs.pop('name') - self.subresource = self.name.split('/')[1] + self.subresource = kwargs.pop('subresource', None) or self.name.split('/')[1] self.namespaced = kwargs.pop('namespaced', False) self.verbs = kwargs.pop('verbs', None) self.extra_args = kwargs @@ -262,14 +263,15 @@ def __getattr__(self, name): return partial(getattr(self.parent.client, name), self) def to_dict(self): - return { + d = { 'kind': self.kind, 'name': self.name, 'subresource': self.subresource, 'namespaced': self.namespaced, - 'verbs': self.verbs, - 'extra_args': self.extra_args, + 'verbs': self.verbs } + d.update(self.extra_args) + return d class ResourceInstance(object): diff --git a/dynamic/test_discovery.py b/dynamic/test_discovery.py index 4897f24..639ccdd 100644 --- a/dynamic/test_discovery.py +++ b/dynamic/test_discovery.py @@ -38,3 +38,24 @@ def test_init_cache_from_file(self): # test no Discoverer._write_cache called self.assertTrue(mtime1 == mtime2) + + def test_cache_decoder_resource_and_subresource(self): + client = DynamicClient(api_client.ApiClient(configuration=self.config)) + # first invalidate cache + client.resources.invalidate_cache() + + # do Discoverer.__init__ + client = DynamicClient(api_client.ApiClient(configuration=self.config)) + # the resources of client will use _cache['resources'] in memory + deploy1 = client.resources.get(kind='Deployment') + + # do Discoverer.__init__ + client = DynamicClient(api_client.ApiClient(configuration=self.config)) + # the resources of client will use _cache['resources'] decode from cache file + deploy2 = client.resources.get(kind='Deployment') + + # test Resource is the same + self.assertTrue(deploy1 == deploy2) + + # test Subresource is the same + self.assertTrue(deploy1.status == deploy2.status)