Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: changes to node-sdk-core to work with service factory feature #72

Merged
merged 10 commits into from
Nov 19, 2019

Conversation

SaltedCaramelCoffee
Copy link
Contributor

Checklist
  • npm test passes (tip: npm run lint-fix can correct most style issues)
  • tests are included
  • documentation is changed or added

@padamstx padamstx self-requested a review November 14, 2019 21:37
@CLAassistant
Copy link

CLAassistant commented Nov 14, 2019

CLA assistant check
All committers have signed the CLA.

@codecov
Copy link

codecov bot commented Nov 14, 2019

Codecov Report

Merging #72 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@          Coverage Diff          @@
##           master    #72   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           1      1           
  Lines           8      8           
  Branches        1      1           
=====================================
  Hits            8      8

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update dbe1064...17503f9. Read the comment docs.

Copy link
Member

@padamstx padamstx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good... just left a few comments about some nitpicky things :)
I'll approve assuming those are addressed.
I suggested alternate comments for testcases and a new name for a function or two. My specific naming suggestions aren't that important, but the general point is to maybe just use a more descriptive name, etc.

@@ -97,8 +96,31 @@ function filterPropertiesByServiceName(envObj: any, serviceName: string): any {
* Pulls credentials from VCAP_SERVICES env property that IBM Cloud sets
*
*/
function getCredentials(name) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After copying this code from its original package into this file, I think it would be good to give this a more descriptive name.
In fact, I'll suggest that you rename getCredentialsFromCloud to be getPropertiesFromVCAP and then rename getCredentials to be something like getVCAPCredentialsForService, or just move the getCredentials logic into the newly-renamed getPropertiesFromVCAP.

expect(credentials.password).toEqual("not-a-password")
});

it('should return first service in the list when multiple services found', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it('should return first service in the list when multiple services found', () => {
it('should return first service in the list when matching on primary service key', () => {

expect(Object.keys(credentials).length).toEqual(0);
});

it('should return empty credential object when looking for credentials by service name', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it('should return empty credential object when looking for credentials by service name', () => {
it('should return credential object matching service name field not first list element', () => {

expect(credentials.password).toEqual("not-a-password")
});

it('should return empty credential object when looking for credentials by service name', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it('should return empty credential object when looking for credentials by service name', () => {
it('should return credential object when matching service name field on first list element', () => {

expect(credentials.password).toEqual("not-a-password")
});

it('should return empty credential object when service key is no-creds-service-two', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it('should return empty credential object when service key is no-creds-service-two', () => {
it('should return empty credential object when matching on service name with no credentials field', () => {

Copy link
Member

@dpopp07 dpopp07 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good! I made a few suggestions, which don't necessarily need to be adhered to. I'm mostly requesting changes to get clarity on if this is a breaking change or not before we merge

"plan": "elite"
}
]
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: will you add a newline here?

Suggested change
}
}

lib/base-service.ts Show resolved Hide resolved
auth/utils/read-external-sources.ts Show resolved Hide resolved
const credentials = vcapServices.getCredentials(serviceName);
function getVCAPCredentialsForService(name) {
if (process.env.VCAP_SERVICES) {
const services = JSON.parse(process.env.VCAP_SERVICES);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be safer to wrap the JSON parse in a try/catch since it throws an error if anything about the JSON is malformatted. Not that it should be, since that env variable is generated by the cloud. So this is an optional change, up to you.

Suggested change
const services = JSON.parse(process.env.VCAP_SERVICES);
let services;
try {
services = JSON.parse(process.env.VCAP_SERVICES);
} catch () {
return {};
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrapping the JSON parse in a try/catch might eat up/silence the error, and the user wouldn't know their VCAP_SERVICES env is not parsed. So I think it's fine to let it fail if for some reason JSON is malformatted.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, that's a fair point. I just remembered that the core has it's own logger now. So another thing to consider would be printing an error log for a malformatted VCAP variable so the user knows about it. My only concern here is the code crashing even though the user typically cannot control the contents of VCAP_SERVICES.

auth/utils/read-external-sources.ts Show resolved Hide resolved
auth/utils/read-external-sources.ts Show resolved Hide resolved
if (services[serviceName].length > 0) {
return services[serviceName][0].credentials || {};
} else {
return {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Speaking of the logger, I think it's a good idea to add debug or verbose logging statements before an empty object is returned saying something like "no data read from vcap services"

}
}
}
return {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more place to add a debug

*
* If no match against the service entry's "name" field is found, then find the
* service list with a key matching the serviceKey value. If found, return its
* credentials.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should mention that you are returning the first credentials object in the service list.

Copy link
Member

@dpopp07 dpopp07 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! 👍

Make sure you format the commit message properly to ensure a good message in the CHANGELOG

@SaltedCaramelCoffee SaltedCaramelCoffee merged commit cde4cd6 into master Nov 19, 2019
ibm-devx-automation pushed a commit that referenced this pull request Nov 19, 2019
# [2.0.0](v1.3.0...v2.0.0) (2019-11-19)

### Features

* changes to node-sdk-core to work with service factory feature ([#72](#72)) ([cde4cd6](cde4cd6))

### BREAKING CHANGES

* The `BaseService` will no longer look for configurations externally by default. A new factory method is provided to create an instance from external configuration.

* feat: changes to node-sdk-core to work with service factory feature

* `BaseService` constructor will no longer call `configureService`.

* updated test to reflect base service constructor does not call configureService

* added test for getting credentials from vcap

* removed `name` and `serviceVersion` because they are not referenced anymore

* added comment for vcap parsing function. removed vcap_services dependency

* added debug messages for when returning empty credential
@ibm-devx-automation
Copy link

🎉 This PR is included in version 2.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@padamstx padamstx deleted the feat-service-factory branch November 10, 2020 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants