-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathsetIamPolicy.js
More file actions
73 lines (63 loc) · 1.92 KB
/
Copy pathsetIamPolicy.js
File metadata and controls
73 lines (63 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'use strict';
const ServerlessError = require('serverless/lib/classes/Error').ServerlessError;
module.exports = {
setIamPolicy() {
return Promise.resolve()
.then(() => this.getFunctions())
.then(() => this.setPolicies());
},
getFunctions() {
const project = this.serverless.service.provider.project;
const region = this.options.region;
const params = {
parent: `projects/${project}/locations/${region}`,
};
return this.provider
.request('cloudfunctions', 'projects', 'locations', 'functions', 'list', params)
.then((response) => {
return response.functions;
});
},
setPolicies(functions) {
const policies = this.serverless.service.provider.functionIamBindings;
// If there are no IAM policies configured with any function, there is nothing to
// do here.
if (!policies || !Object.keys(policies).length) {
return Promise.resolve();
}
this.serverless.cli.log('Setting IAM policies...');
const promises = [];
Object.entries(policies).forEach((entry) => {
const func = functions.find((fn) => {
return fn.name === entry[0];
});
if (func) {
const params = {
resource: func.name,
requestBody: {
policy: {
bindings: entry[1],
},
},
};
promises.push(
this.provider.request(
'cloudfunctions',
'projects',
'locations',
'functions',
'setIamPolicy',
params
)
);
} else {
const errorMessage = [
`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 ServerlessError(errorMessage);
}
});
return Promise.all(promises);
},
};