Skip to content

Commit

Permalink
feat: implement package APIs
Browse files Browse the repository at this point in the history
Signed-off-by: Jongmin Kim <[email protected]>
  • Loading branch information
whdalsrnt committed Nov 20, 2024
1 parent 678358b commit d58bac6
Show file tree
Hide file tree
Showing 16 changed files with 615 additions and 20 deletions.
6 changes: 6 additions & 0 deletions src/spaceone/identity/error/error_workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@

class ERROR_WORKSPACE_STATE(ERROR_INVALID_ARGUMENT):
_message = "Workspace is disabled. (workspace_id = {workspace_id})"


class ERROR_DEFAULT_PACKAGE_NOT_ALLOWED(ERROR_INVALID_ARGUMENT):
_message = (
"Default package can not be added to workspace. (package_id = {package_id})"
)
5 changes: 3 additions & 2 deletions src/spaceone/identity/interface/grpc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from spaceone.identity.interface.grpc.project import Project
from spaceone.identity.interface.grpc.project_group import ProjectGroup
from spaceone.identity.interface.grpc.provider import Provider
from spaceone.identity.interface.grpc.package import Package
from spaceone.identity.interface.grpc.role import Role
from spaceone.identity.interface.grpc.role_binding import RoleBinding
from spaceone.identity.interface.grpc.schema import Schema
Expand All @@ -21,8 +22,7 @@
from spaceone.identity.interface.grpc.user_profile import UserProfile
from spaceone.identity.interface.grpc.workspace import Workspace
from spaceone.identity.interface.grpc.workspace_group import WorkspaceGroup
from spaceone.identity.interface.grpc.workspace_group_user import \
WorkspaceGroupUser
from spaceone.identity.interface.grpc.workspace_group_user import WorkspaceGroupUser
from spaceone.identity.interface.grpc.workspace_user import WorkspaceUser

_all_ = ["app"]
Expand All @@ -36,6 +36,7 @@
app.add_service(ProjectGroup)
app.add_service(Project)
app.add_service(Provider)
app.add_service(Package)
app.add_service(Schema)
app.add_service(TrustedAccount)
app.add_service(ServiceAccount)
Expand Down
56 changes: 56 additions & 0 deletions src/spaceone/identity/interface/grpc/package.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from spaceone.core.pygrpc import BaseAPI
from spaceone.api.identity.v2 import package_pb2, package_pb2_grpc
from spaceone.identity.service.package_service import PackageService


class Package(BaseAPI, package_pb2_grpc.PackageServicer):
pb2 = package_pb2
pb2_grpc = package_pb2_grpc

def create(self, request, context):
params, metadata = self.parse_request(request, context)
package_svc = PackageService(metadata)
response: dict = package_svc.create(params)
return self.dict_to_message(response)

def update(self, request, context):
params, metadata = self.parse_request(request, context)
package_svc = PackageService(metadata)
response: dict = package_svc.update(params)
return self.dict_to_message(response)

def delete(self, request, context):
params, metadata = self.parse_request(request, context)
package_svc = PackageService(metadata)
package_svc.delete(params)
return self.empty()

def set_default(self, request, context):
params, metadata = self.parse_request(request, context)
package_svc = PackageService(metadata)
response: dict = package_svc.set_default(params)
return self.dict_to_message(response)

def change_order(self, request, context):
params, metadata = self.parse_request(request, context)
package_svc = PackageService(metadata)
response: dict = package_svc.change_order(params)
return self.dict_to_message(response)

def get(self, request, context):
params, metadata = self.parse_request(request, context)
package_svc = PackageService(metadata)
response: dict = package_svc.get(params)
return self.dict_to_message(response)

def list(self, request, context):
params, metadata = self.parse_request(request, context)
package_svc = PackageService(metadata)
response: dict = package_svc.list(params)
return self.dict_to_message(response)

def stat(self, request, context):
params, metadata = self.parse_request(request, context)
package_svc = PackageService(metadata)
response: dict = package_svc.stat(params)
return self.dict_to_message(response)
12 changes: 12 additions & 0 deletions src/spaceone/identity/interface/grpc/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ def disable(self, request, context):
response: dict = workspace_svc.disable(params)
return self.dict_to_message(response)

def add_package(self, request, context):
params, metadata = self.parse_request(request, context)
workspace_svc = WorkspaceService(metadata)
response: dict = workspace_svc.add_package(params)
return self.dict_to_message(response)

def remove_package(self, request, context):
params, metadata = self.parse_request(request, context)
workspace_svc = WorkspaceService(metadata)
response: dict = workspace_svc.remove_package(params)
return self.dict_to_message(response)

def get(self, request, context):
params, metadata = self.parse_request(request, context)
workspace_svc = WorkspaceService(metadata)
Expand Down
54 changes: 54 additions & 0 deletions src/spaceone/identity/manager/package_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import logging
from typing import Tuple

from mongoengine import QuerySet
from spaceone.core.manager import BaseManager

from spaceone.identity.model.package.database import Package

_LOGGER = logging.getLogger(__name__)


class PackageManager(BaseManager):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.package_model = Package

def create_package(self, params: dict) -> Package:
def _rollback(vo: Package):
_LOGGER.info(
f"[create_package._rollback] Delete package: {vo.package_id} ({vo.name})"
)
vo.delete()

package_vo = self.package_model.create(params)

self.transaction.add_rollback(_rollback, package_vo)

return package_vo

def update_package_by_vo(self, params: dict, package_vo: Package) -> Package:
def _rollback(old_data):
_LOGGER.info(
f'[update_package_by_vo._rollback] Revert Data: {old_data["package_id"]}'
)
package_vo.update(old_data)

self.transaction.add_rollback(_rollback, package_vo.to_dict())
return package_vo.update(params)

