build api metrics: make sure we deserialize metrics in image Create

The code flow here was a bit hard to decipher due to the multiple return
statements in the method. I've cleaned it up a bit and moved the
deserialize_metrics_log call to the happy path before any return
statements.

BUG=chromium:1000449
TEST=api/contrib/call_scripts/sysroot__install_packages && api/contrib/call_scripts/image__create

Change-Id: I7e4e0ec00c0953da3cc8e36b6ada8272758442c1
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/1872178
Tested-by: Will Bradley <wbbradley@chromium.org>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Commit-Queue: Will Bradley <wbbradley@chromium.org>
diff --git a/api/controller/image.py b/api/controller/image.py
index 6296599..eda3123 100644
--- a/api/controller/image.py
+++ b/api/controller/image.py
@@ -80,9 +80,31 @@
                        config=build_config)
 
   output_proto.success = result.success
+
   if result.success:
     # Success -- we need to list out the images we built in the output.
     _PopulateBuiltImages(board, image_types, output_proto)
+
+    if vm_types:
+      # There are VMs to build.
+      assert len(vm_types) == 1
+
+      vm_type = vm_types.pop()
+      is_test = vm_type == _TEST_VM_ID
+      try:
+        vm_path = image.CreateVm(board, is_test=is_test)
+      except image.ImageToVmError as e:
+        cros_build_lib.Die(e)
+
+      new_image = output_proto.images.add()
+      new_image.path = vm_path
+      new_image.type = vm_type
+      new_image.build_target.name = board
+
+    # Read metric events log and pipe them into output_proto.events.
+    deserialize_metrics_log(output_proto.events, prefix=board)
+    return controller.RETURN_CODE_SUCCESS
+
   else:
     # Failure, include all of the failed packages in the output when available.
     if not result.failed_packages:
@@ -97,26 +119,6 @@
 
     return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
 
-  if not vm_types:
-    # No VMs to build, we can exit now.
-    return controller.RETURN_CODE_SUCCESS
-
-  # There can be only one.
-  vm_type = vm_types.pop()
-  is_test = vm_type == _TEST_VM_ID
-  try:
-    vm_path = image.CreateVm(board, is_test=is_test)
-  except image.ImageToVmError as e:
-    cros_build_lib.Die(e)
-
-  new_image = output_proto.images.add()
-  new_image.path = vm_path
-  new_image.type = vm_type
-  new_image.build_target.name = board
-
-  # Read metric events log and pipe them into output_proto.events.
-  deserialize_metrics_log(output_proto.events, prefix=board)
-
 
 def _ParseImagesToCreate(to_build):
   """Helper function to parse the image types to build.