| 
 | 1 | +import { LaunchDarklyUtilsFlags } from './LaunchDarklyUtilsFlags';  | 
 | 2 | +import { LaunchDarklyUtilsRoles } from './LaunchDarklyUtilsRoles';  | 
1 | 3 | import { LaunchDarklyApiClient } from './LaunchDarklyApiClient';  | 
2 | 4 | import { LaunchDarklyLogger } from './LaunchDarklyLogger';  | 
3 |  | -import { default as jsonPatch } from 'fast-json-patch';  | 
4 | 5 | import { default as dotenv } from 'dotenv';  | 
5 |  | -import { default as fs } from 'fs';  | 
6 | 6 | dotenv.config();  | 
7 | 7 | 
 
  | 
8 |  | -let log = LaunchDarklyLogger.logger();  | 
9 |  | - | 
10 | 8 | export class LaunchDarklyUtils {  | 
11 | 9 |     async create(API_TOKEN, customLogger) {  | 
12 |  | -        if (customLogger) log = customLogger;  | 
13 |  | -        this.apiClient = await LaunchDarklyApiClient.create(API_TOKEN, log);  | 
14 |  | -        log.debug(this.apiClient.apis);  | 
15 |  | -        return this;  | 
16 |  | -    }  | 
 | 10 | +        // setup logger  | 
 | 11 | +        this.log = customLogger ? customLogger : LaunchDarklyLogger.logger();  | 
 | 12 | +        this.log.debug('logger attached..');  | 
17 | 13 | 
 
  | 
18 |  | -    async getFeatureFlags(projectKey) {  | 
19 |  | -        return this.apiClient.apis['Feature flags'].getFeatureFlags({ projectKey: projectKey });  | 
20 |  | -    }  | 
 | 14 | +        // create LaunchDarkly apiClient  | 
 | 15 | +        this.apiClient = await LaunchDarklyApiClient.create(API_TOKEN, this.log);  | 
 | 16 | +        this.log.debug('api client instantiated..');  | 
21 | 17 | 
 
  | 
22 |  | -    async getFeatureFlag(projectKey, featureFlagKey, environmentKeyQuery) {  | 
23 |  | -        return this.apiClient.apis['Feature flags'].getFeatureFlag({  | 
24 |  | -            projectKey: projectKey,  | 
25 |  | -            featureFlagKey: featureFlagKey,  | 
26 |  | -            environmentKeyQuery: environmentKeyQuery  | 
27 |  | -        });  | 
28 |  | -    }  | 
 | 18 | +        // attach flag utils  | 
 | 19 | +        this.flags = new LaunchDarklyUtilsFlags(this.apiClient, this.log);  | 
 | 20 | +        this.log.debug(`flag functions: ${this.flags}`);  | 
29 | 21 | 
 
  | 
30 |  | -    async getFeatureFlagState(projectKey, featureFlagKey, environmentKeyQuery) {  | 
31 |  | -        return this.getFeatureFlag(projectKey, featureFlagKey, environmentKeyQuery).then(result => {  | 
32 |  | -            return result.obj.environments[environmentKeyQuery].on;  | 
33 |  | -        });  | 
34 |  | -    }  | 
 | 22 | +        // attach role utils  | 
 | 23 | +        this.roles = new LaunchDarklyUtilsRoles(this.apiClient, this.log);  | 
 | 24 | +        this.log.debug(`role functions: ${this.roles}`);  | 
35 | 25 | 
 
  | 
36 |  | -    async toggleFeatureFlag(projectKey, featureFlagKey, environmentKeyQuery, value) {  | 
37 |  | -        return this.apiClient.apis['Feature flags'].patchFeatureFlag({  | 
38 |  | -            projectKey: projectKey,  | 
39 |  | -            featureFlagKey: featureFlagKey,  | 
40 |  | -            patchDelta: [{ op: 'replace', path: `/environments/${environmentKeyQuery}/on`, value: value }]  | 
41 |  | -        });  | 
42 |  | -    }  | 
43 |  | - | 
44 |  | -    async getCustomRoles() {  | 
45 |  | -        return this.apiClient.apis['Custom roles'].getCustomRoles();  | 
46 |  | -    }  | 
47 |  | - | 
48 |  | -    async getCustomRole(customRoleKey) {  | 
49 |  | -        return this.apiClient.apis['Custom roles']  | 
50 |  | -            .getCustomRole({  | 
51 |  | -                customRoleKey: customRoleKey  | 
52 |  | -            })  | 
53 |  | -            .catch(e => {  | 
54 |  | -                log.error(e);  | 
55 |  | -                throw e;  | 
56 |  | -            });  | 
57 |  | -    }  | 
58 |  | - | 
59 |  | -    async createCustomRole(customRoleKey, customRoleName, customRolePolicyArray, customRoleDescription) {  | 
60 |  | -        let customRole = {  | 
61 |  | -            name: customRoleName,  | 
62 |  | -            key: customRoleKey,  | 
63 |  | -            description: customRoleDescription,  | 
64 |  | -            policy: customRolePolicyArray  | 
65 |  | -        };  | 
66 |  | -        return this.apiClient.apis['Custom roles'].postCustomRole({ customRoleBody: customRole });  | 
67 |  | -    }  | 
68 |  | - | 
69 |  | -    async updateCustomRole(customRoleKey, customRoleName, customRolePolicyArray, customRoleDescription) {  | 
70 |  | -        let updatedCustomRole = {  | 
71 |  | -            name: customRoleName,  | 
72 |  | -            key: customRoleKey,  | 
73 |  | -            description: customRoleDescription,  | 
74 |  | -            policy: customRolePolicyArray  | 
75 |  | -        };  | 
76 |  | - | 
77 |  | -        return this.getCustomRole(customRoleKey)  | 
78 |  | - | 
79 |  | -            .then(customRoleResponse => {  | 
80 |  | -                let patchDelta = jsonPatch.compare(customRoleResponse.obj, updatedCustomRole);  | 
81 |  | -                log.debug(`customRoleDiff for '${customRoleKey}' ${JSON.stringify(patchDelta)}`);  | 
82 |  | -                return patchDelta;  | 
83 |  | -            })  | 
84 |  | -            .then(patchDelta => {  | 
85 |  | -                return this.apiClient.apis['Custom roles'].patchCustomRole({  | 
86 |  | -                    customRoleKey: customRoleKey,  | 
87 |  | -                    patchDelta: patchDelta  | 
88 |  | -                });  | 
89 |  | -            });  | 
90 |  | -    }  | 
91 |  | - | 
92 |  | -    async upsertCustomRole(customRoleKey, customRoleName, customRolePolicyArray, customRoleDescription) {  | 
93 |  | -        return this.getCustomRole(customRoleKey)  | 
94 |  | - | 
95 |  | -            .then(() => {  | 
96 |  | -                log.info(`role '${customRoleKey}' found, updating..`);  | 
97 |  | -                return this.updateCustomRole(  | 
98 |  | -                    customRoleKey,  | 
99 |  | -                    customRoleName,  | 
100 |  | -                    customRolePolicyArray,  | 
101 |  | -                    customRoleDescription  | 
102 |  | -                );  | 
103 |  | -            })  | 
104 |  | - | 
105 |  | -            .catch(() => {  | 
106 |  | -                log.info(`role '${customRoleKey}' not found, creating..`);  | 
107 |  | -                return this.createCustomRole(  | 
108 |  | -                    customRoleKey,  | 
109 |  | -                    customRoleName,  | 
110 |  | -                    customRolePolicyArray,  | 
111 |  | -                    customRoleDescription  | 
112 |  | -                );  | 
113 |  | -            });  | 
114 |  | -    }  | 
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([]));  | 
 | 26 | +        return this;  | 
131 | 27 |     }  | 
132 | 28 | }  | 
0 commit comments