| 1 | from fastapi import HTTPException |
| 2 | from loguru import logger |
| 3 | |
| 4 | from app.connectors.graylog.schema.pipelines import CreatePipeline |
| 5 | from app.connectors.graylog.schema.pipelines import CreatePipelineRule |
| 6 | from app.connectors.graylog.schema.pipelines import GraylogPipelinesResponse |
| 7 | from app.connectors.graylog.schema.pipelines import ModifyPipeline |
| 8 | from app.connectors.graylog.schema.pipelines import Pipeline |
| 9 | from app.connectors.graylog.schema.pipelines import PipelineRule |
| 10 | from app.connectors.graylog.schema.pipelines import PipelineRulesResponse |
| 11 | from app.connectors.graylog.utils.universal import send_get_request |
| 12 | from app.connectors.graylog.utils.universal import send_post_request |
| 13 | from app.connectors.graylog.utils.universal import send_put_request |
| 14 | from app.customer_provisioning.schema.graylog import StreamConnectionToPipelineRequest |
| 15 | from app.customer_provisioning.schema.graylog import StreamConnectionToPipelineResponse |
| 16 | |
| 17 | |
| 18 | async def get_pipelines() -> GraylogPipelinesResponse: |
| 19 | """Get pipelines from Graylog. |
| 20 | |
| 21 | Returns: |
| 22 | GraylogPipelinesResponse: The response object containing the collected pipelines. |
| 23 | |
| 24 | Raises: |
| 25 | HTTPException: If there is an error collecting the pipelines. |
| 26 | """ |
| 27 | logger.info("Getting pipelines from Graylog") |
| 28 | pipelines_collected = await send_get_request( |
| 29 | endpoint="/api/system/pipelines/pipeline", |
| 30 | ) |
| 31 | try: |
| 32 | if pipelines_collected["success"]: |
| 33 | pipelines_list = [Pipeline(**pipeline_data) for pipeline_data in pipelines_collected["data"]] |
| 34 | return GraylogPipelinesResponse( |
| 35 | pipelines=pipelines_list, |
| 36 | success=True, |
| 37 | message="Pipelines collected successfully", |
| 38 | ) |
| 39 | except KeyError as e: |
| 40 | logger.error(f"Failed to collect pipelines key: {e}") |
| 41 | raise HTTPException( |
| 42 | status_code=500, |
| 43 | detail=f"Failed to collect pipelines key: {e}", |
| 44 | ) |
| 45 | except Exception as e: |
| 46 | logger.error(f"Failed to collect pipelines: {e}") |
| 47 | raise HTTPException(status_code=500, detail=f"Failed to collect pipelines: {e}") |
| 48 | |
| 49 | |
| 50 | async def get_pipeline_rules() -> PipelineRulesResponse: |
| 51 | """ |
| 52 | Get pipeline rules from Graylog. |
| 53 | |
| 54 | Returns: |
| 55 | PipelineRulesResponse: The response object containing the pipeline rules. |
| 56 | """ |
| 57 | logger.info("Getting pipeline rules from Graylog") |
| 58 | pipeline_rules_collected = await send_get_request( |
| 59 | endpoint="/api/system/pipelines/rule", |
| 60 | ) |
| 61 | try: |
| 62 | if pipeline_rules_collected["success"]: |
| 63 | pipeline_rules_list = [PipelineRule(**pipeline_rule_data) for pipeline_rule_data in pipeline_rules_collected["data"]] |
| 64 | return PipelineRulesResponse( |
| 65 | pipeline_rules=pipeline_rules_list, |
| 66 | success=True, |
| 67 | message="Pipeline rules collected successfully", |
| 68 | ) |
| 69 | except KeyError as e: |
| 70 | logger.error(f"Failed to collect pipeline rules key: {e}") |
| 71 | raise HTTPException( |
| 72 | status_code=500, |
| 73 | detail=f"Failed to collect pipeline rules key: {e}", |
| 74 | ) |
| 75 | except Exception as e: |
| 76 | logger.error(f"Failed to collect pipeline rules: {e}") |
| 77 | raise HTTPException( |
| 78 | status_code=500, |
| 79 | detail=f"Failed to collect pipeline rules: {e}", |
| 80 | ) |
| 81 | |
| 82 | |
| 83 | async def get_pipeline_rule_by_id(rule_id) -> PipelineRulesResponse: |
| 84 | """ |
| 85 | Get pipeline rules from Graylog by ID. |
| 86 | |
| 87 | Args: |
| 88 | rule_id (str): The ID of the pipeline rule. |
| 89 | |
| 90 | Returns: |
| 91 | PipelineRulesResponse: The response containing the pipeline rule. |
| 92 | |
| 93 | Raises: |
| 94 | HTTPException: If there is an error collecting the pipeline rules. |
| 95 | """ |
| 96 | logger.info(f"Getting pipeline rules from Graylog for pipeline {rule_id}") |
| 97 | pipeline_rules_collected = await send_get_request( |
| 98 | endpoint=f"/api/system/pipelines/rule/{rule_id}", |
| 99 | ) |
| 100 | logger.info(pipeline_rules_collected) |
| 101 | try: |
| 102 | if pipeline_rules_collected["success"]: |
| 103 | pipeline_rule = PipelineRule(**pipeline_rules_collected["data"]) |
| 104 | return PipelineRulesResponse( |
| 105 | pipeline_rules=[pipeline_rule], |
| 106 | success=True, |
| 107 | message="Pipeline rules collected successfully", |
| 108 | ) |
| 109 | except KeyError as e: |
| 110 | logger.error(f"Failed to collect pipeline rules key: {e}") |
| 111 | raise HTTPException( |
| 112 | status_code=500, |
| 113 | detail=f"Failed to collect pipeline rules key: {e}", |
| 114 | ) |
| 115 | except Exception as e: |
| 116 | logger.error(f"Failed to collect pipeline rules: {e}") |
| 117 | raise HTTPException( |
| 118 | status_code=500, |
| 119 | detail=f"Failed to collect pipeline rules: {e}", |
| 120 | ) |
| 121 | |
| 122 | |
| 123 | async def create_pipeline_rule(rule: CreatePipelineRule) -> None: |
| 124 | """ |
| 125 | Creates a pipeline rule with the given title. |
| 126 | """ |
| 127 | endpoint = "/api/system/pipelines/rule" |
| 128 | data = { |
| 129 | "title": rule.title, |
| 130 | "description": rule.description, |
| 131 | "source": rule.source, |
| 132 | } |
| 133 | await send_post_request(endpoint=endpoint, data=data) |
| 134 | |
| 135 | |
| 136 | async def create_pipeline_graylog(pipeline: CreatePipeline) -> None: |
| 137 | """ |
| 138 | Creates a pipeline with the given title in Graylog. |
| 139 | """ |
| 140 | endpoint = "/api/system/pipelines/pipeline" |
| 141 | data = { |
| 142 | "title": pipeline.title, |
| 143 | "description": pipeline.description, |
| 144 | "source": pipeline.source, |
| 145 | } |
| 146 | await send_post_request(endpoint=endpoint, data=data) |
| 147 | |
| 148 | |
| 149 | async def modify_pipeline_graylog(pipeline: ModifyPipeline) -> None: |
| 150 | """ |
| 151 | Modifies a pipeline with the given title in Graylog. |
| 152 | """ |
| 153 | endpoint = f"/api/system/pipelines/pipeline/{pipeline.pipeline_id}" |
| 154 | data = { |
| 155 | "source": pipeline.source, |
| 156 | } |
| 157 | await send_put_request(endpoint=endpoint, data=data) |
| 158 | |
| 159 | |
| 160 | async def get_pipeline_id(subscription: str) -> str: |
| 161 | """ |
| 162 | Retrieves the pipeline ID for a given subscription. |
| 163 | |
| 164 | Args: |
| 165 | subscription (str): The subscription name. |
| 166 | |
| 167 | Returns: |
| 168 | str: The pipeline ID. |
| 169 | |
| 170 | Raises: |
| 171 | HTTPException: If the pipeline ID cannot be retrieved. |
| 172 | """ |
| 173 | logger.info(f"Getting pipeline ID for subscription {subscription}") |
| 174 | pipelines_response = await get_pipelines() |
| 175 | if pipelines_response.success: |
| 176 | for pipeline in pipelines_response.pipelines: |
| 177 | if subscription.lower() in pipeline.description.lower(): |
| 178 | return [pipeline.id] |
| 179 | logger.error(f"Failed to get pipeline ID for subscription {subscription}") |
| 180 | raise HTTPException( |
| 181 | status_code=500, |
| 182 | detail=f"Failed to get pipeline ID for subscription {subscription}", |
| 183 | ) |
| 184 | else: |
| 185 | logger.error(f"Failed to get pipelines: {pipelines_response.message}") |
| 186 | raise HTTPException( |
| 187 | status_code=500, |
| 188 | detail=f"Failed to get pipelines: {pipelines_response.message}", |
| 189 | ) |
| 190 | |
| 191 | |
| 192 | async def connect_stream_to_pipeline( |
| 193 | stream_and_pipeline: StreamConnectionToPipelineRequest, |
| 194 | ): |
| 195 | """ |
| 196 | Connects a stream to a pipeline. |
| 197 | |
| 198 | Args: |
| 199 | stream_and_pipeline (StreamConnectionToPipelineRequest): The request object containing the stream ID and pipeline IDs. |
| 200 | |
| 201 | Returns: |
| 202 | StreamConnectionToPipelineResponse: The response object containing the connection details. |
| 203 | """ |
| 204 | logger.info( |
| 205 | f"Connecting stream {stream_and_pipeline.stream_id} to pipeline {stream_and_pipeline.pipeline_ids}", |
| 206 | ) |
| 207 | response_json = await send_post_request( |
| 208 | endpoint="/api/system/pipelines/connections/to_stream", |
| 209 | data=stream_and_pipeline.model_dump(), |
| 210 | ) |
| 211 | logger.info(f"Response: {response_json}") |
| 212 | return StreamConnectionToPipelineResponse(**response_json) |