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