File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import { PluginType } from '@cloudgraph/sdk'
2+ import { isEmpty , uniqBy } from 'lodash'
23
34import OperationBaseCommand from '../operation'
45
@@ -28,10 +29,32 @@ export default class AddPolicy extends OperationBaseCommand {
2829
2930 // Save policy to CG config file
3031 const config = this . getCGConfig ( )
31- if ( config && config [ provider ] ) {
32- config [ provider ] . policies = config [ provider ] . policies
33- ? [ ...new Set ( [ ...config [ provider ] . policies , key ] ) ]
34- : [ key ]
32+ if ( config ) {
33+ let configuredPolicies =
34+ config . cloudGraph . plugins [ PluginType . PolicyPack ] || [ ]
35+
36+ if ( isEmpty ( configuredPolicies ) ) {
37+ // Set new Policy Pack Plugin array
38+ configuredPolicies = [
39+ {
40+ name : key ,
41+ providers : [ provider ] ,
42+ } ,
43+ ]
44+ } else {
45+ // Add policy to Policy Pack Plugin array
46+ configuredPolicies = [
47+ ...configuredPolicies ,
48+ {
49+ name : key ,
50+ providers : [ provider ] ,
51+ } ,
52+ ]
53+ }
54+ config . cloudGraph . plugins [ PluginType . PolicyPack ] = uniqBy (
55+ configuredPolicies ,
56+ 'name'
57+ )
3558 this . saveCloudGraphConfigFile ( config )
3659 }
3760 }
Original file line number Diff line number Diff line change 1- import { PluginType } from '@cloudgraph/sdk'
1+ import { ConfiguredPlugin , PluginType } from '@cloudgraph/sdk'
2+ import isEmpty from 'lodash/isEmpty'
23
34import OperationBaseCommand from '../operation'
45
@@ -36,16 +37,20 @@ export default class RemovePolicy extends OperationBaseCommand {
3637 if ( manager && ! noSave ) {
3738 manager . removeFromLockFile ( key )
3839
39- const [ provider ] = key . split ( '-' )
4040 const config = this . getCGConfig ( )
41-
42- if ( config [ provider ] ) {
43- config [ provider ] . policies = [
44- ...config [ provider ] . policies . filter (
45- ( policy : string ) => policy !== key
46- ) ,
47- ]
48- this . saveCloudGraphConfigFile ( config )
41+ if ( config ) {
42+ const configuredPolicies =
43+ config . cloudGraph . plugins [ PluginType . PolicyPack ] || [ ]
44+
45+ if ( ! isEmpty ( configuredPolicies ) ) {
46+ // Remove policy from Policy Pack Plugin array
47+ config . cloudGraph . plugins [ PluginType . PolicyPack ] =
48+ configuredPolicies . filter (
49+ ( p : ConfiguredPlugin ) => p . name !== key
50+ )
51+
52+ this . saveCloudGraphConfigFile ( config )
53+ }
4954 }
5055 }
5156 }
You can’t perform that action at this time.
0 commit comments