diff --git a/csm_test_utils/rds/rds_backup.py b/csm_test_utils/rds/rds_backup.py index 437ff03..74e2aa0 100644 --- a/csm_test_utils/rds/rds_backup.py +++ b/csm_test_utils/rds/rds_backup.py @@ -23,35 +23,41 @@ def get_auth_token(endpoint, cloud_config, cloud_name): """Get auth token using data from clouds.yaml file. Token and project_id are returned as a string""" - with open(cloud_config) as clouds_yaml: - data = yaml.safe_load(clouds_yaml) - auth_data = data['clouds'][cloud_name]['auth'] - request_headers = {'Content-Type': CONTENT_TYPE} - request_body = json.dumps({ - 'auth': { - 'identity': { - 'methods': ['password'], - 'password': { - 'user': { - 'name': auth_data['username'], - 'password': auth_data['password'], - 'domain': { - 'name': auth_data['domain_name'] - } - } - } - }, - 'scope': { - 'project': { - 'name': auth_data['project_name'] - } - } - } + try: + with open(cloud_config) as clouds_yaml: + data = yaml.safe_load(clouds_yaml) + auth_data = data['clouds'][cloud_name]['auth'] + request_headers = {'Content-Type': CONTENT_TYPE} + request_body = json.dumps({ + 'auth': { + 'identity': { + 'methods': ['password'], + 'password': { + 'user': { + 'name': auth_data['username'], + 'password': auth_data['password'], + 'domain': { + 'name': auth_data['domain_name'] + } + } + } + }, + 'scope': { + 'project': { + 'name': auth_data['project_name'] + } + } + } }) - url = "/".join([endpoint, API_VERSION, "auth/tokens"]) - response = requests.post(url = url, data = request_body, headers = request_headers) - token = response.headers.get('X-Subject-Token') - project_id = response.json()['token']['project']['id'] + url = "/".join([endpoint, API_VERSION, "auth/tokens"]) + try: + response = requests.post(url = url, data = request_body, headers = request_headers) + token = response.headers.get('X-Subject-Token') + project_id = response.json()['token']['project']['id'] + except requests.exceptions as ex: + LOGGER.exception(ex) + except Exception as ex: + LOGGER.exception(ex) return token, project_id @@ -59,7 +65,10 @@ def get_rds_backup_info(endpoint: str, token: str, project_id: str, **request_pa """Get full information about RDS backups""" url = "/".join([endpoint, API_VERSION, project_id, "backups?"]) request_headers = {'Content-Type': CONTENT_TYPE, 'X-Auth-Token': token} - response = requests.get(url = url, params = request_params, headers = request_headers) + try: + response = requests.get(url = url, params = request_params, headers = request_headers) + except requests.exceptions as ex: + LOGGER.exception(ex) return response @@ -77,7 +86,10 @@ def format_date_time(date_time: str) -> datetime: def get_rds_backup_status(endpoint: str, token: str, project_id: str, instance_id: str, backup_type: str) -> Response: """Return RDS backup status""" request_params = {'instance_id': instance_id, 'backup_type': backup_type} - response = get_rds_backup_info(endpoint, token, project_id, **request_params) + try: + response = get_rds_backup_info(endpoint, token, project_id, **request_params) + except requests.exceptions as ex: + LOGGER.exception(ex) return response @@ -107,8 +119,8 @@ def report(client: Client, endpoint: str, token: str, project_id: str, **request influx_row.add_tag("Status", "RDS Unavailable") influx_row.add_value("Value", Error) collection.append(influx_row) - except Exception as Ex: - return LOGGER.exception(Ex) + except Exception as ex: + return LOGGER.exception(ex) client.report_metric(collection) @@ -121,13 +133,15 @@ def report(client: Client, endpoint: str, token: str, project_id: str, **request def main(): args, _ = AGP.parse_known_args() - token, project_id = get_auth_token(args.endpoint, args.cloud_config, args.cloud_name) request_params = {'instance_id': args.instance_id, 'backup_type': 'auto'} client = Client(args.target, args.telegraf) setup_logger(LOGGER, "rds_backup_monitor", log_dir = args.log_dir, log_format = "[%(asctime)s] %(message)s") LOGGER.info(f"Started monitoring of {client.url} (telegraf at {client.tgf_address})") while True: try: + LOGGER.info("Generate token") + token, project_id = get_auth_token(args.endpoint, args.cloud_config, args.cloud_name) + LOGGER.info("Monitoring") report(client, args.endpoint, token, project_id, **request_params) time.sleep(3600) except KeyboardInterrupt: