Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement Identity Service #408

Merged
merged 8 commits into from
Nov 20, 2024
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
10 changes: 5 additions & 5 deletions src/spaceone/identity/interface/task/v1/dormancy_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def _init_config(self):
self._token = config.get_global("TOKEN")
if self._token is None:
raise ERROR_CONFIGURATION(key="TOKEN")
self._dormancy_check_hours = config.get_global("DORMANCY_CHECK_HOUR", 0)
self._dormancy_check_hour = config.get_global("DORMANCY_CHECK_HOUR", 0)

def create_task(self) -> list:
tasks = []
Expand All @@ -29,7 +29,7 @@ def create_task(self) -> list:

def _create_dormancy_task(self):
current_hour = datetime.utcnow().hour
if current_hour == self._dormancy_check_hours:
if current_hour == self._dormancy_check_hour:
stp = {
"name": "dormancy_schedule",
"version": "v1",
Expand All @@ -45,14 +45,14 @@ def _create_dormancy_task(self):
],
}
print(
f"{utils.datetime_to_iso8601(datetime.utcnow())} [INFO] [create_task] check_dormancy_by_domains => START"
f"{utils.datetime_to_iso8601(datetime.utcnow())} [INFO] [create_task] check_dormancy => START"
)
return [stp]
else:
print(
f"{utils.datetime_to_iso8601(datetime.utcnow())} [INFO] [create_task] check_dormancy_by_domains => SKIP"
f"{utils.datetime_to_iso8601(datetime.utcnow())} [INFO] [create_task] check_dormancy => SKIP"
)
print(
f"{utils.datetime_to_iso8601(datetime.utcnow())} [INFO] [create_task] check_dormancy_by_domains: {self._dormancy_check_hours} hour (UTC)"
f"{utils.datetime_to_iso8601(datetime.utcnow())} [INFO] [create_task] check_dormancy_by: {self._dormancy_check_hour} hour (UTC)"
)
return []
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
5 changes: 2 additions & 3 deletions src/spaceone/identity/manager/user_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
from spaceone.identity.manager.project_manager import ProjectManager
from spaceone.identity.manager.role_binding_manager import RoleBindingManager
from spaceone.identity.manager.user_group_manager import UserGroupManager
from spaceone.identity.manager.workspace_group_manager import \
WorkspaceGroupManager
from spaceone.identity.manager.workspace_group_manager import WorkspaceGroupManager
from spaceone.identity.model.user.database import User

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -108,7 +107,7 @@ def delete_user_by_vo(user_vo: User) -> None:
for rb_vo in rb_vos:
rb_mgr.delete_role_binding_by_vo(rb_vo)

# Delete user groups
# Delete user from user groups
user_group_vos = user_group_mgr.filter_user_groups(
users=user_vo.user_id, domain_id=user_vo.domain_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
10 changes: 7 additions & 3 deletions src/spaceone/identity/model/user_group/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,27 @@
class UserGroup(MongoModel):
user_group_id = StringField(max_length=40, generate_id="ug", unique=True)
name = StringField(max_length=255, unique_with=["domain_id", "workspace_id"])
description = StringField(max_length=255, default=None, null=True)
users = ListField(StringField(max_length=40), default=[])
tags = DictField(Default=None)
workspace_id = StringField(max_length=40, required=True)
domain_id = StringField(max_length=40)
created_at = DateTimeField(auto_now_add=True)

meta = {
"updatable_fields": ["name", "users", "tags"],
"updatable_fields": ["name", "description", "users", "tags"],
"minimal_fields": [
"user_group_id",
"name",
"users",
"workspace_id",
"domain_id",
],
"ordering": ["name", "user_group_id"],
"change_query_keys": {"user_id": "users"},
"ordering": ["name"],
"indexes": [
"name",
"workspace_id",
"domain_id",
"users",
],
}
2 changes: 2 additions & 0 deletions src/spaceone/identity/model/user_group/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

class UserGroupCreateRequest(BaseModel):
name: str
description: Union[str, None] = None
tags: Union[dict, None] = None
workspace_id: str
domain_id: str
Expand All @@ -23,6 +24,7 @@ class UserGroupCreateRequest(BaseModel):
class UserGroupUpdateRequest(BaseModel):
user_group_id: str
name: Union[str, None] = None
description: Union[str, None] = None
tags: Union[dict, None] = None
workspace_id: str
domain_id: str
Expand Down
Loading
Loading