diff --git a/ocw/lib/db.py b/ocw/lib/db.py index 823d3a34..c8a94c76 100644 --- a/ocw/lib/db.py +++ b/ocw/lib/db.py @@ -1,6 +1,7 @@ import json import traceback import logging +from os.path import basename from datetime import datetime, timedelta, timezone import dateutil.parser as dateparser from django.db import transaction @@ -99,9 +100,9 @@ def gce_extract_data(csp_instance, namespace: str, default_ttl: int) -> dict: 'id': csp_instance['id'], 'first_seen': first_seen, 'namespace': namespace, - 'region': GCE.url_to_name(csp_instance['zone']), + 'region': basename(csp_instance['zone']), 'provider': ProviderChoice.GCE, - 'type': GCE.url_to_name(csp_instance['machineType']), + 'type': basename(csp_instance['machineType']), 'default_ttl': default_ttl } diff --git a/ocw/lib/gce.py b/ocw/lib/gce.py index 79795c9d..4e8d3b6b 100644 --- a/ocw/lib/gce.py +++ b/ocw/lib/gce.py @@ -1,4 +1,5 @@ import json +from os.path import basename from datetime import timezone from dateutil.parser import parse import googleapiclient.discovery @@ -88,17 +89,13 @@ def list_zones(self, region) -> list: .get(project=self.project, region=region) .execute() ) - return [GCE.url_to_name(z) for z in region["zones"]] + return [basename(z) for z in region["zones"]] def delete_instance(self, instance_id, zone) -> None: self._delete_resource( self.compute_client().instances, instance_id, project=self.project, zone=zone, instance=instance_id ) - @staticmethod - def url_to_name(url) -> str: - return url[url.rindex("/")+1:] - @staticmethod def get_error_reason(error: "googleapiclient.errors.HttpError") -> str: reason = "unknown" diff --git a/tests/test_db.py b/tests/test_db.py index 9df4742a..f292f761 100644 --- a/tests/test_db.py +++ b/tests/test_db.py @@ -1,3 +1,4 @@ +from os.path import basename from ocw.lib.db import update_run, ec2_extract_data, gce_extract_data, azure_extract_data, delete_instance from webui.PCWConfig import PCWConfig from faker import Faker @@ -105,9 +106,9 @@ def test_gce_extract_data(extract_data): assert rez['id'] == csp_instance['id'] assert rez['first_seen'] == csp_instance['creationTimestamp'] assert rez['namespace'] == extract_data['namespace'] - assert rez['region'] == GCE.url_to_name(csp_instance['zone']) + assert rez['region'] == basename(csp_instance['zone']) assert rez['provider'] == ProviderChoice.GCE - assert rez['type'] == GCE.url_to_name(csp_instance['machineType']) + assert rez['type'] == basename(csp_instance['machineType']) assert rez['default_ttl'] == extract_data['default_ttl']