Skip to content

Commit 25f7f96

Browse files
committed
feat(roles): create and upsert support
1 parent 57f2106 commit 25f7f96

File tree

4 files changed

+203
-25
lines changed

4 files changed

+203
-25
lines changed

api.js

Lines changed: 107 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ log.info(`command line args: ${args}`);
1414

1515
let mode = args[0];
1616
let result;
17-
let projectKey, featureFlagKey, environmentKeyQuery;
17+
let projectKey, featureFlagKey, environmentKeyQuery, customRoleKey;
1818

1919
switch (mode) {
2020
case 'getFeatureFlags':
@@ -31,15 +31,15 @@ log.info(`command line args: ${args}`);
3131
featureFlagKey = args[2];
3232
environmentKeyQuery = args[3];
3333
if (!projectKey || projectKey.trim() === '') {
34-
result = 'please supply a projectKey as second parameter'
34+
result = 'please supply a projectKey as second parameter';
3535
break;
3636
}
3737
if (!featureFlagKey || featureFlagKey.trim() === '') {
38-
result = 'please supply a featureFlagKey as third parameter'
38+
result = 'please supply a featureFlagKey as third parameter';
3939
break;
4040
}
4141
if (!environmentKeyQuery || environmentKeyQuery.trim() === '') {
42-
result = 'please supply a environmentKeyQuery as fourth parameter'
42+
result = 'please supply a environmentKeyQuery as fourth parameter';
4343
break;
4444
}
4545
result = await ldUtils.getFeatureFlag(projectKey, featureFlagKey, environmentKeyQuery);
@@ -50,15 +50,15 @@ log.info(`command line args: ${args}`);
5050
featureFlagKey = args[2];
5151
environmentKeyQuery = args[3];
5252
if (!projectKey || projectKey.trim() === '') {
53-
result = 'please supply a projectKey as second parameter'
53+
result = 'please supply a projectKey as second parameter';
5454
break;
5555
}
5656
if (!featureFlagKey || featureFlagKey.trim() === '') {
57-
result = 'please supply a featureFlagKey as third parameter'
57+
result = 'please supply a featureFlagKey as third parameter';
5858
break;
5959
}
6060
if (!environmentKeyQuery || environmentKeyQuery.trim() === '') {
61-
result = 'please supply a environmentKeyQuery as fourth parameter'
61+
result = 'please supply a environmentKeyQuery as fourth parameter';
6262
break;
6363
}
6464
result = await ldUtils.getFeatureFlagState(projectKey, featureFlagKey, environmentKeyQuery);
@@ -69,20 +69,20 @@ log.info(`command line args: ${args}`);
6969
featureFlagKey = args[2];
7070
environmentKeyQuery = args[3];
7171
if (!projectKey || projectKey.trim() === '') {
72-
result = 'please supply a projectKey as second parameter'
72+
result = 'please supply a projectKey as second parameter';
7373
break;
7474
}
7575
if (!featureFlagKey || featureFlagKey.trim() === '') {
76-
result = 'please supply a featureFlagKey as third parameter'
76+
result = 'please supply a featureFlagKey as third parameter';
7777
break;
7878
}
7979
if (!environmentKeyQuery || environmentKeyQuery.trim() === '') {
80-
result = 'please supply a environmentKeyQuery as fourth parameter'
80+
result = 'please supply a environmentKeyQuery as fourth parameter';
8181
break;
8282
}
8383
let enabled = args[4];
8484
if (enabled === undefined || !['true', 'false'].includes(enabled)) {
85-
result = 'please supply either \'true\' or \'false\' as fifth parameter';
85+
result = `please supply either 'true' or 'false' as fifth parameter`;
8686
break;
8787
}
8888
enabled = enabled === 'true';
@@ -93,8 +93,103 @@ log.info(`command line args: ${args}`);
9393
result = await ldUtils.getCustomRoles();
9494
break;
9595

96+
case 'getCustomRole':
97+
customRoleKey = args[1];
98+
if (!customRoleKey || customRoleKey.trim() === '') {
99+
result = 'please supply a customRoleKey as second parameter'
100+
break;
101+
}
102+
result = await ldUtils.getCustomRole(customRoleKey);
103+
break;
104+
105+
case 'createCustomRole':
106+
customRoleKey = args[1];
107+
let customRoleName = args[2];
108+
let customRolePolicyArray = JSON.parse(args[3]);
109+
let customRoleDescription = args[4];
110+
111+
if (!customRoleKey || customRoleKey.trim() === '') {
112+
result = 'please supply a customRoleKey as second parameter';
113+
break;
114+
}
115+
116+
if (!customRoleName || customRoleName.trim() === '') {
117+
result = 'please supply a customRoleName as third parameter';
118+
break;
119+
}
120+
121+
if (!customRolePolicyArray) {
122+
result = 'please supply a customRolePolicyArray as fourth parameter';
123+
break;
124+
}
125+
126+
result = await ldUtils.createCustomRole(
127+
customRoleKey,
128+
customRoleName,
129+
customRolePolicyArray,
130+
customRoleDescription
131+
);
132+
133+
break;
134+
135+
case 'updateCustomRole':
136+
customRoleKey = args[1];
137+
customRoleName = args[2];
138+
customRolePolicyArray = JSON.parse(args[3]);
139+
customRoleDescription = args[4];
140+
141+
if (!customRoleKey || customRoleKey.trim() === '') {
142+
result = 'please supply a customRoleKey as second parameter';
143+
break;
144+
}
145+
146+
if (!customRoleName || customRoleName.trim() === '') {
147+
result = 'please supply a customRoleName as third parameter';
148+
break;
149+
}
150+
151+
if (!customRolePolicyArray) {
152+
result = 'please supply a customRolePolicyArray as fourth parameter';
153+
break;
154+
}
155+
result = await ldUtils.updateCustomRole(
156+
customRoleKey,
157+
customRoleName,
158+
customRolePolicyArray,
159+
customRoleDescription
160+
);
161+
break;
162+
163+
case 'upsertCustomRole':
164+
customRoleKey = args[1];
165+
customRoleName = args[2];
166+
customRolePolicyArray = JSON.parse(args[3]);
167+
customRoleDescription = args[4];
168+
169+
if (!customRoleKey || customRoleKey.trim() === '') {
170+
result = 'please supply a customRoleKey as second parameter';
171+
break;
172+
}
173+
174+
if (!customRoleName || customRoleName.trim() === '') {
175+
result = 'please supply a customRoleName as third parameter';
176+
break;
177+
}
178+
179+
if (!customRolePolicyArray) {
180+
result = 'please supply a customRolePolicyArray as fourth parameter';
181+
break;
182+
}
183+
result = await ldUtils.upsertCustomRole(
184+
customRoleKey,
185+
customRoleName,
186+
customRolePolicyArray,
187+
customRoleDescription
188+
);
189+
break;
190+
96191
default:
97-
result = 'please supply a mode parameter: getFeatureFlags, getFeatureFlag';
192+
result = 'please supply a valid mode parameter';
98193

99194
}
100195

