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: check missing GCP services #2036

Merged
merged 6 commits into from
Nov 8, 2023
Merged
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
38 changes: 37 additions & 1 deletion 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 @@ -89,6 +89,22 @@ def instances(project: str) -> Dict[str, str]:
return {_["description"]: _["name"] for _ in data}


def activated_services() -> Set[str]:
"""Return a list of activated services."""
check_credentials()
output = subprocess.check_output(
[
"gcloud",
"services",
"list",
"--enabled",
"--format=json(config.title)",
]
)
data = json.loads(output)
return {service["config"]["title"] for service in data}


def cluster_exists(cluster_name: str, project_id: str, region: str) -> bool:
"""Check if a GKE cluster exists."""
try:
Expand Down Expand Up @@ -250,6 +266,26 @@ def gcp_cleanup(config: schema.Main):
delete_service_account(service_account_name, project_id)


def check_missing_service() -> None:
"""Check if all required services are activated."""
required = {
"Compute Engine API",
"Kubernetes Engine API",
"Cloud Monitoring API",
"Cloud Autoscaling API",
"Identity and Access Management (IAM) API",
"Cloud Resource Manager API",
}
activated = activated_services()
common = required.intersection(activated)
missing = required.difference(common)
if missing:
raise ValueError(
f"""Missing required services: {missing}\n
Please see the documentation for more information: {constants.GCP_ENV_DOCS}"""
)


# Getting pricing data could come from here
# https://cloudpricingcalculator.appspot.com/static/data/pricelist.json

Expand Down
54 changes: 54 additions & 0 deletions tests/tests_unit/test_provider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from contextlib import nullcontext

import pytest

from _nebari.provider.cloud.google_cloud import check_missing_service


@pytest.mark.parametrize(
"activated_services, exception",
[
(
{
"Compute Engine API",
"Kubernetes Engine API",
"Cloud Monitoring API",
"Cloud Autoscaling API",
"Identity and Access Management (IAM) API",
"Cloud Resource Manager API",
},
nullcontext(),
),
(
{
"Compute Engine API",
"Kubernetes Engine API",
"Cloud Monitoring API",
"Cloud Autoscaling API",
"Identity and Access Management (IAM) API",
"Cloud Resource Manager API",
"Cloud SQL Admin API",
},
nullcontext(),
),
(
{
"Compute Engine API",
"Kubernetes Engine API",
"Cloud Monitoring API",
"Cloud Autoscaling API",
"Cloud SQL Admin API",
},
pytest.raises(ValueError, match=r"Missing required services:.*"),
),
],
)
def test_gcp_missing_service(monkeypatch, activated_services, exception):
def mock_return():
return activated_services

monkeypatch.setattr(
"_nebari.provider.cloud.google_cloud.activated_services", mock_return
)
with exception:
check_missing_service()