2016-08-17 05:25:42 +00:00
|
|
|
angular.module('createNetwork', [])
|
2017-09-19 14:58:30 +00:00
|
|
|
.controller('CreateNetworkController', ['$q', '$scope', '$state', 'PluginService', 'Notifications', 'NetworkService', 'LabelHelper', 'Authentication', 'ResourceControlService', 'FormValidator',
|
|
|
|
function ($q, $scope, $state, PluginService, Notifications, NetworkService, LabelHelper, Authentication, ResourceControlService, FormValidator) {
|
2017-09-06 13:11:38 +00:00
|
|
|
|
2016-08-17 05:25:42 +00:00
|
|
|
$scope.formValues = {
|
|
|
|
DriverOptions: [],
|
|
|
|
Subnet: '',
|
2016-12-25 20:32:17 +00:00
|
|
|
Gateway: '',
|
2017-09-19 14:58:30 +00:00
|
|
|
Labels: [],
|
|
|
|
AccessControlData: new AccessControlFormData()
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.state = {
|
|
|
|
formValidationError: ''
|
2016-08-17 05:25:42 +00:00
|
|
|
};
|
|
|
|
|
2017-09-06 13:11:38 +00:00
|
|
|
$scope.availableNetworkDrivers = [];
|
|
|
|
|
2016-08-17 05:25:42 +00:00
|
|
|
$scope.config = {
|
|
|
|
Driver: 'bridge',
|
|
|
|
CheckDuplicate: true,
|
|
|
|
Internal: false,
|
2016-09-23 04:54:58 +00:00
|
|
|
// Force IPAM Driver to 'default', should not be required.
|
|
|
|
// See: https://github.com/docker/docker/issues/25735
|
2016-08-17 05:25:42 +00:00
|
|
|
IPAM: {
|
2016-09-23 04:54:58 +00:00
|
|
|
Driver: 'default',
|
2016-08-17 05:25:42 +00:00
|
|
|
Config: []
|
2016-12-25 20:32:17 +00:00
|
|
|
},
|
|
|
|
Labels: {}
|
2016-08-17 05:25:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.addDriverOption = function() {
|
|
|
|
$scope.formValues.DriverOptions.push({ name: '', value: '' });
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.removeDriverOption = function(index) {
|
|
|
|
$scope.formValues.DriverOptions.splice(index, 1);
|
|
|
|
};
|
|
|
|
|
2016-12-25 20:32:17 +00:00
|
|
|
$scope.addLabel = function() {
|
2017-07-10 07:33:09 +00:00
|
|
|
$scope.formValues.Labels.push({ key: '', value: ''});
|
2016-12-25 20:32:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.removeLabel = function(index) {
|
|
|
|
$scope.formValues.Labels.splice(index, 1);
|
|
|
|
};
|
|
|
|
|
2016-08-17 05:25:42 +00:00
|
|
|
function prepareIPAMConfiguration(config) {
|
|
|
|
if ($scope.formValues.Subnet) {
|
|
|
|
var ipamConfig = {};
|
|
|
|
ipamConfig.Subnet = $scope.formValues.Subnet;
|
|
|
|
if ($scope.formValues.Gateway) {
|
|
|
|
ipamConfig.Gateway = $scope.formValues.Gateway ;
|
|
|
|
}
|
|
|
|
config.IPAM.Config.push(ipamConfig);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function prepareDriverOptions(config) {
|
|
|
|
var options = {};
|
|
|
|
$scope.formValues.DriverOptions.forEach(function (option) {
|
|
|
|
options[option.name] = option.value;
|
|
|
|
});
|
|
|
|
config.Options = options;
|
|
|
|
}
|
|
|
|
|
2016-12-25 20:32:17 +00:00
|
|
|
function prepareLabelsConfig(config) {
|
2017-07-10 07:33:09 +00:00
|
|
|
config.Labels = LabelHelper.fromKeyValueToLabelHash($scope.formValues.Labels);
|
2016-12-25 20:32:17 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 05:25:42 +00:00
|
|
|
function prepareConfiguration() {
|
|
|
|
var config = angular.copy($scope.config);
|
|
|
|
prepareIPAMConfiguration(config);
|
|
|
|
prepareDriverOptions(config);
|
2016-12-25 20:32:17 +00:00
|
|
|
prepareLabelsConfig(config);
|
2016-08-17 05:25:42 +00:00
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2017-09-19 14:58:30 +00:00
|
|
|
function validateForm(accessControlData, isAdmin) {
|
|
|
|
$scope.state.formValidationError = '';
|
|
|
|
var error = '';
|
|
|
|
error = FormValidator.validateAccessControl(accessControlData, isAdmin);
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
$scope.state.formValidationError = error;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-17 05:25:42 +00:00
|
|
|
$scope.create = function () {
|
2017-09-19 14:58:30 +00:00
|
|
|
$('#createResourceSpinner').show();
|
|
|
|
|
|
|
|
var networkConfiguration = prepareConfiguration();
|
|
|
|
var accessControlData = $scope.formValues.AccessControlData;
|
|
|
|
var userDetails = Authentication.getUserDetails();
|
|
|
|
var isAdmin = userDetails.role === 1 ? true : false;
|
|
|
|
|
|
|
|
if (!validateForm(accessControlData, isAdmin)) {
|
|
|
|
$('#createResourceSpinner').hide();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkService.create(networkConfiguration)
|
|
|
|
.then(function success(data) {
|
|
|
|
var networkIdentifier = data.Id;
|
|
|
|
var userId = userDetails.ID;
|
|
|
|
return ResourceControlService.applyResourceControl('network', networkIdentifier, userId, accessControlData, []);
|
|
|
|
})
|
|
|
|
.then(function success() {
|
|
|
|
Notifications.success('Network successfully created');
|
|
|
|
$state.go('networks', {}, {reload: true});
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
Notifications.error('Failure', err, 'An error occured during network creation');
|
|
|
|
})
|
|
|
|
.finally(function final() {
|
|
|
|
$('#createResourceSpinner').hide();
|
|
|
|
});
|
2016-08-17 05:25:42 +00:00
|
|
|
};
|
2017-09-06 13:11:38 +00:00
|
|
|
|
|
|
|
function initView() {
|
|
|
|
$('#loadingViewSpinner').show();
|
|
|
|
var endpointProvider = $scope.applicationState.endpoint.mode.provider;
|
|
|
|
var apiVersion = $scope.applicationState.endpoint.apiVersion;
|
|
|
|
if(endpointProvider !== 'DOCKER_SWARM') {
|
|
|
|
PluginService.networkPlugins(apiVersion < 1.25)
|
|
|
|
.then(function success(data){
|
|
|
|
$scope.availableNetworkDrivers = data;
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
Notifications.error('Failure', err, 'Unable to retrieve network drivers');
|
|
|
|
})
|
|
|
|
.finally(function final() {
|
|
|
|
$('#loadingViewSpinner').hide();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
initView();
|
2016-08-17 05:25:42 +00:00
|
|
|
}]);
|