pydo icon indicating copy to clipboard operation
pydo copied to clipboard

Filter snapshots by prefix name not working properly

Open agusmakmun opened this issue 8 months ago • 6 comments

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

agusmakmun avatar Apr 15 '25 08:04 agusmakmun

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)

kishlay-singh-DO avatar Apr 21 '25 16:04 kishlay-singh-DO

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.

agusmakmun avatar Apr 23 '25 10:04 agusmakmun

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)

kishlay-singh-DO avatar May 05 '25 06:05 kishlay-singh-DO

Thanks @kishlay-singh-DO, yes right now we're using that strategy for temporay solution.

agusmakmun avatar May 07 '25 06:05 agusmakmun

Hi! I’m interested in working on this issue. Could you please assign it to me? Thank you!

Raj-Dwivedi2005 avatar Oct 08 '25 21:10 Raj-Dwivedi2005

/assign

kushagras22 avatar Oct 12 '25 12:10 kushagras22