Skip to content

Commit c071ad6

Browse files
authored
Merge pull request #269 from QuanMPhm/262/cluster_name
Add internal cluster name as a resource attribute
2 parents 95fc76b + 3e16e51 commit c071ad6

File tree

7 files changed

+52
-28
lines changed

7 files changed

+52
-28
lines changed

src/coldfront_plugin_cloud/attributes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class CloudAllocationAttribute:
3434
RESOURCE_DEFAULT_NETWORK_CIDR = "OpenStack Default Network CIDR"
3535

3636
RESOURCE_EULA_URL = "EULA URL"
37+
RESOURCE_CLUSTER_NAME = "Internal Cluster Name"
3738

3839
RESOURCE_ATTRIBUTES = [
3940
CloudResourceAttribute(name=RESOURCE_AUTH_URL),
@@ -48,6 +49,7 @@ class CloudAllocationAttribute:
4849
CloudResourceAttribute(name=RESOURCE_EULA_URL),
4950
CloudResourceAttribute(name=RESOURCE_DEFAULT_PUBLIC_NETWORK),
5051
CloudResourceAttribute(name=RESOURCE_DEFAULT_NETWORK_CIDR),
52+
CloudResourceAttribute(name=RESOURCE_CLUSTER_NAME),
5153
]
5254

5355
# TODO: Migration to rename the OpenStack specific prefix out of these attrs

src/coldfront_plugin_cloud/management/commands/add_openshift_resource.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ def add_arguments(self, parser):
2424
parser.add_argument(
2525
"--name", type=str, required=True, help="Name of OpenShift resource"
2626
)
27+
parser.add_argument(
28+
"--internal-name",
29+
type=str,
30+
required=False,
31+
help="Internal name of cluster used for invoicing. Defaults to public name",
32+
)
2733
parser.add_argument(
2834
"--api-url",
2935
type=str,
@@ -99,3 +105,12 @@ def handle(self, *args, **options):
99105
resource=openshift,
100106
value="true" if options["ibm_storage_available"] else "false",
101107
)
108+
ResourceAttribute.objects.get_or_create(
109+
resource_attribute_type=ResourceAttributeType.objects.get(
110+
name=attributes.RESOURCE_CLUSTER_NAME
111+
),
112+
resource=openshift,
113+
value=options["internal_name"]
114+
if options["internal_name"]
115+
else options["name"],
116+
)

src/coldfront_plugin_cloud/management/commands/add_openstack_resource.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ def add_arguments(self, parser):
1717
parser.add_argument(
1818
"--name", type=str, required=True, help="Name of OpenStack resource"
1919
)
20+
parser.add_argument(
21+
"--internal-name",
22+
type=str,
23+
required=False,
24+
help="Internal name of cluster used for invoicing. Defaults to public name",
25+
)
2026
parser.add_argument(
2127
"--auth-url",
2228
type=str,
@@ -133,6 +139,15 @@ def handle(self, *args, **options):
133139
resource=openstack,
134140
value=options["role"],
135141
)
142+
ResourceAttribute.objects.get_or_create(
143+
resource_attribute_type=ResourceAttributeType.objects.get(
144+
name=attributes.RESOURCE_CLUSTER_NAME
145+
),
146+
resource=openstack,
147+
value=options["internal_name"]
148+
if options["internal_name"]
149+
else options["name"],
150+
)
136151

137152
# Quantity values do not make sense for an ESI allocation
138153
if not options["esi"]:

src/coldfront_plugin_cloud/management/commands/calculate_storage_gb_hours.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@
2020

2121
_RATES = None
2222

23-
RESOURCE_NAME_TO_NERC_SERVICE = {
24-
"NERC": "stack",
25-
"NERC-OCP": "ocp-prod",
26-
"NERC-OCP-EDU": "academic",
27-
}
28-
2923

3024
def get_rates():
3125
# nerc-rates doesn't work with Python 3.9, which is what ColdFront is currently
@@ -197,23 +191,22 @@ def upload_to_s3(s3_endpoint, s3_bucket, file_location, invoice_month):
197191
logger.info(f"Uploaded to {secondary_location}.")
198192

