|
| 1 | +logger = logging.getLogger(__name__) |
| 2 | + |
| 3 | +from argo_config import ArgoConfig |
| 4 | +from argo_web_api import ArgoWebApi, TopoItem |
| 5 | +from argo_ams_library import ( |
| 6 | + AmsServiceException, |
| 7 | + AmsUser, |
| 8 | + AmsUserProject, |
| 9 | + ArgoMessagingService, |
| 10 | +) |
| 11 | +import requests |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +def check_hdfs(config: ArgoConfig, tenant_id: str, tenant_name: str) -> bool: |
| 16 | + """Checks if data for today exist in hdfs tenant folders""" |
| 17 | + |
| 18 | + logger.debug( |
| 19 | + f"tenant: {tenant_name} ({tenant_id}) - retrieving report information from web-api..." |
| 20 | + ) |
| 21 | + today = date.today().strftime('%Y-%m-%d') |
| 22 | + url = f"https://{config.hdfs_check_path}/{tenant_name}/mdata/{today}?op=LISTSTATUS" |
| 23 | + headers = { |
| 24 | + "x-api-key": tenant_access_token, |
| 25 | + "Accept": "application/json", |
| 26 | + } |
| 27 | + |
| 28 | + response = requests.get(url, headers=headers, timeout=REQUEST_TIMEOUT) |
| 29 | + response.raise_for_status() |
| 30 | + |
| 31 | + result = response.json().get("FileStatuses").get("FileStatus") |
| 32 | + if len(result) > 0: |
| 33 | + return True |
| 34 | + return False |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +def check_readiness(config: ArgoConfig, tenant_id: str, tenant_name: str): |
| 39 | + """Checks tenants readiness by doing web-api requests to see if topology and |
| 40 | + reports are defined and also by checking if data are present both in ams and hdfs""" |
| 41 | + |
| 42 | + web_api = ArgoWebApi(config) |
| 43 | + |
| 44 | + # get access token from config file |
| 45 | + tenant_token = config.tenants.get(tenant_name,{}).get("web_api_token") |
| 46 | + |
| 47 | + # check if topology exists |
| 48 | + topology_ready = True |
| 49 | + topology_msg = [] |
| 50 | + topo_endpoints = web_api.get_topology(tenant_id, tenant_name, tenant_token, TopoItem.ENDPOINTS) |
| 51 | + topo_groups = web_api.get_topology(tenant_id, tenant_name, tenant_token, TopoItem.GROUPS) |
| 52 | + topo_service_types = web_api.get_topology(tenant_id, tenant_name, tenant_token, TopoItem.SERVICE_TYPES) |
| 53 | + |
| 54 | + if len(topo_endpoints) > 0: |
| 55 | + topology_msg.append("Topology endpoints are set.") |
| 56 | + else: |
| 57 | + topology_msg.append("Topology endpoints are missing!") |
| 58 | + topology_ready = False |
| 59 | + |
| 60 | + if len(topo_groups) > 0: |
| 61 | + topology_msg.append("Topology groups are set.") |
| 62 | + else: |
| 63 | + topology_msg.append("Topology groups are missing!") |
| 64 | + topology_ready = False |
| 65 | + |
| 66 | + if len(topo_groups) > 0: |
| 67 | + topology_msg.append("Topology service-types are set.") |
| 68 | + else: |
| 69 | + topology_msg.append("Topology service-types are missing!") |
| 70 | + topology_ready = False |
| 71 | + |
| 72 | + # check reports |
| 73 | + reports_ready = True |
| 74 | + reports_msg = "Tenant has at least one report" |
| 75 | + |
| 76 | + reports = web_api.get_reports(tenant_id, tenant_name, tenant_token) |
| 77 | + if len(reports) < 0: |
| 78 | + reports_msg = "Tenant has no reports!" |
| 79 | + |
| 80 | + # check ams |
| 81 | + ams = ArgoMessagingService( |
| 82 | + endpoint=config.ams_endpoint, token=config.ams_admin_token, project=tenant_name |
| 83 | + ) |
| 84 | + |
| 85 | + topic_metric_data = ams.get_topic("metric_data") |
| 86 | + topic_metric_data ams.get |
0 commit comments