1 | var txain = require('txain')
|
2 | var utils = require('./utils')
|
3 | var AWS = require('aws-sdk')
|
4 | var _ = require('underscore')
|
5 | var inquirer = require('inquirer')
|
6 | var util = require('util')
|
7 |
|
8 | module.exports = function(yargs) {
|
9 | yargs.command('create', 'Creates a new AWS API', function (yargs) {
|
10 | module.exports.create(utils.end)
|
11 | })
|
12 | .command('select', 'Selects an existing AWS API to be used for creating API resources', function (yargs) {
|
13 | module.exports.select(utils.end)
|
14 | })
|
15 | .command('show', 'Shows the information of an existing AWS API', function(yargs) {
|
16 | module.exports.show(utils.end)
|
17 | })
|
18 | .command('deploy', 'Synchronizes and deploys the API to the selected stage', function(yargs) {
|
19 | module.exports.deploy(utils.end)
|
20 | })
|
21 | .wrap(yargs.terminalWidth())
|
22 | .help('help')
|
23 | .argv
|
24 | }
|
25 |
|
26 | module.exports.show = function(callback) {
|
27 | var config = utils.readConfig()
|
28 | var api = config.api
|
29 | if (!api) {
|
30 | console.log('No API selected yet. Please use `backbeam-lambda api select` to select an existing API')
|
31 | process.exit(1)
|
32 | }
|
33 | console.log(api.name, '('+api.description+')', api.id)
|
34 | callback()
|
35 | }
|
36 |
|
37 | module.exports.deploy = function(callback) {
|
38 | var region = utils.requiresRegion()
|
39 | var api = utils.requiresAPI()
|
40 | var stage = utils.requiresStage()
|
41 | var apigateway = new AWS.APIGateway()
|
42 | console.log('Deploying API (%s) to region %s and stage %s', api.name, region, stage)
|
43 |
|
44 | txain(function(callback) {
|
45 | var params = {
|
46 | restApiId: api.id,
|
47 | stageName: stage,
|
48 | }
|
49 | apigateway.createDeployment(params, callback)
|
50 | })
|
51 | .then(function(response, callback) {
|
52 | var url = 'https://'+api.id+'.execute-api.eu-west-1.amazonaws.com/'+stage
|
53 | console.log('API deployed successfully')
|
54 | console.log('Invoke URL:', url)
|
55 | })
|
56 | .end(callback)
|
57 | }
|
58 |
|
59 | module.exports.create = function(callback) {
|
60 | var region = utils.requiresRegion()
|
61 | console.log('Creating API for region', region)
|
62 | var apigateway = new AWS.APIGateway()
|
63 |
|
64 | var questions = [
|
65 | {
|
66 | name: 'name',
|
67 | message: 'API name',
|
68 | },
|
69 | {
|
70 | name: 'description',
|
71 | message: 'API description',
|
72 | }
|
73 | ]
|
74 | inquirer.prompt(questions, function(answers) {
|
75 | txain(function(callback) {
|
76 | apigateway.createRestApi(answers, callback)
|
77 | })
|
78 | .then(function(api, callback) {
|
79 | var config = utils.readConfig()
|
80 | config.api = _.pick(api, 'id', 'name', 'description')
|
81 | utils.writeConfig(config)
|
82 | console.log('Updated API configuration successfully')
|
83 | })
|
84 | .end(callback)
|
85 | })
|
86 | }
|
87 |
|
88 | module.exports.select = function(callback) {
|
89 | var region = utils.requiresRegion()
|
90 | console.log('Querying APIs for region', region)
|
91 |
|
92 | var apigateway = new AWS.APIGateway()
|
93 |
|
94 | txain(function(callback) {
|
95 | apigateway.getRestApis({}, callback)
|
96 | })
|
97 | .then(function(data, callback) {
|
98 | if (data.items.length === 0) {
|
99 | var question = {
|
100 | name: 'create',
|
101 | type: 'confirm',
|
102 | message: 'No existing APIs found. Do you want to create one?',
|
103 | }
|
104 | inquirer.prompt([question], function(answers) {
|
105 | if (answers.create) {
|
106 | module.exports.create(callback)
|
107 | }
|
108 | })
|
109 | return
|
110 | }
|
111 |
|
112 | var choices = data.items.map(function(item) {
|
113 | return {
|
114 | name: item.name,
|
115 | value: item,
|
116 | }
|
117 | })
|
118 | choices.push(new inquirer.Separator())
|
119 | choices.push({ name: 'Or create a new API', value: '' })
|
120 |
|
121 | var question = {
|
122 | name: 'api',
|
123 | type: 'list',
|
124 | choices: choices,
|
125 | message: 'Choose an API',
|
126 | }
|
127 | inquirer.prompt([question], function(answers) {
|
128 | var api = answers.api
|
129 | if (!api) {
|
130 | return module.exports.create(callback)
|
131 | }
|
132 | var config = utils.readConfig()
|
133 | config.api = _.pick(api, 'id', 'name', 'description')
|
134 | utils.writeConfig(config)
|
135 | console.log('Updated API configuration successfully')
|
136 | callback()
|
137 | })
|
138 | })
|
139 | .end(callback)
|
140 | }
|