Skip to content

Commit

Permalink
feat: add DomainConfig.set and UserConfig.set APIs
Browse files Browse the repository at this point in the history
Signed-off-by: Jongmin Kim <[email protected]>
  • Loading branch information
whdalsrnt committed Mar 16, 2022
1 parent 51f2388 commit 8689651
Show file tree
Hide file tree
Showing 16 changed files with 236 additions and 21 deletions.
6 changes: 6 additions & 0 deletions src/spaceone/config/api/v1/domain_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ def update(self, request, context):
with self.locator.get_service('DomainConfigService', metadata) as domain_config_service:
return self.locator.get_info('DomainConfigInfo', domain_config_service.update(params))

def set(self, request, context):
params, metadata = self.parse_request(request, context)

with self.locator.get_service('DomainConfigService', metadata) as domain_config_service:
return self.locator.get_info('DomainConfigInfo', domain_config_service.set(params))

def delete(self, request, context):
params, metadata = self.parse_request(request, context)

Expand Down
6 changes: 6 additions & 0 deletions src/spaceone/config/api/v1/user_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ def update(self, request, context):
with self.locator.get_service('UserConfigService', metadata) as user_config_service:
return self.locator.get_info('UserConfigInfo', user_config_service.update(params))

def set(self, request, context):
params, metadata = self.parse_request(request, context)

with self.locator.get_service('UserConfigService', metadata) as user_config_service:
return self.locator.get_info('UserConfigInfo', user_config_service.set(params))

def delete(self, request, context):
params, metadata = self.parse_request(request, context)

Expand Down
1 change: 1 addition & 0 deletions src/spaceone/config/info/domain_config_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def DomainConfigInfo(domain_config_vo: DomainConfig, minimal=False):
'data': change_struct_type(domain_config_vo.data),
'tags': change_struct_type(utils.tags_to_dict(domain_config_vo.tags)),
'domain_id': domain_config_vo.domain_id,
'updated_at': utils.datetime_to_iso8601(domain_config_vo.updated_at),
'created_at': utils.datetime_to_iso8601(domain_config_vo.created_at)
})

Expand Down
1 change: 1 addition & 0 deletions src/spaceone/config/info/user_config_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def UserConfigInfo(user_config_vo: UserConfig, minimal=False):
'tags': change_struct_type(utils.tags_to_dict(user_config_vo.tags)),
'user_id': user_config_vo.user_id,
'domain_id': user_config_vo.domain_id,
'updated_at': utils.datetime_to_iso8601(user_config_vo.updated_at),
'created_at': utils.datetime_to_iso8601(user_config_vo.created_at)
})

Expand Down
3 changes: 3 additions & 0 deletions src/spaceone/config/manager/domain_config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def delete_domain_config(self, name, domain_id):
def get_domain_config(self, name, domain_id, only=None):
return self.domain_config_model.get(name=name, domain_id=domain_id, only=only)

def filter_domain_configs(self, **conditions):
return self.domain_config_model.filter(**conditions)

def list_domain_configs(self, query={}):
return self.domain_config_model.query(**query)

Expand Down
3 changes: 3 additions & 0 deletions src/spaceone/config/manager/user_config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def delete_user_config(self, name, domain_id):
def get_user_config(self, name, domain_id, only=None):
return self.user_config_model.get(name=name, domain_id=domain_id, only=only)

def filter_user_configs(self, **conditions):
return self.user_config_model.filter(**conditions)

def list_user_configs(self, query={}):
return self.user_config_model.query(**query)

Expand Down
4 changes: 3 additions & 1 deletion src/spaceone/config/model/domain_config_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ class DomainConfig(MongoModel):
tags = ListField(EmbeddedDocumentField(DomainConfigTag))
domain_id = StringField(max_length=40)
created_at = DateTimeField(auto_now_add=True)
updated_at = DateTimeField(auto_now=True)

