Skip to content

Commit 99993b3

Browse files
authored
Merge pull request #57 from cloudgraphdev/fix/CG-893
fix: Handle add policy without a cg config file
2 parents 05b472c + 8ffb45d commit 99993b3

File tree

2 files changed

+39
-30
lines changed

2 files changed

+39
-30
lines changed

src/commands/policy/add.ts

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { PluginType } from '@cloudgraph/sdk'
22
import { isEmpty, uniqBy } from 'lodash'
3+
import { DEFAULT_CG_CONFIG } from '../../utils/constants'
34

45
import OperationBaseCommand from '../operation'
56

@@ -28,37 +29,35 @@ export default class AddPolicy extends OperationBaseCommand {
2829
} = installedPolicy
2930

3031
// 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+
]
6152
}
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)
6261
}
6362
} catch (error) {
6463
this.logger.debug(error)

src/utils/constants.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,13 @@ export const messages = {
2525
plural: 'providers',
2626
},
2727
}
28+
29+
export const DEFAULT_CG_CONFIG = {
30+
cloudGraph: {
31+
plugins: {},
32+
storageConfig: DEFAULT_CONFIG,
33+
versionLimit: 10,
34+
queryEngine: 'playground',
35+
port: '5555',
36+
},
37+
}

0 commit comments

Comments
 (0)