2019-03-21 05:46:49 +00:00
|
|
|
import _ from "lodash-es";
|
|
|
|
import { AccessControlFormData } from "Portainer/components/accessControlForm/porAccessControlFormModel";
|
|
|
|
|
|
|
|
import angular from "angular";
|
|
|
|
|
|
|
|
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 = {
|
|
|
|
Name: "",
|
|
|
|
Labels: [],
|
|
|
|
AccessControlData: new AccessControlFormData(),
|
|
|
|
ConfigContent: ""
|
|
|
|
};
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
formValidationError: ""
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
this.formValues.Name = data.Name + "_copy";
|
|
|
|
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;
|
|
|
|
this.Notifications.error("Failure", err, "Unable to clone config");
|
|
|
|
}
|
2019-03-21 05:46:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addLabel() {
|
|
|
|
this.formValues.Labels.push({ name: "", value: "" });
|
|
|
|
}
|
|
|
|
|
|
|
|
removeLabel(index) {
|
|
|
|
this.formValues.Labels.splice(index, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
prepareLabelsConfig(config) {
|
|
|
|
let labels = {};
|
|
|
|
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) {
|
|
|
|
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
|
|
|
|
2019-03-21 05:46:49 +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);
|
2019-03-21 05:46:49 +00:00
|
|
|
this.Notifications.success("Config successfully created");
|
|
|
|
this.$state.go("docker.configs", {}, { reload: true });
|
|
|
|
} catch (err) {
|
|
|
|
this.Notifications.error("Failure", err, "Unable to create config");
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
angular
|
|
|
|
.module("portainer.docker")
|
|
|
|
.controller("CreateConfigController", CreateConfigController);
|