Skip to content

Commit

Permalink
chore: adjust imports & cleanup unused gcp util functions (#118)
Browse files Browse the repository at this point in the history
* chore: adjust imports & cleanup unused gcp util functions

* chore: adjust imports & cleanup unused gcp util functions

* chore: adjust imports & cleanup unused gcp util functions
  • Loading branch information
jsilva authored Mar 13, 2024
1 parent 194be21 commit a85a84c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 38 deletions.
9 changes: 4 additions & 5 deletions src/wanna/core/services/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def __init__(
self.build_dir = self.work_dir / "build" / "docker"
self.wanna_project_name = wanna_project_name
self.project_id = gcp_profile.project_id
self.project_number = convert_project_id_to_project_number(self.project_id)
self.location = gcp_profile.region
self.docker_build_config_path = os.getenv(
"WANNA_DOCKER_BUILD_CONFIG", self.work_dir / "dockerbuild.yaml"
Expand Down Expand Up @@ -165,16 +166,15 @@ def _build_image(
docker_image_ref=docker_image_ref,
tags=tags,
)
project = convert_project_id_to_project_number(self.project_id)
build_id = op.metadata.build.id
base = "https://console.cloud.google.com/cloud-build/builds"
if self.cloud_build_workerpool:
link = (
base
+ f";region={self.cloud_build_workerpool_location}/{build_id}?project={project}"
+ f";region={self.cloud_build_workerpool_location}/{build_id}?project={self.project_number}"
)
else:
link = base + f"/{build_id}?project={project}"
link = base + f"/{build_id}?project={self.project_number}"
try:
op.result()
self._write_context_dir_checksum(
Expand Down Expand Up @@ -391,10 +391,9 @@ def _build_image_on_gcp_cloud_build(
DEFAULT_POLLING._timeout = self.cloud_build_timeout

if self.cloud_build_workerpool:
project_number = convert_project_id_to_project_number(self.project_id)
options = BuildOptions(
pool=BuildOptions.PoolOption(
name=f"projects/{project_number}/locations/{self.cloud_build_workerpool_location}"
name=f"projects/{self.project_number}/locations/{self.cloud_build_workerpool_location}"
f"/workerPools/{self.cloud_build_workerpool}"
)
)
Expand Down
42 changes: 9 additions & 33 deletions src/wanna/core/utils/gcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@

if should_validate:
# since is very slow to import all these, we do it only when validation is required
import google.auth
from google.auth.exceptions import DefaultCredentialsError
from google.cloud import storage
from google.cloud.compute import MachineTypesClient, ZonesClient
from google.cloud.compute_v1 import RegionsClient
from google.cloud.compute_v1.services.images import ImagesClient
from google.cloud.compute_v1 import (
ImagesClient,
MachineTypesClient,
RegionsClient,
ZonesClient,
)
from google.cloud.compute_v1.types import ListImagesRequest
from google.cloud.resourcemanager_v3.services.projects import ProjectsClient

from google.cloud import storage
from google.cloud.resourcemanager_v3.services.projects import ProjectsClient

from wanna.core.utils.credentials import get_credentials

Expand All @@ -26,32 +28,6 @@
)


def are_gcp_credentials_set() -> bool:
"""
Function to verify if the default GCP credentials can be abstracted
from environment.
Returns:
True if GCP credentials can be found, False otherwise
"""
try:
_credentials, _project_id = google.auth.default()
return True
except DefaultCredentialsError:
return False


def get_current_local_gcp_project_id() -> str:
"""
Get current local GCP default project.
Returns:
project_id: your current local GCP default project
"""
_, project_id = google.auth.default()
return project_id


def get_available_compute_machine_types(project_id: str, zone: str) -> List[str]:
"""
Get available GCP Compute Engine Machine Types based on project and zone.
Expand Down
9 changes: 9 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ def mock_pipeline_get_project_id():
yield _fixture


@pytest.fixture(scope="session", autouse=True)
def mock_docker_service_get_project_id():
with mock.patch(
"wanna.core.services.docker.convert_project_id_to_project_number",
mocks.mock_convert_project_id_to_project_number,
) as _fixture:
yield _fixture


@pytest.fixture(scope="session", autouse=True)
def mock_metrics_api():
with mock.patch(
Expand Down

0 comments on commit a85a84c

Please sign in to comment.