|
| 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 | + "strings" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +func resourceSysdigSecureList() *schema.Resource { |
| 12 | + timeout := 30 * time.Second |
| 13 | + |
| 14 | + return &schema.Resource{ |
| 15 | + Create: resourceSysdigListCreate, |
| 16 | + Update: resourceSysdigListUpdate, |
| 17 | + Read: resourceSysdigListRead, |
| 18 | + Delete: resourceSysdigListDelete, |
| 19 | + |
| 20 | + Timeouts: &schema.ResourceTimeout{ |
| 21 | + Create: schema.DefaultTimeout(timeout), |
| 22 | + Update: schema.DefaultTimeout(timeout), |
| 23 | + Read: schema.DefaultTimeout(timeout), |
| 24 | + Delete: schema.DefaultTimeout(timeout), |
| 25 | + }, |
| 26 | + |
| 27 | + Schema: map[string]*schema.Schema{ |
| 28 | + "name": { |
| 29 | + Type: schema.TypeString, |
| 30 | + Required: true, |
| 31 | + ForceNew: true, |
| 32 | + }, |
| 33 | + "items": { |
| 34 | + Type: schema.TypeList, |
| 35 | + Required: true, |
| 36 | + Elem: &schema.Schema{ |
| 37 | + Type: schema.TypeString, |
| 38 | + }, |
| 39 | + }, |
| 40 | + "append": { |
| 41 | + Type: schema.TypeBool, |
| 42 | + Optional: true, |
| 43 | + }, |
| 44 | + "version": { |
| 45 | + Type: schema.TypeInt, |
| 46 | + Computed: true, |
| 47 | + }, |
| 48 | + }, |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func resourceSysdigListCreate(d *schema.ResourceData, meta interface{}) error { |
| 53 | + client := meta.(*SysdigClients).sysdigSecureClient |
| 54 | + |
| 55 | + list := listFromResourceData(d) |
| 56 | + list, err := client.CreateList(list) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + |
| 61 | + d.SetId(strconv.Itoa(list.ID)) |
| 62 | + d.Set("version", list.Version) |
| 63 | + |
| 64 | + return nil |
| 65 | +} |
| 66 | + |
| 67 | +func resourceSysdigListUpdate(d *schema.ResourceData, meta interface{}) error { |
| 68 | + client := meta.(*SysdigClients).sysdigSecureClient |
| 69 | + |
| 70 | + list := listFromResourceData(d) |
| 71 | + list.Version = d.Get("version").(int) |
| 72 | + |
| 73 | + id, _ := strconv.Atoi(d.Id()) |
| 74 | + list.ID = id |
| 75 | + |
| 76 | + _, err := client.UpdateList(list) |
| 77 | + return err |
| 78 | +} |
| 79 | + |
| 80 | +func resourceSysdigListRead(d *schema.ResourceData, meta interface{}) error { |
| 81 | + client := meta.(*SysdigClients).sysdigSecureClient |
| 82 | + |
| 83 | + id, _ := strconv.Atoi(d.Id()) |
| 84 | + list, err := client.GetListById(id) |
| 85 | + |
| 86 | + if err != nil { |
| 87 | + d.SetId("") |
| 88 | + } |
| 89 | + |
| 90 | + d.Set("name", list.Name) |
| 91 | + d.Set("version", list.Version) |
| 92 | + d.Set("items", list.Items.Items) |
| 93 | + d.Set("append", list.Append) |
| 94 | + |
| 95 | + return nil |
| 96 | +} |
| 97 | + |
| 98 | +func resourceSysdigListDelete(d *schema.ResourceData, meta interface{}) error { |
| 99 | + client := meta.(*SysdigClients).sysdigSecureClient |
| 100 | + |
| 101 | + id, _ := strconv.Atoi(d.Id()) |
| 102 | + |
| 103 | + return client.DeleteList(id) |
| 104 | +} |
| 105 | + |
| 106 | +func listFromResourceData(d *schema.ResourceData) secure.List { |
| 107 | + list := secure.List{ |
| 108 | + Name: d.Get("name").(string), |
| 109 | + Append: d.Get("append").(bool), |
| 110 | + Items: secure.Items{Items: []string{}}, |
| 111 | + } |
| 112 | + |
| 113 | + items := d.Get("items").([]interface{}) |
| 114 | + for _, item := range items { |
| 115 | + if item_str, ok := item.(string); ok { |
| 116 | + item_str = strings.TrimSpace(item_str) |
| 117 | + list.Items.Items = append(list.Items.Items, item_str) |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return list |
| 122 | +} |
0 commit comments