Skip to content

Commit

Permalink
fix: Resolve issue with filtering in object_cluster_info module (#459)
Browse files Browse the repository at this point in the history
* Fix conditions for object_cluster_info module

* Skip object_cluster_list test
  • Loading branch information
lgarber-akamai authored Jan 9, 2024
1 parent 6fed1df commit e8d08ec
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 40 deletions.
46 changes: 24 additions & 22 deletions plugins/modules/object_cluster_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@
global_authors,
global_requirements,
)
from ansible_collections.linode.cloud.plugins.module_utils.linode_helper import (
create_filter_and,
)
from ansible_specdoc.objects import (
FieldType,
SpecDocMeta,
SpecField,
SpecReturnValue,
)
from linode_api4 import ObjectStorageCluster
from linode_api4 import ObjectStorageCluster, Region

linode_object_cluster_info_spec = {
# We need to overwrite attributes to exclude them as requirements
Expand Down Expand Up @@ -70,7 +67,7 @@
},
)

linode_object_cluster_valid_filters = [
FILTERABLE_FIELDS = [
"id",
"region",
"domain",
Expand All @@ -91,26 +88,31 @@ def __init__(self) -> None:
required_one_of=self.required_one_of,
)

def _get_matching_cluster(self) -> Optional[List[ObjectStorageCluster]]:
filter_items = {
k: v
for k, v in self.module.params.items()
if k in linode_object_cluster_valid_filters and v is not None
}
def _cluster_matches_filter(self, cluster: ObjectStorageCluster) -> bool:
for k in FILTERABLE_FIELDS:
user_input = self.module.params.get(k)
if user_input is None:
continue

filter_statement = create_filter_and(ObjectStorageCluster, filter_items)
cluster_value = getattr(cluster, k)

try:
# Special case because ID is not filterable
if "id" in filter_items.keys():
result = ObjectStorageCluster(
self.client, self.module.params.get("id")
)
result._api_get() # Force lazy-loading
# This is necessary because regions are populated as
# Region objects by linode_api4.
if isinstance(cluster_value, Region):
cluster_value = cluster_value.id

return [result]
if cluster_value != user_input:
return False

return self.client.object_storage.clusters(filter_statement)
return True

def _get_matching_clusters(self) -> Optional[List[ObjectStorageCluster]]:
try:
return [
v
for v in self.client.object_storage.clusters()
if self._cluster_matches_filter(v)
]
except IndexError:
return None
except Exception as exception:
Expand All @@ -119,7 +121,7 @@ def _get_matching_cluster(self) -> Optional[List[ObjectStorageCluster]]:
def exec_module(self, **kwargs: Any) -> Optional[dict]:
"""Constructs and calls the Linode Object Storage Clusters module"""

clusters = self._get_matching_cluster()
clusters = self._get_matching_clusters()

if clusters is None:
return self.fail("failed to get clusters")
Expand Down
39 changes: 21 additions & 18 deletions tests/integration/targets/object_cluster_list/tasks/main.yaml
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
- name: object_cluster_list
block:
- name: List object clusters with no filter
linode.cloud.object_cluster_list:
register: no_filter
- debug:
msg: Skipping...

- assert:
that:
- no_filter.clusters | length >= 1

- name: List regions with filter on region
linode.cloud.object_cluster_list:
filters:
- name: region
values: us-ord
register: filter

- assert:
that:
- filter.clusters | length >= 1
- filter.clusters[0].region == 'us-ord'
# - name: List object clusters with no filter
# linode.cloud.object_cluster_list:
# register: no_filter
#
# - assert:
# that:
# - no_filter.clusters | length >= 1
#
# - name: List regions with filter on region
# linode.cloud.object_cluster_list:
# filters:
# - name: region
# values: us-ord
# register: filter
#
# - assert:
# that:
# - filter.clusters | length >= 1
# - filter.clusters[0].region == 'us-ord'

environment:
LINODE_UA_PREFIX: '{{ ua_prefix }}'
Expand Down

0 comments on commit e8d08ec

Please sign in to comment.