Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion codecarbon/external/geography.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def extract_gcp_region(zone: str) -> str:
"""
projects/705208488469/zones/us-central1-a -> us-central1
"""
google_region_regex = r"[a-z]+-[a-z]+[0-9]"
google_region_regex = r"[a-z]+-[a-z]+[0-9]+"
return re.search(google_region_regex, zone).group(0)

extract_region_for_provider: Dict[str, Callable] = {
Expand Down
15 changes: 15 additions & 0 deletions tests/test_geography.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest
from unittest import mock

import pytest
import responses

from codecarbon.external.geography import CloudMetadata, GeoMetadata
Expand All @@ -15,6 +16,20 @@
)


@pytest.mark.parametrize(
"region", ["europe-west12", "europe-west10", "us-central1", "asia-northeast3"]
)
@pytest.mark.parametrize("prefix", ["", "projects/123/zones/"])
def test_cloud_metadata_gcp_region_number(region, prefix):
metadata = {"provider": "gcp", "metadata": {"zone": f"{prefix}{region}-a"}}
with mock.patch(
"codecarbon.external.geography.get_env_cloud_details", return_value=metadata
):
cloud = CloudMetadata.from_utils()
assert cloud.provider == "gcp"
assert cloud.region == region


class TestCloudMetadata(unittest.TestCase):
@mock.patch(
"codecarbon.external.geography.get_env_cloud_details",
Expand Down
Loading