|
| 1 | +package sysdig |
| 2 | + |
| 3 | +import ( |
| 4 | + "strconv" |
| 5 | + "strings" |
| 6 | + |
| 7 | + v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | +) |
| 10 | + |
| 11 | +func createPolicyDataSourceSchema() map[string]*schema.Schema { |
| 12 | + return map[string]*schema.Schema{ |
| 13 | + "name": { |
| 14 | + Type: schema.TypeString, |
| 15 | + Required: true, |
| 16 | + }, |
| 17 | + "type": { |
| 18 | + Type: schema.TypeString, |
| 19 | + Optional: true, |
| 20 | + Default: "falco", |
| 21 | + ValidateDiagFunc: validateDiagFunc(validatePolicyType), |
| 22 | + }, |
| 23 | + "id": { |
| 24 | + Type: schema.TypeInt, |
| 25 | + Computed: true, |
| 26 | + }, |
| 27 | + "description": { |
| 28 | + Type: schema.TypeString, |
| 29 | + Computed: true, |
| 30 | + }, |
| 31 | + "severity": { |
| 32 | + Type: schema.TypeInt, |
| 33 | + Computed: true, |
| 34 | + }, |
| 35 | + "enabled": { |
| 36 | + Type: schema.TypeBool, |
| 37 | + Computed: true, |
| 38 | + }, |
| 39 | + "runbook": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Optional: true, |
| 42 | + }, |
| 43 | + "scope": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Computed: true, |
| 46 | + }, |
| 47 | + "rules": { |
| 48 | + Type: schema.TypeList, |
| 49 | + Computed: true, |
| 50 | + Elem: &schema.Resource{ |
| 51 | + Schema: map[string]*schema.Schema{ |
| 52 | + "name": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Computed: true, |
| 55 | + }, |
| 56 | + "enabled": { |
| 57 | + Type: schema.TypeBool, |
| 58 | + Computed: true, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + "notification_channels": { |
| 64 | + Type: schema.TypeSet, |
| 65 | + Computed: true, |
| 66 | + Elem: &schema.Schema{ |
| 67 | + Type: schema.TypeInt, |
| 68 | + }, |
| 69 | + }, |
| 70 | + "actions": { |
| 71 | + Type: schema.TypeList, |
| 72 | + Computed: true, |
| 73 | + Optional: true, |
| 74 | + Elem: &schema.Resource{ |
| 75 | + Schema: map[string]*schema.Schema{ |
| 76 | + "container": { |
| 77 | + Type: schema.TypeString, |
| 78 | + Optional: true, |
| 79 | + Computed: true, |
| 80 | + }, |
| 81 | + "capture": { |
| 82 | + Type: schema.TypeList, |
| 83 | + Optional: true, |
| 84 | + Computed: true, |
| 85 | + Elem: &schema.Resource{ |
| 86 | + Schema: map[string]*schema.Schema{ |
| 87 | + "seconds_after_event": { |
| 88 | + Type: schema.TypeInt, |
| 89 | + Computed: true, |
| 90 | + }, |
| 91 | + "seconds_before_event": { |
| 92 | + Type: schema.TypeInt, |
| 93 | + Computed: true, |
| 94 | + }, |
| 95 | + "name": { |
| 96 | + Type: schema.TypeString, |
| 97 | + Computed: true, |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func policyDataSourceToResourceData(policy v2.Policy, d *schema.ResourceData) { |
| 109 | + d.SetId(strconv.Itoa(policy.ID)) |
| 110 | + |
| 111 | + _ = d.Set("name", policy.Name) |
| 112 | + if policy.Type != "" { |
| 113 | + _ = d.Set("type", policy.Type) |
| 114 | + } else { |
| 115 | + _ = d.Set("type", "falco") |
| 116 | + } |
| 117 | + |
| 118 | + _ = d.Set("description", policy.Description) |
| 119 | + _ = d.Set("severity", policy.Severity) |
| 120 | + _ = d.Set("enabled", policy.Enabled) |
| 121 | + _ = d.Set("scope", policy.Scope) |
| 122 | + _ = d.Set("notification_channels", policy.NotificationChannelIds) |
| 123 | + _ = d.Set("runbook", policy.Runbook) |
| 124 | + |
| 125 | + actions := []map[string]interface{}{{}} |
| 126 | + for _, action := range policy.Actions { |
| 127 | + if action.Type != "POLICY_ACTION_CAPTURE" { |
| 128 | + action := strings.Replace(action.Type, "POLICY_ACTION_", "", 1) |
| 129 | + actions[0]["container"] = strings.ToLower(action) |
| 130 | + //d.Set("actions.0.container", strings.ToLower(action)) |
| 131 | + } else { |
| 132 | + actions[0]["capture"] = []map[string]interface{}{{ |
| 133 | + "seconds_after_event": action.AfterEventNs / 1000000000, |
| 134 | + "seconds_before_event": action.BeforeEventNs / 1000000000, |
| 135 | + "name": action.Name, |
| 136 | + }} |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + _ = d.Set("actions", actions) |
| 141 | + |
| 142 | + rules := []map[string]interface{}{} |
| 143 | + |
| 144 | + for _, rule := range policy.Rules { |
| 145 | + rules = append(rules, map[string]interface{}{ |
| 146 | + "name": rule.Name, |
| 147 | + "enabled": rule.Enabled, |
| 148 | + }) |
| 149 | + } |
| 150 | + |
| 151 | + _ = d.Set("rules", rules) |
| 152 | +} |
0 commit comments