|
| 1 | +package aquasec |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + |
| 6 | + "github.com/aquasecurity/terraform-provider-aquasec/client" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | +) |
| 9 | + |
| 10 | +func resourceAquaLabels() *schema.Resource { |
| 11 | + return &schema.Resource{ |
| 12 | + Description: "The data source `aquasec_aqua_labels` provides a method to query all aqua labels within the Aqua account management." + |
| 13 | + "The fields returned from this query are detailed in the Schema section below.", |
| 14 | + Read: resourceAquaLabelRead, |
| 15 | + Create: resourceAquaLabelCreate, |
| 16 | + Update: resourceAquaLabelUpdate, |
| 17 | + Delete: resourceAquaLabelDelete, |
| 18 | + Importer: &schema.ResourceImporter{ |
| 19 | + StateContext: schema.ImportStatePassthroughContext, |
| 20 | + }, |
| 21 | + Schema: map[string]*schema.Schema{ |
| 22 | + "name": { |
| 23 | + Type: schema.TypeString, |
| 24 | + Description: "Aqua label name.", |
| 25 | + Required: true, |
| 26 | + }, |
| 27 | + "description": { |
| 28 | + Type: schema.TypeString, |
| 29 | + Description: "Aqua label description.", |
| 30 | + Optional: true, |
| 31 | + }, |
| 32 | + "created": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Description: "The creation date of the Aqua label.", |
| 35 | + Computed: true, |
| 36 | + }, |
| 37 | + "author": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Description: "The name of the user who created the Aqua label.", |
| 40 | + Computed: true, |
| 41 | + }, |
| 42 | + }, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func resourceAquaLabelCreate(d *schema.ResourceData, m interface{}) error { |
| 47 | + ac := m.(*client.Client) |
| 48 | + aquaLabel := client.AquaLabel{ |
| 49 | + Name: d.Get("name").(string), |
| 50 | + } |
| 51 | + |
| 52 | + description, ok := d.GetOk("description") |
| 53 | + if ok { |
| 54 | + aquaLabel.Description = description.(string) |
| 55 | + } |
| 56 | + |
| 57 | + err := ac.CreateAquaLabel(&aquaLabel) |
| 58 | + |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + d.SetId(aquaLabel.Name) |
| 63 | + return resourceAquaLabelRead(d, m) |
| 64 | +} |
| 65 | + |
| 66 | +func resourceAquaLabelRead(d *schema.ResourceData, m interface{}) error { |
| 67 | + log.Println("[DEBUG] inside resourceAquaLabelRead") |
| 68 | + c := m.(*client.Client) |
| 69 | + r, err := c.GetAquaLabel(d.Id()) |
| 70 | + |
| 71 | + if err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + d.Set("name", r.Name) |
| 75 | + d.Set("description", r.Description) |
| 76 | + d.Set("created", r.Created) |
| 77 | + d.Set("author", r.Author) |
| 78 | + |
| 79 | + return nil |
| 80 | +} |
| 81 | + |
| 82 | +func resourceAquaLabelUpdate(d *schema.ResourceData, m interface{}) error { |
| 83 | + c := m.(*client.Client) |
| 84 | + |
| 85 | + if d.HasChanges("description") { |
| 86 | + aqua_lable := client.AquaLabel{ |
| 87 | + Name: d.Get("name").(string), |
| 88 | + } |
| 89 | + |
| 90 | + description, ok := d.GetOk("description") |
| 91 | + if ok { |
| 92 | + aqua_lable.Description = description.(string) |
| 93 | + } |
| 94 | + |
| 95 | + err := c.UpdateAquaLabel(&aqua_lable) |
| 96 | + |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + d.SetId(d.Get("name").(string)) |
| 101 | + return nil |
| 102 | + } |
| 103 | + return resourceAquaLabelRead(d, m) |
| 104 | +} |
| 105 | + |
| 106 | +func resourceAquaLabelDelete(d *schema.ResourceData, m interface{}) error { |
| 107 | + c := m.(*client.Client) |
| 108 | + id := d.Id() |
| 109 | + err := c.DeleteAquaLabel(id) |
| 110 | + if err == nil { |
| 111 | + d.SetId("") |
| 112 | + } else { |
| 113 | + return err |
| 114 | + } |
| 115 | + return nil |
| 116 | +} |
0 commit comments