|
| 1 | +package sysdig |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | +) |
| 11 | + |
| 12 | +func dataSourceSysdigSecureRuleStatefulCount() *schema.Resource { |
| 13 | + timeout := 1 * time.Minute |
| 14 | + |
| 15 | + return &schema.Resource{ |
| 16 | + ReadContext: dataSourceSysdigRuleStatefulCountRead, |
| 17 | + |
| 18 | + Timeouts: &schema.ResourceTimeout{ |
| 19 | + Read: schema.DefaultTimeout(timeout), |
| 20 | + }, |
| 21 | + |
| 22 | + Schema: map[string]*schema.Schema{ |
| 23 | + "name": { |
| 24 | + Type: schema.TypeString, |
| 25 | + Required: true, |
| 26 | + }, |
| 27 | + "source": { |
| 28 | + Type: schema.TypeString, |
| 29 | + Required: true, |
| 30 | + ValidateDiagFunc: validateDiagFunc(validateStatefulRuleSource), |
| 31 | + }, |
| 32 | + "rule_count": { |
| 33 | + Type: schema.TypeInt, |
| 34 | + Computed: true, |
| 35 | + }, |
| 36 | + }, |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func dataSourceSysdigRuleStatefulCountRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 41 | + client, err := getSecureRuleClient(meta.(SysdigClients)) |
| 42 | + if err != nil { |
| 43 | + return diag.FromErr(err) |
| 44 | + } |
| 45 | + |
| 46 | + ruleName := d.Get("name").(string) |
| 47 | + ruleType := d.Get("source").(string) |
| 48 | + rules, err := client.GetStatefulRuleGroup(ctx, ruleName, ruleType) |
| 49 | + if err != nil { |
| 50 | + return diag.FromErr(err) |
| 51 | + } |
| 52 | + |
| 53 | + d.SetId(fmt.Sprintf("count_%s", ruleName)) |
| 54 | + _ = d.Set("name", ruleName) |
| 55 | + _ = d.Set("rule_count", len(rules)) |
| 56 | + |
| 57 | + return nil |
| 58 | +} |
0 commit comments