2016-06-07 10:49:38 +02:00
|
|
|
(function() {
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
angular
|
|
|
|
.module('harbor.replication')
|
|
|
|
.directive('createPolicy', createPolicy);
|
|
|
|
|
2016-06-15 11:46:23 +02:00
|
|
|
CreatePolicyController.$inject = ['$scope', 'ListReplicationPolicyService', 'ListDestinationService', 'UpdateDestinationService', 'PingDestinationService', 'CreateReplicationPolicyService', 'UpdateReplicationPolicyService', '$location', 'getParameterByName'];
|
2016-06-11 15:16:01 +02:00
|
|
|
|
2016-06-15 11:46:23 +02:00
|
|
|
function CreatePolicyController($scope, ListReplicationPolicyService, ListDestinationService, UpdateDestinationService, PingDestinationService, CreateReplicationPolicyService, UpdateReplicationPolicyService, $location, getParameterByName) {
|
2016-06-07 10:49:38 +02:00
|
|
|
var vm = this;
|
|
|
|
|
2016-06-11 15:16:01 +02:00
|
|
|
//Since can not set value for textarea by using vm
|
|
|
|
//use $scope for instead.
|
|
|
|
$scope.replication = {};
|
|
|
|
$scope.replication.policy = {};
|
2016-06-13 12:12:46 +02:00
|
|
|
$scope.replication.destination = {};
|
2016-06-11 15:16:01 +02:00
|
|
|
|
2016-06-13 12:12:46 +02:00
|
|
|
var vm0 = $scope.replication.policy;
|
|
|
|
var vm1 = $scope.replication.destination;
|
2016-06-17 12:07:33 +02:00
|
|
|
|
2016-06-13 12:12:46 +02:00
|
|
|
vm.selectDestination = selectDestination;
|
2016-06-11 15:16:01 +02:00
|
|
|
vm.projectId = getParameterByName('project_id', $location.absUrl());
|
2016-06-13 12:12:46 +02:00
|
|
|
|
2016-06-17 12:07:33 +02:00
|
|
|
$scope.$on('$locationChangeSuccess', function() {
|
|
|
|
vm.projectId = getParameterByName('project_id', $location.absUrl());
|
|
|
|
});
|
|
|
|
|
2016-06-13 12:12:46 +02:00
|
|
|
vm.addNew = addNew;
|
|
|
|
vm.edit = edit;
|
2016-06-15 11:46:23 +02:00
|
|
|
vm.prepareDestination = prepareDestination;
|
2016-06-17 12:07:33 +02:00
|
|
|
vm.create = create;
|
|
|
|
vm.update = update;
|
2016-06-13 12:12:46 +02:00
|
|
|
vm.pingDestination = pingDestination;
|
|
|
|
|
2016-06-11 15:16:01 +02:00
|
|
|
$scope.$watch('vm.destinations', function(current) {
|
|
|
|
if(current) {
|
2016-06-13 12:12:46 +02:00
|
|
|
vm1.selection = current[0];
|
|
|
|
vm1.endpoint = vm1.selection.endpoint;
|
|
|
|
vm1.username = vm1.selection.username;
|
|
|
|
vm1.password = vm1.selection.password;
|
|
|
|
}
|
|
|
|
});
|
2016-06-17 12:07:33 +02:00
|
|
|
|
2016-06-14 12:47:32 +02:00
|
|
|
$scope.$watch('vm.action+","+vm.policyId', function(current) {
|
2016-06-13 12:12:46 +02:00
|
|
|
if(current) {
|
|
|
|
console.log('Current action for replication policy:' + current);
|
2016-06-14 12:47:32 +02:00
|
|
|
var parts = current.split(',');
|
|
|
|
vm.action = parts[0];
|
|
|
|
vm.policyId = Number(parts[1]);
|
|
|
|
switch(parts[0]) {
|
2016-06-13 12:12:46 +02:00
|
|
|
case 'ADD_NEW':
|
2016-06-17 12:07:33 +02:00
|
|
|
vm.addNew();
|
|
|
|
break;
|
2016-06-13 12:12:46 +02:00
|
|
|
case 'EDIT':
|
2016-06-17 12:07:33 +02:00
|
|
|
vm.edit(vm.policyId);
|
|
|
|
break;
|
2016-06-13 12:12:46 +02:00
|
|
|
}
|
2016-06-11 15:16:01 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-06-13 12:12:46 +02:00
|
|
|
function selectDestination(item) {
|
|
|
|
vm1.selection = item;
|
|
|
|
vm1.endpoint = item.endpoint;
|
|
|
|
vm1.username = item.username;
|
|
|
|
vm1.password = item.password;
|
|
|
|
}
|
|
|
|
|
|
|
|
function prepareDestination() {
|
|
|
|
ListDestinationService('')
|
2016-06-11 15:16:01 +02:00
|
|
|
.success(listDestinationSuccess)
|
|
|
|
.error(listDestinationFailed);
|
|
|
|
}
|
2016-06-13 12:12:46 +02:00
|
|
|
|
2016-06-17 12:07:33 +02:00
|
|
|
function addNew() {
|
2016-06-13 12:12:46 +02:00
|
|
|
vm0.name = '';
|
|
|
|
vm0.description = '';
|
|
|
|
vm0.enabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function edit(policyId) {
|
|
|
|
console.log('Edit policy ID:' + policyId);
|
|
|
|
ListReplicationPolicyService(policyId)
|
|
|
|
.success(listReplicationPolicySuccess)
|
|
|
|
.error(listReplicationPolicyFailed);
|
|
|
|
}
|
2016-06-11 15:16:01 +02:00
|
|
|
|
2016-06-17 12:07:33 +02:00
|
|
|
function create(policy) {
|
2016-06-11 15:16:01 +02:00
|
|
|
CreateReplicationPolicyService(policy)
|
|
|
|
.success(createReplicationPolicySuccess)
|
|
|
|
.error(createReplicationPolicyFailed);
|
|
|
|
}
|
|
|
|
|
2016-06-17 12:07:33 +02:00
|
|
|
function update(policy) {
|
2016-06-14 12:47:32 +02:00
|
|
|
console.log('Update policy ID:' + vm.policyId);
|
|
|
|
UpdateReplicationPolicyService(vm.policyId, policy)
|
|
|
|
.success(updateReplicationPolicySuccess)
|
|
|
|
.error(updateReplicationPolicyFailed);
|
2016-06-15 11:46:23 +02:00
|
|
|
|
|
|
|
var targetId = vm1.selection.id;
|
|
|
|
console.log('Update target ID:' + targetId);
|
|
|
|
var target = {
|
|
|
|
'name': vm1.selection.name,
|
|
|
|
'endpoint': vm1.endpoint,
|
|
|
|
'username': vm1.username,
|
|
|
|
'password': vm1.password
|
|
|
|
};
|
|
|
|
UpdateDestinationService(targetId, target)
|
|
|
|
.success(updateDestinationSuccess)
|
|
|
|
.error(updateDestinationFailed);
|
2016-06-14 12:47:32 +02:00
|
|
|
}
|
|
|
|
|
2016-06-13 12:12:46 +02:00
|
|
|
function pingDestination() {
|
2016-06-15 11:46:23 +02:00
|
|
|
var target = {
|
|
|
|
'name': vm1.selection.name,
|
|
|
|
'endpoint': vm1.endpoint,
|
|
|
|
'username': vm1.username,
|
|
|
|
'password': vm1.password
|
|
|
|
};
|
|
|
|
PingDestinationService(target)
|
2016-06-13 12:12:46 +02:00
|
|
|
.success(pingDestinationSuccess)
|
|
|
|
.error(pingDestinationFailed);
|
|
|
|
}
|
|
|
|
|
2016-06-11 15:16:01 +02:00
|
|
|
function listDestinationSuccess(data, status) {
|
2016-06-17 12:07:33 +02:00
|
|
|
vm.destinations = data || [];
|
2016-06-11 15:16:01 +02:00
|
|
|
}
|
|
|
|
function listDestinationFailed(data, status) {
|
|
|
|
console.log('Failed list destination:' + data);
|
|
|
|
}
|
2016-06-13 12:12:46 +02:00
|
|
|
function listReplicationPolicySuccess(data, status) {
|
2016-06-14 12:47:32 +02:00
|
|
|
var replicationPolicy = data;
|
2016-06-13 12:12:46 +02:00
|
|
|
vm0.name = replicationPolicy.name;
|
|
|
|
vm0.description = replicationPolicy.description;
|
2016-06-14 12:47:32 +02:00
|
|
|
vm0.enabled = replicationPolicy.enabled == 1;
|
2016-06-13 12:12:46 +02:00
|
|
|
vm.targetId = replicationPolicy.target_id;
|
|
|
|
}
|
|
|
|
function listReplicationPolicyFailed(data, status) {
|
|
|
|
console.log('Failed list replication policy:' + data);
|
|
|
|
}
|
2016-06-11 15:16:01 +02:00
|
|
|
function createReplicationPolicySuccess(data, status) {
|
|
|
|
console.log('Successful create replication policy.');
|
2016-06-17 12:07:33 +02:00
|
|
|
vm.reload();
|
2016-06-11 15:16:01 +02:00
|
|
|
}
|
|
|
|
function createReplicationPolicyFailed(data, status) {
|
2016-06-17 12:07:33 +02:00
|
|
|
if(status === 409) {
|
|
|
|
alert('Policy name already exists.');
|
|
|
|
}
|
2016-06-11 15:16:01 +02:00
|
|
|
console.log('Failed create replication policy.');
|
2016-06-07 10:49:38 +02:00
|
|
|
}
|
2016-06-14 12:47:32 +02:00
|
|
|
function updateReplicationPolicySuccess(data, status) {
|
|
|
|
console.log('Successful update replication policy.');
|
2016-06-17 12:07:33 +02:00
|
|
|
vm.reload();
|
2016-06-14 12:47:32 +02:00
|
|
|
}
|
|
|
|
function updateReplicationPolicyFailed(data, status) {
|
|
|
|
console.log('Failed update replication policy.');
|
|
|
|
}
|
2016-06-15 11:46:23 +02:00
|
|
|
function updateDestinationSuccess(data, status) {
|
|
|
|
console.log('Successful update destination.');
|
|
|
|
}
|
|
|
|
function updateDestinationFailed(data, status) {
|
|
|
|
console.log('Failed update destination.');
|
|
|
|
}
|
2016-06-13 12:12:46 +02:00
|
|
|
function pingDestinationSuccess(data, status) {
|
2016-06-15 11:46:23 +02:00
|
|
|
alert('Successful ping target.');
|
2016-06-13 12:12:46 +02:00
|
|
|
}
|
|
|
|
function pingDestinationFailed(data, status) {
|
|
|
|
alert('Failed ping target:' + data);
|
|
|
|
}
|
2016-06-07 10:49:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function createPolicy() {
|
|
|
|
var directive = {
|
|
|
|
'restrict': 'E',
|
2016-06-16 08:10:35 +02:00
|
|
|
'templateUrl': '/static/resources/js/components/replication/create-policy.directive.html',
|
2016-06-11 15:16:01 +02:00
|
|
|
'scope': {
|
2016-06-13 12:12:46 +02:00
|
|
|
'policyId': '@',
|
|
|
|
'modalTitle': '@',
|
|
|
|
'reload': '&',
|
|
|
|
'action': '='
|
2016-06-11 15:16:01 +02:00
|
|
|
},
|
|
|
|
'link': link,
|
2016-06-07 10:49:38 +02:00
|
|
|
'controller': CreatePolicyController,
|
|
|
|
'controllerAs': 'vm',
|
|
|
|
'bindToController': true
|
|
|
|
};
|
|
|
|
return directive;
|
2016-06-11 15:16:01 +02:00
|
|
|
|
2016-06-17 12:07:33 +02:00
|
|
|
function link(scope, element, attr, ctrl) {
|
|
|
|
|
|
|
|
element.find('#createPolicyModal').on('show.bs.modal', function() {
|
2016-06-15 11:46:23 +02:00
|
|
|
ctrl.prepareDestination();
|
|
|
|
scope.form.$setPristine();
|
2016-06-17 12:07:33 +02:00
|
|
|
scope.form.$setUntouched();
|
|
|
|
});
|
|
|
|
|
2016-06-11 15:16:01 +02:00
|
|
|
ctrl.save = save;
|
2016-06-15 11:46:23 +02:00
|
|
|
|
2016-06-11 15:16:01 +02:00
|
|
|
function save(form) {
|
|
|
|
var postPayload = {
|
|
|
|
'projectId': Number(ctrl.projectId),
|
2016-06-13 12:12:46 +02:00
|
|
|
'targetId': form.destination.selection.id,
|
2016-06-11 15:16:01 +02:00
|
|
|
'name': form.policy.name,
|
|
|
|
'enabled': form.policy.enabled ? 1 : 0,
|
|
|
|
'description': form.policy.description,
|
|
|
|
'cron_str': '',
|
|
|
|
'start_time': ''
|
|
|
|
};
|
2016-06-14 12:47:32 +02:00
|
|
|
switch(ctrl.action) {
|
|
|
|
case 'ADD_NEW':
|
2016-06-17 12:07:33 +02:00
|
|
|
ctrl.create(postPayload);
|
|
|
|
break;
|
2016-06-14 12:47:32 +02:00
|
|
|
case 'EDIT':
|
2016-06-17 12:07:33 +02:00
|
|
|
ctrl.update(postPayload);
|
|
|
|
break;
|
2016-06-14 12:47:32 +02:00
|
|
|
}
|
2016-06-11 15:16:01 +02:00
|
|
|
element.find('#createPolicyModal').modal('hide');
|
|
|
|
}
|
|
|
|
}
|
2016-06-07 10:49:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
})();
|