Merge "Consolidate code for setting node maintenance state"

This commit is contained in:
Jenkins 2017-06-05 12:31:29 +00:00 committed by Gerrit Code Review
commit 71d1045ed1
3 changed files with 66 additions and 127 deletions

View File

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

View File

@ -40,46 +40,32 @@
}; };
return service; 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 * @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 {object[]} nodes - List of node objects
* @param {boolean} mode - Desired maintenance state. * @param {boolean} mode - Desired maintenance state.
* 'true' -> Node is in maintenance mode * 'true' -> Node is in maintenance mode
* 'false' -> Node is not in maintenance mode * 'false' -> Node is not in maintenance mode
* @return {promise} * @return {promise}
*/ */
function setMaintenance(nodes, mode) { function setMaintenance(nodes, mode) {
return mode ? putNodeInMaintenanceMode(nodes) var promise;
: removeNodeFromMaintenanceMode(nodes); 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, deleteNode: deleteNode,
deletePort: deletePort, deletePort: deletePort,
setPowerState: setPowerState, setPowerState: setPowerState,
putNodeInMaintenanceMode: putNodeInMaintenanceMode, setMaintenance: setMaintenance,
removeNodeFromMaintenanceMode: removeNodeFromMaintenanceMode,
setProvisionState: setProvisionState, setProvisionState: setProvisionState,
getPowerTransitions : getPowerTransitions getPowerTransitions : getPowerTransitions
}; };
@ -120,41 +119,37 @@
// maintenance // maintenance
function putNodeInMaintenanceMode(nodes, reason) { /**
return applyFuncToNodes( * @description Set the maintenance state of a list of nodes
function(node, reason) { *
if (node.maintenance !== false) { * @param {object[]} nodes - List of node objects
var msg = gettext("Node %s is already in maintenance mode."); * @param {boolean} mode - True if the nodes are to be put in
return $q.reject(interpolate(msg, [node.uuid], false)); * maintenance mode, otherwise false.
} * @param {string} [reason] - Optional reason for putting nodes in
return ironic.putNodeInMaintenanceMode(node.uuid, reason).then( * 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) { function (result) {
node.maintenance = true; node.maintenance = mode;
node.maintenance_reason = reason; node.maintenance_reason =
mode && angular.isDefined(reason) ? reason : "";
return result; return result;
} }
); );
}, }
nodes, promises.push(promise);
reason); });
} return $q.all(promises);
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);
} }
/* /*
@ -202,30 +197,6 @@
return deleteModalService.open($rootScope, ports, context); 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 * @name horizon.dashboard.admin.ironic.actions.getPowerTransitions
* @description Get the list of power transitions for a specified * @description Get the list of power transitions for a specified