[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/acloud_main.py b/public/acloud_main.py
index 4469366..4a7a7b1 100755
--- a/public/acloud_main.py
+++ b/public/acloud_main.py
@@ -321,6 +321,9 @@
     cfg = config_mgr.Load()
     cfg.OverrideWithArgs(args)
 
+    # Check access.
+    device_driver.CheckAccess(cfg)
+
     if args.which == CMD_CREATE:
         report = device_driver.CreateAndroidVirtualDevices(
             cfg,
diff --git a/public/config.py b/public/config.py
index ebc2927..a423c01 100755
--- a/public/config.py
+++ b/public/config.py
@@ -105,6 +105,9 @@
             for device, orientation in
             internal_cfg.device_default_orientation_map.iteritems()
         }
+        self.no_project_access_msg_map = {
+            project: msg for project, msg
+            in internal_cfg.no_project_access_msg_map.iteritems()}
         self.min_machine_size = internal_cfg.min_machine_size
         self.disk_image_name = internal_cfg.disk_image_name
         self.disk_image_mime_type = internal_cfg.disk_image_mime_type
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]