Skip to content

Commit f9b9723

Browse files
committed
Move credential load/save storage functions under get/setFlows
1 parent e06cadd commit f9b9723

8 files changed

Lines changed: 240 additions & 315 deletions

File tree

red/runtime/nodes/credentials.js

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2014, 2015 IBM Corp.
2+
* Copyright 2014, 2016 IBM Corp.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,34 +19,40 @@ var when = require("when");
1919
var log = require("../log");
2020

2121
var credentialCache = {};
22-
var storage = null;
2322
var credentialsDef = {};
23+
var dirty = false;
2424

25-
module.exports = {
26-
init: function (_storage) {
27-
storage = _storage;
25+
var api = module.exports = {
26+
init: function() {
27+
dirty = false;
28+
credentialCache = {};
29+
credentialsDef = {};
2830
},
2931

3032
/**
31-
* Loads the credentials from storage.
33+
* Sets the credentials from storage.
3234
*/
33-
load: function () {
34-
return storage.getCredentials().then(function (creds) {
35-
credentialCache = creds;
36-
}).otherwise(function (err) {
37-
log.warn(log._("nodes.credentials.error",{message: err}));
38-
});
35+
load: function (credentials) {
36+
credentialCache = credentials;
37+
dirty = false;
38+
return when.resolve();
39+
// return storage.getCredentials().then(function (creds) {
40+
// credentialCache = creds;
41+
// }).otherwise(function (err) {
42+
// log.warn(log._("nodes.credentials.error",{message: err}));
43+
// });
3944
},
4045

4146
/**
4247
* Adds a set of credentials for the given node id.
4348
* @param id the node id for the credentials
4449
* @param creds an object of credential key/value pairs
45-
* @return a promise for the saving of credentials to storage
50+
* @return a promise for backwards compatibility TODO: can this be removed?
4651
*/
4752
add: function (id, creds) {
4853
credentialCache[id] = creds;
49-
return storage.saveCredentials(credentialCache);
54+
dirty = true;
55+
return when.resolve();
5056
},
5157

5258
/**
@@ -65,7 +71,7 @@ module.exports = {
6571
*/
6672
delete: function (id) {
6773
delete credentialCache[id];
68-
storage.saveCredentials(credentialCache);
74+
dirty = true;
6975
},
7076

7177
/**
@@ -77,6 +83,9 @@ module.exports = {
7783
var existingIds = {};
7884
config.forEach(function(n) {
7985
existingIds[n.id] = true;
86+
if (n.credentials) {
87+
api.extract(n);
88+
}
8089
});
8190
var deletedCredentials = false;
8291
for (var c in credentialCache) {
@@ -88,10 +97,9 @@ module.exports = {
8897
}
8998
}
9099
if (deletedCredentials) {
91-
return storage.saveCredentials(credentialCache);
92-
} else {
93-
return when.resolve();
100+
dirty = true;
94101
}
102+
return when.resolve();
95103
},
96104

97105
/**
@@ -146,23 +154,25 @@ module.exports = {
146154
}
147155
}
148156
credentialCache[nodeID] = savedCredentials;
157+
dirty = true;
149158
}
150159
},
151160

152-
/**
153-
* Saves the credentials to storage
154-
* @return a promise for the saving of credentials to storage
155-
*/
156-
save: function () {
157-
return storage.saveCredentials(credentialCache);
158-
},
159-
160161
/**
161162
* Gets the credential definition for the given node type
162163
* @param type the node type
163164
* @return the credential definition
164165
*/
165166
getDefinition: function (type) {
166167
return credentialsDef[type];
168+
},
169+
170+
dirty: function() {
171+
return dirty;
172+
},
173+
174+
export: function() {
175+
dirty = false;
176+
return when.resolve(credentialCache);
167177
}
168178
}

red/runtime/nodes/flows/index.js

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ function init(runtime) {
6767
}
6868
}
6969
function load() {
70-
return storage.getFlows().then(function(flows) {
71-
return credentials.load().then(function() {
72-
return setConfig(flows,"load");
70+
return storage.getFlows().then(function(config) {
71+
return credentials.load(config.credentials).then(function() {
72+
return setConfig(config.flows,"load");
7373
});
7474
}).otherwise(function(err) {
7575
log.warn(log._("nodes.flows.error",{message:err.toString()}));
@@ -81,48 +81,41 @@ function setConfig(_config,type,muteLog) {
8181
var config = clone(_config);
8282
type = type||"full";
8383

84-
var credentialsChanged = false;
85-
var credentialSavePromise = null;
8684
var configSavePromise = null;
8785

8886
var diff;
8987
var newFlowConfig = flowUtil.parseConfig(clone(config));
9088
if (type !== 'full' && type !== 'load') {
9189
diff = flowUtil.diffConfigs(activeFlowConfig,newFlowConfig);
9290
}
93-
config.forEach(function(node) {
94-
if (node.credentials) {
95-
credentials.extract(node);
96-
credentialsChanged = true;
97-
}
98-
});
99-
if (credentialsChanged) {
100-
credentialSavePromise = credentials.save();
101-
} else {
102-
credentialSavePromise = when.resolve();
103-
}
91+
10492
if (type === 'load') {
105-
configSavePromise = credentialSavePromise;
10693
type = 'full';
94+
configSavePromise = when.resolve();
10795
} else {
108-
configSavePromise = credentialSavePromise.then(function() {
109-
return storage.saveFlows(config);
96+
credentials.clean(config);
97+
var credsDirty = credentials.dirty();
98+
configSavePromise = credentials.export().then(function(creds) {
99+
var saveConfig = {
100+
flows: config,
101+
credentialsDirty:credsDirty,
102+
credentials: creds
103+
}
104+
storage.saveFlows(saveConfig);
110105
});
111106
}
112107

113108
return configSavePromise
114109
.then(function() {
115110
activeConfig = config;
116111
activeFlowConfig = newFlowConfig;
117-
return credentials.clean(activeConfig).then(function() {
118-
if (started) {
119-
return stop(type,diff,muteLog).then(function() {
120-
context.clean(activeFlowConfig);
121-
start(type,diff,muteLog);
122-
}).otherwise(function(err) {
123-
})
124-
}
125-
});
112+
if (started) {
113+
return stop(type,diff,muteLog).then(function() {
114+
context.clean(activeFlowConfig);
115+
start(type,diff,muteLog);
116+
}).otherwise(function(err) {
117+
})
118+
}
126119
});
127120
}
128121

red/runtime/nodes/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ function createNode(node,def) {
7777

7878
function init(runtime) {
7979
settings = runtime.settings;
80-
credentials.init(runtime.storage);
80+
credentials.init();
8181
flows.init(runtime);
8282
registry.init(runtime);
8383
context.init(runtime.settings);

red/runtime/storage/index.js

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2013, 2015 IBM Corp.
2+
* Copyright 2013, 2016 IBM Corp.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -55,17 +55,35 @@ var storageModuleInterface = {
5555
return storageModule.init(runtime.settings);
5656
},
5757
getFlows: function() {
58-
return storageModule.getFlows();
59-
},
60-
saveFlows: function(flows) {
61-
return storageModule.saveFlows(flows);
62-
},
63-
getCredentials: function() {
64-
return storageModule.getCredentials();
58+
return storageModule.getFlows().then(function(flows) {
59+
return storageModule.getCredentials().then(function(creds) {
60+
return {
61+
flows: flows,
62+
credentials: creds
63+
}
64+
})
65+
});
6566
},
66-
saveCredentials: function(credentials) {
67-
return storageModule.saveCredentials(credentials);
67+
saveFlows: function(config) {
68+
var flows = config.flows;
69+
var credentials = config.credentials;
70+
var credentialSavePromise;
71+
if (config.credentialsDirty) {
72+
credentialSavePromise = storageModule.saveCredentials(credentials);
73+
} else {
74+
credentialSavePromise = when.resolve();
75+
}
76+
77+
return credentialSavePromise.then(function() {
78+
return storageModule.saveFlows(flows);
79+
});
6880
},
81+
// getCredentials: function() {
82+
// return storageModule.getCredentials();
83+
// },
84+
// saveCredentials: function(credentials) {
85+
// return storageModule.saveCredentials(credentials);
86+
// },
6987
getSettings: function() {
7088
if (settingsAvailable) {
7189
return storageModule.getSettings();

0 commit comments

Comments
 (0)