Dear All,
I am trying to connect to Cloudability using API but I dont know how to make it happen.
I tried with the API key and secret, I was able to get a auth_token, but when calling a list of cost reports (as an example), I was not able to get it, getting an Unauthorized response from the server.
I found some threads here, saying that I should use the Cloudability API Key instead of the Frontdoor one, so I enabled the API Key for Cloudability in Account Settings Preferences, and changed my code to use that API Key instead the Frontdoor one, and then was not able to connect.
Is there any documentation with examples in how we should do it? I didnt find anything that could illustrate it.
Below is my code, in python.
I appreciate any help ;-)
Thanks
Gines
import requests
import json
# ----------------------- STEP 1: AUTHENTICATION -----------------------
def get_auth_token(api_key, api_secret):
"""Authenticate with Cloudability and return the token."""
auth_url = "https://frontdoor.apptio.com/service/apikeylogin"
headers = {
"Accept": "application/json",
"Content-Type": "application/json"
}
payload = {
"keyAccess": api_key,
"keySecret": api_secret
}
response = requests.post(auth_url, json=payload, headers=headers)
# Debug: Print full response details
print("\n๐น Status Code:", response.status_code)
print("๐น Response Headers:", response.headers)
print("๐น Response Body:", response.text)
if response.status_code == 200:
auth_token = response.headers.get("apptio-opentoken")
if not auth_token:
# Fallback: Check if token is provided in the JSON body
try:
body = response.json()
auth_token = body.get("token")
except Exception as e:
print("Error parsing JSON:", e)
if auth_token:
print("\nโ
Authentication Successful!")
print("๐น Token:", auth_token)
return auth_token
else:
print("\nโ Authentication Failed! Token not found in response.")
else:
print("\nโ Authentication Failed! Status Code:", response.status_code)
print("๐น Response:", response.text)
return None
# ----------------------- STEP 2: FETCH COST REPORT -----------------------
def get_cost_report(auth_token):
"""Fetch cost report data using the retrieved authentication token."""
reports_url = "https://api.cloudability.com/v3/reporting/cost/run"
# Prepare the payload with properly formatted dates (YYYY-MM-DD)
payload = {
"start_date": "2024-04-01", # Replace with the actual beginning date of last quarter
"end_date": "2024-06-30", # Replace with the actual end date of last quarter
"dimensions": ["vendor"],
"metrics": ["total_adjusted_amortized_cost"]
}
# Use the token in the Authorization header with Bearer scheme
headers = {
"Authorization": f"Bearer {auth_token}",
"Accept": "application/json",
"Content-Type": "application/json"
}
# Disable redirects to ensure the header is not dropped if a redirect occurs
response = requests.post(reports_url, headers=headers, json=payload, allow_redirects=False)
print("\n๐น Status Code:", response.status_code)
print("๐น Response Headers:", response.headers)
if response.status_code == 200:
reports = response.json()
print("\n๐น Cost Report (Formatted JSON):")
print(json.dumps(reports, indent=4))
return reports
else:
print("\nโ Failed to retrieve cost report!")
print("๐น Response Body:", response.text)
return None
# ----------------------- MAIN EXECUTION -----------------------
if __name__ == "__main__":
# ๐ API Credentials (Store these securely in a .env file or environment variables)
API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXX"
API_SECRET = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
# Step 1: Get the authentication token
token = get_auth_token(API_KEY, API_SECRET)
if token:
# Step 2: Fetch the cost report using the token
get_cost_report(token)