|
| 1 | +import json |
| 2 | +import os |
| 3 | + |
| 4 | +from robot.api import logger |
| 5 | +from robot.api.deco import keyword |
| 6 | + |
| 7 | +from resources.errors import ConfigurationError |
| 8 | +from resources.minder_restapi_lib import MinderRestApiLib |
| 9 | + |
| 10 | + |
| 11 | +class DataSources: |
| 12 | + def __init__(self): |
| 13 | + self.rest_api = MinderRestApiLib() |
| 14 | + self.datasources = dict() |
| 15 | + |
| 16 | + @keyword |
| 17 | + def client_lists_data_sources(self): |
| 18 | + """Lists available data sources.""" |
| 19 | + project = os.getenv("MINDER_PROJECT") |
| 20 | + if not project: |
| 21 | + raise ConfigurationError("MINDER_PROJECT environment variable is not set") |
| 22 | + |
| 23 | + params = { |
| 24 | + "context.project_id": project, |
| 25 | + } |
| 26 | + |
| 27 | + return self.rest_api.get_request("/data_sources", params=params) |
| 28 | + |
| 29 | + @keyword |
| 30 | + def client_adds_a_data_source(self, data_source_name): |
| 31 | + project = os.getenv("MINDER_PROJECT") |
| 32 | + if not project: |
| 33 | + raise ConfigurationError("MINDER_PROJECT environment variable is not set") |
| 34 | + |
| 35 | + data_source = { |
| 36 | + "version": "v1", |
| 37 | + "type": "data-source", |
| 38 | + "name": data_source_name, |
| 39 | + "context": { |
| 40 | + "project_id": project, |
| 41 | + }, |
| 42 | + "rest": { |
| 43 | + "def": { |
| 44 | + "license": { |
| 45 | + "endpoint": "https://raw.githubusercontent.com/spdx/license-list-data/refs/heads/main/json/licenses.json", |
| 46 | + "parse": "json", |
| 47 | + "input_schema": {}, |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + try: |
| 54 | + resp = self.rest_api.post_request( |
| 55 | + "/data_source", data=json.dumps({"dataSource": data_source}) |
| 56 | + ) |
| 57 | + self.datasources[resp["dataSource"]["name"]] = resp["dataSource"] |
| 58 | + except Exception as e: |
| 59 | + logger.error(f"Failed to create data source: {str(e)}") |
| 60 | + raise |
| 61 | + |
| 62 | + @keyword |
| 63 | + def client_updates_a_data_source(self, data_source_name): |
| 64 | + project = os.getenv("MINDER_PROJECT") |
| 65 | + if not project: |
| 66 | + raise ConfigurationError("MINDER_PROJECT environment variable is not set") |
| 67 | + |
| 68 | + data_source = self.datasources[data_source_name] |
| 69 | + if not data_source: |
| 70 | + raise ConfigurationError(f"No data sources created for {data_source_name}") |
| 71 | + |
| 72 | + data_source["rest"]["def"]["license"]["endpoint"] = "https://github.com" |
| 73 | + logger.info(f"updating datasource {data_source}") |
| 74 | + |
| 75 | + try: |
| 76 | + resp = self.rest_api.put_request( |
| 77 | + "/data_source", |
| 78 | + data=json.dumps({"dataSource": data_source}), |
| 79 | + ) |
| 80 | + self.datasources[resp["dataSource"]["name"]] = resp["dataSource"] |
| 81 | + except Exception as e: |
| 82 | + logger.error(f"Failed to update data source: {str(e)}") |
| 83 | + raise |
| 84 | + |
| 85 | + @keyword |
| 86 | + def delete_data_source(self, data_source_id): |
| 87 | + """Delete a Minder data source by ID.""" |
| 88 | + project = os.getenv("MINDER_PROJECT") |
| 89 | + if not project: |
| 90 | + raise ConfigurationError("MINDER_PROJECT environment variable is not set") |
| 91 | + |
| 92 | + params = { |
| 93 | + "context.project_id": project, |
| 94 | + } |
| 95 | + |
| 96 | + try: |
| 97 | + logger.info( |
| 98 | + self.rest_api.delete_request(f"/data_source/{data_source_id}", params=params) |
| 99 | + ) |
| 100 | + except Exception as e: |
| 101 | + logger.error(f"Failed to delete data source: {str(e)}") |
| 102 | + raise |
| 103 | + |
| 104 | + @keyword |
| 105 | + def cleanup_minder_data_sources(self): |
| 106 | + """Deletes all created data sources.""" |
| 107 | + for datasource in self.datasources.values(): |
| 108 | + logger.info(f"cleaning up datasource {datasource}") |
| 109 | + self.delete_data_source(datasource["id"]) |
| 110 | + |
| 111 | + @keyword |
| 112 | + def data_sources_are_empty(self, data_sources): |
| 113 | + if len(data_sources["dataSources"]): |
| 114 | + raise ValueError("Results should be empty") |
| 115 | + |
| 116 | + @keyword |
| 117 | + def data_sources_are_not_empty(self, data_sources): |
| 118 | + if not len(data_sources["dataSources"]): |
| 119 | + raise ValueError("Results should not be empty") |
0 commit comments