2020-04-10 21:54:53 +00:00
|
|
|
angular.module('portainer.docker').controller('SwarmNodeDetailsPanelController', [
|
|
|
|
'NodeService',
|
|
|
|
'LabelHelper',
|
|
|
|
'Notifications',
|
|
|
|
'$state',
|
|
|
|
function SwarmNodeDetailsPanelController(NodeService, LabelHelper, Notifications, $state) {
|
|
|
|
var ctrl = this;
|
|
|
|
ctrl.state = {
|
|
|
|
managerAddress: '',
|
|
|
|
hasChanges: false,
|
|
|
|
};
|
|
|
|
ctrl.$onChanges = $onChanges;
|
|
|
|
ctrl.addLabel = addLabel;
|
|
|
|
ctrl.updateNodeLabels = updateNodeLabels;
|
|
|
|
ctrl.updateNodeAvailability = updateNodeAvailability;
|
|
|
|
ctrl.saveChanges = saveChanges;
|
|
|
|
ctrl.cancelChanges = cancelChanges;
|
2018-10-07 22:44:08 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
var managerRole = 'manager';
|
2018-10-07 22:44:08 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
function $onChanges() {
|
|
|
|
if (!ctrl.details) {
|
|
|
|
return;
|
2018-10-07 22:44:08 +00:00
|
|
|
}
|
2020-04-10 21:54:53 +00:00
|
|
|
if (ctrl.details.role === managerRole) {
|
|
|
|
ctrl.state.managerAddress = '(' + ctrl.details.managerAddress + ')';
|
2018-10-07 22:44:08 +00:00
|
|
|
}
|
2020-04-10 21:54:53 +00:00
|
|
|
}
|
2018-10-07 22:44:08 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
function addLabel() {
|
|
|
|
ctrl.details.nodeLabels.push({
|
|
|
|
key: '',
|
|
|
|
value: '',
|
|
|
|
originalValue: '',
|
|
|
|
originalKey: '',
|
|
|
|
});
|
|
|
|
}
|
2018-10-07 22:44:08 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
function updateNodeLabels(labels) {
|
|
|
|
ctrl.details.nodeLabels = labels;
|
|
|
|
ctrl.state.hasChanges = true;
|
|
|
|
}
|
2018-10-07 22:44:08 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
function updateNodeAvailability(availability) {
|
|
|
|
ctrl.details.availability = availability;
|
|
|
|
ctrl.state.hasChanges = true;
|
|
|
|
}
|
2018-10-07 22:44:08 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
function saveChanges() {
|
|
|
|
var originalNode = ctrl.originalNode;
|
|
|
|
var config = {
|
|
|
|
Name: originalNode.Name,
|
|
|
|
Availability: ctrl.details.availability,
|
|
|
|
Role: originalNode.Role,
|
|
|
|
Labels: LabelHelper.fromKeyValueToLabelHash(ctrl.details.nodeLabels),
|
|
|
|
Id: originalNode.Id,
|
|
|
|
Version: originalNode.Version,
|
|
|
|
};
|
2018-10-07 22:44:08 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
NodeService.updateNode(config).then(onUpdateSuccess).catch(notifyOnError);
|
2018-10-07 22:44:08 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
function onUpdateSuccess() {
|
|
|
|
Notifications.success('Node successfully updated', 'Node updated');
|
|
|
|
$state.go('docker.nodes.node', { id: originalNode.Id }, { reload: true });
|
2018-10-07 22:44:08 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
function notifyOnError(error) {
|
|
|
|
Notifications.error('Failure', error, 'Failed to update node');
|
2018-10-07 22:44:08 +00:00
|
|
|
}
|
2020-04-10 21:54:53 +00:00
|
|
|
}
|
2018-10-07 22:44:08 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
function cancelChanges() {
|
|
|
|
cancelLabelChanges();
|
|
|
|
ctrl.details.availability = ctrl.originalNode.Availability;
|
|
|
|
ctrl.state.hasChanges = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function cancelLabelChanges() {
|
|
|
|
ctrl.details.nodeLabels = ctrl.details.nodeLabels
|
|
|
|
.filter(function (label) {
|
|
|
|
return label.originalValue || label.originalKey;
|
|
|
|
})
|
|
|
|
.map(function (label) {
|
|
|
|
return Object.assign(label, {
|
|
|
|
value: label.originalValue,
|
|
|
|
key: label.originalKey,
|
2018-10-07 22:44:08 +00:00
|
|
|
});
|
2020-04-10 21:54:53 +00:00
|
|
|
});
|
2018-10-07 22:44:08 +00:00
|
|
|
}
|
2020-04-10 21:54:53 +00:00
|
|
|
},
|
|
|
|
]);
|