|
| 1 | +package sysdig |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "strconv" |
| 8 | + "time" |
| 9 | + |
| 10 | + v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 13 | +) |
| 14 | + |
| 15 | +func dataSourceSysdigSecureRuleStateful() *schema.Resource { |
| 16 | + timeout := 1 * time.Minute |
| 17 | + |
| 18 | + return &schema.Resource{ |
| 19 | + ReadContext: dataSourceSysdigRuleStatefulRead, |
| 20 | + |
| 21 | + Timeouts: &schema.ResourceTimeout{ |
| 22 | + Read: schema.DefaultTimeout(timeout), |
| 23 | + }, |
| 24 | + |
| 25 | + Schema: map[string]*schema.Schema{ |
| 26 | + "name": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Required: true, |
| 29 | + ForceNew: true, |
| 30 | + }, |
| 31 | + "version": { |
| 32 | + Type: schema.TypeInt, |
| 33 | + Computed: true, |
| 34 | + }, |
| 35 | + "source": { |
| 36 | + Type: schema.TypeString, |
| 37 | + Required: true, |
| 38 | + ValidateDiagFunc: validateDiagFunc(validateStatefulRuleSource), |
| 39 | + }, |
| 40 | + "ruletype": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Required: true, |
| 43 | + ValidateDiagFunc: validateDiagFunc(validateStatefulRuleType), |
| 44 | + }, |
| 45 | + "append": { |
| 46 | + Type: schema.TypeBool, |
| 47 | + Optional: true, |
| 48 | + Default: true, |
| 49 | + }, |
| 50 | + "exceptions": { |
| 51 | + Type: schema.TypeList, |
| 52 | + Required: true, |
| 53 | + Elem: &schema.Resource{ |
| 54 | + Schema: map[string]*schema.Schema{ |
| 55 | + "name": { |
| 56 | + Type: schema.TypeString, |
| 57 | + Required: true, |
| 58 | + }, |
| 59 | + "values": { |
| 60 | + Type: schema.TypeString, |
| 61 | + Required: true, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func dataSourceSysdigRuleStatefulRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 71 | + client, err := getSecureRuleClient(meta.(SysdigClients)) |
| 72 | + if err != nil { |
| 73 | + return diag.FromErr(err) |
| 74 | + } |
| 75 | + |
| 76 | + id, err := strconv.Atoi(d.Id()) |
| 77 | + if err != nil { |
| 78 | + return diag.FromErr(err) |
| 79 | + } |
| 80 | + |
| 81 | + nameObj, ok := d.GetOk("name") |
| 82 | + if !ok { |
| 83 | + return diag.FromErr(errors.New("name is required")) |
| 84 | + } |
| 85 | + |
| 86 | + name := nameObj.(string) |
| 87 | + |
| 88 | + sourceObj, ok := d.GetOk("source") |
| 89 | + if !ok { |
| 90 | + return diag.FromErr(errors.New("source is required")) |
| 91 | + } |
| 92 | + |
| 93 | + source := sourceObj.(string) |
| 94 | + |
| 95 | + rules, err := client.GetStatefulRuleGroup(ctx, name, source) |
| 96 | + if err != nil { |
| 97 | + return diag.FromErr(err) |
| 98 | + } |
| 99 | + |
| 100 | + if len(rules) == 0 { |
| 101 | + d.SetId("") |
| 102 | + } |
| 103 | + |
| 104 | + var rule v2.Rule |
| 105 | + |
| 106 | + for _, r := range rules { |
| 107 | + if r.ID == id { |
| 108 | + rule = r |
| 109 | + break |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + exceptions := make([]any, 0, len(rule.Details.Exceptions)) |
| 114 | + for _, exception := range rule.Details.Exceptions { |
| 115 | + if exception == nil { |
| 116 | + return diag.Errorf("exception is nil") |
| 117 | + } |
| 118 | + valuesData, err := json.Marshal(exception.Values) |
| 119 | + if err != nil { |
| 120 | + return diag.Errorf("error marshalling exception values '%+v': %s", exception.Values, err) |
| 121 | + } |
| 122 | + |
| 123 | + exceptions = append(exceptions, map[string]any{ |
| 124 | + "name": exception.Name, |
| 125 | + "values": string(valuesData), |
| 126 | + }) |
| 127 | + } |
| 128 | + |
| 129 | + if err := d.Set("exceptions", exceptions); err != nil { |
| 130 | + return diag.FromErr(err) |
| 131 | + } |
| 132 | + |
| 133 | + return nil |
| 134 | +} |
0 commit comments