Skip to content

Token manager

TokenManager

Bases: BaseModel

create_service_account

create_service_account()

Create a new service account.

:param name: Desired name for the service account :param role: Role for the service account ('Viewer', 'Editor', or 'Admin') :return: Dictionary containing service account details or None if creation failed

Source code in src/zenbt/grafana/token_manager.py
def create_service_account(self):
    """
    Create a new service account.

    :param name: Desired name for the service account
    :param role: Role for the service account ('Viewer', 'Editor', or 'Admin')
    :return: Dictionary containing service account details or None if creation failed
    """
    payload = {"name": self.service_account_name, "role": "Admin"}
    response = requests.post(
        f"{self.grafana_url}/api/serviceaccounts",
        headers=self.headers,
        auth=self.auth,
        data=json.dumps(payload),
    )
    if response.status_code == 201:
        return response.json()
    else:
        print(
            f"Failed to create service account. Status code: {response.status_code}, Response: {response.text}"
        )
        return None

delete_service_account

delete_service_account(service_account_id)

Delete an existing service account by its ID.

:param service_account_id: ID of the service account to be deleted :return: True if deletion was successful, False otherwise

Source code in src/zenbt/grafana/token_manager.py
def delete_service_account(self, service_account_id):
    """
    Delete an existing service account by its ID.

    :param service_account_id: ID of the service account to be deleted
    :return: True if deletion was successful, False otherwise
    """
    response = requests.delete(
        f"{self.grafana_url}/api/serviceaccounts/{service_account_id}",
        headers=self.headers,
        auth=self.auth,
    )
    if response.status_code == 200:
        print(f"Service Account with ID {service_account_id} deleted successfully.")
        return True
    else:
        print(
            f"Failed to delete service account. Status code: {response.status_code}, Response: {response.text}"
        )
        return False

delete_service_account_by_name

delete_service_account_by_name()

Delete a service account by its name.

:param name: Name of the service account to be deleted :return: True if deletion was successful, False otherwise

Source code in src/zenbt/grafana/token_manager.py
def delete_service_account_by_name(self):
    """
    Delete a service account by its name.

    :param name: Name of the service account to be deleted
    :return: True if deletion was successful, False otherwise
    """
    service_account_id = self.get_service_account_id_by_name()
    if service_account_id is None:
        return False

    response = requests.delete(
        f"{self.grafana_url}/api/serviceaccounts/{service_account_id}",
        headers=self.headers,
        auth=self.auth,
    )
    if response.status_code == 200:
        print(
            f"Service account '{self.service_account_name}' deleted successfully."
        )
        return True
    else:
        print(
            f"Failed to delete service account '{self.service_account_name}'. Status code: {response.status_code}, Response: {response.text}"
        )
        return False

generate_service_account_token

generate_service_account_token(
    service_account_id, token_name
)

Generate a token for an existing service account.

:param service_account_id: ID of the service account :param token_name: Desired name for the token :return: Dictionary containing token details or None if generation failed

Source code in src/zenbt/grafana/token_manager.py
def generate_service_account_token(self, service_account_id, token_name):
    """
    Generate a token for an existing service account.

    :param service_account_id: ID of the service account
    :param token_name: Desired name for the token
    :return: Dictionary containing token details or None if generation failed
    """
    payload = {"name": token_name}
    response = requests.post(
        f"{self.grafana_url}/api/serviceaccounts/{service_account_id}/tokens",
        headers=self.headers,
        auth=self.auth,
        data=json.dumps(payload),
    )
    if response.status_code == 200:
        return response.json()
    else:
        print(
            f"Failed to create service account token. Status code: {response.status_code}, Response: {response.text}"
        )
        return None

get_service_account_id_by_name

get_service_account_id_by_name()

Retrieve the ID of a service account by its name.

:param name: Name of the service account :return: Service account ID if found, None otherwise

Source code in src/zenbt/grafana/token_manager.py
def get_service_account_id_by_name(self):
    """
    Retrieve the ID of a service account by its name.

    :param name: Name of the service account
    :return: Service account ID if found, None otherwise
    """
    response = requests.get(
        f"{self.grafana_url}/api/serviceaccounts/search",
        headers=self.headers,
        auth=self.auth,
        params={"query": self.service_account_name},
    )
    if response.status_code == 200:
        accounts = response.json().get("serviceAccounts", [])
        for account in accounts:
            if account["name"] == self.service_account_name:
                return account["id"]
        print(f"Service account with name '{self.service_account_name}' not found.")
        return None
    else:
        print(
            f"Failed to retrieve service accounts. Status code: {response.status_code}, Response: {response.text}"
        )
        return None