2020-04-10 21:54:53 +00:00
|
|
|
import _ from 'lodash-es';
|
|
|
|
import { AccessControlFormData } from 'Portainer/components/accessControlForm/porAccessControlFormModel';
|
2019-03-21 05:46:49 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
import angular from 'angular';
|
2019-03-21 05:46:49 +00:00
|
|
|
|
|
|
|
class CreateConfigController {
|
|
|
|
/* @ngInject */
|
2019-06-17 14:51:39 +00:00
|
|
|
constructor($async, $state, $transition$, Notifications, ConfigService, Authentication, FormValidator, ResourceControlService) {
|
2019-03-21 05:46:49 +00:00
|
|
|
this.$state = $state;
|
|
|
|
this.$transition$ = $transition$;
|
|
|
|
this.Notifications = Notifications;
|
|
|
|
this.ConfigService = ConfigService;
|
|
|
|
this.Authentication = Authentication;
|
|
|
|
this.FormValidator = FormValidator;
|
|
|
|
this.ResourceControlService = ResourceControlService;
|
2019-06-17 14:51:39 +00:00
|
|
|
this.$async = $async;
|
2019-03-21 05:46:49 +00:00
|
|
|
|
|
|
|
this.formValues = {
|
2020-04-10 21:54:53 +00:00
|
|
|
Name: '',
|
2019-03-21 05:46:49 +00:00
|
|
|
Labels: [],
|
|
|
|
AccessControlData: new AccessControlFormData(),
|
2020-04-10 21:54:53 +00:00
|
|
|
ConfigContent: '',
|
2019-03-21 05:46:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.state = {
|
2020-04-10 21:54:53 +00:00
|
|
|
formValidationError: '',
|
2019-03-21 05:46:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.editorUpdate = this.editorUpdate.bind(this);
|
2019-06-17 14:51:39 +00:00
|
|
|
this.createAsync = this.createAsync.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
async $onInit() {
|
|
|
|
if (!this.$transition$.params().id) {
|
|
|
|
this.formValues.displayCodeEditor = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
let data = await this.ConfigService.config(this.$transition$.params().id);
|
2020-04-10 21:54:53 +00:00
|
|
|
this.formValues.Name = data.Name + '_copy';
|
2019-06-17 14:51:39 +00:00
|
|
|
this.formValues.Data = data.Data;
|
|
|
|
let labels = _.keys(data.Labels);
|
|
|
|
for (let i = 0; i < labels.length; i++) {
|
|
|
|
let labelName = labels[i];
|
|
|
|
let labelValue = data.Labels[labelName];
|
|
|
|
this.formValues.Labels.push({ name: labelName, value: labelValue });
|
|
|
|
}
|
|
|
|
this.formValues.displayCodeEditor = true;
|
|
|
|
} catch (err) {
|
|
|
|
this.formValues.displayCodeEditor = true;
|
2020-04-10 21:54:53 +00:00
|
|
|
this.Notifications.error('Failure', err, 'Unable to clone config');
|
2019-06-17 14:51:39 +00:00
|
|
|
}
|
2019-03-21 05:46:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addLabel() {
|
2020-04-10 21:54:53 +00:00
|
|
|
this.formValues.Labels.push({ name: '', value: '' });
|
2019-03-21 05:46:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
removeLabel(index) {
|
|
|
|
this.formValues.Labels.splice(index, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
prepareLabelsConfig(config) {
|
|
|
|
let labels = {};
|
2020-04-10 21:54:53 +00:00
|
|
|
this.formValues.Labels.forEach(function (label) {
|
2017-11-06 08:47:31 +00:00
|
|
|
if (label.name && label.value) {
|
2019-03-21 05:46:49 +00:00
|
|
|
labels[label.name] = label.value;
|
2017-11-06 08:47:31 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
config.Labels = labels;
|
|
|
|
}
|
|
|
|
|
2019-03-21 05:46:49 +00:00
|
|
|
prepareConfigData(config) {
|
|
|
|
let configData = this.formValues.ConfigContent;
|
2017-11-06 08:47:31 +00:00
|
|
|
config.Data = btoa(unescape(encodeURIComponent(configData)));
|
|
|
|
}
|
|
|
|
|
2019-03-21 05:46:49 +00:00
|
|
|
prepareConfiguration() {
|
|
|
|
let config = {};
|
|
|
|
config.Name = this.formValues.Name;
|
|
|
|
this.prepareConfigData(config);
|
|
|
|
this.prepareLabelsConfig(config);
|
2017-11-06 08:47:31 +00:00
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2019-03-21 05:46:49 +00:00
|
|
|
validateForm(accessControlData, isAdmin) {
|
2020-04-10 21:54:53 +00:00
|
|
|
this.state.formValidationError = '';
|
|
|
|
let error = '';
|
|
|
|
error = this.FormValidator.validateAccessControl(accessControlData, isAdmin);
|
2017-11-06 08:47:31 +00:00
|
|
|
|
|
|
|
if (error) {
|
2019-03-21 05:46:49 +00:00
|
|
|
this.state.formValidationError = error;
|
2017-11-06 08:47:31 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-17 14:51:39 +00:00
|
|
|
create() {
|
|
|
|
return this.$async(this.createAsync);
|
|
|
|
}
|
|
|
|
|
|
|
|
async createAsync() {
|
2019-11-12 23:41:42 +00:00
|
|
|
const accessControlData = this.formValues.AccessControlData;
|
|
|
|
const userDetails = this.Authentication.getUserDetails();
|
|
|
|
const isAdmin = this.Authentication.isAdmin();
|
2017-11-06 08:47:31 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
if (this.formValues.ConfigContent === '') {
|
|
|
|
this.state.formValidationError = 'Config content must not be empty';
|
2018-02-27 07:19:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-21 05:46:49 +00:00
|
|
|
if (!this.validateForm(accessControlData, isAdmin)) {
|
2017-11-06 08:47:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-12 23:41:42 +00:00
|
|
|
const config = this.prepareConfiguration();
|
2019-03-21 05:46:49 +00:00
|
|
|
|
|
|
|
try {
|
2019-11-12 23:41:42 +00:00
|
|
|
const data = await this.ConfigService.create(config);
|
|
|
|
const resourceControl = data.Portainer.ResourceControl;
|
|
|
|
const userId = userDetails.ID;
|
|
|
|
await this.ResourceControlService.applyResourceControl(userId, accessControlData, resourceControl);
|
2020-04-10 21:54:53 +00:00
|
|
|
this.Notifications.success('Config successfully created');
|
|
|
|
this.$state.go('docker.configs', {}, { reload: true });
|
2019-03-21 05:46:49 +00:00
|
|
|
} catch (err) {
|
2020-04-10 21:54:53 +00:00
|
|
|
this.Notifications.error('Failure', err, 'Unable to create config');
|
2019-03-21 05:46:49 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-06 08:47:31 +00:00
|
|
|
|
2019-03-21 05:46:49 +00:00
|
|
|
editorUpdate(cm) {
|
|
|
|
this.formValues.ConfigContent = cm.getValue();
|
|
|
|
}
|
|
|
|
}
|
2018-08-22 06:41:02 +00:00
|
|
|
|
2019-03-21 05:46:49 +00:00
|
|
|
export default CreateConfigController;
|
2020-04-10 21:54:53 +00:00
|
|
|
angular.module('portainer.docker').controller('CreateConfigController', CreateConfigController);
|