|
| 1 | +package sysdig |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/draios/terraform-provider-sysdig/sysdig/secure" |
| 5 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 6 | + "strconv" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +func resourceSysdigSecureMacro() *schema.Resource { |
| 11 | + timeout := 30 * time.Second |
| 12 | + |
| 13 | + return &schema.Resource{ |
| 14 | + Create: resourceSysdigMacroCreate, |
| 15 | + Update: resourceSysdigMacroUpdate, |
| 16 | + Read: resourceSysdigMacroRead, |
| 17 | + Delete: resourceSysdigMacroDelete, |
| 18 | + |
| 19 | + Timeouts: &schema.ResourceTimeout{ |
| 20 | + Create: schema.DefaultTimeout(timeout), |
| 21 | + Update: schema.DefaultTimeout(timeout), |
| 22 | + Read: schema.DefaultTimeout(timeout), |
| 23 | + Delete: schema.DefaultTimeout(timeout), |
| 24 | + }, |
| 25 | + |
| 26 | + Schema: map[string]*schema.Schema{ |
| 27 | + "name": { |
| 28 | + Type: schema.TypeString, |
| 29 | + Required: true, |
| 30 | + ForceNew: true, |
| 31 | + }, |
| 32 | + "append": { |
| 33 | + Type: schema.TypeBool, |
| 34 | + Optional: true, |
| 35 | + Default: false, |
| 36 | + }, |
| 37 | + "condition": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Required: true, |
| 40 | + }, |
| 41 | + "version": { |
| 42 | + Type: schema.TypeInt, |
| 43 | + Computed: true, |
| 44 | + }, |
| 45 | + }, |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func resourceSysdigMacroCreate(d *schema.ResourceData, meta interface{}) error { |
| 50 | + client := meta.(*SysdigClients).sysdigSecureClient |
| 51 | + |
| 52 | + macro := macroFromResourceData(d) |
| 53 | + macro, err := client.CreateMacro(macro) |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + |
| 58 | + d.SetId(strconv.Itoa(macro.ID)) |
| 59 | + d.Set("version", macro.Version) |
| 60 | + |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +func resourceSysdigMacroUpdate(d *schema.ResourceData, meta interface{}) error { |
| 65 | + client := meta.(*SysdigClients).sysdigSecureClient |
| 66 | + |
| 67 | + macro := macroFromResourceData(d) |
| 68 | + macro.Version = d.Get("version").(int) |
| 69 | + |
| 70 | + id, _ := strconv.Atoi(d.Id()) |
| 71 | + macro.ID = id |
| 72 | + |
| 73 | + _, err := client.UpdateMacro(macro) |
| 74 | + return err |
| 75 | +} |
| 76 | + |
| 77 | +func resourceSysdigMacroRead(d *schema.ResourceData, meta interface{}) error { |
| 78 | + client := meta.(*SysdigClients).sysdigSecureClient |
| 79 | + |
| 80 | + id, _ := strconv.Atoi(d.Id()) |
| 81 | + macro, err := client.GetMacroById(id) |
| 82 | + |
| 83 | + if err != nil { |
| 84 | + d.SetId("") |
| 85 | + } |
| 86 | + |
| 87 | + d.Set("name", macro.Name) |
| 88 | + d.Set("version", macro.Version) |
| 89 | + d.Set("items", macro.Condition.Condition) |
| 90 | + d.Set("append", macro.Append) |
| 91 | + |
| 92 | + return nil |
| 93 | +} |
| 94 | + |
| 95 | +func resourceSysdigMacroDelete(d *schema.ResourceData, meta interface{}) error { |
| 96 | + client := meta.(*SysdigClients).sysdigSecureClient |
| 97 | + |
| 98 | + id, _ := strconv.Atoi(d.Id()) |
| 99 | + |
| 100 | + return client.DeleteMacro(id) |
| 101 | +} |
| 102 | + |
| 103 | +func macroFromResourceData(d *schema.ResourceData) secure.Macro { |
| 104 | + return secure.Macro{ |
| 105 | + Name: d.Get("name").(string), |
| 106 | + Append: d.Get("append").(bool), |
| 107 | + Condition: secure.MacroCondition{Condition: d.Get("condition").(string)}, |
| 108 | + } |
| 109 | +} |
0 commit comments