From fa57cda20c265ea299a8d1f13121c4cf06553d29 Mon Sep 17 00:00:00 2001
From: Shu Muto <shu-mutou@rf.jp.nec.com>
Date: Tue, 13 Jun 2017 18:58:21 +0900
Subject: [PATCH] Add parameters for cluster creation

This patch adds 'image_driver', 'image_pull_policy' and
'restart_policy' parameters into cluster creation action.

Change-Id: I479e85787b0a2518968e8bb774159073b1573162
---
 zun_ui/api/client.py                          |  4 ++++
 .../containers/create/container-model.js      |  4 ++++
 .../containers/create/create.service.js       |  6 +++++
 .../containers/create/info/info.help.html     | 17 +++++++++++++
 .../containers/create/info/info.html          | 22 +++++++++++++++++
 .../create/spec/container.spec.controller.js  |  9 ++++++-
 .../containers/create/spec/spec.help.html     |  2 ++
 .../containers/create/spec/spec.html          | 24 +++++++++++++++++++
 8 files changed, 87 insertions(+), 1 deletion(-)

diff --git a/zun_ui/api/client.py b/zun_ui/api/client.py
index a416c94..2cf4921 100644
--- a/zun_ui/api/client.py
+++ b/zun_ui/api/client.py
@@ -15,6 +15,7 @@ from horizon import exceptions
 from horizon.utils.memoized import memoized
 import logging
 from openstack_dashboard.api import base
+from zunclient.common import utils
 from zunclient.v1 import client as zun_client
 
 
@@ -52,6 +53,9 @@ def container_create(request, **kwargs):
         elif key == "interactive":
             args["interactive"] = value
             continue
+        elif key == "restart_policy":
+            args[key] = utils.check_restart_policy(value)
+            continue
 
         if key in CONTAINER_CREATE_ATTRS:
             args[str(key)] = str(value)
diff --git a/zun_ui/static/dashboard/container/containers/create/container-model.js b/zun_ui/static/dashboard/container/containers/create/container-model.js
index e75ecb2..f2581fd 100644
--- a/zun_ui/static/dashboard/container/containers/create/container-model.js
+++ b/zun_ui/static/dashboard/container/containers/create/container-model.js
@@ -37,9 +37,13 @@
         uuid: null,
         name: null,
         image: null,
+        image_driver: "docker",
+        image_pull_policy: null,
         command: null,
         cpu: null,
         memory: null,
+        restart_policy: null,
+        restart_policy_max_retry: null,
         environment: null,
         workdir: null,
         labels: null,
diff --git a/zun_ui/static/dashboard/container/containers/create/create.service.js b/zun_ui/static/dashboard/container/containers/create/create.service.js
index b783152..82e0313 100644
--- a/zun_ui/static/dashboard/container/containers/create/create.service.js
+++ b/zun_ui/static/dashboard/container/containers/create/create.service.js
@@ -80,6 +80,12 @@
     }
 
     function submit() {
+      if (model.newContainerSpec.restart_policy === "on-failure") {
+        model.newContainerSpec.restart_policy =
+          model.newContainerSpec.restart_policy + ":" +
+          model.newContainerSpec.restart_policy_max_retry;
+      }
+      delete model.newContainerSpec.restart_policy_max_retry;
       return model.createContainer().then(success);
     }
 
diff --git a/zun_ui/static/dashboard/container/containers/create/info/info.help.html b/zun_ui/static/dashboard/container/containers/create/info/info.help.html
index 2865b64..47d7cb5 100644
--- a/zun_ui/static/dashboard/container/containers/create/info/info.help.html
+++ b/zun_ui/static/dashboard/container/containers/create/info/info.help.html
@@ -3,6 +3,23 @@
   <dd translate>An arbitrary human-readable name.</dd>
   <dt translate>Image</dt>
   <dd translate>Name or ID of container image.</dd>
+  <dt translate>Image Driver</dt>
+  <dd>
+    <translate>The image driver to use to pull container image.</translate>
+    <ul>
+      <li translate>Docker: Pull the image from Docker Hub.</li>
+      <li translate>Glance: Pull the image from Glance.</li>
+    </ul>
+  </dd>
+  <dt translate>Image Pull Policy</dt>
+  <dd translate>
+    The policy which determines if the image should be pulled prior to starting the container.
+    <ul>
+      <li translate>If not present: only pull the image if it does not already exist on the node.</li>
+      <li translate>Always: Always pull the image from repository.</li>
+      <li translate>Never: never pull the image.</li>
+    </ul>
+  </dd>
   <dt translate>Command</dt>
   <dd translate>Command sent to the container.</dd>
 </dl>
