Skip to content

Commit

Permalink
review updates
Browse files Browse the repository at this point in the history
  • Loading branch information
edaniszewski committed Jul 2, 2020
1 parent a2b6736 commit b980fee
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
31 changes: 17 additions & 14 deletions deploy/lib/setIamPolicy.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const _ = require('lodash');
const BbPromise = require('bluebird');
const ServerlessError = require('serverless/lib/classes/Error').ServerlessError;

module.exports = {
setIamPolicy() {
Expand Down Expand Up @@ -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);
},
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 5 additions & 8 deletions package/lib/compileFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit b980fee

Please sign in to comment.