Skip to content

Commit

Permalink
refactor: Update transaction roles and refactor create, update, delet…
Browse files Browse the repository at this point in the history
…e method in Region Service

Signed-off-by: lhhyung <[email protected]>
  • Loading branch information
lhhyung committed Dec 10, 2024
1 parent d50cc34 commit a3659c0
Showing 1 changed file with 29 additions and 36 deletions.
65 changes: 29 additions & 36 deletions src/spaceone/inventory_v2/service/region_service.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import logging
from typing import Union

from mongoengine import QuerySet
from spaceone.core import utils
from spaceone.core.error import *
from spaceone.core.service import *

from spaceone.inventory_v2.manager.region_manager import RegionManager
from spaceone.inventory_v2.manager.identity_manager import IdentityManager
from spaceone.inventory_v2.model import Region
from spaceone.inventory_v2.model.region.request import *
from spaceone.inventory_v2.model.region.response import *
from spaceone.inventory_v2.model.region.database import Region

_LOGGER = logging.getLogger(__name__)
_KEYWORD_FILTER = ["region_id", "name", "region_code"]
Expand All @@ -29,8 +27,9 @@ def __init__(self, metadata):

@transaction(
permission="inventory-v2:Region.write",
role_types=["DOMAIN_ADMIN"],
role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER"],
)
@convert_model
def create(self, params: RegionCreateRequest) -> Union[RegionResponse, dict]:
"""
Args:
Expand All @@ -47,19 +46,18 @@ def create(self, params: RegionCreateRequest) -> Union[RegionResponse, dict]:
Returns:
RegionResponse:
"""
region_vo = self.create_resource(params.dict())

return self.create_resource(params)
# return self.create_resource(params)
return RegionResponse(**region_vo.to_dict())

@convert_model
def create_resource(
self, params: RegionCreateRequest
) -> Union[RegionResponse, dict]:
def create_resource(self, params: dict) -> Region:

identity_mgr = IdentityManager()

domain_id = params.domain_id
workspace_id = params.workspace_id
resource_group = params.resource_group
domain_id = params["domain_id"]
workspace_id = params.get("workspace_id")
resource_group = params["resource_group"]

# Check permission by resource group
if resource_group == "WORKSPACE":
Expand All @@ -68,21 +66,20 @@ def create_resource(

identity_mgr.check_workspace(workspace_id, domain_id)
else:
params.workspace_id = "*"
params["workspace_id"] = "*"

region_id = f"{params.provider}-{params.region_code}"
region_id = f'{params["provider"]}-{params["region_code"]}'

params_data = params.dict()
params_data["region_id"] = region_id
params["region_id"] = region_id

region_vo = self.region_mgr.create_region(params_data)

return RegionResponse(**region_vo.to_dict())
return self.region_mgr.create_region(params)

@transaction(
permission="inventory-v2:Region.write",
role_types=["DOMAIN_ADMIN"],
role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER"],
)
@change_value_by_rule("APPEND", "workspace_id", "*")
@convert_model
def update(self, params: RegionUpdateRequest) -> Union[RegionResponse, dict]:
"""
Args:
Expand All @@ -97,27 +94,24 @@ def update(self, params: RegionUpdateRequest) -> Union[RegionResponse, dict]:
region_vo (object)
"""

return self.update_resource(params)
region_vo = self.update_resource(params.dict(exclude_unset=True))

@convert_model
def update_resource(
self, params: RegionUpdateRequest
) -> Union[RegionResponse, dict]:
return RegionResponse(**region_vo.to_dict())

region_vo = self.region_mgr.get_region(
params.region_id, params.domain_id, params.workspace_id
)
def update_resource(self, params: dict) -> Region:

region_vo = self.region_mgr.update_region_by_vo(
params.dict(exclude_unset=True), region_vo
region_vo = self.region_mgr.get_region(
params["region_id"], params["domain_id"], params.get("workspace_id")
)

return RegionResponse(**region_vo.to_dict())
return self.region_mgr.update_region_by_vo(params, region_vo)

@transaction(
permission="inventory-v2:Region.write",
role_types=["DOMAIN_ADMIN"],
role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER"],
)
@change_value_by_rule("APPEND", "workspace_id", "*")
@convert_model
def delete(self, params: RegionDeleteRequest) -> None:
"""
Args:
Expand All @@ -130,12 +124,11 @@ def delete(self, params: RegionDeleteRequest) -> None:
None
"""

self.delete_resource(params)
self.delete_resource(params.dict())

@convert_model
def delete_resource(self, params: RegionDeleteRequest) -> None:
def delete_resource(self, params: dict) -> None:
region_vo = self.region_mgr.get_region(
params.region_id, params.domain_id, params.workspace_id
params["region_id"], params["domain_id"], params.get("workspace_id")
)
self.region_mgr.delete_region_by_vo(region_vo)

Expand Down

0 comments on commit a3659c0

Please sign in to comment.