Skip to content

Commit

Permalink
ENH: specify required field when retrieving available gcp regions (#2033
Browse files Browse the repository at this point in the history
)
  • Loading branch information
fangchenli authored Nov 8, 2023
1 parent 1f4891f commit 748bb6a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
14 changes: 7 additions & 7 deletions src/_nebari/provider/cloud/google_cloud.py
Original file line number Diff line number Diff line change
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"]
["gcloud", "compute", "regions", "list", "--format=json(name)"]
)
data = json.loads(output.decode("utf-8"))
return {_["description"]: _["name"] for _ in data}
data = json.loads(output)
return {_["name"] for _ in data}


@functools.lru_cache()
Expand Down Expand Up @@ -293,9 +293,9 @@ def check_missing_service() -> None:
### 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 @@ -355,7 +355,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

0 comments on commit 748bb6a

Please sign in to comment.