-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (66 loc) · 2.44 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'use strict';
var fs = require('fs');
const { v4: uuidv4 } = require('uuid');
class ServerlessPlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.provider = this.serverless.getProvider('aws');
this.commands = {
postmanenv: {
usage: 'Creates a postman environment file from http endpoints',
lifecycleEvents: ['create'],
options: {
},
},
};
this.hooks = {
'postmanenv:create': this.createPostmanEnv.bind(this)
};
}
createPostmanEnv() {
this.serverless.cli.log('Create Postman environment file');
const stackName = this.provider.naming.getStackName(this.options.stage);
// Get information about the endpoints
this.provider.request('CloudFormation', 'describeStacks', { StackName: stackName }, this.options.stage, this.options.region)
.then(response => {
const endpoint = response.Stacks[0].Outputs
.find(service => service.OutputKey === 'ServiceEndpoint')
.OutputValue;
// Get information about the api key ids
this.provider.request('CloudFormation', 'describeStackResources', { StackName: stackName }, this.options.stage, this.options.region)
.then(response => {
const apiKeys = response.StackResources
.filter(stackResource => stackResource.ResourceType == 'AWS::ApiGateway::ApiKey')
.map(apiKeyResource => apiKeyResource.PhysicalResourceId)
// Get name and value information of api key
this.provider.request('APIGateway', 'getApiKeys', { includeValues: true }, this.options.stage, this.options.region)
.then(response => {
const relevantApiKeys = response.items
.filter(apiKey => apiKeys.includes(apiKey.id))
.map(apiKey => {
return {
key: 'apiKey' + apiKey.name,
value: apiKey.value,
enabled: true
}
})
let values = relevantApiKeys
values.push({
key: 'apiurl',
value: endpoint,
enabled: true
})
var postmanenv = {
id: uuidv4(),
name: stackName,
values
}
fs.writeFileSync('./postman_environment.json', JSON.stringify(postmanenv, null, 2));
this.serverless.cli.log('Created postman_environment.json');
})
})
});
}
}
module.exports = ServerlessPlugin;