Skip to content

Commit

Permalink
Merge pull request #252 from yjinjo/master
Browse files Browse the repository at this point in the history
Add Agent resource and modify Service Account resource
  • Loading branch information
yjinjo authored Apr 11, 2024
2 parents 31ec8b4 + b6f5d00 commit 6ba5fa3
Show file tree
Hide file tree
Showing 18 changed files with 693 additions and 345 deletions.
2 changes: 2 additions & 0 deletions src/spaceone/identity/interface/grpc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from spaceone.identity.interface.grpc.user_group import UserGroup
from spaceone.identity.interface.grpc.workspace import Workspace
from spaceone.identity.interface.grpc.workspace_user import WorkspaceUser
from spaceone.identity.interface.grpc.agent import Agent

_all_ = ["app"]

Expand All @@ -43,3 +44,4 @@
app.add_service(UserGroup)
app.add_service(App)
app.add_service(Token)
app.add_service(Agent)
52 changes: 52 additions & 0 deletions src/spaceone/identity/interface/grpc/agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from spaceone.core.pygrpc import BaseAPI
from spaceone.api.identity.v2 import agent_pb2, agent_pb2_grpc
from spaceone.identity.service.agent_service import (
AgentService,
)


class Agent(BaseAPI, agent_pb2_grpc.AgentServicer):
pb2 = agent_pb2
pb2_grpc = agent_pb2_grpc

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

def enable(self, request, context):
params, metadata = self.parse_request(request, context)
agent_svc = AgentService(metadata)
response: dict = agent_svc.enable(params)
return self.dict_to_message(response)

def disable(self, request, context):
params, metadata = self.parse_request(request, context)
agent_svc = AgentService(metadata)
response: dict = agent_svc.disable(params)
return self.dict_to_message(response)

def regenerate(self, request, context):
params, metadata = self.parse_request(request, context)
agent_svc = AgentService(metadata)
response: dict = agent_svc.regenerate(params)
return self.dict_to_message(response)

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

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

def list(self, request, context):
params, metadata = self.parse_request(request, context)
agent_svc = AgentService(metadata)
response: dict = agent_svc.list(params)
return self.dict_to_message(response)
36 changes: 5 additions & 31 deletions src/spaceone/identity/interface/grpc/service_account.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from google.protobuf.json_format import ParseDict
from spaceone.core.pygrpc import BaseAPI
from spaceone.api.identity.v2 import service_account_pb2, service_account_pb2_grpc, app_pb2
from spaceone.api.identity.v2 import (
service_account_pb2,
service_account_pb2_grpc,
app_pb2,
)
from spaceone.identity.service.service_account_service import (
ServiceAccountService,
)
Expand All @@ -16,12 +20,6 @@ def create(self, request, context):
response: dict = service_account_svc.create(params)
return self.dict_to_message(response)

def create_app(self, request, context):
params, metadata = self.parse_request(request, context)
service_account_svc = ServiceAccountService(metadata)
response: dict = service_account_svc.create_app(params)
return ParseDict(response, app_pb2.AppInfo())

def update(self, request, context):
params, metadata = self.parse_request(request, context)
service_account_svc = ServiceAccountService(metadata)
Expand All @@ -46,30 +44,6 @@ def delete(self, request, context):
service_account_svc.delete(params)
return self.empty()

def enable_app(self, request, context):
params, metadata = self.parse_request(request, context)
service_account_svc = ServiceAccountService(metadata)
response: dict = service_account_svc.enable_app(params)
return ParseDict(response, app_pb2.AppInfo())

def disable_app(self, request, context):
params, metadata = self.parse_request(request, context)
service_account_svc = ServiceAccountService(metadata)
response: dict = service_account_svc.disable_app(params)
return ParseDict(response, app_pb2.AppInfo())

def regenerate_app(self, request, context):
params, metadata = self.parse_request(request, context)
service_account_svc = ServiceAccountService(metadata)
response: dict = service_account_svc.regenerate_app(params)
return ParseDict(response, app_pb2.AppInfo())

