| 1 | from loguru import logger |
| 2 | from sqlalchemy.ext.asyncio import AsyncSession |
| 3 | |
| 4 | from app.customer_provisioning.services.grafana import delete_grafana_dashboard_folder |
| 5 | from app.customer_provisioning.services.grafana import delete_grafana_datasource |
| 6 | from app.customer_provisioning.services.graylog import delete_content_pack |
| 7 | from app.customer_provisioning.services.graylog import uninstall_content_pack |
| 8 | from app.network_connectors.models.network_connectors import ( |
| 9 | CustomerNetworkConnectorsMeta, |
| 10 | ) |
| 11 | from app.stack_provisioning.graylog.schema.decommission import ( |
| 12 | DecommissionNetworkContentPackResponse, |
| 13 | ) |
| 14 | from app.stack_provisioning.graylog.services.utils import set_deployed_flag |
| 15 | |
| 16 | |
| 17 | async def uninstall_and_delete_content_pack(content_pack_id: str): |
| 18 | """ |
| 19 | Uninstalls and deletes a content pack. |
| 20 | |
| 21 | Args: |
| 22 | content_pack_id (str): The ID of the content pack to be uninstalled and deleted. |
| 23 | """ |
| 24 | await uninstall_content_pack(content_pack_id) |
| 25 | await delete_content_pack(content_pack_id) |
| 26 | |
| 27 | |
| 28 | async def delete_grafana_resources(organization_id: str, folder_uid: str, datasource_uid: str): |
| 29 | """ |
| 30 | Deletes Grafana resources. |
| 31 | |
| 32 | Args: |
| 33 | organization_id (str): The ID of the Grafana organization. |
| 34 | folder_uid (str): The UID of the Grafana Dashboard Folder. |
| 35 | datasource_uid (str): The UID of the Grafana Datasource. |
| 36 | """ |
| 37 | await delete_grafana_dashboard_folder(organization_id=organization_id, folder_uid=folder_uid) |
| 38 | await delete_grafana_datasource(organization_id=organization_id, datasource_uid=datasource_uid) |
| 39 | |
| 40 | |
| 41 | async def decommission_network_connector( |
| 42 | network_connector_meta: CustomerNetworkConnectorsMeta, |
| 43 | session: AsyncSession, |
| 44 | ) -> DecommissionNetworkContentPackResponse: |
| 45 | """ |
| 46 | Decommissions the network connector by performing the following steps: |
| 47 | 1. Uninstalls and deletes the content pack associated with the network connector. |
| 48 | 2. Deletes the Grafana resources associated with the network connector. |
| 49 | 3. Deletes the network connector meta from the session. |
| 50 | |
| 51 | Args: |
| 52 | network_connector_meta (CustomerNetworkConnectorsMeta): The metadata of the network connector to be decommissioned. |
| 53 | session (AsyncSession): The database session. |
| 54 | |
| 55 | Returns: |
| 56 | DecommissionNetworkContentPackResponse: The response of the decommission operation. |
| 57 | """ |
| 58 | logger.info(f"Decommissioning network connector {network_connector_meta.network_connector_name}") |
| 59 | |
| 60 | # Uninstall and delete the content pack Input ID and Stream ID |
| 61 | await uninstall_and_delete_content_pack(network_connector_meta.graylog_content_pack_input_id) |
| 62 | await uninstall_and_delete_content_pack(network_connector_meta.graylog_content_pack_stream_id) |
| 63 | |
| 64 | # Delete the Grafana resources |
| 65 | await delete_grafana_resources( |
| 66 | organization_id=network_connector_meta.grafana_org_id, |
| 67 | folder_uid=network_connector_meta.grafana_dashboard_folder_id, |
| 68 | datasource_uid=network_connector_meta.grafana_datasource_uid, |
| 69 | ) |
| 70 | |
| 71 | # Delete the network connector meta from the session |
| 72 | await session.delete(network_connector_meta) |
| 73 | await session.commit() |
| 74 | |
| 75 | await set_deployed_flag( |
| 76 | customer_code=network_connector_meta.customer_code, |
| 77 | network_connector_service_name=network_connector_meta.network_connector_name, |
| 78 | flag=False, |
| 79 | session=session, |
| 80 | ) |
| 81 | |
| 82 | return DecommissionNetworkContentPackResponse( |
| 83 | message=( |
| 84 | f"Network connector {network_connector_meta.network_connector_name} has been " |
| 85 | f"decommissioned. However, the indices still remain. If you want to remove the " |
| 86 | f"index set, do so within Graylog." |
| 87 | ), |
| 88 | success=True, |
| 89 | ) |