Consolidate code for setting node maintenance state

Replace individual functions for putting-nodes-in and removing-nodes-from
maintenance with a single setMaintenance function. I believe this change
improves maintainability, and is more consistent with the undrlying client
api.

Change-Id: Ic23c72daa873891776fbb7669ccb44bfc6a11f4b
This commit is contained in:
Peter Piela 2017-05-18 09:28:19 -04:00
parent 2ec553f5bf
commit 15f4560b28
3 changed files with 66 additions and 127 deletions

View File

@ -52,9 +52,8 @@
getBootDevice: getBootDevice,
nodeGetConsole: nodeGetConsole,
nodeSetConsoleMode: nodeSetConsoleMode,
nodeSetMaintenance: nodeSetMaintenance,
nodeSetPowerState: nodeSetPowerState,
putNodeInMaintenanceMode: putNodeInMaintenanceMode,
removeNodeFromMaintenanceMode: removeNodeFromMaintenanceMode,
setNodeProvisionState: setNodeProvisionState,
updateNode: updateNode,
updatePort: updatePort,
@ -170,49 +169,32 @@
}
/**
* @description Put the node in maintenance mode.
* @description Set the maintenance state of a node
*
* http://developer.openstack.org/api-ref/baremetal/#set-maintenance-flag
*
* @param {string} uuid UUID or logical name of a node.
* @param {string} reason Reason for why node is being put into
* maintenance mode
* @param {string} nodeId UUID or logical name of a node.
* @param {boolean} mode - True to put the node in maintenance mode,
* false to remove it from maintenance mode.
* @param {string} reason - Reason for putting the node in maintenance.
* @return {promise} Promise
*/
function putNodeInMaintenanceMode(uuid, reason) {
return apiService.patch('/api/ironic/nodes/' + uuid + '/maintenance',
{maint_reason: reason
? reason
: gettext("No reason given.")})
.catch(function(response) {
var msg = interpolate(
gettext('Unable to put the Ironic node %s in maintenance mode: %s'),
[uuid, response.data],
false);
toastService.add('error', msg);
return $q.reject(msg);
});
}
function nodeSetMaintenance(nodeId, mode, reason) {
var url = '/api/ironic/nodes/' + nodeId + '/maintenance';
var promise = mode
? apiService.patch(url,
{maint_reason: reason ? reason
: gettext("No reason given.")})
: apiService.delete(url);
/**
* @description Remove the node from maintenance mode.
*
* http://developer.openstack.org/api-ref/baremetal/#clear-maintenance-flag
*
* @param {string} uuid UUID or logical name of a node.
* @return {promise} Promise
*/
function removeNodeFromMaintenanceMode(uuid) {
return apiService.delete('/api/ironic/nodes/' + uuid + '/maintenance')
.catch(function(response) {
var msg = interpolate(
gettext(
'Unable to remove the Ironic node %s from maintenance mode: %s'),
[uuid, response.data],
false);
toastService.add('error', msg);
return $q.reject(msg);
});
return promise.catch(function(response) {
var msg = interpolate(
gettext('Unable to set Ironic node %s maintenance state: %s'),
[nodeId, response.data],
false);
toastService.add('error', msg);
return $q.reject(msg);
});
}
/**

View File

@ -40,46 +40,32 @@
};
return service;
/*
* @description Put a specified list of nodes into mainenance.
* A modal dialog is used to prompt the user for a reason for
* putting the nodes in maintenance mode.
*
* @param {object[]} nodes - List of node objects
* @return {promise}
*/
function putNodeInMaintenanceMode(nodes) {
var options = {
controller: "MaintenanceController as ctrl",
templateUrl: basePath + '/maintenance/maintenance.html'
};
return $uibModal.open(options).result.then(function(reason) {
return nodeActions.putNodeInMaintenanceMode(nodes, reason);
});
}
/*
* @description Take a specified list of nodes out of mainenance
*
* @param {object[]} nodes - List of node objects
* @return {promise}
*/
function removeNodeFromMaintenanceMode(nodes) {
return nodeActions.removeNodeFromMaintenanceMode(nodes);
}
/*
* @description Set the maintenance mode of a specified list of nodes
*
* If nodes are being put into maintenance mode a modal dialog is used
* to prompt the user for a reason.
*
* @param {object[]} nodes - List of node objects
* @param {boolean} mode - Desired maintenance state.
* 'true' -> Node is in maintenance mode
* 'false' -> Node is not in maintenance mode
* @return {promise}
*/
*/
function setMaintenance(nodes, mode) {
return mode ? putNodeInMaintenanceMode(nodes)
: removeNodeFromMaintenanceMode(nodes);
var promise;
if (mode) {
var options = {
controller: "MaintenanceController as ctrl",
templateUrl: basePath + '/maintenance/maintenance.html'
};
promise = $uibModal.open(options).result.then(function(reason) {
return nodeActions.setMaintenance(nodes, true, reason);
});
} else {
promise = nodeActions.setMaintenance(nodes, false);
}
return promise;
}
}
})();

