Skip to content

Commit 6071027

Browse files
authored
Merge 19c7535 into 8da72c1
2 parents 8da72c1 + 19c7535 commit 6071027

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

grafana_api/datasource.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,3 +1059,82 @@ def update_datasource_cache(
10591059
"There is no uid or the right datasource_cache object defined."
10601060
)
10611061
raise ValueError
1062+
1063+
1064+
class DatasourceLabelBasedAccessControl:
1065+
"""The class includes all necessary methods to access the Grafana datasource label based access control for teams API endpoints. It's required that the API token got the corresponding datasource access rights. Please check the used methods docstring for the necessary access rights. The functionality is a Grafana Cloud feature. Only cloud Loki data sources are supported
1066+
1067+
Args:
1068+
grafana_api_model (APIModel): Inject a Grafana API model object that includes all necessary values and information
1069+
1070+
Attributes:
1071+
grafana_api_model (APIModel): This is where we store the grafana_api_model
1072+
"""
1073+
1074+
def __init__(self, grafana_api_model: APIModel):
1075+
self.grafana_api_model = grafana_api_model
1076+
1077+
def get_lbac_rules_for_datasoure(self, uid: str) -> list:
1078+
"""The method includes a functionality to get all datasource label based access control rules for team specified by the datasource uid
1079+
1080+
Args:
1081+
uid (str): Specify the uid of the datasource
1082+
1083+
Required Permissions:
1084+
Action: datasources:read
1085+
Scope: [datasources:*, datasources:uid:*, datasources:uid:<id>]
1086+
1087+
Raises:
1088+
ValueError: Missed specifying a necessary value
1089+
Exception: Unspecified error by executing the API call
1090+
1091+
Returns:
1092+
api_call (list): Returns all LBAC rules
1093+
"""
1094+
1095+
if len(uid) != 0:
1096+
api_call: list = Api(self.grafana_api_model).call_the_api(
1097+
f"{APIEndpoints.DATASOURCES.value}/{uid}/lbac/teams",
1098+
)
1099+
1100+
if api_call is None:
1101+
logging.error(f"Check the error: {api_call}.")
1102+
raise Exception
1103+
else:
1104+
return api_call
1105+
else:
1106+
logging.error("There is no uid defined.")
1107+
raise ValueError
1108+
1109+
def update_lbac_rules_for_datasoure(self, uid: str) -> dict:
1110+
"""The method includes a functionality to enable the datasource cache specified by the datasource uid
1111+
1112+
Args:
1113+
uid (str): Specify the uid of the datasource
1114+
1115+
Required Permissions:
1116+
Action: datasources:write, datasources.permissions:write
1117+
Scope: [datasources:*, datasources:uid:*, datasources:uid:<id>]
1118+
1119+
Raises:
1120+
ValueError: Missed specifying a necessary value
1121+
Exception: Unspecified error by executing the API call
1122+
1123+
Returns:
1124+
api_call (dict): Returns a datasource
1125+
"""
1126+
1127+
if len(uid) != 0:
1128+
api_call: dict = Api(self.grafana_api_model).call_the_api(
1129+
f"{APIEndpoints.DATASOURCES.value}/{uid}/lbac/teams",
1130+
RequestsMethods.POST,
1131+
)
1132+
1133+
if api_call == dict() or api_call.get("dataSourceID") is None:
1134+
logging.error(f"Check the error: {api_call}.")
1135+
raise Exception
1136+
else:
1137+
return api_call
1138+
else:
1139+
logging.error("There is no uid defined.")
1140+
raise ValueError

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
setuptools.setup(
1010
name="grafana-api-sdk",
11-
version="0.7.2",
11+
version="0.8.0",
1212
author="Pascal Zimmermann",
1313
author_email="[email protected]",
1414
description="A Grafana API SDK",

0 commit comments

Comments
 (0)