import json
import requests


def add_role_to_cloudcheckr(env, admin_api_key, account_name, role_arn):
	"""
	Uses the cross-account role created by the cloud formation stack to add it to CloudCheckr.
	Uses the edit_credential Admin API call.
	"""

	if role_arn is None:
		print("Role Arn from Cloudformation stack was not found, so not credentials were added to CloudCheckr")
		return None

	api_url = env + "/api/account.json/edit_credential"

	edit_credential_info = json.dumps({"use_account": account_name, "aws_role_arn": role_arn})

	r7 = requests.post(api_url, headers = {"Content-Type": "application/json", "access_key": admin_api_key}, data = edit_credential_info)

	if "Message" in r7.json():
		print("Successfully added the role " + str(role_arn) + " to the CloudCheckr Account " + account_name)
		print(r7.json())
	else:
		print("FAILED to add the role " + str(role_arn) + " to the CloudCheckr Account " + account_name)
		print(r7.json())
	return None