11package aquasec
22
33import (
4+ "context"
45 "fmt"
56 "log"
67 "strings"
78 "time"
89
910 "github.com/aquasecurity/terraform-provider-aquasec/client"
11+ "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1012 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1113 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1214)
1315
1416func resourceEnforcerGroup () * schema.Resource {
1517 return & schema.Resource {
16- Create : resourceEnforcerGroupCreate ,
17- Read : resourceEnforcerGroupRead ,
18- Update : resourceEnforcerGroupUpdate ,
19- Delete : resourceEnforcerGroupDelete ,
18+ CreateContext : resourceEnforcerGroupCreate ,
19+ ReadContext : resourceEnforcerGroupRead ,
20+ UpdateContext : resourceEnforcerGroupUpdate ,
21+ DeleteContext : resourceEnforcerGroupDelete ,
2022 Importer : & schema.ResourceImporter {
2123 StateContext : schema .ImportStatePassthroughContext ,
2224 },
@@ -204,7 +206,7 @@ func resourceEnforcerGroup() *schema.Resource {
204206 Description : "Set `True` to enable these Host Runtime Policy controls: `OS Users and Groups Allowed` and `OS Users and Groups Blocked`" ,
205207 Optional : true ,
206208 },
207- "host_forensics " : {
209+ "host_forensics_collection " : {
208210 Type : schema .TypeBool ,
209211 Description : "Select Enabled to send activity logs in your host to the Aqua Server for forensics purposes." ,
210212 Optional : true ,
@@ -372,6 +374,37 @@ func resourceEnforcerGroup() *schema.Resource {
372374 Optional : true ,
373375 ValidateFunc : validation .StringInSlice ([]string {"docker" , "crio" , "containerd" , "garden" }, false ),
374376 },
377+ "schedule_scan_settings" : {
378+ Type : schema .TypeList ,
379+ Description : "Scheduling scan time for which you are creating the Enforcer group." ,
380+ Optional : true ,
381+ Elem : & schema.Resource {
382+ Schema : map [string ]* schema.Schema {
383+ "disabled" : {
384+ Type : schema .TypeBool ,
385+ Optional : true ,
386+ },
387+ "is_custom" : {
388+ Type : schema .TypeBool ,
389+ Optional : true ,
390+ },
391+ "days" : {
392+ Type : schema .TypeList ,
393+ Optional : true ,
394+ Elem : & schema.Schema {
395+ Type : schema .TypeInt ,
396+ },
397+ },
398+ "time" : {
399+ Type : schema .TypeList ,
400+ Optional : true ,
401+ Elem : & schema.Schema {
402+ Type : schema .TypeInt ,
403+ },
404+ },
405+ },
406+ },
407+ },
375408 "sync_host_images" : {
376409 Type : schema .TypeBool ,
377410 Description : "Set `True` to configure Enforcers to discover local host images. Discovered images will be listed under Images > Host Images, as well as under Infrastructure (in the Images tab for applicable hosts)." ,
@@ -403,28 +436,24 @@ func resourceEnforcerGroup() *schema.Resource {
403436 }
404437}
405438
406- func resourceEnforcerGroupCreate (d * schema.ResourceData , m interface {}) error {
439+ func resourceEnforcerGroupCreate (ctx context. Context , d * schema.ResourceData , m interface {}) diag. Diagnostics {
407440 ac := m .(* client.Client )
408441
409442 group := expandEnforcerGroup (d )
410443 err := ac .CreateEnforcerGroup (group )
411444
412445 if err != nil {
413- return err
446+ return diag . FromErr ( err )
414447 }
415448
416- err = resourceEnforcerGroupRead (d , m )
417-
418- if err == nil {
419- d .SetId (d .Get ("group_id" ).(string ))
420- } else {
421- return err
449+ if diags := resourceEnforcerGroupRead (ctx , d , m ); diags .HasError () {
450+ return diags
422451 }
423-
452+ d . SetId ( d . Get ( "group_id" ).( string ))
424453 return nil
425454}
426455
427- func resourceEnforcerGroupRead (d * schema.ResourceData , m interface {}) error {
456+ func resourceEnforcerGroupRead (ctx context. Context , d * schema.ResourceData , m interface {}) diag. Diagnostics {
428457 var name string
429458 ac := m .(* client.Client )
430459 groupId , ok := d .GetOk ("group_id" )
@@ -442,7 +471,7 @@ func resourceEnforcerGroupRead(d *schema.ResourceData, m interface{}) error {
442471 d .SetId ("" )
443472 return nil
444473 }
445- return err
474+ return diag . FromErr ( err )
446475 }
447476
448477 d .Set ("group_id" , r .ID )
@@ -460,7 +489,7 @@ func resourceEnforcerGroupRead(d *schema.ResourceData, m interface{}) error {
460489 d .Set ("behavioral_engine" , r .BehavioralEngine )
461490 d .Set ("host_behavioral_engine" , r .BehavioralEngine )
462491 d .Set ("forensics" , r .ContainerForensicsCollection )
463- d .Set ("host_forensics " , r .HostForensicsCollection )
492+ d .Set ("host_forensics_collection " , r .HostForensicsCollection )
464493 d .Set ("host_network_protection" , r .HostNetworkProtection )
465494 d .Set ("user_access_control" , r .UserAccessControl )
466495 d .Set ("image_assurance" , r .ImageAssurance )
@@ -470,6 +499,7 @@ func resourceEnforcerGroupRead(d *schema.ResourceData, m interface{}) error {
470499 d .Set ("token" , r .Token )
471500 d .Set ("command" , flattenCommands (r .Command ))
472501 d .Set ("orchestrator" , flattenOrchestrators (r .Orchestrator ))
502+ d .Set ("schedule_scan_settings" , flattenScheduleScanSettings (r .ScheduleScanSettings ))
473503 d .Set ("host_os" , r .HostOs )
474504 d .Set ("install_command" , r .InstallCommand )
475505 d .Set ("hosts_count" , r .HostsCount )
@@ -511,8 +541,7 @@ func resourceEnforcerGroupRead(d *schema.ResourceData, m interface{}) error {
511541 return nil
512542}
513543
514- func resourceEnforcerGroupUpdate (d * schema.ResourceData , m interface {}) error {
515-
544+ func resourceEnforcerGroupUpdate (ctx context.Context , d * schema.ResourceData , m interface {}) diag.Diagnostics {
516545 if d .HasChanges ("admission_control" ,
517546 "allow_kube_enforcer_audit" ,
518547 "allowed_applications" ,
@@ -537,6 +566,7 @@ func resourceEnforcerGroupUpdate(d *schema.ResourceData, m interface{}) error {
537566 "host_os" ,
538567 "host_protection" ,
539568 "host_user_protection" ,
569+ "host_forensics_collection" ,
540570 "image_assurance" ,
541571 "kube_bench_image_name" ,
542572 "logical_name" ,
@@ -553,6 +583,7 @@ func resourceEnforcerGroupUpdate(d *schema.ResourceData, m interface{}) error {
553583 "type" ,
554584 "user_access_control" ,
555585 "orchestrator" ,
586+ "schedule_scan_settings" ,
556587 ) {
557588
558589 ac := m .(* client.Client )
@@ -561,23 +592,23 @@ func resourceEnforcerGroupUpdate(d *schema.ResourceData, m interface{}) error {
561592 err := ac .UpdateEnforcerGroup (group )
562593
563594 if err == nil {
564- _ = d .Set ("last_updated " , time .Now ().Format ( time . RFC850 ))
595+ _ = d .Set ("last_update " , time .Now ().Unix ( ))
565596 } else {
566597 log .Println ("[DEBUG] error while updating enforcer r: " , err )
567- return err
598+ return diag . FromErr ( err )
568599 }
569600 }
570601 return nil
571602}
572603
573- func resourceEnforcerGroupDelete (d * schema.ResourceData , m interface {}) error {
604+ func resourceEnforcerGroupDelete (ctx context. Context , d * schema.ResourceData , m interface {}) diag. Diagnostics {
574605 ac := m .(* client.Client )
575606 name := d .Id ()
576607 err := ac .DeleteEnforcerGroup (name )
577608 if err != nil {
578- return err
609+ return diag . FromErr ( err )
579610 }
580- return err
611+ return diag . FromErr ( err )
581612}
582613
583614func expandEnforcerGroup (d * schema.ResourceData ) client.EnforcerGroup {
@@ -714,7 +745,7 @@ func expandEnforcerGroup(d *schema.ResourceData) client.EnforcerGroup {
714745 enforcerGroup .HostBehavioralEngine = hostBehavioralEngine .(bool )
715746 }
716747
717- hostForensics , ok := d .GetOk ("host_forensics " )
748+ hostForensics , ok := d .GetOk ("host_forensics_collection " )
718749 if ok {
719750 enforcerGroup .HostForensicsCollection = hostForensics .(bool )
720751 }
@@ -827,5 +858,33 @@ func expandEnforcerGroup(d *schema.ResourceData) client.EnforcerGroup {
827858 }
828859 }
829860
861+ if v , ok := d .GetOk ("schedule_scan_settings" ); ok {
862+ scanSettingsList := v .([]interface {})
863+ if len (scanSettingsList ) > 0 && scanSettingsList [0 ] != nil {
864+ catData := scanSettingsList [0 ].(map [string ]interface {})
865+
866+ sDisabled := catData ["disabled" ].(bool )
867+ sIsCustom := catData ["is_custom" ].(bool )
868+
869+ rawDays := catData ["days" ].([]interface {})
870+ sDays := make ([]int , len (rawDays ))
871+ for i , v := range rawDays {
872+ sDays [i ] = v .(int )
873+ }
874+
875+ rawTime := catData ["time" ].([]interface {})
876+ sTime := make ([]int , len (rawTime ))
877+ for i , v := range rawTime {
878+ sTime [i ] = v .(int )
879+ }
880+
881+ enforcerGroup .ScheduleScanSettings = client.EnforcerScheduleScanSettings {
882+ Disabled : sDisabled ,
883+ IsCustom : sIsCustom ,
884+ Days : sDays ,
885+ Time : sTime ,
886+ }
887+ }
888+ }
830889 return enforcerGroup
831890}
0 commit comments