View File

@ -60,8 +60,7 @@
deleteNode: deleteNode,
deletePort: deletePort,
setPowerState: setPowerState,
putNodeInMaintenanceMode: putNodeInMaintenanceMode,
removeNodeFromMaintenanceMode: removeNodeFromMaintenanceMode,
setMaintenance: setMaintenance,
setProvisionState: setProvisionState,
getPowerTransitions : getPowerTransitions
};
@ -120,41 +119,37 @@
// maintenance
function putNodeInMaintenanceMode(nodes, reason) {
return applyFuncToNodes(
function(node, reason) {
if (node.maintenance !== false) {
var msg = gettext("Node %s is already in maintenance mode.");
return $q.reject(interpolate(msg, [node.uuid], false));
}
return ironic.putNodeInMaintenanceMode(node.uuid, reason).then(
/**
* @description Set the maintenance state of a list of nodes
*
* @param {object[]} nodes - List of node objects
* @param {boolean} mode - True if the nodes are to be put in
* maintenance mode, otherwise false.
* @param {string} [reason] - Optional reason for putting nodes in
* maintenance mode.
* @return {promise} promise
*/
function setMaintenance(nodes, mode, reason) {
var promises = [];
angular.forEach(nodes, function(node) {
var promise;
if (node.maintenance === mode) {
var msg = gettext(
"Node %s is already in target maintenance state.");
promise = $q.reject(interpolate(msg, [node.uuid], false));
} else {
promise = ironic.nodeSetMaintenance(node.uuid, mode, reason).then(
function (result) {
node.maintenance = true;
node.maintenance_reason = reason;
node.maintenance = mode;
node.maintenance_reason =
mode && angular.isDefined(reason) ? reason : "";
return result;
}
);
},
nodes,
reason);
}
function removeNodeFromMaintenanceMode(nodes) {
return applyFuncToNodes(
function(node) {
if (node.maintenance !== true) {
var msg = gettext("Node %s is not in maintenance mode.");
return $q.reject(interpolate(msg, [node.uuid], false));
}
return ironic.removeNodeFromMaintenanceMode(node.uuid).then(
function (result) {
node.maintenance = false;
node.maintenance_reason = "";
return result;
}
);
},
nodes);
}
promises.push(promise);
});
return $q.all(promises);
}
/*
@ -202,30 +197,6 @@
return deleteModalService.open($rootScope, ports, context);
}
/*
* @name horizon.dashboard.admin.ironic.actions.applyFuncToNodes
* @description Apply a specified function to each member of a
* collection of nodes
*
* @param {function} fn Function to be applied.
* The function should accept a node as the first argument. An optional
* second argument can be used to provide additional information.
* The function should return a promise.
* @param {Array<node>} nodes - Collection of nodes
* @param {object} extra - Additional argument passed to the function
* @return {promise} - Single promise that represents the combined
* return status from all function invocations. The promise is rejected
* if any individual call fails.
*/
function applyFuncToNodes(fn, nodes, extra) {
var promises = [];
angular.forEach(nodes,
function(node) {
promises.push(fn(node, extra));
});
return $q.all(promises);
}
/*
* @name horizon.dashboard.admin.ironic.actions.getPowerTransitions
* @description Get the list of power transitions for a specified