Check gcp project have billing account in acloud setup.
Bug: 133732464
Test: acloud-dev setup --gcp-init --force
P.S. Please use gcp project with billing disabled.
Change-Id: I0ff5ab1f960a5126d92d53091fc61ed41a3e4850
diff --git a/setup/gcp_setup_runner.py b/setup/gcp_setup_runner.py
index 0a1b281..c093926 100644
--- a/setup/gcp_setup_runner.py
+++ b/setup/gcp_setup_runner.py
@@ -36,12 +36,14 @@
_COMPUTE_ENGINE_SERVICE
]
_BUILD_SERVICE_ACCOUNT = "android-build-prod@system.gserviceaccount.com"
+_BILLING_ENABLE_MSG = "billingEnabled: true"
_DEFAULT_SSH_FOLDER = os.path.expanduser("~/.ssh")
_DEFAULT_SSH_KEY = "acloud_rsa"
_DEFAULT_SSH_PRIVATE_KEY = os.path.join(_DEFAULT_SSH_FOLDER,
_DEFAULT_SSH_KEY)
_DEFAULT_SSH_PUBLIC_KEY = os.path.join(_DEFAULT_SSH_FOLDER,
_DEFAULT_SSH_KEY + ".pub")
+_GCLOUD_COMPONENT_ALPHA = "alpha"
# Bucket naming parameters
_BUCKET_HEADER = "gs://"
_BUCKET_LENGTH_LIMIT = 63
@@ -247,6 +249,8 @@
google_sdk_init = google_sdk.GoogleSDK()
try:
google_sdk_runner = GoogleSDKBins(google_sdk_init.GetSDKBinPath())
+ google_sdk_init.InstallGcloudComponent(google_sdk_runner,
+ _GCLOUD_COMPONENT_ALPHA)
self._SetupProject(google_sdk_runner)
self._EnableGcloudServices(google_sdk_runner)
self._CreateStableHostImage()
@@ -311,6 +315,7 @@
Setup project and zone.
Setup client ID and client secret.
+ Make sure billing account enabled in project.
Setup Google Cloud Storage bucket.
Args:
@@ -321,6 +326,7 @@
project_changed = self._UpdateProject(gcloud_runner)
if self._NeedClientIDSetup(project_changed):
self._SetupClientIDSecret()
+ self._CheckBillingEnable(gcloud_runner)
self._SetupStorageBucket(gcloud_runner)
def _UpdateProject(self, gcloud_runner):
@@ -371,6 +377,31 @@
UpdateConfigFile(self.config_path, "client_id", self.client_id)
UpdateConfigFile(self.config_path, "client_secret", self.client_secret)
+ def _CheckBillingEnable(self, gcloud_runner):
+ """Check billing enabled in gcp project.
+
+ The billing info get by gcloud alpha command. Here is one example:
+ $ gcloud alpha billing projects describe project_name
+ billingAccountName: billingAccounts/011BXX-A30XXX-9XXXX
+ billingEnabled: true
+ name: projects/project_name/billingInfo
+ projectId: project_name
+
+ Args:
+ gcloud_runner: A GcloudRunner class to run "gcloud" command.
+
+ Raises:
+ NoBillingError: gcp project doesn't enable billing account.
+ """
+ billing_info = gcloud_runner.RunGcloud(
+ ["alpha", "billing", "projects", "describe", self.project])
+ if _BILLING_ENABLE_MSG not in billing_info:
+ raise errors.NoBillingError(
+ "Please set billing account to project(%s) by following the "
+ "instructions here: "
+ "https://cloud.google.com/billing/docs/how-to/modify-project"
+ % self.project)
+
def _SetupStorageBucket(self, gcloud_runner):
"""Setup storage_bucket_name in config file.