Skip to content

Commit

Permalink
Merge pull request #90 from sanukerinc/master
Browse files Browse the repository at this point in the history
Enhance alias logs function #89 #62
  • Loading branch information
HyperBrain authored Mar 7, 2018
2 parents dfdfa69 + 2d75caf commit 8a1bf52
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ class AwsAlias {
logCommand.options.alias = {
usage: 'Alias'
};
logCommand.options.version = {
usage: 'Logs a specific version of the function'
};
logCommand.commands = _.assign({}, logCommand.commands, {
api: {
usage: 'Output the logs of a deployed APIG stage (alias)',
Expand Down
27 changes: 18 additions & 9 deletions lib/logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,21 @@ module.exports = {
orderBy: 'LastEventTime',
};

let aliasGetAliasFunctionVersion;
// Check if --version is specified
if (this._options.version) {
aliasGetAliasFunctionVersion = BbPromise.resolve(this._options.version);
} else {
aliasGetAliasFunctionVersion = this.aliasGetAliasLatestFunctionVersionByFunctionName(this._alias, this._lambdaName);
}
// Get currently deployed function version for the alias to
// setup the stream filter correctly
return this.aliasGetAliasFunctionVersions(this._alias)
.then(versions => {
return _.map(
_.filter(versions, [ 'functionName', this._lambdaName ]),
version => version.functionVersion);
})
return aliasGetAliasFunctionVersion
.then(version => {
if (!version) {
return BbPromise.reject(new this.serverless.classes.Error('Function alias not found.'));
}

return this.provider
.request('CloudWatchLogs',
'describeLogStreams',
Expand All @@ -75,13 +81,16 @@ module.exports = {
throw new this.serverless.classes
.Error('No existing streams for the function alias');
}

return _.map(
const logStreamNames = _.map(
_.filter(reply.logStreams, stream => _.includes(stream.logStreamName, `[${version}]`)),
stream => stream.logStreamName);

if (_.isEmpty(logStreamNames)) {
return BbPromise.reject(new this.serverless.classes.Error('No existing streams for this function version. If you want to view logs of a specific function version, please use --version'));
}
return logStreamNames;
});
});

},

apiLogsGetLogStreams() {
Expand Down
9 changes: 9 additions & 0 deletions lib/stackInformation.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,13 @@ module.exports = {
});
},

aliasGetAliasLatestFunctionVersionByFunctionName(aliasName, functionName) {
return this._provider.request('Lambda',
'getAlias',
{ FunctionName: functionName, Name: aliasName },
this._options.stage,
this._options.region)
.then(result => _.get(result, 'FunctionVersion', null));
},

};
13 changes: 4 additions & 9 deletions test/logs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('logs', () => {
let sandbox;
let providerRequestStub;
let logStub;
let aliasGetAliasFunctionVersionsStub;
let aliasGetAliasLatestFunctionVersionByFunctionNameStub;
let aliasStacksDescribeResourceStub;

before(() => {
Expand All @@ -45,7 +45,7 @@ describe('logs', () => {
awsAlias = new AWSAlias(serverless, options);
providerRequestStub = sandbox.stub(awsAlias._provider, 'request');
logStub = sandbox.stub(serverless.cli, 'log');
aliasGetAliasFunctionVersionsStub = sandbox.stub(awsAlias, 'aliasGetAliasFunctionVersions');
aliasGetAliasLatestFunctionVersionByFunctionNameStub = sandbox.stub(awsAlias, 'aliasGetAliasLatestFunctionVersionByFunctionName');
aliasStacksDescribeResourceStub = sandbox.stub(awsAlias, 'aliasStacksDescribeResource');

logStub.returns();
Expand Down Expand Up @@ -206,12 +206,7 @@ describe('logs', () => {
],
};
providerRequestStub.resolves(streamReply);
aliasGetAliasFunctionVersionsStub.returns(BbPromise.resolve([
{
functionName: 'func1',
functionVersion: '20'
}
]));
aliasGetAliasLatestFunctionVersionByFunctionNameStub.returns(BbPromise.resolve('20'));
awsAlias._lambdaName = 'func1';

return expect(awsAlias.logsGetLogStreams()).to.be.fulfilled
Expand Down Expand Up @@ -239,7 +234,7 @@ describe('logs', () => {

it('should throw error if no log streams found', () => {
providerRequestStub.resolves();
aliasGetAliasFunctionVersionsStub.returns(BbPromise.resolve([]));
aliasGetAliasLatestFunctionVersionByFunctionNameStub.returns(BbPromise.resolve(null));

return expect(awsAlias.logsGetLogStreams()).to.be.rejectedWith("");
});
Expand Down

0 comments on commit 8a1bf52

Please sign in to comment.