Skip to content

Commit 9d69e92

Browse files
committed
feat(cloudFoundry): get apiKey from VCAP_SERVICES
1 parent f1aee78 commit 9d69e92

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

lib/utils/configReader.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,20 @@ ConfigReader.prototype._getSystemConfig = function () {
3131

3232
ConfigReader.prototype._getCloudFoundryConfig = function () {
3333
var VCAP_APPLICATION = process.env.VCAP_APPLICATION
34+
var VCAP_SERVICES = process.env.VCAP_SERVICES
35+
var apiKey
3436

3537
try {
3638
VCAP_APPLICATION = JSON.parse(VCAP_APPLICATION)
39+
VCAP_SERVICES = JSON.parse(VCAP_SERVICES)
40+
apiKey = VCAP_SERVICES.trace[0].credentials.TRACE_API_KEY
3741
} catch (ex) {
3842
return {}
3943
}
4044

4145
return {
42-
serviceName: VCAP_APPLICATION.name
46+
serviceName: VCAP_APPLICATION.name,
47+
apiKey: apiKey
4348
}
4449
}
4550

lib/utils/configReader.spec.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,25 @@ describe('Config Reader module', function () {
164164
name: 'test-app'
165165
})
166166

167+
process.env.VCAP_SERVICES = JSON.stringify({
168+
trace: [{
169+
credentials: {
170+
TRACE_API_KEY: 'test-key'
171+
}
172+
}]
173+
})
174+
167175
var configReader = ConfigReader.create({
168176
reporter: 'dummy',
169-
collectorApiUrl: 'http://c.a.b',
170-
apiKey: 'api-key'
177+
collectorApiUrl: 'http://c.a.b'
171178
})
172179

173180
var cfg = configReader.getConfig()
174181

175182
expect(cfg.serviceName).to.eql('test-app')
183+
expect(cfg.apiKey).to.eql('test-key')
176184
delete process.env.VCAP_APPLICATION
185+
delete process.env.VCAP_SERVICES
177186
})
178187

179188
it('can find config file by default config', function () {

0 commit comments

Comments
 (0)