-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #252 from yjinjo/master
Add Agent resource and modify Service Account resource
- Loading branch information
Showing
18 changed files
with
693 additions
and
345 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.