Skip to content

Commit

Permalink
[COST-4333] Fix OCP on Azure matching by resource-ids (#4824)
Browse files Browse the repository at this point in the history
Co-authored-by: maskarb <[email protected]>
  • Loading branch information
esebesto and maskarb authored Dec 7, 2023
1 parent 7a33cb7 commit 61de77c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
5 changes: 2 additions & 3 deletions koku/masu/test/util/aws/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from uuid import uuid4

import boto3
import numpy as np
import pandas as pd
from botocore.exceptions import ClientError
from dateutil.relativedelta import relativedelta
Expand Down Expand Up @@ -755,7 +754,7 @@ def test_match_openshift_labels_with_nan_resources(self):

matched_tags = [{"key": "value"}]
data = [
{"lineitem_resourceid": np.nan, "lineitem_unblendedcost": 1, "resourcetags": '{"key": "value"}'},
{"lineitem_resourceid": "", "lineitem_unblendedcost": 1, "resourcetags": '{"key": "value"}'},
]

df = pd.DataFrame(data)
Expand All @@ -779,7 +778,7 @@ def test_match_openshift_resource_with_nan_labels(self):

matched_tags = [{"key": "value"}]
data = [
{"lineitem_resourceid": "id1", "lineitem_unblendedcost": 1, "resourcetags": np.nan},
{"lineitem_resourceid": "id1", "lineitem_unblendedcost": 1, "resourcetags": ""},
]

df = pd.DataFrame(data)
Expand Down
9 changes: 4 additions & 5 deletions koku/masu/test/util/azure/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# Copyright 2021 Red Hat Inc.
# SPDX-License-Identifier: Apache-2.0
#
import numpy as np
import pandas as pd
from django_tenants.utils import schema_context

Expand Down Expand Up @@ -116,9 +115,9 @@ def test_match_openshift_resources_and_labels_resource_nan(self):
]
matched_tags = []
data = [
{"resourceid": np.nan, "instanceid": "id1", "pretaxcost": 1, "tags": '{"key": "value"}'},
{"resourceid": np.nan, "instanceid": "id2", "pretaxcost": 1, "tags": '{"key": "other_value"}'},
{"resourceid": np.nan, "instanceid": "id3", "pretaxcost": 1, "tags": '{"keyz": "value"}'},
{"resourceid": "", "instanceid": "id1", "pretaxcost": 1, "tags": '{"key": "value"}'},
{"resourceid": "", "instanceid": "id2", "pretaxcost": 1, "tags": '{"key": "other_value"}'},
{"resourceid": "", "instanceid": "id3", "pretaxcost": 1, "tags": '{"keyz": "value"}'},
]

df = pd.DataFrame(data)
Expand Down Expand Up @@ -148,7 +147,7 @@ def test_match_openshift_resource_with_nan_labels(self):

matched_tags = [{"key": "value"}]
data = [
{"resourceid": "id1", "pretaxcost": 1, "tags": np.nan},
{"resourceid": "id1", "pretaxcost": 1, "tags": ""},
]

df = pd.DataFrame(data)
Expand Down
6 changes: 3 additions & 3 deletions koku/masu/util/aws/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -880,14 +880,14 @@ def match_openshift_resources_and_labels(data_frame, cluster_topologies, matched
resource_ids = tuple(resource_ids)
data_frame["resource_id_matched"] = False
resource_id_df = data_frame["lineitem_resourceid"]
if not resource_id_df.isna().values.all():
if not resource_id_df.eq("").all():
LOG.info("Matching OpenShift on AWS by resource ID.")
resource_id_matched = resource_id_df.str.endswith(resource_ids)
data_frame["resource_id_matched"] = resource_id_matched

data_frame["special_case_tag_matched"] = False
tags = data_frame["resourcetags"]
if not tags.isna().values.all():
if not tags.eq("").all():
tags = tags.str.lower()
LOG.info("Matching OpenShift on AWS by tags.")
special_case_tag_matched = tags.str.contains(
Expand All @@ -903,7 +903,7 @@ def match_openshift_resources_and_labels(data_frame, cluster_topologies, matched
tag_values.extend(list(tag.values()))

any_tag_matched = None
if not tags.isna().values.all():
if not tags.eq("").all():
tag_matched = tags.str.contains("|".join(tag_keys)) & tags.str.contains("|".join(tag_values))
data_frame["tag_matched"] = tag_matched
any_tag_matched = tag_matched.any()
Expand Down
8 changes: 4 additions & 4 deletions koku/masu/util/azure/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,17 @@ def match_openshift_resources_and_labels(data_frame, cluster_topologies, matched
matchable_resources = list(nodes) + list(volumes)
data_frame["resource_id_matched"] = False
resource_id_df = data_frame["resourceid"]
if resource_id_df.isna().values.all():
if resource_id_df.eq("").all():
resource_id_df = data_frame["instanceid"]

if not resource_id_df.isna().values.all():
if not resource_id_df.eq("").all():
LOG.info("Matching OpenShift on Azure by resource ID.")
resource_id_matched = resource_id_df.str.contains("|".join(matchable_resources))
data_frame["resource_id_matched"] = resource_id_matched

data_frame["special_case_tag_matched"] = False
tags = data_frame["tags"]
if not tags.isna().values.all():
if not tags.eq("").all():
tags = tags.str.lower()
LOG.info("Matching OpenShift on Azure by tags.")
special_case_tag_matched = tags.str.contains(
Expand All @@ -207,7 +207,7 @@ def match_openshift_resources_and_labels(data_frame, cluster_topologies, matched
tag_values.extend(list(tag.values()))

any_tag_matched = None
if not tags.isna().values.all():
if not tags.eq("").all():
tag_matched = tags.str.contains("|".join(tag_keys)) & tags.str.contains("|".join(tag_values))
data_frame["tag_matched"] = tag_matched
any_tag_matched = tag_matched.any()
Expand Down

0 comments on commit 61de77c

Please sign in to comment.