package-lock.json

Lines changed: 24 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"test": "npm run build && cross-env NODE_ENV=test nyc ./node_modules/.bin/mocha --timeout 10000 --require babel-core/register --require babel-polyfill './dist/test/**/*.spec.js'",
1212
"semantic-release": "semantic-release"
1313
},
14-
"author": "",
14+
"author": "Andrew Vaughan <andrewv@smartcreations.com.au> (https://halt.sh)",
1515
"license": "MIT",
1616
"repository": {
1717
"type": "git",
@@ -21,8 +21,6 @@
2121
"launchdarkly",
2222
"featureflags"
2323
],
24-
"author": "Andrew Vaughan <andrewv@smartcreations.com.au> (https://halt.sh)",
25-
"license": "MIT",
2624
"bugs": {
2725
"url": "https://github.com/wyvern8/launchdarkly-nodeutils/issues"
2826
},
@@ -33,6 +31,7 @@
3331
"babel-polyfill": "^6.26.0",
3432
"bunyan": "^1.8.12",
3533
"bunyan-format": "^0.2.1",
34+
"fast-json-patch": "^2.0.6",
3635
"js-yaml": "^3.10.0",
3736
"ldclient-node": "^3.3.2",
3837
"swagger-client": "^3.4.7",
@@ -74,7 +73,7 @@
7473
"dotenv": "^5.0.0",
7574
"eslint": "^4.15.0",
7675
"eslint-config-prettier": "2.9.0",
77-
"eslint-plugin-prettier": "2.6.0",
76+
"eslint-plugin-prettier": "^2.6.0",
7877
"firstline": "^1.2.1",
7978
"format-json": "^1.0.3",
8079
"mocha": "^5.0.0",

