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

Add new feature PublicConfig resource #8

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/spaceone/config/interface/grpc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from spaceone.core.pygrpc.server import GRPCServer
from spaceone.config.interface.grpc.domain_config import DomainConfig
from spaceone.config.interface.grpc.workspace_config import WorkspaceConfig
from spaceone.config.interface.grpc.public_config import PublicConfig
from spaceone.config.interface.grpc.user_config import UserConfig

_all_ = ["app"]

app = GRPCServer()
app.add_service(DomainConfig)
app.add_service(WorkspaceConfig)
app.add_service(UserConfig)
app.add_service(PublicConfig)
51 changes: 51 additions & 0 deletions src/spaceone/config/interface/grpc/public_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from spaceone.api.config.v1 import public_config_pb2, public_config_pb2_grpc
from spaceone.core.pygrpc import BaseAPI

from spaceone.config.service.public_config_service import PublicConfigService


class PublicConfig(BaseAPI, public_config_pb2_grpc.PublicConfigServicer):
pb2 = public_config_pb2
pb2_grpc = public_config_pb2_grpc

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

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

def set(self, request, context):
params, metadata = self.parse_request(request, context)
public_config_svc = PublicConfigService(metadata)
response: dict = public_config_svc.set(params)
return self.dict_to_message(response)

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

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

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

def stat(self, request, context):
params, metadata = self.parse_request(request, context)
public_config_svc = PublicConfigService(metadata)
response: dict = public_config_svc.stat(params)
return self.dict_to_message(response)
53 changes: 0 additions & 53 deletions src/spaceone/config/interface/grpc/workspace_config.py

This file was deleted.

39 changes: 39 additions & 0 deletions src/spaceone/config/manager/identity_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import logging

from spaceone.core import cache
from spaceone.core import config
from spaceone.core.manager import BaseManager
from spaceone.core.connector.space_connector import SpaceConnector
from spaceone.core.auth.jwt.jwt_util import JWTUtil

_LOGGER = logging.getLogger(__name__)


class IdentityManager(BaseManager):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
token = self.transaction.get_meta("token")
self.token_type = JWTUtil.get_value_from_token(token, "typ")
self.identity_conn: SpaceConnector = self.locator.get_connector(
SpaceConnector, service="identity"
)

def check_workspace(self, workspace_id: str, domain_id: str) -> None:
system_token = config.get_global("TOKEN")

self.identity_conn.dispatch(
"Workspace.check",
{"workspace_id": workspace_id, "domain_id": domain_id},
token=system_token,
)

@cache.cacheable(key="inventory:project:{domain_id}:{project_id}", expire=600)
def get_project(self, project_id: str, domain_id: str):
if self.token_type == "SYSTEM_TOKEN":
return self.identity_conn.dispatch(
"Project.get", {"project_id": project_id}, x_domain_id=domain_id
)
else:
return self.identity_conn.dispatch(
"Project.get", {"project_id": project_id}
)
66 changes: 66 additions & 0 deletions src/spaceone/config/manager/public_config_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import logging
from typing import Tuple, Union

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

from spaceone.config.model.public_config.database import PublicConfig

_LOGGER = logging.getLogger(__name__)


class PublicConfigManager(BaseManager):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.public_config_model = PublicConfig

def create_public_config(self, params: dict) -> PublicConfig:
def _rollback(vo: PublicConfig) -> None:
_LOGGER.info(
f"[create_public_config._rollback] " f"Delete public config : {vo.name}"
)
vo.delete()

public_config_vo: PublicConfig = self.public_config_model.create(params)
self.transaction.add_rollback(_rollback, public_config_vo)

return public_config_vo

def update_public_config_by_vo(
self, params: dict, public_config_vo: PublicConfig
) -> PublicConfig:
def _rollback(old_data: dict):
_LOGGER.info(
f'[update_public_config_by_vo._rollback] Revert Data : {old_data["name"]}'
)
public_config_vo.update(old_data)

self.transaction.add_rollback(_rollback, public_config_vo.to_dict())

