Skip to content

Instantly share code, notes, and snippets.

@rssnyder
Created October 23, 2023 13:44
Show Gist options
  • Save rssnyder/88e2fac45574ccfb4c975649cda034d4 to your computer and use it in GitHub Desktop.
Save rssnyder/88e2fac45574ccfb4c975649cda034d4 to your computer and use it in GitHub Desktop.
create a perspective for a group of aws accounts using python
from os import getenv
from requests import post
HEADERS = {"x-api-key": getenv("HARNESS_PLATFORM_API_KEY")}
PARAMS = {
"routingId": getenv("HARNESS_ACCOUNT_ID"),
"accountIdentifier": getenv("HARNESS_ACCOUNT_ID"),
}
def create_perspective(name: str, account_ids: list):
"""
Create a perspective for a number of aws accounts
name: name of the target perspective
account_ids: a list of account ids formatted as strings
"""
resp = post(
"https://app.harness.io/gateway/ccm/api/perspective",
headers=HEADERS,
params=PARAMS,
json={
"viewVersion": "v1",
"viewTimeRange": {"viewTimeRangeType": "LAST_7"},
"viewType": "CUSTOMER",
"viewVisualization": {
"groupBy": {
"fieldId": "product",
"fieldName": "Product",
"identifier": "COMMON",
"identifierName": "COMMON",
},
"chartType": "STACKED_LINE_CHART",
},
"name": name,
"viewRules": [
{
"viewConditions": [
{
"type": "VIEW_ID_CONDITION",
"viewField": {
"fieldId": "awsUsageaccountid",
"fieldName": "Account",
"identifierName": "AWS",
"identifier": "AWS",
},
"viewOperator": "IN",
"values": account_ids,
}
]
}
],
"viewPreferences": {
"showAnomalies": True,
"includeOthers": False,
"includeUnallocatedCost": False,
},
},
)
if resp.status_code == 200:
print(resp.json().get("status"))
return True
else:
print(resp.text)
return False
if __name__ == "__main__":
create_perspective("this_is_a_test", ["759984737373"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment