zun-ui/zun_ui/static/dashboard/container/containers/actions/create.service.js
Shu Muto 2fe0d61529 Use Angular-Schema-Form for container creation
To modify dialog easily, this patch uses Angular-Schema-Form
for container creation dialog.
This also makes easier to implement update dialog in the future.

Change-Id: Id369ec6650931433e3e5fd4569838c3ff420e504
2017-06-14 15:27:37 +09:00

106 lines
3.3 KiB
JavaScript

/**
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
(function() {
'use strict';
/**
* @ngdoc overview
* @name horizon.dashboard.container.containers.create.service
* @description Service for the container create modal
*/
angular
.module('horizon.dashboard.container.containers')
.factory('horizon.dashboard.container.containers.create.service', createService);
createService.$inject = [
'horizon.app.core.openstack-service-api.policy',
'horizon.app.core.openstack-service-api.zun',
'horizon.dashboard.container.containers.resourceType',
'horizon.dashboard.container.containers.workflow',
'horizon.framework.util.actions.action-result.service',
'horizon.framework.util.i18n.gettext',
'horizon.framework.util.q.extensions',
'horizon.framework.widgets.form.ModalFormService',
'horizon.framework.widgets.toast.service'
];
function createService(
policy, zun, resourceType, workflow,
actionResult, gettext, $qExtensions, modal, toast
) {
var message = {
success: gettext('Container %s was successfully created.')
};
var service = {
initAction: initAction,
perform: perform,
allowed: allowed
};
return service;
//////////////
function initAction() {
}
function perform() {
var title, submitText;
title = gettext('Create Container');
submitText = gettext('Create');
var config = workflow.init('create', title, submitText);
return modal.open(config).then(submit);
}
function allowed() {
return policy.ifAllowed({ rules: [['container', 'add_container']] });
}
function submit(context) {
if (context.model.restart_policy === "on-failure") {
if (!context.model.restart_policy_max_retry) {
context.model.restart_policy_max_retry = 0;
}
context.model.restart_policy =
context.model.restart_policy + ":" +
context.model.restart_policy_max_retry;
}
delete context.model.restart_policy_max_retry;
context.model = cleanNullProperties(context.model);
return zun.createContainer(context.model).then(success);
}
function success(response) {
response.data.id = response.data.uuid;
toast.add('success', interpolate(message.success, [response.data.name]));
var result = actionResult.getActionResult().created(resourceType, response.data.name);
return result.result;
}
function cleanNullProperties(model) {
// Initially clean fields that don't have any value.
// Not only "null", blank too.
for (var key in model) {
if (model.hasOwnProperty(key) && model[key] === null || model[key] === "" ||
key === "tabs") {
delete model[key];
}
}
return model;
}
}
})();