Merge "acloud: grab the build target name from the image name."
diff --git a/create/local_image_remote_instance.py b/create/local_image_remote_instance.py
index 299576e..e3b33b9 100644
--- a/create/local_image_remote_instance.py
+++ b/create/local_image_remote_instance.py
@@ -51,6 +51,7 @@
             "-l %(login_user)s %(ip_addr)s ")
 _SSH_CMD_MAX_RETRY = 2
 _SSH_CMD_RETRY_SLEEP = 3
+_USER_BUILD = "userbuild"
 
 class RemoteInstanceDeviceFactory(base_device_factory.BaseDeviceFactory):
     """A class that can produce a cuttlefish device.
@@ -127,13 +128,19 @@
         """Create a single configured cuttlefish device.
 
         Override method from parent class.
+        build_target: The format is like "aosp_cf_x86_phone". We only get info
+                      from the user build image file name. If the file name is
+                      not custom format (no "-"), We will use the original
+                      flavor as our build_target.
 
         Returns:
             A string, representing instance name.
         """
-        #TODO(117487673): Grab the build target name from the image name.
+        image_name = os.path.basename(self._local_image_artifact)
+        build_target = self._avd_spec.flavor if "-" not in image_name else image_name.split(
+            "-")[0]
         instance = self._compute_client.GenerateInstanceName(
-            build_target=self._avd_spec.flavor, build_id="local")
+            build_target=build_target, build_id=_USER_BUILD)
         # Create an instance from Stable Host Image
         self._compute_client.CreateInstance(
             instance=instance,
diff --git a/create/local_image_remote_instance_test.py b/create/local_image_remote_instance_test.py
index ec91b45..e54fc19 100644
--- a/create/local_image_remote_instance_test.py
+++ b/create/local_image_remote_instance_test.py
@@ -18,6 +18,7 @@
 Create class that is responsible for creating a remote instance AVD with a
 local image.
 """
+import uuid
 
 import subprocess
 import time
@@ -26,6 +27,7 @@
 import mock
 
 from acloud import errors
+from acloud.create import avd_spec
 from acloud.create import local_image_remote_instance
 from acloud.internal.lib import auth
 from acloud.internal.lib import cvd_compute_client
@@ -89,6 +91,35 @@
         self.Patch(subprocess, "check_call", return_value=True)
         self.assertEqual(factory._ShellCmdWithRetry("fake cmd"), True)
 
+    # pylint: disable=protected-access
+    def testCreateGceInstanceName(self):
+        """test create gce instance."""
+        # Mock uuid
+        args = mock.MagicMock()
+        args.local_image = "/tmp/path"
+        args.config_file = ""
+        args.avd_type = "cf"
+        args.flavor = "phone"
+        fake_avd_spec = avd_spec.AVDSpec(args)
+
+        fake_uuid = mock.MagicMock(hex="1234")
+        self.Patch(uuid, "uuid4", return_value=fake_uuid)
+        self.Patch(cvd_compute_client.CvdComputeClient, "CreateInstance")
+        fake_host_package_name = "/fake/host_package.tar.gz"
+        fake_image_name = "/fake/aosp_cf_x86_phone-img-eng.username.zip"
+
+        factory = local_image_remote_instance.RemoteInstanceDeviceFactory(
+            fake_avd_spec,
+            fake_image_name,
+            fake_host_package_name)
+        self.assertEqual(factory._CreateGceInstance(), "ins-1234-userbuild-aosp-cf-x86-phone")
+
+        fake_image_name = "/fake/aosp_cf_x86_phone.username.zip"
+        factory = local_image_remote_instance.RemoteInstanceDeviceFactory(
+            fake_avd_spec,
+            fake_image_name,
+            fake_host_package_name)
+        self.assertEqual(factory._CreateGceInstance(), "ins-1234-userbuild-phone")
 
 if __name__ == "__main__":
     unittest.main()