Skip to content

Commit

Permalink
[COST-4844] Serializer update for ordering by storageclass (#5174)
Browse files Browse the repository at this point in the history
  • Loading branch information
myersCody authored and djnakabaale committed Jul 9, 2024
1 parent 2d56a40 commit 14f6e4d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 27 deletions.
18 changes: 13 additions & 5 deletions koku/api/report/ocp/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,19 @@
DISTRIBUTED_COST_INTERNAL = {"distributed_cost": "cost_total_distributed"}


def order_by_field_requires_group_by(data, order_name, group_by_key):
def order_by_field_requires_group_by(data, order_name, group_by_keys):
error = {}
if order_name in data.get("order_by", {}) and group_by_key not in data.get("group_by", {}):
error["order_by"] = gettext(f"Cannot order by field {order_name} without grouping by {group_by_key}.")
raise serializers.ValidationError(error)
if order_name in data.get("order_by", {}):
# Ensure group_by_keys is a list of keys to check
if not isinstance(group_by_keys, list):
group_by_keys = [group_by_keys]

# Check if none of the required group_by keys are present
if not any(key in data.get("group_by", {}) for key in group_by_keys):
error["order_by"] = gettext(
f"Cannot order by field {order_name} without grouping by one of {', '.join(group_by_keys)}."
)
raise serializers.ValidationError(error)


class OCPGroupBySerializer(GroupSerializer):
Expand Down Expand Up @@ -165,7 +173,7 @@ def validate(self, data):
error["order_by"] = gettext("Cannot order by delta without a delta param")
raise serializers.ValidationError(error)
order_by_field_requires_group_by(data, DISTRIBUTED_COST_INTERNAL["distributed_cost"], "project")
order_by_field_requires_group_by(data, "storage_class", "persistentvolumeclaim")
order_by_field_requires_group_by(data, "storage_class", ["persistentvolumeclaim", "storageclass"])
order_by_field_requires_group_by(data, "persistentvolumeclaim", "persistentvolumeclaim")
if data.get("delta") == DISTRIBUTED_COST_INTERNAL["distributed_cost"] and "project" not in data.get(
"group_by", {}
Expand Down
47 changes: 25 additions & 22 deletions koku/api/report/test/ocp/test_ocp_query_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,28 +131,31 @@ def test_cpu_memory_order_bys(self):

def test_storage_class_order_bys(self):
"""Test that we can order by the pvc values."""
url = "?group_by[project]=*&group_by[persistentvolumeclaim]=*&order_by[storage_class]=desc"
query_params = self.mocked_query_params(url, OCPVolumeView)
handler = OCPReportQueryHandler(query_params)
query_data = handler.execute_query()
self.assertIsNotNone(query_data.get("data"))
self.assertIsNotNone(query_data.get("total"))
self.assertIsNotNone(query_data["total"].get("storage_class"))
first_date = query_data["data"][0]
tested = False
for cluster in first_date.get("projects", []):
pvc_list = cluster.get("persistentvolumeclaims")
storage_class_order_result = []
expected = None
for pvc in pvc_list:
for pvc_value in pvc.get("values", []):
storage_class_order_result.append(pvc_value.get("storage_class"))
if not expected:
expected = deepcopy(storage_class_order_result)
expected.sort(reverse=True)
self.assertEqual(storage_class_order_result, expected)
tested = True
self.assertTrue(tested)
group_bys = ["persistentvolumeclaim", "storageclass"]
for group_by in group_bys:
with self.subTest(group_by=group_by):
url = f"?group_by[project]=*&group_by[{group_by}]=*&order_by[storage_class]=desc"
query_params = self.mocked_query_params(url, OCPVolumeView)
handler = OCPReportQueryHandler(query_params)
query_data = handler.execute_query()
self.assertIsNotNone(query_data.get("data"))
self.assertIsNotNone(query_data.get("total"))
self.assertIsNotNone(query_data["total"].get("storage_class"))
first_date = query_data["data"][0]
tested = False
for project in first_date.get("projects", []):
group_list = project.get(f"{group_by}s")
storage_class_order_result = []
expected = None
for element in group_list:
for element_value in element.get("values", []):
storage_class_order_result.append(element_value.get("storage_class"))
if not expected:
expected = deepcopy(storage_class_order_result)
expected.sort(reverse=True)
self.assertEqual(storage_class_order_result, expected)
tested = True
self.assertTrue(tested)

def test_persistentvolumeclaim_order_by(self):
"""Test that we can order by the pvc values."""
Expand Down

0 comments on commit 14f6e4d

Please sign in to comment.