src/LaunchDarklyUtils.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { LaunchDarklyApiClient } from './LaunchDarklyApiClient';
22
import { LaunchDarklyLogger } from './LaunchDarklyLogger';
3+
import { default as jsonPatch } from 'fast-json-patch';
34
import { default as dotenv } from 'dotenv';
45
dotenv.config();
56

@@ -42,4 +43,72 @@ export class LaunchDarklyUtils {
4243
async getCustomRoles() {
4344
return this.apiClient.apis['Custom roles'].getCustomRoles();
4445
}
46+
47+
async getCustomRole(customRoleKey) {
48+
return this.apiClient.apis['Custom roles']
49+
.getCustomRole({
50+
customRoleKey: customRoleKey
51+
})
52+
.catch(e => {
53+
log.error(e);
54+
throw e;
55+
});
56+
}
57+
58+
async createCustomRole(customRoleKey, customRoleName, customRolePolicyArray, customRoleDescription) {
59+
let customRole = {
60+
name: customRoleName,
61+
key: customRoleKey,
62+
description: customRoleDescription,
63+
policy: customRolePolicyArray
64+
};
65+
return this.apiClient.apis['Custom roles'].postCustomRole({ customRoleBody: customRole });
66+
}
67+
68+
async updateCustomRole(customRoleKey, customRoleName, customRolePolicyArray, customRoleDescription) {
69+
let updatedCustomRole = {
70+
name: customRoleName,
71+
key: customRoleKey,
72+
description: customRoleDescription,
73+
policy: customRolePolicyArray
74+
};
75+
76+
return this.getCustomRole(customRoleKey)
77+
78+
.then(customRoleResponse => {
79+
let patchDelta = jsonPatch.compare(customRoleResponse.obj, updatedCustomRole);
80+
log.debug(`customRoleDiff for '${customRoleKey}' ${JSON.stringify(patchDelta)}`);
81+
return patchDelta;
82+
})
83+
.then(patchDelta => {
84+
return this.apiClient.apis['Custom roles'].patchCustomRole({
85+
customRoleKey: customRoleKey,
86+
patchDelta: patchDelta
87+
});
88+
});
89+
}
90+
91+
async upsertCustomRole(customRoleKey, customRoleName, customRolePolicyArray, customRoleDescription) {
92+
return this.getCustomRole(customRoleKey)
93+
94+
.then(() => {
95+
log.info(`role '${customRoleKey}' found, updating..`);
96+
return this.updateCustomRole(
97+
customRoleKey,
98+
customRoleName,
99+
customRolePolicyArray,
100+
customRoleDescription
101+
);
102+
})
103+
104+
.catch(() => {
105+
log.info(`role '${customRoleKey}' not found, creating..`);
106+
return this.createCustomRole(
107+
customRoleKey,
108+
customRoleName,
109+
customRolePolicyArray,
110+
customRoleDescription
111+
);
112+
});
113+
}
45114
}

0 commit comments

Comments
 (0)