[Acloud] Check if user has access to the project on start
Cherry-pick cl/148795271
TEST: Manually test the google3 version. can't test the android tree
version untill b/35918788 is fixed
BUG:32411892
Change-Id: I8920db33f82d035218ef608962849628bbc8d9eb
diff --git a/public/device_driver.py b/public/device_driver.py
index d0bdf35..0f2acca 100755
--- a/public/device_driver.py
+++ b/public/device_driver.py
@@ -563,3 +563,21 @@
r.AddError(str(e))
r.SetStatus(report.Status.FAIL)
return r
+
+
+def CheckAccess(cfg):
+ """Check if user has access.
+
+ Args:
+ cfg: An AcloudConfig instance.
+ """
+ credentials = auth.CreateCredentials(cfg, ALL_SCOPES)
+ compute_client = android_compute_client.AndroidComputeClient(
+ cfg, credentials)
+ logger.info("Checking if user has access to project %s", cfg.project)
+ if not compute_client.CheckAccess():
+ logger.error("User does not have access to project %s", cfg.project)
+ # Print here so that command line user can see it.
+ print "Looks like you do not have access to %s. " % cfg.project
+ if cfg.project in cfg.no_project_access_msg_map:
+ print cfg.no_project_access_msg_map[cfg.project]