def delete_app(self, request, context):
params, metadata = self.parse_request(request, context)
service_account_svc = ServiceAccountService(metadata)
service_account_svc.delete_app(params)
return self.empty()

def get(self, request, context):
params, metadata = self.parse_request(request, context)
service_account_svc = ServiceAccountService(metadata)
Expand Down
66 changes: 66 additions & 0 deletions src/spaceone/identity/manager/agent_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.identity.model import Agent


_LOGGER = logging.getLogger(__name__)


class AgentManager(BaseManager):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.agent_model = Agent

def create_agent(self, params: dict) -> Agent:
def _rollback(vo: Agent):
_LOGGER.info(f"[create_agent._rollback] " f"Delete agent: ({vo.agent_id})")
agent_vo.delete()

agent_vo = self.agent_model.create(params)
self.transaction.add_rollback(_rollback, agent_vo)

return agent_vo

def get_agent(
self,
service_account_id: str,
domain_id: str,
workspace_id: str,
user_projects: Union[list, None] = None,
) -> Agent:
conditions = {
"service_account_id": service_account_id,
"domain_id": domain_id,
"workspace_id": workspace_id,
}

if user_projects:
conditions["project_id"] = user_projects

return self.agent_model.get(**conditions)

def update_agent_by_vo(self, params: dict, agent_vo: Agent) -> Agent:
def _rollback(old_data):
_LOGGER.info(
f"[update_agent_by_vo._rollback] Revert Data : "
f"{old_data['agent_id']}"
)
agent_vo.update(old_data)

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

return agent_vo.update(params)

@staticmethod
def delete_agent_by_vo(agent_vo: Agent) -> None:
agent_vo.delete()

def filter_agents(self, **conditions) -> QuerySet:
return self.agent_model.filter(**conditions)

