2016-06-07 10:49:38 +02:00
|
|
|
(function() {
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
angular
|
|
|
|
.module('harbor.system.management')
|
|
|
|
.directive('createDestination', createDestination);
|
|
|
|
|
2016-07-02 18:22:45 +02:00
|
|
|
CreateDestinationController.$inject = ['$scope', 'ListDestinationService', 'CreateDestinationService', 'UpdateDestinationService', 'PingDestinationService', 'ListDestinationPolicyService', '$filter', 'trFilter', '$timeout'];
|
2016-06-07 10:49:38 +02:00
|
|
|
|
2016-07-02 18:22:45 +02:00
|
|
|
function CreateDestinationController($scope, ListDestinationService, CreateDestinationService, UpdateDestinationService, PingDestinationService, ListDestinationPolicyService, $filter, trFilter, $timeout) {
|
2016-06-07 10:49:38 +02:00
|
|
|
var vm = this;
|
|
|
|
|
2016-06-14 12:47:32 +02:00
|
|
|
$scope.destination = {};
|
|
|
|
|
|
|
|
var vm0 = $scope.destination;
|
|
|
|
vm.addNew = addNew;
|
|
|
|
vm.edit = edit;
|
|
|
|
vm.create = create;
|
|
|
|
vm.update = update;
|
2016-06-15 11:46:23 +02:00
|
|
|
vm.pingDestination = pingDestination;
|
2016-06-14 12:47:32 +02:00
|
|
|
|
2016-06-22 13:03:12 +02:00
|
|
|
vm.editable = true;
|
2016-07-06 06:56:44 +02:00
|
|
|
vm.notAvailable = false;
|
2016-06-28 17:42:05 +02:00
|
|
|
vm.pingAvailable = true;
|
|
|
|
vm.pingMessage = '';
|
|
|
|
|
2016-07-02 18:22:45 +02:00
|
|
|
vm.closeError = closeError;
|
|
|
|
vm.toggleErrorMessage = false;
|
|
|
|
vm.errorMessages = [];
|
|
|
|
|
2016-07-03 06:10:42 +02:00
|
|
|
vm.pingTIP = false;
|
|
|
|
|
2016-06-28 17:42:05 +02:00
|
|
|
$scope.$watch('destination.endpoint', function(current) {
|
|
|
|
if(current) {
|
|
|
|
vm.notAvailable = false;
|
|
|
|
}else{
|
|
|
|
vm.notAvailable = true;
|
|
|
|
}
|
|
|
|
});
|
2016-06-22 13:03:12 +02:00
|
|
|
|
2016-06-14 12:47:32 +02:00
|
|
|
function addNew() {
|
2016-06-22 13:03:12 +02:00
|
|
|
vm.modalTitle = $filter('tr')('add_new_destination', []);
|
2016-06-14 12:47:32 +02:00
|
|
|
vm0.name = '';
|
|
|
|
vm0.endpoint = '';
|
|
|
|
vm0.username = '';
|
|
|
|
vm0.password = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
function edit(targetId) {
|
2016-06-22 13:03:12 +02:00
|
|
|
vm.editable = true;
|
|
|
|
vm.modalTitle = $filter('tr')('edit_destination', []);
|
|
|
|
ListDestinationService(targetId)
|
|
|
|
.success(getDestinationSuccess)
|
|
|
|
.error(getDestinationFailed);
|
2016-06-14 12:47:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function create(destination) {
|
|
|
|
CreateDestinationService(destination.name, destination.endpoint,
|
2016-06-07 10:49:38 +02:00
|
|
|
destination.username, destination.password)
|
|
|
|
.success(createDestinationSuccess)
|
|
|
|
.error(createDestinationFailed);
|
|
|
|
}
|
2016-06-14 12:47:32 +02:00
|
|
|
|
2016-06-07 10:49:38 +02:00
|
|
|
function createDestinationSuccess(data, status) {
|
2016-06-17 10:11:46 +02:00
|
|
|
console.log('Successful created destination.');
|
|
|
|
vm.reload();
|
2016-06-07 10:49:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function createDestinationFailed(data, status) {
|
2016-06-17 12:07:33 +02:00
|
|
|
if(status === 409) {
|
2016-07-02 18:22:45 +02:00
|
|
|
vm.errorMessages.push($filter('tr')('destination_already_exists'));
|
|
|
|
}else{
|
2016-07-04 06:34:34 +02:00
|
|
|
vm.errorMessages.push($filter('tr')('failed_to_create_destination') + data);
|
2016-06-17 12:07:33 +02:00
|
|
|
}
|
2016-07-04 06:34:34 +02:00
|
|
|
console.log('Failed to create destination:' + data);
|
2016-06-07 10:49:38 +02:00
|
|
|
}
|
|
|
|
|
2016-06-14 12:47:32 +02:00
|
|
|
function update(destination) {
|
|
|
|
UpdateDestinationService(vm.targetId, destination)
|
|
|
|
.success(updateDestinationSuccess)
|
|
|
|
.error(updateDestinationFailed);
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateDestinationSuccess(data, status) {
|
|
|
|
console.log('Successful update destination.');
|
2016-06-17 10:11:46 +02:00
|
|
|
vm.reload();
|
2016-06-14 12:47:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateDestinationFailed(data, status) {
|
2016-07-04 06:34:34 +02:00
|
|
|
vm.errorMessages.push($filter('tr')('failed_to_update_destination') + data);
|
|
|
|
console.log('Failed to update destination.');
|
2016-06-14 12:47:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getDestinationSuccess(data, status) {
|
|
|
|
var destination = data;
|
|
|
|
vm0.name = destination.name;
|
|
|
|
vm0.endpoint = destination.endpoint;
|
|
|
|
vm0.username = destination.username;
|
|
|
|
vm0.password = destination.password;
|
2016-06-22 13:03:12 +02:00
|
|
|
|
|
|
|
ListDestinationPolicyService(destination.id)
|
|
|
|
.success(listDestinationPolicySuccess)
|
|
|
|
.error(listDestinationPolicyFailed);
|
2016-06-14 12:47:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function getDestinationFailed(data, status) {
|
2016-07-02 18:22:45 +02:00
|
|
|
vm.errorMessages.push($filter('tr')('failed_get_destination'));
|
2016-07-04 06:34:34 +02:00
|
|
|
console.log('Failed to get destination.');
|
2016-06-14 12:47:32 +02:00
|
|
|
}
|
2016-06-15 11:46:23 +02:00
|
|
|
|
2016-06-22 13:03:12 +02:00
|
|
|
function listDestinationPolicySuccess(data, status) {
|
|
|
|
for(var i in data) {
|
|
|
|
if(data[i].enabled === 1) {
|
|
|
|
vm.editable = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function listDestinationPolicyFailed(data, status) {
|
2016-07-02 18:22:45 +02:00
|
|
|
vm.errorMessages.push($filter('tr')('failed_get_destination_policies'));
|
2016-07-04 06:34:34 +02:00
|
|
|
console.log('Failed to list destination policy:' + data);
|
2016-06-22 13:03:12 +02:00
|
|
|
}
|
|
|
|
|
2016-06-15 11:46:23 +02:00
|
|
|
function pingDestination() {
|
2016-07-03 06:10:42 +02:00
|
|
|
|
|
|
|
vm.pingTIP = true;
|
2016-06-28 17:42:05 +02:00
|
|
|
vm.pingAvailable = false;
|
|
|
|
|
2016-06-15 11:46:23 +02:00
|
|
|
var target = {
|
|
|
|
'name': vm0.name,
|
|
|
|
'endpoint': vm0.endpoint,
|
|
|
|
'username': vm0.username,
|
|
|
|
'password': vm0.password
|
|
|
|
};
|
|
|
|
PingDestinationService(target)
|
|
|
|
.success(pingDestinationSuccess)
|
|
|
|
.error(pingDestinationFailed);
|
|
|
|
}
|
2016-07-02 18:22:45 +02:00
|
|
|
|
|
|
|
function closeError() {
|
2016-07-07 05:51:03 +02:00
|
|
|
vm.errorMessages = [];
|
2016-07-02 18:22:45 +02:00
|
|
|
vm.toggleErrorMessage = false;
|
|
|
|
}
|
|
|
|
|
2016-06-15 11:46:23 +02:00
|
|
|
function pingDestinationSuccess(data, status) {
|
2016-06-28 17:42:05 +02:00
|
|
|
vm.pingAvailable = true;
|
2016-07-03 06:10:42 +02:00
|
|
|
vm.pingTIP = false;
|
2016-06-28 17:42:05 +02:00
|
|
|
vm.pingMessage = $filter('tr')('successful_ping_target', []);
|
2016-06-15 11:46:23 +02:00
|
|
|
}
|
|
|
|
function pingDestinationFailed(data, status) {
|
2016-07-03 06:10:42 +02:00
|
|
|
|
|
|
|
vm.pingTIP = false;
|
2016-07-06 06:56:44 +02:00
|
|
|
vm.pingMessage = $filter('tr')('failed_to_ping_target', []) + (data && data.length > 0 ? ':' + data : '');
|
2016-06-15 11:46:23 +02:00
|
|
|
}
|
2016-06-07 10:49:38 +02:00
|
|
|
}
|
|
|
|
|
2016-07-02 18:22:45 +02:00
|
|
|
function createDestination($timeout) {
|
2016-06-07 10:49:38 +02:00
|
|
|
var directive = {
|
|
|
|
'restrict': 'E',
|
2016-06-16 08:10:35 +02:00
|
|
|
'templateUrl': '/static/resources/js/components/system-management/create-destination.directive.html',
|
2016-06-14 12:47:32 +02:00
|
|
|
'scope': {
|
|
|
|
'action': '@',
|
|
|
|
'targetId': '@',
|
|
|
|
'reload': '&'
|
|
|
|
},
|
|
|
|
'link': link,
|
2016-06-07 10:49:38 +02:00
|
|
|
'controller': CreateDestinationController,
|
|
|
|
'controllerAs': 'vm',
|
|
|
|
'bindToController': true
|
|
|
|
};
|
|
|
|
return directive;
|
2016-06-14 12:47:32 +02:00
|
|
|
|
|
|
|
function link(scope, element, attrs, ctrl) {
|
|
|
|
|
2016-06-17 12:07:33 +02:00
|
|
|
element.find('#createDestinationModal').on('show.bs.modal', function() {
|
2016-07-06 06:56:44 +02:00
|
|
|
scope.$apply(function(){
|
|
|
|
scope.form.$setPristine();
|
|
|
|
scope.form.$setUntouched();
|
|
|
|
|
|
|
|
ctrl.notAvailble = false;
|
|
|
|
ctrl.pingAvailable = true;
|
|
|
|
ctrl.pingMessage = '';
|
|
|
|
|
|
|
|
ctrl.pingTIP = false;
|
|
|
|
ctrl.toggleErrorMessage = false;
|
|
|
|
ctrl.errorMessages = [];
|
|
|
|
|
|
|
|
switch(ctrl.action) {
|
|
|
|
case 'ADD_NEW':
|
|
|
|
ctrl.addNew();
|
|
|
|
break;
|
|
|
|
case 'EDIT':
|
|
|
|
ctrl.edit(ctrl.targetId);
|
|
|
|
break;
|
2016-07-02 18:22:45 +02:00
|
|
|
}
|
2016-07-06 06:56:44 +02:00
|
|
|
|
|
|
|
scope.$watch('vm.errorMessages', function(current) {
|
|
|
|
if(current && current.length > 0) {
|
|
|
|
ctrl.toggleErrorMessage = true;
|
|
|
|
}
|
|
|
|
}, true);
|
|
|
|
|
|
|
|
});
|
2016-06-15 11:46:23 +02:00
|
|
|
});
|
|
|
|
|
2016-06-14 12:47:32 +02:00
|
|
|
ctrl.save = save;
|
|
|
|
|
|
|
|
function save(destination) {
|
2016-06-17 12:07:33 +02:00
|
|
|
if(destination) {
|
2016-07-02 18:22:45 +02:00
|
|
|
ctrl.toggleErrorMessage = false;
|
|
|
|
ctrl.errorMessages = [];
|
|
|
|
|
2016-06-14 12:47:32 +02:00
|
|
|
switch(ctrl.action) {
|
|
|
|
case 'ADD_NEW':
|
|
|
|
ctrl.create(destination);
|
|
|
|
break;
|
|
|
|
case 'EDIT':
|
|
|
|
ctrl.update(destination);
|
2016-06-17 12:07:33 +02:00
|
|
|
break;
|
2016-06-14 12:47:32 +02:00
|
|
|
}
|
2016-07-02 18:22:45 +02:00
|
|
|
|
|
|
|
$timeout(function() {
|
|
|
|
if(!ctrl.toggleErrorMessage) {
|
|
|
|
element.find('#createDestinationModal').modal('hide');
|
|
|
|
}
|
|
|
|
}, 50);
|
2016-06-14 12:47:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-07 10:49:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
})();
|