hcloud-python icon indicating copy to clipboard operation
hcloud-python copied to clipboard

Wrong filters combination in client.servers.get_all

Open agusmakmun opened this issue 7 months ago • 1 comments

Bug Report

Current Behavior

I'm trying to using filter combination using hcloud-python, and found weird things, where the combination is not working as expected. I believe the conditionals should be using AND method to filter, not using OR.

  1. Filter by status running only => no servers.
  2. Filter by status running + machine name => found servers.

Input Code

In [1]: client.servers.get_all(name="devsecops-box-wgkx6y1e", status=["running"])
Out[1]: [<hcloud.servers.client.BoundServer object at 0x7f73a83c8d60>]

In [2]: client.servers.get_all(status=["running"])
Out[2]: []

Expected behavior/code

In [1]: client.servers.get_all(name="devsecops-box-wgkx6y1e", status=["running"])
Out[1]: [] <--- should be empty as well.

Environment

  • Python Version: 3.9
  • Hcloud-Python Version: 1.32.0

Possible Solution

I have checked to the code, and there is no problem with this SDK, probably because of wrong result of /servers in BE:

https://github.com/hetznercloud/hcloud-python/blob/main/hcloud/servers/client.py#L514C9-L514C85

response = self._client.request(url="/servers", method="GET", params=params)

Additional context/Screenshots

Platform HCloud Client
Screenshot 2023-12-05 at 20 09 31 Screenshot 2023-12-05 at 20 10 47

Alternatively, I'm using this script to handle the above issue:

try:
    # NOTE: have filter combination issue especially for status,
    # please remove below conditionals once it fixed.
    # https://github.com/hetznercloud/hcloud-python/issues/333
    servers: List[BoundServer] = self.client.servers.get_all(**params)
    if only_running:
        servers = [
            server
            for server in servers
            if server.status == HetznerServerStatusChoices.running
        ]
    if use_environment:
        servers = [
            server
            for server in servers
            if server.labels.get("environment") == self.environment
        ]
    return servers
except APIException:
    ...
return []

Please let me know once it fixed, so I can update my code.

agusmakmun avatar Dec 05 '23 13:12 agusmakmun