199193
def handle(self, *args, **options):
200-
def get_outages_for_service(resource_name: str):
194+
def get_outages_for_service(cluster_name: str):
201195
"""Get outages for a service from nerc-rates.
202196
203-
:param resource_name: Name of the resource to get outages for.
197+
:param cluster_name: Name of the cluster to get outages for.
204198
:return: List of excluded intervals or None.
205199
"""
206-
service_name = RESOURCE_NAME_TO_NERC_SERVICE.get(resource_name)
207-
if service_name:
208-
return utils.load_outages_from_nerc_rates(
209-
options["start"], options["end"], service_name
210-
)
211-
return None
200+
return utils.load_outages_from_nerc_rates(
201+
options["start"], options["end"], cluster_name
202+
)
212203

213204
def process_invoice_row(allocation, attrs, su_name, rate):
214205
"""Calculate the value and write the bill using the writer."""
215-
resource_name = allocation.resources.first().name
216-
excluded_intervals_list = get_outages_for_service(resource_name)
206+
internal_cluster_name = allocation.resources.first().get_attribute(
207+
attributes.RESOURCE_CLUSTER_NAME
208+
)
209+
excluded_intervals_list = get_outages_for_service(internal_cluster_name)
217210

218211
time = 0
219212
for attribute in attrs:
@@ -234,7 +227,7 @@ def process_invoice_row(allocation, attrs, su_name, rate):
234227
attributes.ALLOCATION_PROJECT_ID
235228
),
236229
PI=allocation.project.pi.email,
237-
Cluster_Name=allocation.resources.first().name,
230+
Cluster_Name=internal_cluster_name,
238231
Institution_Specific_Code=allocation.get_attribute(
239232
attributes.ALLOCATION_INSTITUTION_SPECIFIC_CODE
240233
)

src/coldfront_plugin_cloud/tests/base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,15 @@ def new_esi_resource(name=None, auth_url=None) -> Resource:
6363
return Resource.objects.get(name=resource_name)
6464

6565
@staticmethod
66-
def new_openstack_resource(name=None, auth_url=None) -> Resource:
66+
def new_openstack_resource(
67+
name=None, internal_name=None, auth_url=None
68+
) -> Resource:
6769
resource_name = name or uuid.uuid4().hex
6870

6971
call_command(
7072
"add_openstack_resource",
7173
name=resource_name,
74+
internal_name=internal_name,
7275
auth_url=auth_url or f"https://{resource_name}/identity/v3",
7376
projects_domain="default",
7477
users_domain="default",

src/coldfront_plugin_cloud/tests/unit/test_attribute_migration.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ def test_rename_identity_url(self):
124124
new_resource_attrs,
125125
):
126126
call_command("register_cloud_attributes")
127-
resource = self.new_openstack_resource("Example", auth_url_val)
127+
resource = self.new_openstack_resource(
128+
"Example", auth_url=auth_url_val
129+
)
128130

129131
self.assertEqual(
130132
resource.get_attribute(new_auth_url_name),

src/coldfront_plugin_cloud/tests/unit/test_calculate_quota_unit_hours.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,6 @@ def test_new_allocation_quota(self, mock_load_outages):
9292
"2020-03",
9393
)
9494

95-
# Verify that load_outages_from_nerc_rates is not called when resource name
96-
# doesn't match NERC service mapping
97-
mock_load_outages.assert_not_called()
98-
9995
def test_new_allocation_quota_expired(self):
10096
"""Test that expiration doesn't affect invoicing."""
10197
self.resource = self.new_openshift_resource(
@@ -607,10 +603,6 @@ def test_load_excluded_intervals_invalid(self):
607603
with self.assertRaises(AssertionError):
608604
utils.load_excluded_intervals(invalid_interval)
609605

610-
@patch(
611-
"coldfront_plugin_cloud.management.commands.calculate_storage_gb_hours.RESOURCE_NAME_TO_NERC_SERVICE",
612-
{"TEST-RESOURCE": "test-service"},
613-
)
614606
@patch(
615607
"coldfront_plugin_cloud.management.commands.calculate_storage_gb_hours.get_rates"
616608
)
@@ -633,7 +625,9 @@ def test_nerc_outages_integration(self, mock_rates_loader):
633625
with freezegun.freeze_time("2020-03-01"):
634626
user = self.new_user()
635627
project = self.new_project(pi=user)
636-
resource = self.new_openstack_resource(name="TEST-RESOURCE")
628+
resource = self.new_openstack_resource(
629+
name="TEST-RESOURCE", internal_name="test-service"
630+
)
637631
allocation = self.new_allocation(project, resource, 100)
638632
for attr, val in [
639633
(attributes.ALLOCATION_PROJECT_NAME, "test"),

0 commit comments

Comments
 (0)