Move credential load/save storage functions under get/setFlows

This commit is contained in:
Nick O'Leary
2016-09-21 21:58:50 +01:00
parent e06cadd761
commit f9b972349d
8 changed files with 251 additions and 326 deletions

View File

@@ -1,5 +1,5 @@
/**
* Copyright 2014, 2015 IBM Corp.
* Copyright 2014, 2016 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,34 +19,40 @@ var when = require("when");
var log = require("../log");
var credentialCache = {};
var storage = null;
var credentialsDef = {};
var dirty = false;
module.exports = {
init: function (_storage) {
storage = _storage;
var api = module.exports = {
init: function() {
dirty = false;
credentialCache = {};
credentialsDef = {};
},
/**
* Loads the credentials from storage.
* Sets the credentials from storage.
*/
load: function () {
return storage.getCredentials().then(function (creds) {
credentialCache = creds;
}).otherwise(function (err) {
log.warn(log._("nodes.credentials.error",{message: err}));
});
load: function (credentials) {
credentialCache = credentials;
dirty = false;
return when.resolve();
// return storage.getCredentials().then(function (creds) {
// credentialCache = creds;
// }).otherwise(function (err) {
// log.warn(log._("nodes.credentials.error",{message: err}));
// });
},
/**
* Adds a set of credentials for the given node id.
* @param id the node id for the credentials
* @param creds an object of credential key/value pairs
* @return a promise for the saving of credentials to storage
* @return a promise for backwards compatibility TODO: can this be removed?
*/
add: function (id, creds) {
credentialCache[id] = creds;
return storage.saveCredentials(credentialCache);
dirty = true;
return when.resolve();
},
/**
@@ -65,7 +71,7 @@ module.exports = {
*/
delete: function (id) {
delete credentialCache[id];
storage.saveCredentials(credentialCache);
dirty = true;
},
/**
@@ -77,6 +83,9 @@ module.exports = {
var existingIds = {};
config.forEach(function(n) {
existingIds[n.id] = true;
if (n.credentials) {
api.extract(n);
}
});
var deletedCredentials = false;
for (var c in credentialCache) {
@@ -88,10 +97,9 @@ module.exports = {
}
}
if (deletedCredentials) {
return storage.saveCredentials(credentialCache);
} else {
return when.resolve();
dirty = true;
}
return when.resolve();
},
/**
@@ -146,17 +154,10 @@ module.exports = {
}
}
credentialCache[nodeID] = savedCredentials;
dirty = true;
}
},
/**
* Saves the credentials to storage
* @return a promise for the saving of credentials to storage
*/
save: function () {
return storage.saveCredentials(credentialCache);
},
/**
* Gets the credential definition for the given node type
* @param type the node type
@@ -164,5 +165,14 @@ module.exports = {
*/
getDefinition: function (type) {
return credentialsDef[type];
},
dirty: function() {
return dirty;
},
export: function() {
dirty = false;
return when.resolve(credentialCache);
}
}

View File

@@ -67,9 +67,9 @@ function init(runtime) {
}
}
function load() {
return storage.getFlows().then(function(flows) {
return credentials.load().then(function() {
return setConfig(flows,"load");
return storage.getFlows().then(function(config) {
return credentials.load(config.credentials).then(function() {
return setConfig(config.flows,"load");
});
}).otherwise(function(err) {
log.warn(log._("nodes.flows.error",{message:err.toString()}));
@@ -81,8 +81,6 @@ function setConfig(_config,type,muteLog) {
var config = clone(_config);
type = type||"full";
var credentialsChanged = false;
var credentialSavePromise = null;
var configSavePromise = null;
var diff;
@@ -90,23 +88,20 @@ function setConfig(_config,type,muteLog) {
if (type !== 'full' && type !== 'load') {
diff = flowUtil.diffConfigs(activeFlowConfig,newFlowConfig);
}
config.forEach(function(node) {
if (node.credentials) {
credentials.extract(node);
credentialsChanged = true;
}
});
if (credentialsChanged) {
credentialSavePromise = credentials.save();
} else {
credentialSavePromise = when.resolve();
}
if (type === 'load') {
configSavePromise = credentialSavePromise;
type = 'full';
configSavePromise = when.resolve();
} else {
configSavePromise = credentialSavePromise.then(function() {
return storage.saveFlows(config);
credentials.clean(config);
var credsDirty = credentials.dirty();
configSavePromise = credentials.export().then(function(creds) {
var saveConfig = {
flows: config,
credentialsDirty:credsDirty,
credentials: creds
}
storage.saveFlows(saveConfig);
});
}
@@ -114,15 +109,13 @@ function setConfig(_config,type,muteLog) {
.then(function() {
activeConfig = config;
activeFlowConfig = newFlowConfig;
return credentials.clean(activeConfig).then(function() {
if (started) {
return stop(type,diff,muteLog).then(function() {
context.clean(activeFlowConfig);
start(type,diff,muteLog);
}).otherwise(function(err) {
})
}
});
if (started) {
return stop(type,diff,muteLog).then(function() {
context.clean(activeFlowConfig);
start(type,diff,muteLog);
}).otherwise(function(err) {
})
}
});
}

View File

@@ -77,7 +77,7 @@ function createNode(node,def) {
function init(runtime) {
settings = runtime.settings;
credentials.init(runtime.storage);
credentials.init();
flows.init(runtime);
registry.init(runtime);
context.init(runtime.settings);

View File

@@ -1,5 +1,5 @@
/**
* Copyright 2013, 2015 IBM Corp.
* Copyright 2013, 2016 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -55,17 +55,35 @@ var storageModuleInterface = {
return storageModule.init(runtime.settings);
},
getFlows: function() {
return storageModule.getFlows();
return storageModule.getFlows().then(function(flows) {
return storageModule.getCredentials().then(function(creds) {
return {
flows: flows,
credentials: creds
}
})
});
},
saveFlows: function(flows) {
return storageModule.saveFlows(flows);
},
getCredentials: function() {
return storageModule.getCredentials();
},
saveCredentials: function(credentials) {
return storageModule.saveCredentials(credentials);
saveFlows: function(config) {
var flows = config.flows;
var credentials = config.credentials;
var credentialSavePromise;
if (config.credentialsDirty) {
credentialSavePromise = storageModule.saveCredentials(credentials);
} else {
credentialSavePromise = when.resolve();
}
return credentialSavePromise.then(function() {
return storageModule.saveFlows(flows);
});
},
// getCredentials: function() {
// return storageModule.getCredentials();
// },
// saveCredentials: function(credentials) {
// return storageModule.saveCredentials(credentials);
// },
getSettings: function() {
if (settingsAvailable) {
return storageModule.getSettings();