Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: specify required field when retrieving available gcp regions #2033

Merged
merged 7 commits into from
Nov 8, 2023
16 changes: 8 additions & 8 deletions src/_nebari/provider/cloud/google_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json
import os
import subprocess
from typing import Dict, List
from typing import Dict, List, Set

from _nebari import constants
from _nebari.provider.cloud.commons import filter_by_highest_supported_k8s_version
Expand Down Expand Up @@ -30,14 +30,14 @@ def projects() -> Dict[str, str]:


@functools.lru_cache()
def regions(project: str) -> Dict[str, str]:
"""Return a dict of available regions."""
def regions() -> Set[str]:
"""Return a set of available regions."""
check_credentials()
output = subprocess.check_output(
["gcloud", "compute", "regions", "list", "--project", project, "--format=json"]
iameskild marked this conversation as resolved.
Show resolved Hide resolved
["gcloud", "compute", "regions", "list", "--format=json(name)"]
)
data = json.loads(output.decode("utf-8"))
return {_["description"]: _["name"] for _ in data}
iameskild marked this conversation as resolved.
Show resolved Hide resolved
data = json.loads(output)
return {_["name"] for _ in data}


@functools.lru_cache()
Expand Down Expand Up @@ -257,9 +257,9 @@ def gcp_cleanup(config: schema.Main):
### PYDANTIC VALIDATORS ###


def validate_region(project_id: str, region: str) -> str:
def validate_region(region: str) -> str:
"""Validate the GCP region is valid."""
available_regions = regions(project_id)
available_regions = regions()
if region not in available_regions:
raise ValueError(
f"Region {region} is not one of available regions {available_regions}"
Expand Down
2 changes: 1 addition & 1 deletion src/_nebari/stages/infrastructure/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ def validate_all(cls, values):
raise ValueError("The `google_cloud_platform.region` field is required.")

# validate region
google_cloud.validate_region(project_id, region)
google_cloud.validate_region(region)

# validate kubernetes version
kubernetes_version = values.get("kubernetes_version")
Expand Down
2 changes: 1 addition & 1 deletion src/_nebari/subcommands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ def check_cloud_provider_region(region: str, cloud_provider: str) -> str:
if not region:
region = GCP_DEFAULT_REGION
rich.print(DEFAULT_REGION_MSG.format(region=region))
if region not in google_cloud.regions(os.environ["PROJECT_ID"]):
if region not in google_cloud.regions():
raise ValueError(
f"Invalid region `{region}`. Please refer to the GCP docs for a list of valid regions: {GCP_REGIONS}"
)
Expand Down
Loading