From 86046e6bcad8f3d2919e7dd43b79fa2b195faaca Mon Sep 17 00:00:00 2001 From: Jongmin Kim Date: Sat, 10 Apr 2021 21:46:17 +0900 Subject: [PATCH] Change tags message type Change grpc timestamp type to string type(iso8601) Signed-off-by: Jongmin Kim --- .../config/info/domain_config_info.py | 6 ++-- src/spaceone/config/info/user_config_info.py | 6 ++-- .../config/service/domain_config_service.py | 12 +++++-- .../config/service/user_config_service.py | 12 +++++-- test/api/test_domain_config_api.py | 31 ++++++++++-------- test/api/test_user_config_api.py | 32 +++++++++++-------- test/service/test_domain_config_service.py | 22 +++++-------- test/service/test_user_config_service.py | 22 +++++-------- 8 files changed, 77 insertions(+), 66 deletions(-) diff --git a/src/spaceone/config/info/domain_config_info.py b/src/spaceone/config/info/domain_config_info.py index 1d669d3..4a2091d 100644 --- a/src/spaceone/config/info/domain_config_info.py +++ b/src/spaceone/config/info/domain_config_info.py @@ -1,7 +1,7 @@ import functools -from spaceone.api.core.v1 import tag_pb2 from spaceone.api.config.v1 import domain_config_pb2 from spaceone.core.pygrpc.message_type import * +from spaceone.core import utils from spaceone.config.model.domain_config_model import DomainConfig __all__ = ['DomainConfigInfo', 'DomainConfigsInfo'] @@ -15,10 +15,10 @@ def DomainConfigInfo(domain_config_vo: DomainConfig, minimal=False): if not minimal: info.update({ 'data': change_struct_type(domain_config_vo.data), - 'tags': [tag_pb2.Tag(key=tag.key, value=tag.value) for tag in domain_config_vo.tags], + 'tags': change_struct_type(utils.tags_to_dict(domain_config_vo.tags)), 'schema': domain_config_vo.schema, 'domain_id': domain_config_vo.domain_id, - 'created_at': change_timestamp_type(domain_config_vo.created_at) + 'created_at': utils.datetime_to_iso8601(domain_config_vo.created_at) }) return domain_config_pb2.DomainConfigInfo(**info) diff --git a/src/spaceone/config/info/user_config_info.py b/src/spaceone/config/info/user_config_info.py index 14599d8..e8a4228 100644 --- a/src/spaceone/config/info/user_config_info.py +++ b/src/spaceone/config/info/user_config_info.py @@ -1,7 +1,7 @@ import functools -from spaceone.api.core.v1 import tag_pb2 from spaceone.api.config.v1 import user_config_pb2 from spaceone.core.pygrpc.message_type import * +from spaceone.core import utils from spaceone.config.model.user_config_model import UserConfig __all__ = ['UserConfigInfo', 'UserConfigsInfo'] @@ -15,9 +15,9 @@ def UserConfigInfo(user_config_vo: UserConfig, minimal=False): if not minimal: info.update({ 'data': change_struct_type(user_config_vo.data), - 'tags': [tag_pb2.Tag(key=tag.key, value=tag.value) for tag in user_config_vo.tags], + 'tags': change_struct_type(utils.tags_to_dict(user_config_vo.tags)), 'domain_id': user_config_vo.domain_id, - 'created_at': change_timestamp_type(user_config_vo.created_at) + 'created_at': utils.datetime_to_iso8601(user_config_vo.created_at) }) return user_config_pb2.UserConfigInfo(**info) diff --git a/src/spaceone/config/service/domain_config_service.py b/src/spaceone/config/service/domain_config_service.py index 2cf1291..d4ea419 100644 --- a/src/spaceone/config/service/domain_config_service.py +++ b/src/spaceone/config/service/domain_config_service.py @@ -1,6 +1,7 @@ import logging from spaceone.core.service import * +from spaceone.core import utils from spaceone.config.manager.domain_config_manager import DomainConfigManager _LOGGER = logging.getLogger(__name__) @@ -25,7 +26,7 @@ def create(self, params): 'name': 'str', 'data': 'dict', 'schema': 'str', - 'tags': 'list', + 'tags': 'dict', 'domain_id': 'str' } @@ -33,6 +34,9 @@ def create(self, params): domain_config_vo (object) """ + if 'tags' in params: + params['tags'] = utils.dict_to_tags(params['tags']) + return self.domain_config_mgr.create_domain_config(params) @transaction(append_meta={'authorization.scope': 'DOMAIN'}) @@ -45,13 +49,17 @@ def update(self, params): 'name': 'str', 'data': 'dict', 'schema': 'str', - 'tags': 'list', + 'tags': 'dict', 'domain_id': 'str' } Returns: domain_config_vo (object) """ + + if 'tags' in params: + params['tags'] = utils.dict_to_tags(params['tags']) + return self.domain_config_mgr.update_domain_config(params) @transaction(append_meta={'authorization.scope': 'DOMAIN'}) diff --git a/src/spaceone/config/service/user_config_service.py b/src/spaceone/config/service/user_config_service.py index d7ca20a..1521a7d 100644 --- a/src/spaceone/config/service/user_config_service.py +++ b/src/spaceone/config/service/user_config_service.py @@ -1,6 +1,7 @@ import logging from spaceone.core.service import * +from spaceone.core import utils from spaceone.config.manager.user_config_manager import UserConfigManager _LOGGER = logging.getLogger(__name__) @@ -24,7 +25,7 @@ def create(self, params): params (dict): { 'name': 'str', 'data': 'dict', - 'tags': 'list', + 'tags': 'dict', 'domain_id': 'str' } @@ -32,6 +33,9 @@ def create(self, params): user_config_vo (object) """ + if 'tags' in params: + params['tags'] = utils.dict_to_tags(params['tags']) + return self.user_config_mgr.create_user_config(params) @transaction(append_meta={'authorization.scope': 'DOMAIN'}) @@ -43,13 +47,17 @@ def update(self, params): params (dict): { 'name': 'str', 'data': 'dict', - 'tags': 'list', + 'tags': 'dict', 'domain_id': 'str' } Returns: user_config_vo (object) """ + + if 'tags' in params: + params['tags'] = utils.dict_to_tags(params['tags']) + return self.user_config_mgr.update_user_config(params) @transaction(append_meta={'authorization.scope': 'DOMAIN'}) diff --git a/test/api/test_domain_config_api.py b/test/api/test_domain_config_api.py index 8b492fe..e7245f0 100644 --- a/test/api/test_domain_config_api.py +++ b/test/api/test_domain_config_api.py @@ -1,4 +1,5 @@ import unittest +import copy from unittest.mock import patch from mongoengine import connect, disconnect from google.protobuf.json_format import MessageToDict @@ -19,9 +20,17 @@ class _MockDomainConfigService(BaseService): def create(self, params): + params = copy.deepcopy(params) + if 'tags' in params: + params['tags'] = utils.dict_to_tags(params['tags']) + return DomainConfigFactory(**params) def update(self, params): + params = copy.deepcopy(params) + if 'tags' in params: + params['tags'] = utils.dict_to_tags(params['tags']) + return DomainConfigFactory(**params) def delete(self, params): @@ -62,12 +71,9 @@ def test_create_domain_config(self, mock_parse_request, *args): 'config_key': 'config_value' }, 'schema': utils.random_string(), - 'tags': [ - { - 'key': 'tag_key', - 'value': 'tag_value' - } - ], + 'tags': { + utils.random_string(): utils.random_string() + }, 'domain_id': utils.generate_id('domain') } mock_parse_request.return_value = (params, {}) @@ -81,7 +87,7 @@ def test_create_domain_config(self, mock_parse_request, *args): self.assertIsInstance(domain_config_info, domain_config_pb2.DomainConfigInfo) self.assertEqual(domain_config_info.name, params['name']) self.assertDictEqual(MessageToDict(domain_config_info.data), params['data']) - self.assertListEqual(domain_config_data['tags'], params['tags']) + self.assertDictEqual(domain_config_data['tags'], params['tags']) self.assertEqual(domain_config_info.domain_id, params['domain_id']) self.assertIsNotNone(getattr(domain_config_info, 'created_at', None)) @@ -95,12 +101,9 @@ def test_update_domain_config(self, mock_parse_request, *args): 'update_config_key': 'update_config_value' }, 'schema': utils.random_string(), - 'tags': [ - { - 'key': 'update_key', - 'value': 'update_value' - } - ], + 'tags': { + 'update_key': 'update_value' + }, 'domain_id': utils.generate_id('domain') } mock_parse_request.return_value = (params, {}) @@ -113,7 +116,7 @@ def test_update_domain_config(self, mock_parse_request, *args): self.assertIsInstance(domain_config_info, domain_config_pb2.DomainConfigInfo) self.assertDictEqual(MessageToDict(domain_config_info.data), params['data']) - self.assertListEqual(domain_config_data['tags'], params['tags']) + self.assertDictEqual(domain_config_data['tags'], params['tags']) @patch.object(BaseAPI, '__init__', return_value=None) @patch.object(Locator, 'get_service', return_value=_MockDomainConfigService()) diff --git a/test/api/test_user_config_api.py b/test/api/test_user_config_api.py index 6626a58..03d40d7 100644 --- a/test/api/test_user_config_api.py +++ b/test/api/test_user_config_api.py @@ -1,4 +1,5 @@ import unittest +import copy from unittest.mock import patch from mongoengine import connect, disconnect from google.protobuf.json_format import MessageToDict @@ -19,9 +20,18 @@ class _MockUserConfigService(BaseService): def create(self, params): + params = copy.deepcopy(params) + if 'tags' in params: + params['tags'] = utils.dict_to_tags(params['tags']) + return UserConfigFactory(**params) def update(self, params): + + params = copy.deepcopy(params) + if 'tags' in params: + params['tags'] = utils.dict_to_tags(params['tags']) + return UserConfigFactory(**params) def delete(self, params): @@ -61,12 +71,9 @@ def test_create_user_config(self, mock_parse_request, *args): 'data': { 'config_key': 'config_value' }, - 'tags': [ - { - 'key': 'tag_key', - 'value': 'tag_value' - } - ], + 'tags': { + utils.random_string(): utils.random_string() + }, 'domain_id': utils.generate_id('domain') } mock_parse_request.return_value = (params, {}) @@ -80,7 +87,7 @@ def test_create_user_config(self, mock_parse_request, *args): self.assertIsInstance(user_config_info, user_config_pb2.UserConfigInfo) self.assertEqual(user_config_info.name, params['name']) self.assertDictEqual(MessageToDict(user_config_info.data), params['data']) - self.assertListEqual(user_config_data['tags'], params['tags']) + self.assertDictEqual(user_config_data['tags'], params['tags']) self.assertEqual(user_config_info.domain_id, params['domain_id']) self.assertIsNotNone(getattr(user_config_info, 'created_at', None)) @@ -93,12 +100,9 @@ def test_update_user_config(self, mock_parse_request, *args): 'data': { 'update_config_key': 'update_config_value' }, - 'tags': [ - { - 'key': 'update_key', - 'value': 'update_value' - } - ], + 'tags': { + 'update_key': 'update_value' + }, 'domain_id': utils.generate_id('domain') } mock_parse_request.return_value = (params, {}) @@ -111,7 +115,7 @@ def test_update_user_config(self, mock_parse_request, *args): self.assertIsInstance(user_config_info, user_config_pb2.UserConfigInfo) self.assertDictEqual(MessageToDict(user_config_info.data), params['data']) - self.assertListEqual(user_config_data['tags'], params['tags']) + self.assertDictEqual(user_config_data['tags'], params['tags']) @patch.object(BaseAPI, '__init__', return_value=None) @patch.object(Locator, 'get_service', return_value=_MockUserConfigService()) diff --git a/test/service/test_domain_config_service.py b/test/service/test_domain_config_service.py index 15dc55e..905e7c4 100644 --- a/test/service/test_domain_config_service.py +++ b/test/service/test_domain_config_service.py @@ -49,12 +49,9 @@ def test_create_domain_config(self, *args): 'key': 'value' }, 'schema': 'test_schema', - 'tags': [ - { - 'key': 'tag_key', - 'value': 'tag_value' - } - ], + 'tags': { + utils.random_string(): utils.random_string() + }, 'domain_id': utils.generate_id('domain') } @@ -69,7 +66,7 @@ def test_create_domain_config(self, *args): self.assertEqual(params['name'], domain_config_vo.name) self.assertEqual(params['data'], domain_config_vo.data) self.assertEqual(params['schema'], domain_config_vo.schema) - self.assertEqual(params.get('tags', {}), domain_config_vo.to_dict()['tags']) + self.assertEqual(params['tags'], utils.tags_to_dict(domain_config_vo.tags)) self.assertEqual(params['domain_id'], domain_config_vo.domain_id) @patch.object(MongoModel, 'connect', return_value=None) @@ -82,12 +79,9 @@ def test_update_domain_config(self, *args): 'update_data_key': 'update_data_value' }, 'schema': 'update_schema', - 'tags': [ - { - 'key': 'update_key', - 'value': 'update_value' - } - ], + 'tags': { + 'update_key': 'update_value' + }, 'domain_id': self.domain_id } @@ -101,7 +95,7 @@ def test_update_domain_config(self, *args): self.assertIsInstance(domain_config_vo, DomainConfig) self.assertEqual(params['data'], domain_config_vo.data) self.assertEqual(params['schema'], domain_config_vo.schema) - self.assertEqual(params.get('tags', {}), domain_config_vo.to_dict()['tags']) + self.assertEqual(params['tags'], utils.tags_to_dict(domain_config_vo.tags)) self.assertEqual(params['domain_id'], domain_config_vo.domain_id) @patch.object(MongoModel, 'connect', return_value=None) diff --git a/test/service/test_user_config_service.py b/test/service/test_user_config_service.py index 0460f15..dc1e231 100644 --- a/test/service/test_user_config_service.py +++ b/test/service/test_user_config_service.py @@ -48,12 +48,9 @@ def test_create_user_config(self, *args): 'data': { 'key': 'value' }, - 'tags': [ - { - 'key': 'tag_key', - 'value': 'tag_value' - } - ], + 'tags': { + utils.random_string(): utils.random_string() + }, 'domain_id': utils.generate_id('domain') } @@ -67,7 +64,7 @@ def test_create_user_config(self, *args): self.assertIsInstance(user_config_vo, UserConfig) self.assertEqual(params['name'], user_config_vo.name) self.assertEqual(params['data'], user_config_vo.data) - self.assertEqual(params.get('tags', {}), user_config_vo.to_dict()['tags']) + self.assertEqual(params['tags'], utils.tags_to_dict(user_config_vo.tags)) self.assertEqual(params['domain_id'], user_config_vo.domain_id) @patch.object(MongoModel, 'connect', return_value=None) @@ -79,12 +76,9 @@ def test_update_user_config(self, *args): 'data': { 'update_data_key': 'update_data_value' }, - 'tags': [ - { - 'key': 'update_key', - 'value': 'update_value' - } - ], + 'tags': { + 'update_key': 'update_value' + }, 'domain_id': self.domain_id } @@ -97,7 +91,7 @@ def test_update_user_config(self, *args): self.assertIsInstance(user_config_vo, UserConfig) self.assertEqual(params['data'], user_config_vo.data) - self.assertEqual(params.get('tags', {}), user_config_vo.to_dict()['tags']) + self.assertEqual(params['tags'], utils.tags_to_dict(user_config_vo.tags)) self.assertEqual(params['domain_id'], user_config_vo.domain_id) @patch.object(MongoModel, 'connect', return_value=None)