mirror of
https://github.com/serverless/serverless.git
synced 2026-01-25 15:07:39 +00:00
deploy api: Resource creation is working
This commit is contained in:
parent
6a2a762114
commit
160e2c80f5
@ -45,8 +45,17 @@ module.exports = function(JAWS) {
|
||||
// Check Project's jaws.json for restApiId, otherwise create an api
|
||||
if (JAWS._meta.projectJson.restApiId) {
|
||||
|
||||
state.restApiId = JAWS._meta.projectJson.restApiId;
|
||||
resolve(state);
|
||||
client.showRestApi(JAWS._meta.projectJson.restApiId).then(function(response) {
|
||||
|
||||
console.log(response);
|
||||
state.restApiId = JAWS._meta.projectJson.restApiId;
|
||||
resolve(state);
|
||||
|
||||
}).catch(function(error) {
|
||||
reject(new JawsError(
|
||||
error.message,
|
||||
JawsError.errorCodes.UNKNOWN));
|
||||
});
|
||||
|
||||
} else {
|
||||
|
||||
@ -78,7 +87,6 @@ module.exports = function(JAWS) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
|
||||
state.lambdas = [];
|
||||
state.lambdaPaths = [];
|
||||
|
||||
// Check each lambda has a 'path', otherwise it's not an API endpoint
|
||||
async.eachSeries(lambdas, function(lambda, cb){
|
||||
@ -93,12 +101,6 @@ module.exports = function(JAWS) {
|
||||
|
||||
// Push to state's lambdas
|
||||
state.lambdas.push(lambdaJson);
|
||||
|
||||
// Push paths to state.lambdaPaths
|
||||
var paths = lambdaJson.lambda.path.split('/');
|
||||
for (var i=0; i < paths.length; i++) {
|
||||
if (state.lambdaPaths.indexOf(paths[i]) === -1) state.lambdaPaths.push(paths[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return cb();
|
||||
@ -120,49 +122,87 @@ module.exports = function(JAWS) {
|
||||
|
||||
return client.listResources(state.restApiId).then(function(response) {
|
||||
|
||||
// Parse HAL response
|
||||
state.resources = response._embedded.item;
|
||||
if (!Array.isArray(state.resources)) state.resources = [state.resources];
|
||||
return new Promise(function(resolve, reject) {
|
||||
|
||||
// Create New/Missing Resources
|
||||
async.eachLimit(state.lambdaPaths, 5, function(lambdaPath, cb) {
|
||||
// Parse HAL response
|
||||
state.resources = response._embedded.item;
|
||||
if (!Array.isArray(state.resources)) state.resources = [state.resources];
|
||||
|
||||
// Skip if resource already exists
|
||||
// Find '/' resource id
|
||||
for(var i=0; i < state.resources.length; i++) {
|
||||
var resourcePath = state.resources[i].path.replace(/\//g, '');
|
||||
if (lambdaPath === resourcePath) return cb();
|
||||
if (state.resources[i].path === '/') state.resourceParentId = state.resources[i].id;
|
||||
}
|
||||
|
||||
console.log('creating: ', lambdaPath);
|
||||
// Loop through each Lambda to check if it's required resources have been created
|
||||
async.eachSeries(state.lambdas, function(lambda, lambdaCb) {
|
||||
|
||||
}, function(error){
|
||||
console.log(error);
|
||||
var lambdaResources = lambda.lambda.path.split('/');
|
||||
|
||||
// Loop through each required resource sequentially
|
||||
async.eachSeries(lambdaResources, function(lambdaResource, resourceCb) {
|
||||
|
||||
lambdaResource = lambdaResource.replace(/\//g, '');
|
||||
|
||||
// Check if resource already exists on API Gateway
|
||||
for(var i=0; i < state.resources.length; i++) {
|
||||
|
||||
// Skip if '/' root resource
|
||||
if (state.resources[i].path === '/') continue;
|
||||
|
||||
var resourcePath = state.resources[i].pathPart;
|
||||
if (lambdaResource === resourcePath) return resourceCb();
|
||||
}
|
||||
|
||||
// Find Resource Parent ID
|
||||
var index = lambdaResources.indexOf(lambdaResource);
|
||||
var parentId = null;
|
||||
|
||||
if (index === 0) {
|
||||
|
||||
// If index is 0, add resource parent id
|
||||
parentId = state.resourceParentId;
|
||||
} else {
|
||||
|
||||
// Otherwise, pull id from parent resource
|
||||
var parentResource = lambdaResources[index - 1];
|
||||
for (i=0; i < state.resources.length; i++) {
|
||||
var resourcePath = state.resources[i].path.replace(/\//g, '');
|
||||
if (parentResource === resourcePath) parentId = state.resources[i].id;
|
||||
}
|
||||
}
|
||||
|
||||
// Create Resource
|
||||
client.createResource(state.restApiId, parentId, lambdaResource).then(function(response) {
|
||||
|
||||
// Add resource to state.resources and callback
|
||||
state.resources.push(response);
|
||||
return resourceCb();
|
||||
|
||||
});
|
||||
|
||||
}, function(error) {
|
||||
|
||||
return lambdaCb();
|
||||
|
||||
});
|
||||
}, function(error){
|
||||
console.log('All resources created: ', state);
|
||||
|
||||
|
||||
|
||||
|
||||
// TODO: Destroy Inactive Resources
|
||||
|
||||
|
||||
|
||||
|
||||
resolve(state);
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Process Single Lambda
|
||||
* @param lambda
|
||||
* @returns {Promise}
|
||||
* @private
|
||||
*/
|
||||
JAWS._dapiProcessLambda = function(lambdaPath) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
|
||||
var json = require(path.join(JAWS._meta.projectRootPath, lambdaPath));
|
||||
|
||||
// Check if Lambda is REST API endpoint
|
||||
if (!json.lambda || !json.lambda.path) resolve(false);
|
||||
|
||||
// Find or create resources
|
||||
var resources = json.lambda.path.split('/');
|
||||
|
||||
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Deploy API
|
||||
@ -179,5 +219,9 @@ module.exports = function(JAWS) {
|
||||
.then(this._dapiFindOrCreateApi)
|
||||
.then(this._dapiFindAllLambdas)
|
||||
.then(this._dapiProcessResources);
|
||||
|
||||
|
||||
// TODO: Create methods, response templates, etc.
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
@ -49,7 +49,7 @@
|
||||
"gulp": "^3.9.0",
|
||||
"inquirer": "^0.9.0",
|
||||
"insert-module-globals": "^6.5.2",
|
||||
"jaws-api-gateway-client": "0.1.0",
|
||||
"jaws-api-gateway-client": "^0.3.0",
|
||||
"moment": "^2.10.6",
|
||||
"node-uuid": "^1.4.2",
|
||||
"node-zip": "^1.1.0",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user