1 |
|
2 | 'use strict'
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | const request = require('request')
|
12 |
|
13 | const readCors = require('./cors/read.js')
|
14 | const readRoutes = require('./routes/read.js')
|
15 | const scope = require('./scope.js')
|
16 |
|
17 | module.exports = async function upsertAPIEnvironment(
|
18 | config /* : BlinkMRCServer */,
|
19 | api /* : API */,
|
20 | environment /* : string */,
|
21 | cwd /* : string */,
|
22 | accessToken /* : string | void */
|
23 | ) /* : Promise<void> */ {
|
24 | const cors = await readCors(cwd)
|
25 | const routes = await readRoutes(cwd)
|
26 | const serverCLIServiceConfig = scope.serverCLIServiceConfig(config)
|
27 | return new Promise((resolve, reject) => {
|
28 | if (!config.project) {
|
29 | return reject(
|
30 | new Error('Please run the "scope" command to set the project scope.')
|
31 | )
|
32 | }
|
33 | request.put(
|
34 | `${serverCLIServiceConfig.origin}/apis/${
|
35 | config.project
|
36 | }/environments/${environment}`,
|
37 | {
|
38 | auth: {
|
39 | bearer: accessToken
|
40 | },
|
41 | json: {
|
42 | routes,
|
43 | cors,
|
44 | vpcSecurityGroupIds: api.vpcSecurityGroupIds,
|
45 | vpcSubnetIds: api.vpcSubnetIds
|
46 | }
|
47 | },
|
48 | (err, response, body) => {
|
49 | if (err) {
|
50 | return reject(err)
|
51 | }
|
52 | if (response.statusCode !== 200) {
|
53 | return reject(
|
54 | new Error(
|
55 | body && body.message
|
56 | ? body.message
|
57 | : 'Unknown error, please try again and contact support if the problem persists'
|
58 | )
|
59 | )
|
60 | }
|
61 | return resolve(body)
|
62 | }
|
63 | )
|
64 | })
|
65 | }
|