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

Updated aws provider to invoke .promise on methods that support it. Otherwise falls back to .send with a callback #5161

Merged
merged 2 commits into from
Jan 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/plugins/aws/provider/awsProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,10 @@ class AwsProvider {
// TODO: Add listeners, put Debug statments here...
// req.on('send', function (r) {console.log(r)});

return BbPromise.fromCallback(cb => {
const promise = req.promise ? req.promise() : BbPromise.fromCallback(cb => {
req.send(cb);
})
.catch(err => {
});
return promise.catch(err => {
let message = err.message;
if (err.message === 'Missing credentials in config') {
const errorMessage = [
Expand Down
39 changes: 36 additions & 3 deletions lib/plugins/aws/provider/awsProvider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe('AwsProvider', () => {
'${opt:stage, \'prod\'}',
];
stages.forEach(stage => {
it(`should not throw an error before variable population
it(`should not throw an error before variable population
even if http event is present and stage is ${stage}`, () => {
const config = {
stage,
Expand Down Expand Up @@ -286,6 +286,39 @@ describe('AwsProvider', () => {
});
});

it('should call correct aws method with a promise', () => {
// mocking API Gateway for testing
class FakeAPIGateway {
constructor(credentials) {
this.credentials = credentials;
}

getRestApis() {
return {
promise: () => BbPromise.resolve({ called: true }),
};
}
}
awsProvider.sdk = {
APIGateway: FakeAPIGateway,
};
awsProvider.serverless.service.environment = {
vars: {},
stages: {
dev: {
vars: {
profile: 'default',
},
regions: {},
},
},
};

return awsProvider.request('APIGateway', 'getRestApis', {}).then(data => {
expect(data.called).to.equal(true);
});
});


it('should request to the specified region if region in options set', () => {
// mocking S3 for testing
Expand Down Expand Up @@ -316,7 +349,7 @@ describe('AwsProvider', () => {
},
},
};
expect(awsProvider.getCredentials()).to.deep.eql({ region: options.region });
expect(awsProvider.getCredentials().region).to.eql(options.region);

return awsProvider
.request('CloudFormation',
Expand All @@ -326,7 +359,7 @@ describe('AwsProvider', () => {
.then(data => {
expect(data).to.eql({ region: 'ap-northeast-1' });
// Requesting different region should not affect region in credentials
expect(awsProvider.getCredentials()).to.deep.eql({ region: options.region });
expect(awsProvider.getCredentials().region).to.eql(options.region);
});
});
it('should retry if error code is 429', (done) => {
Expand Down