| 1 | from fastapi import HTTPException |
| 2 | from loguru import logger |
| 3 | |
| 4 | from app.connectors.grafana.schema.user import UserOrganizationAddRequest |
| 5 | from app.connectors.grafana.schema.user import UserOrganizationAddResponse |
| 6 | from app.connectors.grafana.utils.universal import create_grafana_client |
| 7 | |
| 8 | |
| 9 | async def add_user_to_organization(request: UserOrganizationAddRequest) -> UserOrganizationAddResponse: |
| 10 | """ |
| 11 | Adds a user to a Grafana organization. |
| 12 | |
| 13 | Args: |
| 14 | request (UserOrganizationAddRequest): The request body containing the user and organization data. |
| 15 | |
| 16 | Returns: |
| 17 | UserOrganizationAddResponse: The response containing the result of the user addition. |
| 18 | """ |
| 19 | logger.info("Adding user to Grafana organization") |
| 20 | grafana_client = await create_grafana_client("Grafana") |
| 21 | try: |
| 22 | # Add the user to the specified organization |
| 23 | user = { |
| 24 | "loginorEmail": request.loginorEmail, |
| 25 | "role": request.role, |
| 26 | } |
| 27 | response = grafana_client.organizations.organization_user_add(request.organizationId, user) |
| 28 | logger.info(f"Grafana user added to organization: {response}") |
| 29 | return UserOrganizationAddResponse( |
| 30 | message=f"User {request.loginorEmail} added to organization {request.organizationId}", |
| 31 | status="success", |
| 32 | ) |
| 33 | except Exception as e: |
| 34 | logger.error(f"Error adding user to organization: {e}") |
| 35 | raise HTTPException(status_code=500, detail=f"Error adding user to organization: {e}") |