Skip to content

Commit

Permalink
🔥 Remove fuzzy match for instance name
Browse files Browse the repository at this point in the history
  • Loading branch information
germanattanasio committed Apr 13, 2017
1 parent a3f5a6a commit b391134
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 35 deletions.
10 changes: 0 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,6 @@ If `VCAP_SERVICES` is:
}
```

OR If `PERSONALITY_INSIGHTS_*` (your instance name) is:
```json
{
"password": "<password>",
"url": "<url>",
"username": "<username>"
}
```


Output:
```json
{
Expand Down
20 changes: 4 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,19 @@ module.exports.getCredentials = function(name, plan, iname) {
}
}
//Check if env vars were set directly
var env = process.env,
instance = {};
var env = process.env;
var instance = {};

if(iname) {
iname = iname.toUpperCase().replace(/[\s&-]/g, '_');
if(env[iname]){
if(env[iname]) {
try {
instance = JSON.parse(env[iname]);
} catch(e) {
console.warn('Error parsing JSON from process.env.' + iname );
console.warn(e);
}
}
} else if (name && (Object.keys(name).length !== 0) ) {
name = name.toUpperCase().replace(/[\s&-]/g, '_');
for (var key in env){
if (env.hasOwnProperty(key) && key.startsWith(name)) {
try {
instance = JSON.parse(env[key]);
break;
} catch(e) {
console.warn('Error parsing JSON from process.env.' + key );
console.warn(e);
}
}
}
}
return instance;
};
12 changes: 3 additions & 9 deletions test/test.parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,11 @@ describe('vcap_services', function() {
});

it('should return conversation service credentials', function() {
assert.deepEqual(credentials, vcapServices.getCredentials('conversation'));
assert.deepEqual(credentials, vcapServices.getCredentials(null, null, 'conversation_w1'));
});

it('should return first available nosql db service information', function() {
assert.deepEqual(nosql_x5, vcapServices.getCredentials('cloudant_nosql_db'));
assert.deepEqual(nosql_x5, vcapServices.getCredentials('cloudant_nosql'));
assert.deepEqual(nosql_x5, vcapServices.getCredentials(null, null, 'cloudant_nosql_db_x5'));
});

it('should return instance of nosql db or fall back on service name if instance name DNE', function() {
Expand All @@ -134,14 +132,10 @@ describe('vcap_services', function() {

it('should return {} if the env variable is not upper case', function() {
assert.deepEqual({}, vcapServices.getCredentials(null, null, 'weather_company_data_wu'));
assert.deepEqual({}, vcapServices.getCredentials('weather_company_data'));
assert.deepEqual({}, vcapServices.getCredentials(null, null, 'weather_company_data'));
});

it('should return redis information when name or iname are specified with other delimiters [ -&]', function() {
assert.deepEqual(redis, vcapServices.getCredentials('COMPOSE_FOR_REDIS'));
assert.deepEqual(redis, vcapServices.getCredentials('Compose-for-Redis'));
assert.deepEqual(redis, vcapServices.getCredentials('Compose for Redis'));
assert.deepEqual(redis, vcapServices.getCredentials('Compose&for&Redis'));

assert.deepEqual(redis, vcapServices.getCredentials(null, null, 'COMPOSE_FOR_REDIS_OV'));
assert.deepEqual(redis, vcapServices.getCredentials(null, null, 'Compose-for-Redis-ov'));
Expand All @@ -150,7 +144,7 @@ describe('vcap_services', function() {
});

it('should return {} when the env var is not JSON', function() {
assert.deepEqual({}, vcapServices.getCredentials('OBJECT_STORAGE'));
assert.deepEqual({}, vcapServices.getCredentials(null, null, 'OBJECT_STORAGE'));

assert.deepEqual({}, vcapServices.getCredentials(null, null, 'Object Storage-6j'));
});
Expand Down

0 comments on commit b391134

Please sign in to comment.