Python-chi Examples¶
Cleanup Non-leased Resources¶
Opt-in Cleanup using created_at¶
The above examples are explicitly opt-in, requring the user to set the “delete_at” tag to ensure that nothing is deleted unknowlingly. Alternatively, you can monitor the “created_at” field of resources to check for long-running resources such as in the following example.
from datetime import datetime, timedelta
from chi import util, server, network
MAX_AGE = 3 # days
for s in server.list_servers():
age = util.utcnow() - datetime.fromisoformat(s.created_at)
if age > timedelta(days=MAX_AGE):
confirm = input(f"Delete server {s.name}? Age is {age}. (yes/no): ").strip().lower()
if confirm == "yes":
s.delete()
for net in network.list_networks():
age = util.utcnow() - datetime.fromisoformat(net["created_at"])
if age > timedelta(days=MAX_AGE):
confirm = input(f"Delete the network {net['name']}? Age is {age}. (yes/no): ").strip().lower()
if confirm == "yes":
# nuke_network deletes subnets, routers, and the network itself
network.nuke_network(net_id)