@@ -2,9 +2,9 @@ package sysdig
22
33import (
44 "context"
5+ "errors"
56 "net/http"
67 "strconv"
7- "strings"
88 "time"
99
1010 v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2"
@@ -27,51 +27,21 @@ func resourceSysdigSecureManagedPolicy() *schema.Resource {
2727 Read : schema .DefaultTimeout (timeout ),
2828 },
2929
30- Schema : map [string ]* schema.Schema {
31- "name" : {
32- Type : schema .TypeString ,
33- Required : true ,
34- },
30+ Schema : createPolicySchema (map [string ]* schema.Schema {
3531 "type" : {
3632 Type : schema .TypeString ,
3733 Optional : true ,
3834 Default : "falco" ,
3935 ValidateDiagFunc : validateDiagFunc (validatePolicyType ),
4036 },
41- "enabled" : {
42- Type : schema .TypeBool ,
43- Optional : true ,
44- Default : true ,
45- },
4637 "disabled_rules" : {
4738 Type : schema .TypeSet ,
4839 Optional : true ,
4940 Elem : & schema.Schema {
5041 Type : schema .TypeString ,
5142 },
5243 },
53- "scope" : {
54- Type : schema .TypeString ,
55- Optional : true ,
56- Default : "" ,
57- },
58- "version" : {
59- Type : schema .TypeInt ,
60- Computed : true ,
61- },
62- "notification_channels" : {
63- Type : schema .TypeSet ,
64- Optional : true ,
65- Elem : & schema.Schema {
66- Type : schema .TypeInt ,
67- },
68- },
69- "runbook" : {
70- Type : schema .TypeString ,
71- Optional : true ,
72- },
73- "actions" : policyActionBlockSchema ,
74- },
44+ }),
7545 }
7646}
7747
@@ -81,78 +51,34 @@ func resourceSysdigManagedPolicyCreate(ctx context.Context, d *schema.ResourceDa
8151 return diag .FromErr (err )
8252 }
8353
84- policies , _ , err := client .GetPolicies (ctx )
85- if err != nil {
86- return diag .FromErr (err )
87- }
88-
8954 policyName := d .Get ("name" ).(string )
9055 policyType := d .Get ("type" ).(string )
91- var policy v2.Policy
92- for _ , existingPolicy := range policies {
93- if existingPolicy .Name == policyName && existingPolicy .Type == policyType {
94- if ! existingPolicy .IsDefault {
95- return diag .Errorf ("policy is not a managed policy - use `resource_sysdig_secure_policy`" )
96- }
97- policy = existingPolicy
98- }
99- }
10056
101- if policy .ID == 0 {
102- return diag .Errorf ("unable to find managed policy" )
57+ policy , err := getManagedPolicy (ctx , client , policyName , policyType )
58+ if err != nil {
59+ return diag .FromErr (err )
10360 }
10461
105- updateManagedPolicyFromResourceData (& policy , d )
62+ updateManagedPolicyFromResourceData (policy , d )
10663
107- policy , err = client .UpdatePolicy (ctx , policy )
64+ updatedPolicy , err : = client .UpdatePolicy (ctx , * policy )
10865 if err != nil {
10966 return diag .FromErr (err )
11067 }
11168
112- managedPolicyToResourceData (& policy , d )
69+ managedPolicyToResourceData (& updatedPolicy , d )
11370
11471 return nil
11572}
11673
11774func managedPolicyToResourceData (policy * v2.Policy , d * schema.ResourceData ) {
118- if policy .ID != 0 {
119- d .SetId (strconv .Itoa (policy .ID ))
120- }
75+ commonPolicyToResourceData (policy , d )
12176
122- _ = d .Set ("name" , policy .Name )
12377 if policy .Type != "" {
12478 _ = d .Set ("type" , policy .Type )
12579 } else {
12680 _ = d .Set ("type" , "falco" )
12781 }
128- _ = d .Set ("enabled" , policy .Enabled )
129- _ = d .Set ("scope" , policy .Scope )
130- _ = d .Set ("version" , policy .Version )
131- _ = d .Set ("notification_channels" , policy .NotificationChannelIds )
132- _ = d .Set ("runbook" , policy .Runbook )
133-
134- actions := []map [string ]interface {}{{}}
135- for _ , action := range policy .Actions {
136- if action .Type != "POLICY_ACTION_CAPTURE" {
137- action := strings .Replace (action .Type , "POLICY_ACTION_" , "" , 1 )
138- actions [0 ]["container" ] = strings .ToLower (action )
139- //d.Set("actions.0.container", strings.ToLower(action))
140- } else {
141- actions [0 ]["capture" ] = []map [string ]interface {}{{
142- "seconds_after_event" : action .AfterEventNs / 1000000000 ,
143- "seconds_before_event" : action .BeforeEventNs / 1000000000 ,
144- "name" : action .Name ,
145- }}
146- }
147- }
148-
149- currentContainerAction := d .Get ("actions.0.container" ).(string )
150- currentCaptureAction := d .Get ("actions.0.capture" ).([]interface {})
151- // If the policy retrieved from service has no actions and the current state is default values,
152- // then do not set the "actions" key as it may cause terraform to think there has been a state change
153- if len (policy .Actions ) > 0 || currentContainerAction != "" || len (currentCaptureAction ) > 0 {
154- _ = d .Set ("actions" , actions )
155- }
15682
15783 disabledRules := []string {}
15884 for _ , rule := range policy .Rules {
@@ -164,11 +90,7 @@ func managedPolicyToResourceData(policy *v2.Policy, d *schema.ResourceData) {
16490}
16591
16692func updateManagedPolicyFromResourceData (policy * v2.Policy , d * schema.ResourceData ) {
167- policy .Enabled = d .Get ("enabled" ).(bool )
168- policy .Runbook = d .Get ("runbook" ).(string )
169- policy .Scope = d .Get ("scope" ).(string )
170-
171- addActionsToPolicy (d , policy )
93+ commonPolicyFromResourceData (policy , d )
17294
17395 disabledRules := d .Get ("disabled_rules" ).(* schema.Set )
17496 for _ , rule := range policy .Rules {
@@ -178,12 +100,6 @@ func updateManagedPolicyFromResourceData(policy *v2.Policy, d *schema.ResourceDa
178100 rule .Enabled = true
179101 }
180102 }
181-
182- policy .NotificationChannelIds = []int {}
183- notificationChannelIdSet := d .Get ("notification_channels" ).(* schema.Set )
184- for _ , id := range notificationChannelIdSet .List () {
185- policy .NotificationChannelIds = append (policy .NotificationChannelIds , id .(int ))
186- }
187103}
188104
189105func resourceSysdigManagedPolicyRead (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
@@ -267,3 +183,26 @@ func resourceSysdigManagedPolicyUpdate(ctx context.Context, d *schema.ResourceDa
267183 }
268184 return nil
269185}
186+
187+ func getManagedPolicy (ctx context.Context , client v2.PolicyInterface , policyName string , policyType string ) (* v2.Policy , error ) {
188+ policies , _ , err := client .GetPolicies (ctx )
189+ if err != nil {
190+ return nil , err
191+ }
192+
193+ var policy v2.Policy
194+ for _ , existingPolicy := range policies {
195+ if existingPolicy .Name == policyName && existingPolicy .Type == policyType {
196+ if ! existingPolicy .IsDefault {
197+ return nil , errors .New ("policy is not a managed policy" )
198+ }
199+ policy = existingPolicy
200+ }
201+ }
202+
203+ if policy .ID != 0 {
204+ return & policy , nil
205+ }
206+
207+ return nil , errors .New ("unable to find managed policy" )
208+ }
0 commit comments