Skip to content
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
91 changes: 42 additions & 49 deletions src/plugin/connector/spaceone_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from spaceone.core.connector import BaseConnector
from spaceone.core.error import *

__all__ = ['SpaceONEConnector']
__all__ = ["SpaceONEConnector"]

_LOGGER = logging.getLogger(__name__)

Expand All @@ -22,96 +22,89 @@ def __init__(self, *args, **kwargs):
self.endpoint = None

def init_client(self, options: dict, secret_data: dict, schema: str = None) -> None:
self._check_secret_data(secret_data)
spaceone_endpoint = secret_data['spaceone_endpoint']
self.token = secret_data['spaceone_client_secret']

if spaceone_endpoint.startswith('http') or spaceone_endpoint.startswith('https'):
self.protocol = 'http'
task_type = options.get("task_type", "identity")
self._check_secret_data(secret_data, task_type)
spaceone_endpoint = secret_data["spaceone_endpoint"]
self.token = secret_data["spaceone_client_secret"]

if spaceone_endpoint.startswith("http") or spaceone_endpoint.startswith(
"https"
):
self.protocol = "http"
self.endpoint = spaceone_endpoint
elif spaceone_endpoint.startswith('grpc') or spaceone_endpoint.startswith('grpc+ssl'):
self.protocol = 'grpc'
self.grpc_client: SpaceConnector = SpaceConnector(endpoint=spaceone_endpoint, token=self.token)
elif spaceone_endpoint.startswith("grpc") or spaceone_endpoint.startswith(
"grpc+ssl"
):
self.protocol = "grpc"
self.grpc_client: SpaceConnector = SpaceConnector(
endpoint=spaceone_endpoint, token=self.token
)

def verify_plugin(self, domain_id: str) -> None:
method = 'Project.list'
method = "Project.list"
params = {
"query": {
"filter": [
{"k": "tags.domain_id", "v": domain_id, "o": "eq"}
]
}
"query": {"filter": [{"k": "tags.domain_id", "v": domain_id, "o": "eq"}]}
}
self.dispatch(method, params)

def list_projects(self, domain_id: str):
params = {
'query': {
'filter': [
{'k': 'tags.domain_id', 'v': domain_id, 'o': 'eq'}
]
}
"query": {"filter": [{"k": "tags.domain_id", "v": domain_id, "o": "eq"}]}
}

return self.dispatch('Project.list', params)
return self.dispatch("Project.list", params)

def get_service_account(self, service_account_id):
params = {
'service_account_id': service_account_id
}
params = {"service_account_id": service_account_id}

return self.dispatch('ServiceAccount.get', params)
return self.dispatch("ServiceAccount.get", params)

def update_service_account(self, service_account_id, tags):
params = {
'service_account_id': service_account_id,
'tags': tags
}
params = {"service_account_id": service_account_id, "tags": tags}

return self.dispatch('ServiceAccount.update', params)
return self.dispatch("ServiceAccount.update", params)

def list_service_accounts(self, project_id: str):
params = {
'provider': 'aws',
'project_id': project_id
}
params = {"provider": "aws", "project_id": project_id}

return self.dispatch('ServiceAccount.list', params)
return self.dispatch("ServiceAccount.list", params)

def _get_metadata(self):
return ('token', self.token),
return (("token", self.token),)

def dispatch(self, method: str = None, params: dict = None, **kwargs):
if self.protocol == 'grpc':
if self.protocol == "grpc":
return self.grpc_client.dispatch(method, params, **kwargs)
else:
return self.request(method, params, **kwargs)

def request(self, method, params, **kwargs):
method = self._convert_method_to_snake_case(method)
url = f'{self.endpoint}/{method}'
url = f"{self.endpoint}/{method}"

headers = self._make_request_header(self.token, **kwargs)
response = requests.post(url, json=params, headers=headers)

if response.status_code >= 400:
raise requests.HTTPError(f'HTTP {response.status_code} Error: {response.json()["detail"]}')
raise requests.HTTPError(
f'HTTP {response.status_code} Error: {response.json()["detail"]}'
)

response = response.json()
return response

@staticmethod
def _convert_method_to_snake_case(method):
method = re.sub(r'(?<!^)(?=[A-Z])', '_', method)
method = method.replace('.', '/').replace('_', '-').lower()
method = re.sub(r"(?<!^)(?=[A-Z])", "_", method)
method = method.replace(".", "/").replace("_", "-").lower()
return method

@staticmethod
def _make_request_header(token, **kwargs):
access_token = token
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json',
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json",
}
return headers

Expand All @@ -120,9 +113,9 @@ def _change_message(message):
return MessageToDict(message, preserving_proto_field_name=True)

@staticmethod
def _check_secret_data(secret_data: dict):
if 'spaceone_endpoint' not in secret_data:
raise ERROR_REQUIRED_PARAMETER(key='secret_data.spaceone_endpoint')
def _check_secret_data(secret_data: dict, task_type: str):
if "spaceone_endpoint" not in secret_data:
raise ERROR_REQUIRED_PARAMETER(key="secret_data.spaceone_endpoint")

if 'spaceone_client_secret' not in secret_data:
raise ERROR_REQUIRED_PARAMETER(key='secret_data.spaceone_client_secret')
if "spaceone_client_secret" not in secret_data:
raise ERROR_REQUIRED_PARAMETER(key="secret_data.spaceone_client_secret")
5 changes: 4 additions & 1 deletion src/plugin/manager/job_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ def get_tasks_directory_type(
"is_sync": "true",
"task_type": "directory",
}
task_changed = {"start": start_month}
task_changed = {
"start": start_month,
"filter": {"additional_info.Account ID": account_id},
}
tasks.append({"task_options": task_options, "task_changed": task_changed})

changed.append({"start": start_month})
Expand Down
Loading