Skip to content

Commit

Permalink
Merge pull request #12 from lhhyung/master
Browse files Browse the repository at this point in the history
Refactor Region service
  • Loading branch information
lhhyung authored Dec 11, 2024
2 parents 409d362 + 0954939 commit 3edf832
Show file tree
Hide file tree
Showing 15 changed files with 244 additions and 201 deletions.
2 changes: 1 addition & 1 deletion src/spaceone/inventory_v2/error/region.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ class ERROR_NOT_FOUND_USER_IN_REGION(ERROR_BASE):


class ERROR_ALREADY_EXIST_USER_IN_REGION(ERROR_BASE):
_message = 'A user "{user_id}" is already exist in region ({region_id}).'
_message = 'A user "{user_id}" is already exist in region ({region_id}).'
2 changes: 0 additions & 2 deletions src/spaceone/inventory_v2/info/__init__.py

This file was deleted.

20 changes: 0 additions & 20 deletions src/spaceone/inventory_v2/info/common_info.py

This file was deleted.

39 changes: 0 additions & 39 deletions src/spaceone/inventory_v2/info/region_info.py

This file was deleted.

44 changes: 22 additions & 22 deletions src/spaceone/inventory_v2/interface/grpc/region.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
from spaceone.api.inventory.v2 import region_pb2, region_pb2_grpc
from spaceone.api.inventory_v2.v1 import region_pb2, region_pb2_grpc
from spaceone.core.pygrpc import BaseAPI

class Region(BaseAPI, region_pb2_grpc.RegionServicer):
from spaceone.inventory_v2.service.region_service import RegionService


class Region(BaseAPI, region_pb2_grpc.RegionServicer):
pb2 = region_pb2
pb2_grpc = region_pb2_grpc

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

with self.locator.get_service('RegionService', metadata) as region_service:
return self.locator.get_info('RegionInfo', region_service.create(params))
region_svc = RegionService(metadata)
response: dict = region_svc.create(params)
return self.dict_to_message(response)

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

with self.locator.get_service('RegionService', metadata) as region_service:
return self.locator.get_info('RegionInfo', region_service.update(params))
region_svc = RegionService(metadata)
response: dict = region_svc.update(params)
return self.dict_to_message(response)

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

with self.locator.get_service('RegionService', metadata) as region_service:
region_service.delete(params)
return self.locator.get_info('EmptyInfo')
region_svc = RegionService(metadata)
region_svc.delete(params)
return self.empty()

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

with self.locator.get_service('RegionService', metadata) as region_service:
return self.locator.get_info('RegionInfo', region_service.get(params))
region_svc = RegionService(metadata)
response: dict = region_svc.get(params)
return self.dict_to_message(response)

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

with self.locator.get_service('RegionService', metadata) as region_service:
region_vos, total_count = region_service.list(params)
return self.locator.get_info('RegionsInfo', region_vos, total_count, minimal=self.get_minimal(params))
region_svc = RegionService(metadata)
response: dict = region_svc.list(params)
return self.dict_to_message(response)

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

with self.locator.get_service('RegionService', metadata) as region_service:
return self.locator.get_info('StatisticsInfo', region_service.stat(params))
region_svc = RegionService(metadata)
response: dict = region_svc.stat(params)
return self.dict_to_message(response)
25 changes: 14 additions & 11 deletions src/spaceone/inventory_v2/manager/region_manager.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import logging
from typing import Tuple
from typing import Tuple, Union

from spaceone.core.model.mongo_model import QuerySet
from spaceone.core.manager import BaseManager
from spaceone.core.model.mongo_model import QuerySet
from spaceone.inventory_v2.model.region.database import Region
from spaceone.inventory_v2.lib.resource_manager import ResourceManager
from spaceone.inventory_v2.model.region.region_model import Region

__ALL__ = ["RegionManager"]

_LOGGER = logging.getLogger(__name__)


class RegionManager(BaseManager, ResourceManager):
resource_keys = ["region_id"]
query_method = "list_regions"

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.region_model: Region = self.locator.get_model("Region")
self.region_model = Region()

def create_region(self, params: dict) -> Region:
def _rollback(vo: Region):
Expand All @@ -24,7 +23,6 @@ def _rollback(vo: Region):

region_vo: Region = self.region_model.create(params)
self.transaction.add_rollback(_rollback, region_vo)

return region_vo

def update_region_by_vo(self, params: dict, region_vo: Region) -> Region:
Expand All @@ -41,11 +39,16 @@ def _rollback(old_data):
def delete_region_by_vo(region_vo: Region) -> None:
region_vo.delete()

def get_region(self, region_id: str, domain_id: str) -> Region:
def get_region(
self,
region_id: str,
domain_id: str,
workspace_id: Union[list, str, None] = None,
) -> Region:
conditions = {"region_id": region_id, "domain_id": domain_id}

# if workspace_id:
# conditions.update({"workspace_id": workspace_id})
if workspace_id:
conditions.update({"workspace_id": workspace_id})