def list_agents(self, query: dict) -> Tuple[list, int]:
return self.agent_model.query(**query)
4 changes: 0 additions & 4 deletions src/spaceone/identity/manager/app_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ def get_app(
app_id: str,
domain_id: str,
workspace_id: Union[str, None] = None,
service_account_id: Union[str, None] = None,
) -> App:
conditions = {
"app_id": app_id,
Expand All @@ -64,9 +63,6 @@ def get_app(
if workspace_id:
conditions["workspace_id"] = workspace_id

if service_account_id:
conditions["service_account_id"] = service_account_id

return self.app_model.get(**conditions)

def filter_apps(self, **conditions) -> QuerySet:
Expand Down
34 changes: 17 additions & 17 deletions src/spaceone/identity/manager/service_account_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def update_service_account(self, params: dict) -> ServiceAccount:
return self.update_service_account_by_vo(params, service_account_vo)

def update_service_account_by_vo(
self, params: dict, service_account_vo: ServiceAccount
self, params: dict, service_account_vo: ServiceAccount
) -> ServiceAccount:
def _rollback(old_data):
_LOGGER.info(
Expand All @@ -55,11 +55,11 @@ def delete_service_account_by_vo(service_account_vo: ServiceAccount) -> None:
service_account_vo.delete()

def get_service_account(
self,
service_account_id: str,
domain_id: str,
workspace_id: str = None,
user_projects: List[str] = None,
self,
service_account_id: str,
domain_id: str,
workspace_id: str = None,
user_projects: List[str] = None,
) -> ServiceAccount:
conditions = {"service_account_id": service_account_id, "domain_id": domain_id}

Expand All @@ -81,11 +81,11 @@ def stat_service_accounts(self, query: dict) -> dict:
return self.service_account_model.stat(**query)

def update_secret_project(
self,
service_account_id: str,
domain_id: str,
workspace_id: str,
project_id: str,
self,
service_account_id: str,
domain_id: str,
workspace_id: str,
project_id: str,
) -> None:
secret_connector: SpaceConnector = self.locator.get_connector(
"SpaceConnector", service="secret"
Expand All @@ -106,7 +106,7 @@ def update_secret_project(
)

def delete_secrets(
self, service_account_id: str, domain_id: str, workspace_id: str
self, service_account_id: str, domain_id: str, workspace_id: str
) -> None:
secret_connector: SpaceConnector = self.locator.get_connector(
"SpaceConnector", service="secret"
Expand All @@ -126,7 +126,7 @@ def delete_secrets(
)

def get_all_service_account_ids_using_secret(
self, domain_id: str, workspace_id: str
self, domain_id: str, workspace_id: str
) -> List[str]:
secret_connector: SpaceConnector = self.locator.get_connector(
"SpaceConnector", service="secret"
Expand All @@ -147,10 +147,10 @@ def get_all_service_account_ids_using_secret(

@staticmethod
def _list_secrets(
secret_connector: SpaceConnector,
service_account_id: str,
domain_id: str,
workspace_id: str,
secret_connector: SpaceConnector,
service_account_id: str,
domain_id: str,
workspace_id: str,
) -> dict:
return secret_connector.dispatch(
"Secret.list",
Expand Down
1 change: 1 addition & 0 deletions src/spaceone/identity/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
from spaceone.identity.model.user.database import User
from spaceone.identity.model.user_group.database import UserGroup
from spaceone.identity.model.app.database import App
from spaceone.identity.model.agent.database import Agent
Empty file.
28 changes: 28 additions & 0 deletions src/spaceone/identity/model/agent/database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from mongoengine import StringField, DictField, DateTimeField
from spaceone.core.model.mongo_model import MongoModel


class Agent(MongoModel):
agent_id = StringField(max_length=40, generate_id="agent", unique=True)
options = DictField(default=None)
app_id = StringField(max_length=40)
service_account_id = StringField(max_length=40)
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)

meta = {
"minimal_fields": [
"agent_id",
"options",
"service_account_id",
],
"change_query_keys": {"user_projects": "project_id"},
"ordering": ["-created_at"],
"indexes": [
"service_account_id",
"workspace_id",
"domain_id",
],
}
69 changes: 69 additions & 0 deletions src/spaceone/identity/model/agent/request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from typing import Union, Literal
from pydantic import BaseModel


__all__ = [
"AgentCreateRequest",
"AgentEnableRequest",
"AgentDisableRequest",
"AgentRegenerateRequest",
"AgentDeleteRequest",
"AgentGetRequest",
"AgentSearchQueryRequest",
]

State = Literal["ENABLED", "DISABLED", "EXPIRED"]
RoleType = Literal["DOMAIN_ADMIN", "WORKSPACE_OWNER"]


class AgentCreateRequest(BaseModel):
service_account_id: str
options: Union[dict, None] = None
workspace_id: str
domain_id: str
user_projects: Union[list, None] = None


class AgentEnableRequest(BaseModel):
service_account_id: str
workspace_id: str
domain_id: str
user_projects: Union[list, None] = None


class AgentDisableRequest(BaseModel):
service_account_id: str
workspace_id: str
domain_id: str
user_projects: Union[list, None] = None


class AgentRegenerateRequest(BaseModel):
service_account_id: str
workspace_id: str
domain_id: str
user_projects: Union[list, None] = None


class AgentDeleteRequest(BaseModel):
service_account_id: str
workspace_id: str
domain_id: str
user_projects: Union[list, None] = None


class AgentGetRequest(BaseModel):
service_account_id: str
workspace_id: str
domain_id: str
user_projects: Union[list, None] = None


class AgentSearchQueryRequest(BaseModel):
query: Union[dict, None] = None
agent_id: Union[str, None] = None
state: Union[State, None] = None
service_account_id: Union[str, None] = None
workspace_id: Union[str, None] = None
domain_id: str
user_projects: Union[list, None] = None
Loading

0 comments on commit 6ba5fa3

Please sign in to comment.