diff --git a/zun_ui/static/dashboard/container/containers/create/info/info.html b/zun_ui/static/dashboard/container/containers/create/info/info.html
index 3e3dc1d..b5d243a 100644
--- a/zun_ui/static/dashboard/container/containers/create/info/info.html
+++ b/zun_ui/static/dashboard/container/containers/create/info/info.html
@@ -20,6 +20,28 @@
                placeholder="{$ 'Name or ID of the container image.'|translate $}">
       </div>
     </div>
+    <div class="col-xs-6">
+      <div class="form-group">
+        <label class="control-label" for="container-image-driver" translate>Image Driver</label>
+        <select name="container-image-driver" class="form-control" id="container-image-driver"
+               ng-model="model.newContainerSpec.image_driver">
+          <option value="docker" selected=selected>Docker</option>
+          <option value="glance">Glance</option>
+        </select>
+      </div>
+    </div>
+    <div class="col-xs-6">
+      <div class="form-group">
+        <label class="control-label" for="container-image-pull-policy" translate>Image Pull Policy</label>
+        <select name="container-image-pull-policy" class="form-control" id="container-image-pull-policy"
+               ng-model="model.newContainerSpec.image_pull_policy">
+          <option value="" selected=selected translate>Select policy.</option>
+          <option value="ifnotpresent" translate>If not present</option>
+          <option value="always" translate>Always</option>
+          <option value="never" translate>Never</option>
+        </select>
+      </div>
+    </div>
     <div class="col-xs-12">
       <div class="form-group">
         <label class="control-label" for="container-command" translate>Command</label>
diff --git a/zun_ui/static/dashboard/container/containers/create/spec/container.spec.controller.js b/zun_ui/static/dashboard/container/containers/create/spec/container.spec.controller.js
index aa621a4..7fe9e72 100644
--- a/zun_ui/static/dashboard/container/containers/create/spec/container.spec.controller.js
+++ b/zun_ui/static/dashboard/container/containers/create/spec/container.spec.controller.js
@@ -27,8 +27,15 @@
     .controller('createContainerSpecController', createContainerSpecController);
 
   createContainerSpecController.$inject = [
+    '$scope'
   ];
 
-  function createContainerSpecController() {
+  function createContainerSpecController($scope) {
+    var ctrl = this;
+    ctrl.onChangeRestartPolicy = function () {
+      if ($scope.model.newContainerSpec.restart_policy !== 'on-failure') {
+        $scope.model.newContainerSpec.restart_policy_max_retry = null;
+      }
+    };
   }
 })();
diff --git a/zun_ui/static/dashboard/container/containers/create/spec/spec.help.html b/zun_ui/static/dashboard/container/containers/create/spec/spec.help.html
index 665ad28..95e13fc 100644
--- a/zun_ui/static/dashboard/container/containers/create/spec/spec.help.html
+++ b/zun_ui/static/dashboard/container/containers/create/spec/spec.help.html
@@ -3,4 +3,6 @@
   <dd translate>The number of virtual cpus.</dd>
   <dt translate>Memory</dt>
   <dd translate>The container memory size in MiB.</dd>
+  <dt translate>Restart Policy</dt>
+  <dd translate>Restart policy to apply when a container exits.</dd>
 </dl>
diff --git a/zun_ui/static/dashboard/container/containers/create/spec/spec.html b/zun_ui/static/dashboard/container/containers/create/spec/spec.html
index a083de2..20db818 100644
--- a/zun_ui/static/dashboard/container/containers/create/spec/spec.html
+++ b/zun_ui/static/dashboard/container/containers/create/spec/spec.html
@@ -17,5 +17,29 @@
                id="container-memory">
       </div>
     </div>
+    <div class="col-xs-6">
+      <div class="form-group">
+        <label class="control-label" for="container-restart" translate>Restart Policy</label>
+        <select name="container-restart" class="form-control" id="container-memory"
+                ng-model="model.newContainerSpec.restart_policy"
+                ng-change="ctrl.onChangeRestartPolicy()">
+          <option value="" selected=selected translate>Select policy.</option>
+          <option value="no" translate>No</option>
+          <option value="on-failure" translate>On failure</option>
+          <option value="always" translate>Always</option>
+          <option value="unless-stopped" translate>Unless Stopped</option>
+        </select>
+      </div>
+    </div>
+    <div class="col-xs-6">
+      <div class="form-group">
+        <label class="control-label" for="container-restart-max-retry" translate>Max Retry</label>
+        <input name="container-restart-max-retry" type="number" min="1"
+               class="form-control" ng-model="model.newContainerSpec.restart_policy_max_retry"
+               id="container-restart-max-retry"
+               placeholder="{$ 'Retry times for \'On failure\' policy.'|translate $}"
+               ng-disabled="model.newContainerSpec.restart_policy!=='on-failure'">
+      </div>
+    </div>
   </div>
 </div>