Skip to content

Commit 8a1bf52

Browse files
authored
Merge pull request #90 from sanukerinc/master
Enhance alias logs function #89 #62
2 parents dfdfa69 + 2d75caf commit 8a1bf52

File tree

4 files changed

+34
-18
lines changed

4 files changed

+34
-18
lines changed

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ class AwsAlias {
177177
logCommand.options.alias = {
178178
usage: 'Alias'
179179
};
180+
logCommand.options.version = {
181+
usage: 'Logs a specific version of the function'
182+
};
180183
logCommand.commands = _.assign({}, logCommand.commands, {
181184
api: {
182185
usage: 'Output the logs of a deployed APIG stage (alias)',

lib/logs.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,21 @@ module.exports = {
5555
orderBy: 'LastEventTime',
5656
};
5757

58+
let aliasGetAliasFunctionVersion;
59+
// Check if --version is specified
60+
if (this._options.version) {
61+
aliasGetAliasFunctionVersion = BbPromise.resolve(this._options.version);
62+
} else {
63+
aliasGetAliasFunctionVersion = this.aliasGetAliasLatestFunctionVersionByFunctionName(this._alias, this._lambdaName);
64+
}
5865
// Get currently deployed function version for the alias to
5966
// setup the stream filter correctly
60-
return this.aliasGetAliasFunctionVersions(this._alias)
61-
.then(versions => {
62-
return _.map(
63-
_.filter(versions, [ 'functionName', this._lambdaName ]),
64-
version => version.functionVersion);
65-
})
67+
return aliasGetAliasFunctionVersion
6668
.then(version => {
69+
if (!version) {
70+
return BbPromise.reject(new this.serverless.classes.Error('Function alias not found.'));
71+
}
72+
6773
return this.provider
6874
.request('CloudWatchLogs',
6975
'describeLogStreams',
@@ -75,13 +81,16 @@ module.exports = {
7581
throw new this.serverless.classes
7682
.Error('No existing streams for the function alias');
7783
}
78-
79-
return _.map(
84+
const logStreamNames = _.map(
8085
_.filter(reply.logStreams, stream => _.includes(stream.logStreamName, `[${version}]`)),
8186
stream => stream.logStreamName);
87+
88+
if (_.isEmpty(logStreamNames)) {
89+
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'));
90+
}
91+
return logStreamNames;
8292
});
8393
});
84-
8594
},
8695

8796
apiLogsGetLogStreams() {

lib/stackInformation.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,13 @@ module.exports = {
181181
});
182182
},
183183

184+
aliasGetAliasLatestFunctionVersionByFunctionName(aliasName, functionName) {
185+
return this._provider.request('Lambda',
186+
'getAlias',
187+
{ FunctionName: functionName, Name: aliasName },
188+
this._options.stage,
189+
this._options.region)
190+
.then(result => _.get(result, 'FunctionVersion', null));
191+
},
192+
184193
};

test/logs.test.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('logs', () => {
2323
let sandbox;
2424
let providerRequestStub;
2525
let logStub;
26-
let aliasGetAliasFunctionVersionsStub;
26+
let aliasGetAliasLatestFunctionVersionByFunctionNameStub;
2727
let aliasStacksDescribeResourceStub;
2828

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

5151
logStub.returns();
@@ -206,12 +206,7 @@ describe('logs', () => {
206206
],
207207
};
208208
providerRequestStub.resolves(streamReply);
209-
aliasGetAliasFunctionVersionsStub.returns(BbPromise.resolve([
210-
{
211-
functionName: 'func1',
212-
functionVersion: '20'
213-
}
214-
]));
209+
aliasGetAliasLatestFunctionVersionByFunctionNameStub.returns(BbPromise.resolve('20'));
215210
awsAlias._lambdaName = 'func1';
216211

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

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

244239
return expect(awsAlias.logsGetLogStreams()).to.be.rejectedWith("");
245240
});

0 commit comments

Comments
 (0)