meta = {
'updatable_fields': [
'name',
'data',
'tags'
'tags',
'updated_at'
],
'minimal_fields': [
'name'
Expand Down
4 changes: 3 additions & 1 deletion src/spaceone/config/model/user_config_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ class UserConfig(MongoModel):
user_id = StringField(max_length=40, default=None, null=True)
domain_id = StringField(max_length=40)
created_at = DateTimeField(auto_now_add=True)
updated_at = DateTimeField(auto_now=True)

meta = {
'updatable_fields': [
'name',
'data',
'tags'
'tags',
'updated_at'
],
'minimal_fields': [
'name'
Expand Down
39 changes: 34 additions & 5 deletions src/spaceone/config/service/domain_config_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, *args, **kwargs):
@transaction(append_meta={'authorization.scope': 'DOMAIN'})
@check_required(['name', 'data', 'domain_id'])
def create(self, params):
"""Create config map
"""Create domain config
Args:
params (dict): {
Expand All @@ -42,7 +42,7 @@ def create(self, params):
@transaction(append_meta={'authorization.scope': 'DOMAIN'})
@check_required(['name', 'domain_id'])
def update(self, params):
"""Update config map
"""Update domain config
Args:
params (dict): {
Expand All @@ -61,10 +61,39 @@ def update(self, params):

return self.domain_config_mgr.update_domain_config(params)

@transaction(append_meta={'authorization.scope': 'DOMAIN'})
@check_required(['name', 'data', 'domain_id'])
def set(self, params):
"""Set domain config (create or update)
Args:
params (dict): {
'name': 'str',
'data': 'dict',
'tags': 'dict',
'domain_id': 'str'
}
Returns:
domain_config_vo (object)
"""

domain_id = params['domain_id']

if 'tags' in params:
params['tags'] = utils.dict_to_tags(params['tags'])

domain_config_vos = self.domain_config_mgr.filter_domain_configs(domain_id=domain_id)

if domain_config_vos.count() == 0:
return self.domain_config_mgr.create_domain_config(params)
else:
return self.domain_config_mgr.update_domain_config_by_vo(params, domain_config_vos[0])

@transaction(append_meta={'authorization.scope': 'DOMAIN'})
@check_required(['name', 'domain_id'])
def delete(self, params):
"""Delete config map
"""Delete domain config
Args:
params (dict): {
Expand All @@ -81,7 +110,7 @@ def delete(self, params):
@transaction(append_meta={'authorization.scope': 'DOMAIN'})
@check_required(['name', 'domain_id'])
def get(self, params):
"""Get config map
"""Get domain config
Args:
params (dict): {
Expand All @@ -102,7 +131,7 @@ def get(self, params):
@change_tag_filter('tags')
@append_keyword_filter(['name'])
def list(self, params):
""" List config maps
""" List domain configs
Args:
params (dict): {
Expand Down
53 changes: 46 additions & 7 deletions src/spaceone/config/service/user_config_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from spaceone.core.service import *
from spaceone.core import utils
from spaceone.core.error import *
from spaceone.config.manager.user_config_manager import UserConfigManager

_LOGGER = logging.getLogger(__name__)
Expand All @@ -20,7 +21,7 @@ def __init__(self, *args, **kwargs):
@transaction(append_meta={'authorization.scope': 'USER'})
@check_required(['name', 'data', 'domain_id'])
def create(self, params):
"""Create config map
"""Create user config
Args:
params (dict): {
Expand All @@ -35,8 +36,10 @@ def create(self, params):
"""

user_type = self.transaction.get_meta('authorization.user_type')
if user_type != 'DOMAIN_OWNER':
params['user_id'] = self.transaction.get_meta('user_id')
if user_type == 'DOMAIN_OWNER':
raise ERROR_PERMISSION_DENIED()

params['user_id'] = self.transaction.get_meta('user_id')

if 'tags' in params:
params['tags'] = utils.dict_to_tags(params['tags'])
Expand All @@ -46,7 +49,7 @@ def create(self, params):
@transaction(append_meta={'authorization.scope': 'USER'})
@check_required(['name', 'domain_id'])
def update(self, params):
"""Update config map
"""Update user config
Args:
params (dict): {
Expand All @@ -65,10 +68,46 @@ def update(self, params):

return self.user_config_mgr.update_user_config(params)

@transaction(append_meta={'authorization.scope': 'USER'})
@check_required(['name', 'data', 'domain_id'])
def set(self, params):
"""Set user config (create or update)
Args:
params (dict): {
'name': 'str',
'data': 'dict',
'tags': 'dict',
'domain_id': 'str'
}
Returns:
user_config_vo (object)
"""

domain_id = params['domain_id']

user_type = self.transaction.get_meta('authorization.user_type')
if user_type == 'DOMAIN_OWNER':
raise ERROR_PERMISSION_DENIED()

params['user_id'] = self.transaction.get_meta('user_id')
user_id = params['user_id']

if 'tags' in params:
params['tags'] = utils.dict_to_tags(params['tags'])

user_config_vos = self.user_config_mgr.filter_user_configs(domain_id=domain_id, user_id=user_id)

if user_config_vos.count() == 0:
return self.user_config_mgr.create_user_config(params)
else:
return self.user_config_mgr.update_user_config_by_vo(params, user_config_vos[0])

@transaction(append_meta={'authorization.scope': 'USER'})
@check_required(['name', 'domain_id'])
def delete(self, params):
"""Delete config map
"""Delete user config
Args:
params (dict): {
Expand All @@ -85,7 +124,7 @@ def delete(self, params):
@transaction(append_meta={'authorization.scope': 'USER'})
@check_required(['name', 'domain_id'])
def get(self, params):
"""Get config map
"""Get user config
Args:
params (dict): {
Expand All @@ -109,7 +148,7 @@ def get(self, params):
@change_tag_filter('tags')
@append_keyword_filter(['name'])
def list(self, params):
""" List config maps
""" List user configs
Args:
params (dict): {
Expand Down
40 changes: 38 additions & 2 deletions test/api/test_domain_config_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ def update(self, params):

return DomainConfigFactory(**params)

def set(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):
pass

Expand Down Expand Up @@ -70,7 +77,6 @@ def test_create_domain_config(self, mock_parse_request, *args):
'data': {
'config_key': 'config_value'
},
'schema': utils.random_string(),
'tags': {
utils.random_string(): utils.random_string()
},
Expand All @@ -90,6 +96,7 @@ def test_create_domain_config(self, mock_parse_request, *args):
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))
self.assertIsNotNone(getattr(domain_config_info, 'updated_at', None))

@patch.object(BaseAPI, '__init__', return_value=None)
@patch.object(Locator, 'get_service', return_value=_MockDomainConfigService())
Expand All @@ -100,7 +107,6 @@ def test_update_domain_config(self, mock_parse_request, *args):
'data': {
'update_config_key': 'update_config_value'
},
'schema': utils.random_string(),
'tags': {
'update_key': 'update_value'
},
Expand All @@ -118,6 +124,36 @@ def test_update_domain_config(self, mock_parse_request, *args):
self.assertDictEqual(MessageToDict(domain_config_info.data), params['data'])
self.assertDictEqual(domain_config_data['tags'], params['tags'])

@patch.object(BaseAPI, '__init__', return_value=None)
@patch.object(Locator, 'get_service', return_value=_MockDomainConfigService())
@patch.object(BaseAPI, 'parse_request')
def test_set_domain_config(self, mock_parse_request, *args):
params = {
'name': utils.random_string(),
'data': {
'config_key': 'config_value'
},
'tags': {
utils.random_string(): utils.random_string()
},
'domain_id': utils.generate_id('domain')
}
mock_parse_request.return_value = (params, {})

domain_config_servicer = DomainConfig()
domain_config_info = domain_config_servicer.set({}, {})

print_message(domain_config_info, 'test_set_domain_config')
domain_config_data = MessageToDict(domain_config_info, preserving_proto_field_name=True)

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.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))
self.assertIsNotNone(getattr(domain_config_info, 'updated_at', None))

@patch.object(BaseAPI, '__init__', return_value=None)
@patch.object(Locator, 'get_service', return_value=_MockDomainConfigService())
@patch.object(BaseAPI, 'parse_request')
Expand Down
Loading

0 comments on commit 8689651

Please sign in to comment.