Skip to content

Commit

Permalink
PLT-726: Scan Policy deletion fix
Browse files Browse the repository at this point in the history
  • Loading branch information
SivaanandM committed Oct 9, 2023
1 parent 161ef0f commit 02da17b
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions spectrocloud/cluster_common_policies.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package spectrocloud

import (
"errors"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/spectrocloud/hapi/models"
"github.com/spectrocloud/palette-sdk-go/client"
Expand Down Expand Up @@ -62,7 +63,10 @@ func flattenBackupPolicy(policy *models.V1ClusterBackupConfig) []interface{} {

func updateBackupPolicy(c *client.V1Client, d *schema.ResourceData) error {
if policy := toBackupPolicy(d); policy != nil {
return c.UpdateClusterBackupConfig(d.Id(), policy)
clusterContext := d.Get("context").(string)
return c.ApplyClusterBackupConfig(d.Id(), policy, clusterContext)

Check failure on line 67 in spectrocloud/cluster_common_policies.go

View workflow job for this annotation

GitHub Actions / build-with-coverage

c.ApplyClusterBackupConfig undefined (type *"github.com/spectrocloud/palette-sdk-go/client".V1Client has no field or method ApplyClusterBackupConfig)
} else {
return errors.New("backup policy validation: The backup policy cannot be destroyed. To disable it, set the schedule to an empty string")
}
return nil
}
Expand Down Expand Up @@ -100,6 +104,7 @@ func toScanPolicy(d *schema.ResourceData) *models.V1ClusterComplianceScheduleCon
func flattenScanPolicy(driverSpec map[string]models.V1ComplianceScanDriverSpec) []interface{} {
result := make([]interface{}, 0, 1)
data := make(map[string]interface{})

if v, found := driverSpec["kube-bench"]; found {
data["configuration_scan_schedule"] = v.Config.Schedule.ScheduledRunTime
}
Expand All @@ -109,14 +114,30 @@ func flattenScanPolicy(driverSpec map[string]models.V1ComplianceScanDriverSpec)
if v, found := driverSpec["sonobuoy"]; found {
data["conformance_scan_schedule"] = v.Config.Schedule.ScheduledRunTime
}
result = append(result, data)
if data["configuration_scan_schedule"] == "" && data["penetration_scan_schedule"] == "" && data["conformance_scan_schedule"] == "" {
return result
} else {
result = append(result, data)
}
return result
}

func updateScanPolicy(c *client.V1Client, d *schema.ResourceData) error {
if policy := toScanPolicy(d); policy != nil {
if policy := toScanPolicy(d); policy != nil || d.HasChange("scan_policy") {
ClusterContext := d.Get("context").(string)
if policy == nil {
policy = getEmptyScanPolicy()
}
return c.ApplyClusterScanConfig(d.Id(), policy, ClusterContext)
}
return nil
}

func getEmptyScanPolicy() *models.V1ClusterComplianceScheduleConfig {
scanPolicy := &models.V1ClusterComplianceScheduleConfig{
KubeBench: &models.V1ClusterComplianceScanKubeBenchScheduleConfig{Schedule: &models.V1ClusterFeatureSchedule{ScheduledRunTime: ""}},
KubeHunter: &models.V1ClusterComplianceScanKubeHunterScheduleConfig{Schedule: &models.V1ClusterFeatureSchedule{ScheduledRunTime: ""}},
Sonobuoy: &models.V1ClusterComplianceScanSonobuoyScheduleConfig{Schedule: &models.V1ClusterFeatureSchedule{ScheduledRunTime: ""}},
}
return scanPolicy
}

0 comments on commit 02da17b

Please sign in to comment.