2019-03-21 05:46:49 +00:00
|
|
|
import { AccessControlFormData } from '../../../../portainer/components/accessControlForm/porAccessControlFormModel';
|
|
|
|
import { VolumesNFSFormData } from '../../../components/volumesNFSForm/volumesNFSFormModel';
|
2020-05-15 01:28:51 +00:00
|
|
|
import { VolumesCIFSFormData } from '../../../components/volumesCIFSForm/volumesCifsFormModel';
|
2019-03-21 05:46:49 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
angular.module('portainer.docker').controller('CreateVolumeController', [
|
|
|
|
'$q',
|
|
|
|
'$scope',
|
|
|
|
'$state',
|
|
|
|
'VolumeService',
|
|
|
|
'PluginService',
|
|
|
|
'ResourceControlService',
|
|
|
|
'Authentication',
|
|
|
|
'Notifications',
|
|
|
|
'FormValidator',
|
|
|
|
'HttpRequestHelper',
|
|
|
|
function ($q, $scope, $state, VolumeService, PluginService, ResourceControlService, Authentication, Notifications, FormValidator, HttpRequestHelper) {
|
|
|
|
$scope.formValues = {
|
|
|
|
Driver: 'local',
|
|
|
|
DriverOptions: [],
|
|
|
|
AccessControlData: new AccessControlFormData(),
|
|
|
|
NodeName: null,
|
|
|
|
NFSData: new VolumesNFSFormData(),
|
2020-05-15 01:28:51 +00:00
|
|
|
CIFSData: new VolumesCIFSFormData(),
|
2020-04-10 21:54:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.state = {
|
|
|
|
formValidationError: '',
|
|
|
|
actionInProgress: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.availableVolumeDrivers = [];
|
|
|
|
|
|
|
|
$scope.addDriverOption = function () {
|
|
|
|
$scope.formValues.DriverOptions.push({ name: '', value: '' });
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.removeDriverOption = function (index) {
|
|
|
|
$scope.formValues.DriverOptions.splice(index, 1);
|
|
|
|
};
|
|
|
|
|
|
|
|
function validateForm(accessControlData, isAdmin) {
|
|
|
|
$scope.state.formValidationError = '';
|
|
|
|
var error = '';
|
|
|
|
error = FormValidator.validateAccessControl(accessControlData, isAdmin);
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
$scope.state.formValidationError = error;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2017-05-23 18:56:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 01:28:51 +00:00
|
|
|
function prepareCIFSConfiguration(driverOptions) {
|
|
|
|
const data = $scope.formValues.CIFSData;
|
|
|
|
|
|
|
|
driverOptions.push({ name: 'type', value: 'cifs' });
|
|
|
|
|
|
|
|
let share = data.share.replace('\\', '/');
|
|
|
|
if (share[0] !== '/') {
|
|
|
|
share = '/' + share;
|
|
|
|
}
|
|
|
|
const device = '//' + data.serverAddress + share;
|
|
|
|
driverOptions.push({ name: 'device', value: device });
|
|
|
|
|
|
|
|
const versionNumber = data.versionsNumber[data.versions.indexOf(data.version)];
|
|
|
|
const options = 'username=' + data.username + ',password=' + data.password + ',vers=' + versionNumber;
|
|
|
|
driverOptions.push({ name: 'o', value: options });
|
|
|
|
}
|
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
function prepareNFSConfiguration(driverOptions) {
|
|
|
|
var data = $scope.formValues.NFSData;
|
2018-08-09 08:33:16 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
driverOptions.push({ name: 'type', value: data.version.toLowerCase() });
|
2018-08-09 08:33:16 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
var options = 'addr=' + data.serverAddress + ',' + data.options;
|
|
|
|
driverOptions.push({ name: 'o', value: options });
|
2018-08-09 08:33:16 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
var mountPoint = data.mountPoint[0] === ':' ? data.mountPoint : ':' + data.mountPoint;
|
|
|
|
driverOptions.push({ name: 'device', value: mountPoint });
|
2018-01-21 16:26:24 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
$scope.create = function () {
|
|
|
|
var name = $scope.formValues.Name;
|
|
|
|
var driver = $scope.formValues.Driver;
|
|
|
|
var driverOptions = $scope.formValues.DriverOptions;
|
|
|
|
var storidgeProfile = $scope.formValues.StoridgeProfile;
|
|
|
|
|
|
|
|
if (driver === 'cio:latest' && storidgeProfile) {
|
|
|
|
driverOptions.push({ name: 'profile', value: storidgeProfile.Name });
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($scope.formValues.NFSData.useNFS) {
|
|
|
|
prepareNFSConfiguration(driverOptions);
|
|
|
|
}
|
|
|
|
|
2020-05-15 01:28:51 +00:00
|
|
|
if ($scope.formValues.CIFSData.useCIFS) {
|
|
|
|
prepareCIFSConfiguration(driverOptions);
|
|
|
|
}
|
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
var volumeConfiguration = VolumeService.createVolumeConfiguration(name, driver, driverOptions);
|
|
|
|
var accessControlData = $scope.formValues.AccessControlData;
|
|
|
|
var userDetails = Authentication.getUserDetails();
|
|
|
|
var isAdmin = Authentication.isAdmin();
|
|
|
|
|
|
|
|
if (!validateForm(accessControlData, isAdmin)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var nodeName = $scope.formValues.NodeName;
|
|
|
|
HttpRequestHelper.setPortainerAgentTargetHeader(nodeName);
|
|
|
|
|
|
|
|
$scope.state.actionInProgress = true;
|
|
|
|
VolumeService.createVolume(volumeConfiguration)
|
|
|
|
.then(function success(data) {
|
|
|
|
const userId = userDetails.ID;
|
|
|
|
const resourceControl = data.ResourceControl;
|
|
|
|
return ResourceControlService.applyResourceControl(userId, accessControlData, resourceControl);
|
|
|
|
})
|
|
|
|
.then(function success() {
|
|
|
|
Notifications.success('Volume successfully created');
|
|
|
|
$state.go('docker.volumes', {}, { reload: true });
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
Notifications.error('Failure', err, 'An error occured during volume creation');
|
|
|
|
})
|
|
|
|
.finally(function final() {
|
|
|
|
$scope.state.actionInProgress = false;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
function initView() {
|
|
|
|
var apiVersion = $scope.applicationState.endpoint.apiVersion;
|
|
|
|
var endpointProvider = $scope.applicationState.endpoint.mode.provider;
|
|
|
|
|
|
|
|
PluginService.volumePlugins(apiVersion < 1.25 || endpointProvider === 'VMWARE_VIC')
|
|
|
|
.then(function success(data) {
|
|
|
|
$scope.availableVolumeDrivers = data;
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
Notifications.error('Failure', err, 'Unable to retrieve volume drivers');
|
|
|
|
});
|
2017-05-23 18:56:10 +00:00
|
|
|
}
|
2017-04-01 10:18:46 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
initView();
|
|
|
|
},
|
|
|
|
]);
|