2016-06-07 10:49:38 +02:00
|
|
|
(function() {
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
angular
|
|
|
|
.module('harbor.system.management')
|
|
|
|
.directive('createDestination', createDestination);
|
|
|
|
|
2016-06-22 13:03:12 +02:00
|
|
|
CreateDestinationController.$inject = ['$scope', 'ListDestinationService', 'CreateDestinationService', 'UpdateDestinationService', 'PingDestinationService', 'ListDestinationPolicyService', '$filter', 'trFilter'];
|
2016-06-07 10:49:38 +02:00
|
|
|
|
2016-06-22 13:03:12 +02:00
|
|
|
function CreateDestinationController($scope, ListDestinationService, CreateDestinationService, UpdateDestinationService, PingDestinationService, ListDestinationPolicyService, $filter, trFilter) {
|
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-06-14 12:47:32 +02:00
|
|
|
function addNew() {
|
2016-06-22 13:03:12 +02:00
|
|
|
vm.editable = true;
|
|
|
|
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-06-22 13:03:12 +02:00
|
|
|
alert($filter('tr')('destination_already_exists', []));
|
2016-06-17 12:07:33 +02:00
|
|
|
}
|
2016-06-07 10:49:38 +02:00
|
|
|
console.log('Failed create destination:' + data);
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
console.log('Failed update destination.');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
console.log('Failed get destination.');
|
|
|
|
}
|
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) {
|
|
|
|
console.log('Failed list destination policy:' + data);
|
|
|
|
}
|
|
|
|
|
2016-06-15 11:46:23 +02:00
|
|
|
function pingDestination() {
|
|
|
|
var target = {
|
|
|
|
'name': vm0.name,
|
|
|
|
'endpoint': vm0.endpoint,
|
|
|
|
'username': vm0.username,
|
|
|
|
'password': vm0.password
|
|
|
|
};
|
|
|
|
PingDestinationService(target)
|
|
|
|
.success(pingDestinationSuccess)
|
|
|
|
.error(pingDestinationFailed);
|
|
|
|
}
|
|
|
|
function pingDestinationSuccess(data, status) {
|
2016-06-22 13:03:12 +02:00
|
|
|
alert($filter('tr')('successful_ping_target', []));
|
2016-06-15 11:46:23 +02:00
|
|
|
}
|
|
|
|
function pingDestinationFailed(data, status) {
|
2016-06-22 13:03:12 +02:00
|
|
|
alert($filter('tr')('failed_ping_target', []) + ':' + data);
|
2016-06-15 11:46:23 +02:00
|
|
|
}
|
2016-06-07 10:49:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function createDestination() {
|
|
|
|
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-06-22 13:03:12 +02:00
|
|
|
|
2016-06-17 12:07:33 +02:00
|
|
|
scope.form.$setPristine();
|
|
|
|
scope.form.$setUntouched();
|
2016-06-22 13:03:12 +02:00
|
|
|
|
|
|
|
switch(ctrl.action) {
|
|
|
|
case 'ADD_NEW':
|
|
|
|
ctrl.addNew();
|
|
|
|
break;
|
|
|
|
case 'EDIT':
|
|
|
|
ctrl.edit(ctrl.targetId);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
scope.$apply();
|
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-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
|
|
|
}
|
|
|
|
element.find('#createDestinationModal').modal('hide');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-07 10:49:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
})();
|