Skip to content

Commit

Permalink
Fix token issue (#27)
Browse files Browse the repository at this point in the history
* Fix token issue

* Add logging

* Add logging

* Change log msg
  • Loading branch information
irina-pereiaslavskaia authored Oct 26, 2020
1 parent 2e8c9f2 commit 1adcd5d
Showing 1 changed file with 47 additions and 33 deletions.
80 changes: 47 additions & 33 deletions csm_test_utils/rds/rds_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,52 @@

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


def get_rds_backup_info(endpoint: str, token: str, project_id: str, **request_params) -> Response:
"""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


Expand All @@ -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


Expand Down Expand Up @@ -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)


Expand All @@ -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:
Expand Down

0 comments on commit 1adcd5d

Please sign in to comment.