Build API: Implement the chroot creation endpoint.

BUG=chromium:904937
TEST=run_tests, new tests

Change-Id: I5fb9e38f3a327405c951d991a9ab661149b1fccd
Reviewed-on: https://chromium-review.googlesource.com/1495725
Commit-Ready: Alex Klein <saklein@chromium.org>
Tested-by: Alex Klein <saklein@chromium.org>
Reviewed-by: Lann Martin <lannm@chromium.org>
diff --git a/api/controller/sdk.py b/api/controller/sdk.py
new file mode 100644
index 0000000..880da60
--- /dev/null
+++ b/api/controller/sdk.py
@@ -0,0 +1,46 @@
+# -*- coding: utf-8 -*-
+# Copyright 2019 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""SDK chroot operations."""
+
+from __future__ import print_function
+
+import os
+
+from chromite.lib import cros_build_lib
+from chromite.service import sdk
+
+
+def Create(input_proto, output_proto):
+  """Chroot creation, includes support for replacing an existing chroot.
+
+  Args:
+    input_proto (CreateRequest): The input proto.
+    output_proto (CreateResponse): The output proto.
+  """
+  replace = not input_proto.flags.no_replace
+  bootstrap = input_proto.flags.bootstrap
+  use_image = not input_proto.flags.no_use_image
+
+  chrome_root = input_proto.paths.chrome
+  cache_dir = input_proto.paths.cache
+  chroot_path = input_proto.paths.chroot
+
+  if chroot_path and not os.path.isabs(chroot_path):
+    cros_build_lib.Die('The chroot path must be absolute.')
+
+  paths = sdk.ChrootPaths(cache_dir=cache_dir, chrome_root=chrome_root,
+                          chroot_path=chroot_path)
+  args = sdk.CreateArguments(replace=replace, bootstrap=bootstrap,
+                             use_image=use_image, paths=paths)
+
+  version = sdk.Create(args)
+
+  if version:
+    output_proto.version.version = version
+  else:
+    # This should be very rare, if ever used, but worth noting.
+    cros_build_lib.Die('No chroot version could be found. There was likely an'
+                       'error creating the chroot that was not detected.')