Skip to content

Commit

Permalink
gce: Use os.path.basename() instead of custom method
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardobranco777 committed Jun 16, 2023
1 parent a23872e commit 6e9ba1c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
5 changes: 3 additions & 2 deletions ocw/lib/db.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
}

Expand Down
7 changes: 2 additions & 5 deletions ocw/lib/gce.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from os.path import basename
from datetime import timezone
from dateutil.parser import parse
import googleapiclient.discovery
Expand Down Expand Up @@ -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"
Expand Down
5 changes: 3 additions & 2 deletions tests/test_db.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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']


Expand Down

0 comments on commit 6e9ba1c

Please sign in to comment.