return public_config_vo.update(params)

@staticmethod
def delete_public_config_by_vo(public_config_vo: PublicConfig) -> None:
public_config_vo.delete()

def get_public_config(self, name: str, domain_id: str, workspace_id: Union[list, str, None],
project_id: Union[list, str, None]) -> PublicConfig:
conditions = {
"name": name,
"domain_id": domain_id,
}
if workspace_id:
conditions["workspace_id"] = workspace_id
if project_id:
conditions["project_id"] = project_id

return self.public_config_model.get(**conditions)

def filter_public_configs(self, **conditions):
return self.public_config_model.filter(**conditions)

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

def stat_public_configs(self, query: dict) -> dict:
return self.public_config_model.stat(**query)
56 changes: 0 additions & 56 deletions src/spaceone/config/manager/workspace_config_manager.py

This file was deleted.

2 changes: 1 addition & 1 deletion src/spaceone/config/model/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from spaceone.config.model.user_config_model import UserConfig
from spaceone.config.model.workspace_config.database import WorkspaceConfig
from spaceone.config.model.domain_config_model import DomainConfig
from spaceone.config.model.public_config.database import PublicConfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,25 @@
from spaceone.core.model.mongo_model import MongoModel


class WorkspaceConfig(MongoModel):
name = StringField(max_length=255, unique_with=["domain_id", "workspace_id"])
class PublicConfig(MongoModel):
name = StringField(max_length=255, unique_with=["domain_id"])
data = DictField()
tags = DictField()
resource_group = StringField(max_length=40, choices=("DOMAIN", "WORKSPACE", "PROJECT"))
project_id = StringField(max_length=40)
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", "data", "tags", "updated_at"],
"minimal_fields": ["name"],
"minimal_fields": ["name", "resource_group"],
"change_query_keys": {"user_projects": "project_id"},
"ordering": ["name"],
"indexes": [
"name",
"workspace_id",
"domain_id",
"workspace_id",
],
}
76 changes: 76 additions & 0 deletions src/spaceone/config/model/public_config/request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from typing import Union, Literal
from pydantic import BaseModel

__all__ = [
"PublicConfigCreateRequest",
"PublicConfigUpdateRequest",
"PublicConfigSetRequest",
"PublicConfigDeleteRequest",
"PublicConfigGetRequest",
"PublicConfigSearchQueryRequest",
"PublicConfigQueryRequest",
]

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


class PublicConfigCreateRequest(BaseModel):
name: str
data: dict
tags: Union[dict, None] = None
resource_group: ResourceGroup
user_projects: Union[list, None] = None
project_id: Union[list, str, None] = None
workspace_id: Union[list, str, None] = None
domain_id: str


class PublicConfigUpdateRequest(BaseModel):
name: str
data: Union[dict, None] = None
tags: Union[dict, None] = None
user_projects: Union[list, None] = None
project_id: Union[str, None] = None
workspace_id: Union[str, None] = None
domain_id: str


class PublicConfigSetRequest(BaseModel):
name: str
data: str
tags: Union[dict, None] = None
user_projects: Union[list, None] = None
project_id: Union[str, None] = None
workspace_id: Union[str, None] = None
domain_id: str


class PublicConfigDeleteRequest(BaseModel):
name: str
user_projects: Union[list, None] = None
project_id: Union[str, None] = None
workspace_id: Union[str, None] = None
domain_id: str


class PublicConfigGetRequest(BaseModel):
name: str
user_projects: Union[list, None] = None
project_id: Union[list, str, None] = None
workspace_id: Union[list, str, None] = None
domain_id: str


class PublicConfigSearchQueryRequest(BaseModel):
query: Union[dict, None] = None
name: Union[str, None] = None
user_project: Union[list, None] = None
project_id: Union[list, str, None] = None
workspace_id: Union[list, str, None] = None
domain_id: str


class PublicConfigQueryRequest(BaseModel):
query: dict
workspace_id: str
domain_id: str
Loading
Loading