diff --git a/lib/commands/deploy_api.js b/lib/commands/deploy_api.js index e9a9739b0..444b129e4 100644 --- a/lib/commands/deploy_api.js +++ b/lib/commands/deploy_api.js @@ -5,9 +5,6 @@ * - Deploys project's API Gateway REST API to the specified stage */ -// TODO: Create Integrations for Methods. No need to show/update/delete since all methods are deleted -// TODO: Create IntegrationResponse for each Integration. No need to show/update/delete since all methods are deleted - // Defaults var JawsError = require('../jaws-error'), @@ -31,12 +28,8 @@ Promise.promisifyAll(fs); module.exports = function(JAWS) { - // Instantiate JawsApiGatewayClient - var client = new JawsAPIClient({ - accessKeyId: JAWS._meta.credentials.aws_access_key_id, - secretAccessKey: JAWS._meta.credentials.aws_secret_access_key, - region: JAWS._meta.projectJson.awsRegions[0], - }); + + var client = null; /** @@ -47,13 +40,35 @@ module.exports = function(JAWS) { JAWS._dapiFindOrCreateApi = function(state) { return new Promise(function(resolve, reject) { + // Set Region + state.region = Object.keys(JAWS._meta.projectJson.regions)[0]; + state.restApiId = JAWS._meta.projectJson.regions[state.region].restApiId || null; + + // Validate Stage + if (!JAWS._meta.projectJson.regions[state.region].stages[state.stage]) { + reject(new JawsError( + 'This stage doesn\'t exist. Please add it to the jaws.json file at the root of your project', + JawsError.errorCodes.UNKNOWN)); + } + + // Instantiate JawsApiGatewayClient + client = new JawsAPIClient({ + accessKeyId: JAWS._meta.credentials.aws_access_key_id, + secretAccessKey: JAWS._meta.credentials.aws_secret_access_key, + region: state.region, + }); + + // Start Spinner + state.spinner = new Spinner('%s Creating your REST API for the state "' + state.stage + '"...'); + state.spinner.setSpinnerString('|/-\\'); + state.spinner.start(); + // Check Project's jaws.json for restApiId, otherwise create an api - if (JAWS._meta.projectJson.restApiId) { + if (state.restApiId) { // Show existing REST API - client.showRestApi(JAWS._meta.projectJson.restApiId).then(function(response) { + client.showRestApi(state.restApiId).then(function(response) { - state.restApiId = JAWS._meta.projectJson.restApiId; resolve(state); }).catch(function(error) { @@ -71,7 +86,7 @@ module.exports = function(JAWS) { }).then(function(response) { // Update Project's jaws.json - JAWS._meta.projectJson.restApiId = response.id; + JAWS._meta.projectJson.regions[state.region].restApiId = response.id; var newJson = JSON.stringify(JAWS._meta.projectJson, null, 2); fs.writeFileSync(path.join(JAWS._meta.projectRootPath, 'jaws.json'), newJson); @@ -310,9 +325,9 @@ module.exports = function(JAWS) { "httpMethod" : "POST", "authorizationType": "none", "uri" : "arn:aws:apigateway:" - + JAWS._meta.projectJson.awsRegions[0] + + state.region + ":lambda:path/2015-03-31/functions/arn:aws:lambda:" - + JAWS._meta.projectJson.awsRegions[0] + + state.region + ":814070455730:function:" + [state.stage, JAWS._meta.projectJson.name, endpoint.lambda.functionName].join('_-_').replace(/ /g,'') + "/invocations", @@ -444,6 +459,38 @@ module.exports = function(JAWS) { }); }; + + /** + * Create Deployment + * @param state + * @returns {*} + * @private + */ + JAWS._dapiCreateDeployment = function(state) { + return new Promise(function(resolve, reject) { + + + var deployment = { + "stageName" : state.stage, + "stageDescription" : state.stage, + "description" : "JAWS deployment" + }; + + client.createDeployment(state.restApiId, deployment) + .then(function(response){ + + // Add deployment to state + state.deployment = response; + resolve(state); + }) + .catch(function(error) { + reject(new JawsError( + error.message, + JawsError.errorCodes.UNKNOWN)); + }); + }); + }; + /** * Deploy API * @param stage @@ -454,11 +501,13 @@ module.exports = function(JAWS) { // Create state object for all functions return resolve({ - stage: stage, + stage: stage.toLowerCase().trim(), + region: null, restApiId: null, resourceParentId: null, endpoints: [], }); + }) .then(this._dapiFindOrCreateApi) .then(this._dapiDeleteAllResources) @@ -468,8 +517,13 @@ module.exports = function(JAWS) { .then(this._dapiCreateIntegrations) .then(this._dapiCreateMethodResponses) .then(this._dapiCreateIntegrationResponses) + .then(this._dapiCreateDeployment) .then(function(state) { - console.log(state); + + state.spinner.stop(true); + console.log('API successfully deployed: https://' + state.restApiId + '.execute-api.' + state.region + '.amazonaws.com/' + state.stage + '/'); + + return Promise.resolve(); }); }; }; diff --git a/package.json b/package.json index c3bee56eb..ca9f7c1e1 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,7 @@ "gulp": "^3.9.0", "inquirer": "^0.9.0", "insert-module-globals": "^6.5.2", - "jaws-api-gateway-client": "^0.8.0", + "jaws-api-gateway-client": "^0.9.0", "moment": "^2.10.6", "node-uuid": "^1.4.2", "node-zip": "^1.1.0",