Skip to content

Commit 9386a7a

Browse files
committed
feat(role bulk upsert): supply a json file of roles to create/update
1 parent 6873a45 commit 9386a7a

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,12 @@ The command line modes and parameters map directly to the functions exposed for
7070
| createCustomRole | createCustomRole |
7171
| updateCustomRole | customRoleKey, customRoleName, customRolePolicyArray, customRoleDescription(optional) |
7272
| upsertCustomRole | customRoleKey, customRoleName, customRolePolicyArray, customRoleDescription(optional) |
73+
| bulkUpsertCustomRoles | roleBulkLoadFile |
74+
75+
Bulk upsert iterates over a json file containing an array of role json and either creates or updates each. Promises are resolved sequentially to avoid rate limiting.
76+
77+
```
78+
npm run api -- bulkUpsertCustomRoles ./exampleRoleBulkLoad.json
79+
```
7380

7481
For details on role policy object structures, please see: https://docs.launchdarkly.com/docs/custom-roles

api.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@ log.info(`command line args: ${args}`);
188188
);
189189
break;
190190

191+
case 'bulkUpsertCustomRoles':
192+
let roleBulkLoadFile = args[1];
193+
result = await ldUtils.bulkUpsertCustomRoles(roleBulkLoadFile);
194+
195+
break;
196+
191197
default:
192198
result = 'please supply a valid mode parameter';
193199

exampleRoleBulkLoad.json

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
[
2+
{
3+
"name": "Test role one",
4+
"key": "test-role-one",
5+
"description": "Just testing one",
6+
"policy": [
7+
{
8+
"resources": [
9+
"proj/*"
10+
],
11+
"actions": [
12+
"*"
13+
],
14+
"effect": "deny"
15+
},
16+
{
17+
"resources": [
18+
"proj/*"
19+
],
20+
"actions": [
21+
"read"
22+
],
23+
"effect": "allow"
24+
}
25+
]
26+
},
27+
{
28+
"name": "Test role two",
29+
"key": "test-role-two",
30+
"description": "Just testing two",
31+
"policy": [
32+
{
33+
"resources": [
34+
"proj/*"
35+
],
36+
"actions": [
37+
"*"
38+
],
39+
"effect": "deny"
40+
},
41+
{
42+
"resources": [
43+
"proj/*"
44+
],
45+
"actions": [
46+
"read"
47+
],
48+
"effect": "allow"
49+
}
50+
]
51+
},
52+
{
53+
"name": "Test role three",
54+
"key": "test-role-three",
55+
"description": "Just testing three",
56+
"policy": [
57+
{
58+
"resources": [
59+
"proj/*"
60+
],
61+
"actions": [
62+
"*"
63+
],
64+
"effect": "deny"
65+
},
66+
{
67+
"resources": [
68+
"proj/*"
69+
],
70+
"actions": [
71+
"read"
72+
],
73+
"effect": "allow"
74+
}
75+
]
76+
}
77+
]
78+

src/LaunchDarklyUtils.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { LaunchDarklyApiClient } from './LaunchDarklyApiClient';
22
import { LaunchDarklyLogger } from './LaunchDarklyLogger';
33
import { default as jsonPatch } from 'fast-json-patch';
44
import { default as dotenv } from 'dotenv';
5+
import { default as fs } from 'fs';
56
dotenv.config();
67

78
let log = LaunchDarklyLogger.logger();
@@ -111,4 +112,21 @@ export class LaunchDarklyUtils {
111112
);
112113
});
113114
}
115+
116+
async bulkUpsertCustomRoles(roleBulkLoadFile) {
117+
let filePath = `${process.cwd()}/${roleBulkLoadFile}`;
118+
let roles = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
119+
let that = this;
120+
121+
log.info(`bulk upserting roles from file: ${filePath}`);
122+
123+
return roles.reduce(function(acc, role) {
124+
return acc.then(function(results) {
125+
return that.upsertCustomRole(role.key, role.name, role.policy, role.description).then(function(data) {
126+
results.push(data);
127+
return results;
128+
});
129+
});
130+
}, Promise.resolve([]));
131+
}
114132
}

0 commit comments

Comments
 (0)