@staticmethod
def delete_package_by_vo(package_vo: Package) -> None:
package_vo.delete()

def get_package(self, package_id: str, domain_id: str) -> Package:
return self.package_model.get(package_id=package_id, domain_id=domain_id)

def filter_packages(self, **conditions) -> QuerySet:
return self.package_model.filter(**conditions)

def list_packages(self, query: dict) -> Tuple[QuerySet, int]:
return self.package_model.query(**query)

def stat_package(self, query: dict) -> dict:
return self.package_model.stat(**query)
2 changes: 1 addition & 1 deletion src/spaceone/identity/manager/user_group_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def delete_user_group_by_vo(user_group_vo: UserGroup) -> None:

def get_user_group(
self, user_group_id: str, domain_id: str, workspace_id: str = None
):
) -> UserGroup:
conditions = {"user_group_id": user_group_id, "domain_id": domain_id}

if workspace_id:
Expand Down
Empty file.
29 changes: 29 additions & 0 deletions src/spaceone/identity/model/package/database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from mongoengine import *
from spaceone.core.model.mongo_model import MongoModel


class Package(MongoModel):
package_id = StringField(max_length=40, generate_id="p", unique=True)
name = StringField(max_length=255, unique_with=["domain_id"])
description = StringField(max_length=255, default=None, null=True)
order = IntField(required=True)
is_default = BooleanField(default=False)
tags = DictField(Default=None)
domain_id = StringField(max_length=40)
created_at = DateTimeField(auto_now_add=True)
updated_at = DateTimeField(auto_now=True)

meta = {
"updatable_fields": ["name", "description", "order", "is_default", "tags"],
"minimal_fields": [
"package_id",
"name",
"is_default",
"domain_id",
],
"ordering": ["order"],
"indexes": [
"name",
"domain_id",
],
}
61 changes: 61 additions & 0 deletions src/spaceone/identity/model/package/request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from typing import Union, List
from pydantic import BaseModel

__all__ = [
"PackageCreateRequest",
"PackageUpdateRequest",
"PackageDeleteRequest",
"PackageSetDefaultRequest",
"PackageChangeOrderRequest",
"PackageGetRequest",
"PackageSearchQueryRequest",
"PackageStatQueryRequest",
]


class PackageCreateRequest(BaseModel):
name: str
description: Union[str, None] = None
tags: Union[dict, None] = None
domain_id: str


class PackageUpdateRequest(BaseModel):
package_id: str
name: Union[str, None] = None
description: Union[str, None] = None
tags: Union[dict, None] = None
domain_id: str


class PackageDeleteRequest(BaseModel):
package_id: str
domain_id: str


class PackageSetDefaultRequest(BaseModel):
package_id: str
domain_id: str


class PackageChangeOrderRequest(BaseModel):
package_id: str
order: int
domain_id: str


class PackageGetRequest(BaseModel):
package_id: str
domain_id: str


class PackageSearchQueryRequest(BaseModel):
query: Union[dict, None] = None
package_id: Union[str, None] = None
name: Union[str, None] = None
domain_id: str


class PackageStatQueryRequest(BaseModel):
query: dict
domain_id: str
29 changes: 29 additions & 0 deletions src/spaceone/identity/model/package/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__ = ["PackageResponse", "PackagesResponse"]


class PackageResponse(BaseModel):
package_id: Union[str, None] = None
name: Union[str, None] = None
description: Union[str, None] = None
order: Union[int, None] = None
is_default: Union[bool, 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 PackagesResponse(BaseModel):
results: List[PackageResponse]
total_count: int
3 changes: 3 additions & 0 deletions src/spaceone/identity/model/workspace/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Workspace(MongoModel):
created_by = StringField(max_length=255)
references = ListField(StringField(max_length=255), default=None, null=True)
is_managed = BooleanField(default=False)
packages = ListField(StringField(max_length=40), default=[])

is_dormant = BooleanField(default=False)
dormant_ttl = IntField(default=None, required=True)
Expand All @@ -37,6 +38,7 @@ class Workspace(MongoModel):
"state",
"tags",
"is_managed",
"packages",
"is_dormant",
"dormant_ttl",
"service_account_count",
Expand Down Expand Up @@ -66,6 +68,7 @@ class Workspace(MongoModel):
"indexes": [
"name",
"state",
"packages",
"domain_id",
"created_by",
"is_managed",
Expand Down
14 changes: 14 additions & 0 deletions src/spaceone/identity/model/workspace/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"WorkspaceDeleteRequest",
"WorkspaceEnableRequest",
"WorkspaceDisableRequest",
"WorkspaceAddPackageRequest",
"WorkspaceRemovePackageRequest",
"WorkspaceGetRequest",
"WorkspaceCheckRequest",
"WorkspaceSearchQueryRequest",
Expand Down Expand Up @@ -54,6 +56,18 @@ class WorkspaceDisableRequest(BaseModel):
domain_id: str


class WorkspaceAddPackageRequest(BaseModel):
package_id: str
workspace_id: str
domain_id: str


class WorkspaceRemovePackageRequest(BaseModel):
package_id: str
workspace_id: str
domain_id: str


class WorkspaceGetRequest(BaseModel):
workspace_id: str
domain_id: str
Expand Down
1 change: 1 addition & 0 deletions src/spaceone/identity/model/workspace/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class WorkspaceResponse(BaseModel):
created_by: Union[str, None] = None
references: Union[list, None] = None
is_managed: Union[bool, None] = None
packages: Union[list, None] = None
is_dormant: Union[bool, None] = None
dormant_ttl: Union[int, None] = None
service_account_count: Union[int, None] = None
Expand Down
Loading

0 comments on commit d58bac6

Please sign in to comment.