Filter snapshots by prefix name not working properly
Hell, I'm trying to use the filter snapshots by prefix and it's not working. Can you please support it?
In [1]: client.snapshots.list(params={"name": "my-machine-sa6bnprz"})
Out[1]:
{'snapshots': [{'id': '184246254',
'name': 'my-machine-sa6bnprz',
'regions': ['nyc1'],
'created_at': '2025-04-15T08:30:45Z',
'resource_id': '489468876',
'resource_type': 'droplet',
'min_disk_size': 80,
'size_gigabytes': 2.46,
'tags': []}],
'links': {},
'meta': {'total': 1}}
# Getting empty results when the `name` is not exact same.
In [2]: client.snapshots.list(params={"name": "my-machine"})
Out[2]: {'snapshots': [], 'links': {}, 'meta': {'total': 0}}
In [3]: client.snapshots.list(params={"name": "my-machine-*"})
Out[3]: {'snapshots': [], 'links': {}, 'meta': {'total': 0}}
In [4]: client.snapshots.list(params={"name": "my-machine-"})
Out[4]: {'snapshots': [], 'links': {}, 'meta': {'total': 0}}
Right now, I'm using pydo==0.9.2
Hey @agusmakmun, We've communicated your feature request to the relevant product engineers. For now can you please try the python workaround for the same:-
from pydo import Client
# Initialize the DigitalOcean client
client = Client(token="your_digitalocean_api_token")
# Fetch all snapshots
response = client.snapshots.list()
# Filter snapshots by name prefix
prefix = "my-machine-"
filtered_snapshots = [
snapshot for snapshot in response["snapshots"] if snapshot["name"].startswith(prefix)
]
# Print the filtered snapshots
for snapshot in filtered_snapshots:
print(snapshot)
Hello @kishlay-singh-DO thanks for doing that, can we also filter snapshots by created date?
Because we have a case where we need to cleanup the backup snapshots that created more than 1/2/3 months.
Hi @agusmakmun I'll communicate this request with the corresponding team too. I did try the following pythonic workaround for this query. Could you please use this while we work on your request:-
from pydo import Client
from datetime import datetime
filter_date = datetime.strptime("2023-07-26T10:25:01Z","%Y-%m-%dT%H:%M:%SZ")
# Initialize the DigitalOcean client
client = Client(token="your_digitalocean_api_token")
# Fetch all snapshots
response = client.snapshots.list()
# Filter snapshots by created date
filtered_snapshots = [
snapshot for snapshot in response["snapshots"]
if datetime.strptime(snapshot["created_at"],"%Y-%m-%dT%H:%M:%SZ")>filter_date
]
# Print the filtered snapshots
for snapshot in filtered_snapshots:
print(snapshot)
Thanks @kishlay-singh-DO, yes right now we're using that strategy for temporay solution.
Hi! I’m interested in working on this issue. Could you please assign it to me? Thank you!
/assign