aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--arthur/apis/github/__init__.py6
-rw-r--r--arthur/apis/github/teams.py22
-rw-r--r--arthur/apis/grafana/__init__.py8
-rw-r--r--arthur/apis/grafana/teams.py51
-rw-r--r--arthur/exts/grafana/__init__.py0
5 files changed, 87 insertions, 0 deletions
diff --git a/arthur/apis/github/__init__.py b/arthur/apis/github/__init__.py
new file mode 100644
index 0000000..77988a1
--- /dev/null
+++ b/arthur/apis/github/__init__.py
@@ -0,0 +1,6 @@
+from .teams import GithubTeamNotFoundError, list_team_members
+
+__all__ = (
+ "GithubTeamNotFoundError",
+ "list_team_members",
+)
diff --git a/arthur/apis/github/teams.py b/arthur/apis/github/teams.py
new file mode 100644
index 0000000..141892d
--- /dev/null
+++ b/arthur/apis/github/teams.py
@@ -0,0 +1,22 @@
+import aiohttp
+
+from arthur.config import CONFIG
+
+HEADERS = {
+ "Accept": "application/vnd.github+json",
+ "X-GitHub-Api-Version": "2022-11-28",
+ "Authorization": f"Bearer {CONFIG.github_token.get_secret_value()}",
+}
+BASE_URL = "https://api.github.com"
+
+
+class GithubTeamNotFoundError(aiohttp.ClientResponseError):
+ """Raised when a github team could not be found."""
+
+
+async def list_team_members(team_slug: str, session: aiohttp.ClientSession) -> list[dict[str, str]]:
+ """List all Github teams."""
+ endpoint = BASE_URL + f"/orgs/{CONFIG.github_org}/teams/{team_slug}/members"
+ async with session.get(endpoint, headers=HEADERS) as response:
+ response.raise_for_status()
+ return await response.json()
diff --git a/arthur/apis/grafana/__init__.py b/arthur/apis/grafana/__init__.py
new file mode 100644
index 0000000..9e9910b
--- /dev/null
+++ b/arthur/apis/grafana/__init__.py
@@ -0,0 +1,8 @@
+from .teams import add_user_to_team, get_all_users, list_team_members, list_teams
+
+__all__ = (
+ "add_user_to_team",
+ "get_all_users",
+ "list_team_members",
+ "list_teams",
+)
diff --git a/arthur/apis/grafana/teams.py b/arthur/apis/grafana/teams.py
new file mode 100644
index 0000000..9e3f66a
--- /dev/null
+++ b/arthur/apis/grafana/teams.py
@@ -0,0 +1,51 @@
+import aiohttp
+
+from arthur import logger
+from arthur.config import CONFIG
+
+AUTH_HEADER = {"Authorization": f"Bearer {CONFIG.grafana_token.get_secret_value()}"}
+
+
+async def list_teams(session: aiohttp.ClientSession) -> dict[str, str]:
+ """List all Grafana teams."""
+ endpoint = CONFIG.grafana_url + "/api/teams/search"
+ async with session.get(endpoint, headers=AUTH_HEADER) as response:
+ teams = await response.json()
+ if not response.ok:
+ logger.error(teams)
+ return teams["teams"]
+
+
+async def list_team_members(team_id: int, session: aiohttp.ClientSession) -> list[dict[str, str]]:
+ """List all members within a team."""
+ endpoint = CONFIG.grafana_url + f"/api/teams/{team_id}/members"
+ async with session.get(endpoint, headers=AUTH_HEADER) as response:
+ team_members = await response.json()
+ if not response.ok:
+ logger.error(team_members)
+ return team_members
+
+
+async def add_user_to_team(
+ user_id: int,
+ team_id: int,
+ session: aiohttp.ClientSession,
+) -> dict[str, str]:
+ """Add a Grafana user to a team."""
+ endpoint = CONFIG.grafana_url + f"/api/teams/{team_id}/members"
+ payload = {"userId": user_id}
+ async with session.post(endpoint, headers=AUTH_HEADER, json=payload) as response:
+ add_resp = await response.json()
+ if not response.ok:
+ logger.error(add_resp)
+ return add_resp
+
+
+async def get_all_users(session: aiohttp.ClientSession) -> list[dict[str, str]]:
+ """Get a grafana users."""
+ endpoint = CONFIG.grafana_url + "/api/org/users/lookup"
+ async with session.get(endpoint, headers=AUTH_HEADER) as response:
+ users = await response.json()
+ if not response.ok:
+ logger.error(users)
+ return users
diff --git a/arthur/exts/grafana/__init__.py b/arthur/exts/grafana/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/arthur/exts/grafana/__init__.py