|
1 | 1 | import { PluginType } from '@cloudgraph/sdk' |
2 | 2 | import { isEmpty, uniqBy } from 'lodash' |
| 3 | +import { DEFAULT_CG_CONFIG } from '../../utils/constants' |
3 | 4 |
|
4 | 5 | import OperationBaseCommand from '../operation' |
5 | 6 |
|
@@ -28,37 +29,35 @@ export default class AddPolicy extends OperationBaseCommand { |
28 | 29 | } = installedPolicy |
29 | 30 |
|
30 | 31 | // Save policy to CG config file |
31 | | - const config = this.getCGConfig() |
32 | | - if (config) { |
33 | | - let configuredPolicies = |
34 | | - config.cloudGraph.plugins?.[PluginType.PolicyPack] || [] |
35 | | - if (isEmpty(configuredPolicies)) { |
36 | | - // Set new Policy Pack Plugin array |
37 | | - configuredPolicies = [ |
38 | | - { |
39 | | - name: key, |
40 | | - providers: [provider], |
41 | | - }, |
42 | | - ] |
43 | | - } else { |
44 | | - // Add policy to Policy Pack Plugin array |
45 | | - configuredPolicies = [ |
46 | | - ...configuredPolicies, |
47 | | - { |
48 | | - name: key, |
49 | | - providers: [provider], |
50 | | - }, |
51 | | - ] |
52 | | - } |
53 | | - if (!config.cloudGraph.plugin) { |
54 | | - config.cloudGraph.plugins = {} |
55 | | - } |
56 | | - config.cloudGraph.plugins[PluginType.PolicyPack] = uniqBy( |
57 | | - configuredPolicies, |
58 | | - 'name' |
59 | | - ) |
60 | | - this.saveCloudGraphConfigFile(config) |
| 32 | + const config = this.getCGConfig() || DEFAULT_CG_CONFIG |
| 33 | + let configuredPolicies = |
| 34 | + config.cloudGraph.plugins?.[PluginType.PolicyPack] || [] |
| 35 | + if (isEmpty(configuredPolicies)) { |
| 36 | + // Set new Policy Pack Plugin array |
| 37 | + configuredPolicies = [ |
| 38 | + { |
| 39 | + name: key, |
| 40 | + providers: [provider], |
| 41 | + }, |
| 42 | + ] |
| 43 | + } else { |
| 44 | + // Add policy to Policy Pack Plugin array |
| 45 | + configuredPolicies = [ |
| 46 | + ...configuredPolicies, |
| 47 | + { |
| 48 | + name: key, |
| 49 | + providers: [provider], |
| 50 | + }, |
| 51 | + ] |
61 | 52 | } |
| 53 | + if (!config.cloudGraph.plugin) { |
| 54 | + config.cloudGraph.plugins = {} |
| 55 | + } |
| 56 | + config.cloudGraph.plugins[PluginType.PolicyPack] = uniqBy( |
| 57 | + configuredPolicies, |
| 58 | + 'name' |
| 59 | + ) |
| 60 | + this.saveCloudGraphConfigFile(config) |
62 | 61 | } |
63 | 62 | } catch (error) { |
64 | 63 | this.logger.debug(error) |
|
0 commit comments