diff --git a/deploy/lib/setIamPolicy.js b/deploy/lib/setIamPolicy.js index c584ed2..d2bf7cc 100644 --- a/deploy/lib/setIamPolicy.js +++ b/deploy/lib/setIamPolicy.js @@ -1,7 +1,7 @@ 'use strict'; -const _ = require('lodash'); const BbPromise = require('bluebird'); +const ServerlessError = require('serverless/lib/classes/Error').ServerlessError; module.exports = { setIamPolicy() { @@ -33,37 +33,40 @@ module.exports = { } this.serverless.cli.log('Setting IAM policies...'); - _.forEach(policies, (value, key) => { + const promises = []; + Object.entries(policies).forEach((entry) => { const func = functions.find((fn) => { - return fn.name === key; + return fn.name === entry[0]; }); if (func) { const params = { resource: func.name, requestBody: { policy: { - bindings: value, + bindings: entry[1], }, }, }; - this.provider.request( - 'cloudfunctions', - 'projects', - 'locations', - 'functions', - 'setIamPolicy', - params + promises.push( + this.provider.request( + 'cloudfunctions', + 'projects', + 'locations', + 'functions', + 'setIamPolicy', + params + ) ); } else { const errorMessage = [ - `Unable to set IAM bindings (${value}) for "${key}": function not found for`, + `Unable to set IAM bindings (${entry[1]}) for "${entry[0]}": function not found for`, ` project "${this.serverless.service.provider.project}" in region "${this.options.region}".`, ].join(''); - throw new Error(errorMessage); + throw new ServerlessError(errorMessage); } }); - return BbPromise.resolve(); + return BbPromise.all(promises); }, }; diff --git a/package.json b/package.json index 62ff5c9..65e5f74 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,8 @@ "chalk": "^3.0.0", "fs-extra": "^8.1.0", "googleapis": "^50.0.0", - "lodash": "^4.17.15" + "lodash": "^4.17.15", + "serverless": "^1.74.1" }, "devDependencies": { "@commitlint/cli": "^8.3.5", diff --git a/package/lib/compileFunctions.js b/package/lib/compileFunctions.js index d23d3ce..756f297 100644 --- a/package/lib/compileFunctions.js +++ b/package/lib/compileFunctions.js @@ -62,20 +62,17 @@ module.exports = { // Collect the configured IAM bindings at the function and provider level and merge the // members for each defined role. This transforms the array of IAM bindings into a mapping // in order to easily merge. - const iamBindings = _.reduce( - _.concat( - _.get(funcObject, 'iam.bindings') || [], + const iamBindings = (_.get(funcObject, 'iam.bindings') || []) + .concat( _.get(this, 'serverless.service.provider.iam.bindings') || [], allowUnauthenticated ? [{ role: 'roles/cloudfunctions.invoker', members: ['allUsers'] }] : [] - ), - (result, value) => { + ) + .reduce((result, value) => { result[value.role] = _.union(result[value.role] || [], value.members); return result; - }, - {} - ); + }, {}); if (!funcTemplate.properties.serviceAccountEmail) { delete funcTemplate.properties.serviceAccountEmail;