Coverage for src/main/python/services/resource_groups.py: 100%

1 import sys

2 from typing import Any, cast

3

4 import requests

5

6

7 def get_resource_groups_by_team(

8 bearer_token: str, org_name: str, team_name: str

9) -> list[dict[str, Any]]:

10 """

11 Retrieve a list of resource groups for a given team using bearer token auth.

12

13 Args:

14 bearer_token (str): The bearer token for authentication

15 team_name (str): The name of the team to get resource groups for

16

17 Returns:

18 List[Dict[str, Any]]: A list of project dictionaries

19 """

20 headers = {

21 "Authorization": f"Bearer {bearer_token}",

22 "Content-Type": "application/json",

23 }

24

25 # Replace with actual API endpoint

26 url = f"https://{org_name}.pam.okta.com/v1/teams/{team_name}/resource_groups"

27

28 try:

29 # print(f"Fetching resource groups from URL: {url} with headers: {headers}")

30 response = requests.get(url, headers=headers)

31 response.raise_for_status()

32 return cast(list[dict[str, Any]], response.json()["list"])

33 except requests.exceptions.RequestException as e:

34 print(f"Error fetching resource groups: {e}")

35 return []

36

37

38 def get_projects_by_resource_group(

39 bearer_token: str, org_name: str, team_name: str, resource_group_id: str

40) -> list[dict[str, Any]]:

41 """

42 Retrieve a list of projects for a resource group using bearer token auth.

43

44 Args:

45 bearer_token (str): The bearer token for authentication

46 team_name (str): The name of the team to get projects for

47 resource_group_id (str): The ID of the resource group to filter projects by

48

49 Returns:

50 List[Dict[str, Any]]: A list of project dictionaries

51 """

52 headers = {

53 "Authorization": f"Bearer {bearer_token}",

54 "Content-Type": "application/json",

55 }

56

57 # Replace with actual API endpoint

58 url = f"https://{org_name}.pam.okta.com/v1/teams/{team_name}/resource_groups/{resource_group_id}/projects"

59

60 try:

61 # print(f"Fetching resourcegroups from URL: {url} with headers: {headers}")

62 response = requests.get(url, headers=headers)

63 response.raise_for_status()

64 list = response.json()["list"]

65 # print(f"Projects in resource group {resource_group_id}: {list}")

66 return [

67 {"id": r["id"], "name": r["name"]} for r in list if r["deleted_at"] is None

68 ]

69 except requests.exceptions.RequestException as e:

70 print(f"Error fetching projects: {e}", file=sys.stderr)

71 return []