return self.region_model.get(**conditions)

Expand Down
2 changes: 1 addition & 1 deletion src/spaceone/inventory_v2/model/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from spaceone.inventory_v2.model.asset.database import Asset
from spaceone.inventory_v2.model.asset_type.database import AssetType
from spaceone.inventory_v2.model.region.region_model import Region
from spaceone.inventory_v2.model.region.database import Region
from spaceone.inventory_v2.model.collector.database import Collector
from spaceone.inventory_v2.model.collector_rule.database import CollectorRule
from spaceone.inventory_v2.model.collection_state.database import CollectionState
Expand Down
2 changes: 1 addition & 1 deletion src/spaceone/inventory_v2/model/asset/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from spaceone.inventory_v2.model.asset_type.database import AssetType

from spaceone.inventory_v2.error.asset import ERROR_RESOURCE_ALREADY_DELETED
from spaceone.inventory_v2.model.region.region_model import Region
from spaceone.inventory_v2.model.region.database import Region


class Asset(MongoModel):
Expand Down
2 changes: 1 addition & 1 deletion src/spaceone/inventory_v2/model/collector/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class CollectorGetRequest(BaseModel):
class CollectorSearchQueryRequest(BaseModel):
query: Union[dict, None] = None
collector_id: Union[str, None] = None
workspace_id: Union[str, list, None] = None
workspace_id: Union[list, str, None] = None
domain_id: str


Expand Down
28 changes: 28 additions & 0 deletions src/spaceone/inventory_v2/model/region/database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from mongoengine import *

from spaceone.core.model.mongo_model import MongoModel


class Region(MongoModel):
region_id = StringField(max_length=40, unique=True)
name = StringField(max_length=255)
region_code = StringField(max_length=255)
provider = StringField(max_length=255)
tags = DictField()
resource_group = StringField(max_length=40, choices=("DOMAIN", "WORKSPACE"))
workspace_id = StringField(max_length=40)
domain_id = StringField(max_length=40)
created_at = DateTimeField(auto_now_add=True)
updated_at = DateTimeField(auto_now=True)

meta = {
"updatable_fields": ["name", "tags", "updated_at"],
"minimal_fields": ["region_id", "name", "region_code", "provider"],
"ordering": ["name"],
"indexes": [
"provider",
"resource_group",
"workspace_id",
"domain_id",
],
}
49 changes: 0 additions & 49 deletions src/spaceone/inventory_v2/model/region/region_model.py

This file was deleted.

60 changes: 60 additions & 0 deletions src/spaceone/inventory_v2/model/region/request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from typing import Union, Literal
from pydantic import BaseModel

__all__ = [
"RegionCreateRequest",
"RegionUpdateRequest",
"RegionDeleteRequest",
"RegionGetRequest",
"RegionSearchQueryRequest",
"RegionStatQueryRequest",
]

ResourceGroup = Literal["DOMAIN", "WORKSPACE"]


class RegionCreateRequest(BaseModel):
name: str
region_code: str
provider: str
tags: Union[dict, None] = None
resource_group: ResourceGroup
workspace_id: Union[str, None] = None
domain_id: str


class RegionUpdateRequest(BaseModel):
region_id: str
name: Union[str, None] = None
tags: Union[dict, None] = None
workspace_id: Union[str, None] = None
domain_id: str


class RegionDeleteRequest(BaseModel):
region_id: str
workspace_id: Union[str, None] = None
domain_id: str


class RegionGetRequest(BaseModel):
region_id: str
workspace_id: Union[list, str, None] = None
domain_id: str


class RegionSearchQueryRequest(BaseModel):
query: Union[dict, None] = None
region_id: Union[str, None] = None
name: Union[str, None] = None
region_code: Union[str, None] = None
provider: Union[str, None] = None
exists_only: Union[bool, None] = None
workspace_id: Union[list, str, None] = None
domain_id: str


class RegionStatQueryRequest(BaseModel):
query: dict
workspace_id: Union[list, str, None] = None
domain_id: str
29 changes: 29 additions & 0 deletions src/spaceone/inventory_v2/model/region/response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from datetime import datetime
from typing import Union, List
from pydantic import BaseModel

from spaceone.core import utils

__all__ = ["RegionResponse", "RegionsResponse"]


class RegionResponse(BaseModel):
region_id: Union[str, None] = None
name: Union[str, None] = None
region_code: Union[str, None] = None
provider: Union[str, None] = None
tags: Union[dict, None] = None
domain_id: Union[str, None] = None
created_at: Union[datetime, None] = None
updated_at: Union[datetime, None] = None

def dict(self, *args, **kwargs):
data = super().dict(*args, **kwargs)
data["created_at"] = utils.datetime_to_iso8601(data["created_at"])
data["updated_at"] = utils.datetime_to_iso8601(data["updated_at"])
return data


class RegionsResponse(BaseModel):
results: List[RegionResponse]
total_count: int
Loading

0 comments on commit 3edf832

Please sign in to comment.