Created
April 3, 2023 19:14
-
-
Save judell/4689503cd7423d2c6c726f4aed7df881 to your computer and use it in GitHub Desktop.
delete all but most recent 3 spc snapshots matching a title
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, requests, time | |
org_handle = "acme" | |
workspace_handle = "jon" | |
url = f"https://cloud.steampipe.io/api/v0/org/{org_handle}/workspace/{workspace_handle}/snapshot" | |
headers = { | |
"Authorization": f"Bearer {os.environ['STEAMPIPE_CLOUD_TOKEN']}" | |
} | |
# Fetch all snapshots with the specified title | |
snapshots = [] | |
next_token = None | |
while True: | |
params = {} | |
if next_token: | |
params["next_token"] = next_token | |
response = requests.get(url, headers=headers, params=params) | |
if response.status_code == 200: | |
json_data = response.json() | |
items = json_data.get("items", []) | |
for item in items: | |
if item.get("title") == "Custom query [fbyz8vhd]": | |
snapshots.append(item) | |
next_token = json_data.get("next_token") | |
if not next_token: | |
break | |
else: | |
print(f"Error: Unable to fetch data. HTTP status code: {response.status_code}") | |
break | |
snapshots.sort(key=lambda x: x["created_at"], reverse=True) | |
snapshots_to_remove = snapshots[3:] | |
for snapshot in snapshots_to_remove: | |
snapshot_id = snapshot["id"] | |
delete_url = f'https://cloud.steampipe.io/api/v0/org/{org_handle}/workspace/{workspace_handle}/snapshot/{snapshot_id}' | |
response = requests.delete(delete_url, headers=headers) | |
if response.status_code == 200: | |
print(f"Snapshot {snapshot_id} successfully deleted.") | |
else: | |
print(f"Error: Unable to delete snapshot {snapshot_id}. HTTP status code: {response.status_code}, errror: {response.content}") | |
time.sleep(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment