Skip to content

Commit

Permalink
feat: add user cloud service type
Browse files Browse the repository at this point in the history
  • Loading branch information
ImMin5 committed Mar 5, 2024
1 parent 1f19b1d commit d7b3e7b
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 75 deletions.
1 change: 1 addition & 0 deletions src/plugin/connector/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from plugin.connector.project_connector import ProjectConnector
from plugin.connector.issue_connector import IssueConnector
from plugin.connector.user_connector import UserConnector
14 changes: 11 additions & 3 deletions src/plugin/connector/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,13 @@ def _pagination(
json=data,
)
response_json = response.json()
response_values = response_json.get("values")
if isinstance(response_json, list):
response_values = response_json
else:
response_values = response_json.get("values")

_LOGGER.debug(
f"[dispatch_request] {url} {response.status_code} {response.reason}, {response_json.get('total')}"
f"[dispatch_request] {url} {response.status_code} {response.reason}"
)

if response.status_code != 200:
Expand All @@ -73,7 +77,11 @@ def _pagination(
else:
responses.append(response_json)

if response_json.get("isLast", True) or response_json.get("isLast") is None:
if (
isinstance(response_json, list)
or response_json.get("isLast", True)
or response_json.get("isLast") is None
):
break
else:
url = response_json.get("nextPage")
Expand Down
27 changes: 27 additions & 0 deletions src/plugin/connector/user_connector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import logging
from typing import Generator

from plugin.connector.base import JiraBaseConnector

_LOGGER = logging.getLogger("spaceone")


class UserConnector(JiraBaseConnector):
cloud_service_type = "User"

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def get_all_project_roles(
self,
secret_data: dict,
project_id_or_key: str,
) -> list:
request_url = f"rest/api/3/user/assignable/multiProjectSearch"
_LOGGER.debug(f"[search_role] {request_url}")

query = {"projectKeys": project_id_or_key}

responses = self.dispatch_request("GET", request_url, secret_data, params=query)

return responses
4 changes: 4 additions & 0 deletions src/plugin/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

import logging

from spaceone.core.service import *
Expand Down Expand Up @@ -157,3 +158,6 @@ def check_secret_data(secret_data: dict):

if secret_data.get("api_token") is None:
raise ERROR_REQUIRED_PARAMETER(key="secret_data.api_token")

if secret_data.get("project_key") is None:
raise ERROR_REQUIRED_PARAMETER(key="secret_data.project_key")
5 changes: 3 additions & 2 deletions src/plugin/manager/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from plugin.manager.project_manager import ProjectManager
from plugin.manager.issue_manager import IssueManager
# from plugin.manager.project_manager import ProjectManager
# from plugin.manager.issue_manager import IssueManager
from plugin.manager.user_manager import MemberManager
65 changes: 35 additions & 30 deletions src/plugin/manager/issue_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,35 +57,40 @@ def collect_cloud_service_type(self, options, secret_data, schema):
def collect_cloud_service(self, options, secret_data, schema):
# Return Cloud Service
domain = secret_data["domain"]
for project_info in self.project_connector.get_projects(secret_data):
project_id_or_key = project_info["id"]
for issue_info in self.issue_connector.search_issue(
secret_data, project_id_or_key
).get("issues", []):
reference = {
"resource_id": f"jira:{issue_info['id']}",
"external_link": f"https://{domain}.atlassian.net/browse/{issue_info['key']}",
}
project_key = secret_data.get("project_key")

cloud_service = make_cloud_service(
name=issue_info["fields"]["summary"],
cloud_service_type=self.cloud_service_type,
cloud_service_group=self.cloud_service_group,
provider=self.provider,
data=issue_info,
reference=reference,
account=domain,
)
yield from self._list_issue_from_project(project_key, domain, secret_data)

yield make_response(
cloud_service=cloud_service,
match_keys=[
[
"reference.resource_id",
"provider",
"cloud_service_type",
"cloud_service_group",
"account",
]
],
)
def _list_issue_from_project(
self, project_key: str, domain: str, secret_data: dict
):
for issue_info in self.issue_connector.search_issue(
secret_data, project_key
).get("issues", []):
reference = {
"resource_id": f"jira:{issue_info['id']}",
"external_link": f"https://{domain}.atlassian.net/browse/{issue_info['key']}",
}

cloud_service = make_cloud_service(
name=issue_info["fields"]["summary"],
cloud_service_type=self.cloud_service_type,
cloud_service_group=self.cloud_service_group,
provider=self.provider,
data=issue_info,
reference=reference,
account=domain,
)

yield make_response(
cloud_service=cloud_service,
match_keys=[
[
"reference.resource_id",
"provider",
"cloud_service_type",
"cloud_service_group",
"account",
]
],
)
76 changes: 36 additions & 40 deletions src/plugin/manager/project_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,48 +56,44 @@ def collect_cloud_service_type(self, options, secret_data, schema):
def collect_cloud_service(self, options, secret_data, schema):
# Return Cloud Service
domain = secret_data["domain"]
project_key = secret_data.get("project_key")
project_info = self.project_connector.get_project(secret_data, project_key)

for response in self.project_connector.get_projects(secret_data):
project_id_or_key = response["id"]
project_info = self.project_connector.get_project(
secret_data, project_id_or_key
)

total = self.project_connector.get_project_total_issue_count(
secret_data, project_id_or_key
)

project_info.update(
{
"display_total_issue_count": total,
"display_issue_type_count": len(project_info.get("issueTypes", [])),
}
)
total = self.project_connector.get_project_total_issue_count(
secret_data, project_key
)

reference = {
"resource_id": f"jira:{project_info['id']}",
"external_link": f"https://{domain}.atlassian.net/browse/{project_info['key']}",
project_info.update(
{
"display_total_issue_count": total,
"display_issue_type_count": len(project_info.get("issueTypes", [])),
}
)

cloud_service = make_cloud_service(
name=project_info["name"],
cloud_service_type=self.cloud_service_type,
cloud_service_group=self.cloud_service_group,
provider=self.provider,
data=project_info,
reference=reference,
account=domain,
)
reference = {
"resource_id": f"jira:{project_info['id']}",
"external_link": f"https://{domain}.atlassian.net/browse/{project_info['key']}",
}

yield make_response(
cloud_service=cloud_service,
match_keys=[
[
"reference.resource_id",
"provider",
"cloud_service_type",
"cloud_service_group",
"account",
]
],
)
cloud_service = make_cloud_service(
name=project_info["name"],
cloud_service_type=self.cloud_service_type,
cloud_service_group=self.cloud_service_group,
provider=self.provider,
data=project_info,
reference=reference,
account=domain,
)

yield make_response(
cloud_service=cloud_service,
match_keys=[
[
"reference.resource_id",
"provider",
"cloud_service_type",
"cloud_service_group",
"account",
]
],
)
92 changes: 92 additions & 0 deletions src/plugin/manager/user_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import logging
from typing import Generator

from plugin.connector.project_connector import ProjectConnector
from plugin.connector.user_connector import UserConnector
from plugin.manager.base import JiraBaseManager
from spaceone.inventory.plugin.collector.lib import *

_LOGGER = logging.getLogger("spaceone")


class MemberManager(JiraBaseManager):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.cloud_service_group = "Projects"
self.cloud_service_type = "Member"
self.metadata_path = "metadata/projects/user.yaml"
self.project_connector = ProjectConnector()
self.role_connector = UserConnector()

def collect(
self, options: dict, secret_data: dict, schema: str
) -> Generator[dict, None, None]:
try:
yield from self.collect_cloud_service_type(options, secret_data, schema)
yield from self.collect_cloud_service(options, secret_data, schema)

except Exception as e:
_LOGGER.error(f"[collect] Error {e}")
yield make_error_response(
error=e,
provider=self.provider,
cloud_service_group=self.cloud_service_group,
cloud_service_type=self.cloud_service_type,
)

def collect_cloud_service_type(self, options, secret_data, schema):
tags = {
"spaceone:icon": "https://spaceone-custom-assets.s3.ap-northeast-2.amazonaws.com/console-assets/icons/jira-icon.png"
}

cloud_service_type = make_cloud_service_type(
name=self.cloud_service_type,
group=self.cloud_service_group,
provider=self.provider,
metadata_path=self.metadata_path,
is_primary=True,
is_major=False,
tags=tags,
)

yield make_response(
cloud_service_type=cloud_service_type,
match_keys=[["name", "group", "provider"]],
resource_type="inventory.CloudServiceType",
)

def collect_cloud_service(self, options, secret_data, schema):
# Return Cloud Service
domain = secret_data["domain"]
project_key = secret_data.get("project_key")

for user_info in self.role_connector.get_all_project_roles(
secret_data, project_key
):
reference = {
"resource_id": f"jira:{user_info['accountId']}",
"external_link": f"https://{domain}.atlassian.net/browse/{user_info['accountId']}",
}

cloud_service = make_cloud_service(
name=user_info["displayName"],
cloud_service_type=self.cloud_service_type,
cloud_service_group=self.cloud_service_group,
provider=self.provider,
data=user_info,
reference=reference,
account=domain,
)

yield make_response(
cloud_service=cloud_service,
match_keys=[
[
"reference.resource_id",
"provider",
"cloud_service_type",
"cloud_service_group",
"account",
]
],
)
18 changes: 18 additions & 0 deletions src/plugin/metadata/projects/user.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
search:
fields:
- Name: data.displayName
- Email: data.emailAddress
- Account Id: data.accountId
- Active: data.active


table:
fields:
- Name: data.displayName
- Email: data.emailAddress
- Account Id: data.accountId
- Active: data.active
- Timezone: data.timeZone


0 comments on commit d7b3e7b

Please sign in to comment.