amend aws regions via api call
We monitor a number of AWS Accounts, using the AWS Account Cloud monitoring (These are within a single organisation, but were configured prior to the addition of organisations in LogicMonitor)
I am trying to use an API Call to gather the aws accounts and then to remove monitoring from unused regions.
I have some code to build a script to do this (so i could examine the output before making the changes, and test on one or two aws accounts.), but this is returning every device.
Can anyone help in refactoring the api calls to only change the regions where needed?
SCRIPT
##########################################################################
import requests
import json
# === CONFIGURATION ===
bearer_token = [bearer token]
company = [company] # e.g., 'yourcompany'
base_url = f'https://{company}.logicmonitor.com/santaba/rest'
# === HEADERS ===
def get_headers():
return {
'Authorization': f'Bearer {bearer_token}',
'Content-Type': 'application/json',
'Accept': 'application/json'
}
# === STEP 1: GET AWS ACCOUNTS FROM LOGICMONITOR ===
def get_aws_accounts():
resource_path = '/device/devices'
params = {
'filter': 'system.cloud.type:"AWS"',
'size': 1000
}
url = base_url + resource_path
response = requests.get(url, headers=get_headers(), params=params)
try:
response.raise_for_status()
data = response.json()
print("Raw response:")
print(json.dumps(data, indent=4)) # <-- Add this line to inspect the structure
if 'data' in data and 'items' in data['data']:
return data['data']['items']
else:
print("Unexpected response format.")
return []
except requests.exceptions.HTTPError as http_err:
print(f"HTTP error occurred: {http_err}")
print(f"Response content: {response.text}")
return []
except Exception as err:
print(f"Other error occurred: {err}")
return []
# === STEP 2: PRINT UPDATE COMMANDS ===
def print_update_command(device_id, regions_to_keep):
resource_path = f'/device/devices/{device_id}'
url = base_url + resource_path
payload = {
"properties": [
{
"name": "aws.regions",
"value": ",".join(regions_to_keep)
}
]
}
print(f"\nPATCH {url}")
print("Headers:")
print(json.dumps(get_headers(), indent=4))
print("Payload:")
print(json.dumps(payload, indent=4))
def write_patch_request_to_file(device_id, regions_to_keep, device_name):
url = f"{base_url}/device/devices/{device_id}"
headers = get_headers()
payload = {
"properties": [
{
"name": "aws.regions",
"value": ",".join(regions_to_keep)
}
]
}
curl_cmd = (
f"curl -X PATCH '{url}' "
f"-H 'Authorization: {headers['Authorization']}' "
f"-H 'Content-Type: application/json' "
f"-H 'Accept: application/json' "
f"-d '{json.dumps(payload)}'"
)
with open("patch_requests.txt", "a") as f:
f.write(f"# PATCH request for {device_name} (ID: {device_id})\n")
f.write(curl_cmd + "\n\n")
# === MAIN EXECUTION ===
if __name__ == '__main__':
unused_regions = ['us-east-2', 'us-west-1', 'us-west-2', 'eu-central-1', 'eu-central-2', 'eu-north-1', 'eu-north-2', 'eu-south-1', 'eu-south-2', 'eu-west-1', 'eu-west-3', 'ap-east-1', 'ap-northeast-1', 'ap-northeast-2', 'ap-northeast-3', 'ap-south-1', 'ap-south-2', 'ap-southeast-1', 'ap-southeast-2', 'ap-southeast-3', 'ap-southeast-4', 'af-south-1', 'ca-central-1', 'il-central-1', 'me-central-1', 'me-south-1', 'sa-east-1']
all_regions = ['us-east-1', 'us-east-2', 'us-west-1', 'us-west-2', 'eu-central-1', 'eu-central-2', 'eu-north-1', 'eu-north-2', 'eu-south-1', 'eu-south-2', 'eu-west-1', 'eu-west-2', 'eu-west-3', 'ap-east-1', 'ap-northeast-1', 'ap-northeast-2', 'ap-northeast-3', 'ap-south-1', 'ap-south-2', 'ap-southeast-1', 'ap-southeast-2', 'ap-southeast-3', 'ap-southeast-4', 'af-south-1', 'ca-central-1', 'il-central-1', 'me-central-1', 'me-south-1', 'sa-east-1']
regions_to_keep = [r for r in all_regions if r not in unused_regions]
aws_accounts = get_aws_accounts()
for account in aws_accounts:
device_id = account['id']
name = account['displayName']
write_patch_request_to_file(device_id, regions_to_keep, name)
############################################################################