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");
1919var log = require ( "../log" ) ;
2020
2121var credentialCache = { } ;
22- var storage = null ;
2322var 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}
0 commit comments