|
| 1 | +package keep |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | + "net/http" |
| 10 | + "strconv" |
| 11 | + "strings" |
| 12 | +) |
| 13 | + |
| 14 | +func resourceExtraction() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + CreateContext: resourceCreateExtraction, |
| 17 | + ReadContext: resourceReadExtraction, |
| 18 | + UpdateContext: resourceUpdateExtraction, |
| 19 | + DeleteContext: resourceDeleteExtraction, |
| 20 | + Importer: &schema.ResourceImporter{ |
| 21 | + StateContext: schema.ImportStatePassthroughContext, |
| 22 | + }, |
| 23 | + Schema: map[string]*schema.Schema{ |
| 24 | + "id": { |
| 25 | + Type: schema.TypeString, |
| 26 | + Computed: true, |
| 27 | + Description: "ID of the extraction", |
| 28 | + }, |
| 29 | + "name": { |
| 30 | + Type: schema.TypeString, |
| 31 | + Required: true, |
| 32 | + Description: "Name of the extraction", |
| 33 | + }, |
| 34 | + "description": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Optional: true, |
| 37 | + Description: "Description of the extraction", |
| 38 | + Default: "", |
| 39 | + }, |
| 40 | + "priority": { |
| 41 | + Type: schema.TypeInt, |
| 42 | + Optional: true, |
| 43 | + Description: "Priority of the extraction", |
| 44 | + Default: 0, |
| 45 | + }, |
| 46 | + "attribute": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Required: true, |
| 49 | + Description: "Attribute of the extraction", |
| 50 | + }, |
| 51 | + "condition": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Optional: true, |
| 54 | + Description: "Condition of the extraction", |
| 55 | + Default: "", |
| 56 | + }, |
| 57 | + "disabled": { |
| 58 | + Type: schema.TypeBool, |
| 59 | + Required: true, |
| 60 | + Default: false, |
| 61 | + }, |
| 62 | + "regex": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Required: true, |
| 65 | + Description: "Regex of the extraction", |
| 66 | + }, |
| 67 | + "pre": { |
| 68 | + Type: schema.TypeBool, |
| 69 | + Required: true, |
| 70 | + Default: false, |
| 71 | + Description: "Pre of the extraction", |
| 72 | + }, |
| 73 | + }, |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func resourceCreateExtraction(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 78 | + client := m.(*Client) |
| 79 | + |
| 80 | + body := map[string]interface{}{ |
| 81 | + "name": d.Get("name").(string), |
| 82 | + "description": d.Get("description").(string), |
| 83 | + "priority": d.Get("priority").(int), |
| 84 | + "attribute": d.Get("attribute").(string), |
| 85 | + "condition": d.Get("condition").(string), |
| 86 | + "disabled": d.Get("disabled").(bool), |
| 87 | + "regex": d.Get("regex").(string), |
| 88 | + "pre": d.Get("pre").(bool), |
| 89 | + } |
| 90 | + |
| 91 | + // marshal body |
| 92 | + bodyBytes, err := json.Marshal(body) |
| 93 | + if err != nil { |
| 94 | + return diag.Errorf("cannot marshal extraction body: %s", err) |
| 95 | + } |
| 96 | + |
| 97 | + // create extraction |
| 98 | + req, err := http.NewRequest("POST", client.HostURL+"/extraction/", strings.NewReader(string(bodyBytes))) |
| 99 | + if err != nil { |
| 100 | + return diag.Errorf("cannot create request: %s", err) |
| 101 | + } |
| 102 | + |
| 103 | + // send request |
| 104 | + respBody, err := client.doReq(req) |
| 105 | + if err != nil { |
| 106 | + return diag.Errorf("cannot send request: %s", err) |
| 107 | + } |
| 108 | + |
| 109 | + // unmarshal response |
| 110 | + var response map[string]interface{} |
| 111 | + err = json.Unmarshal(respBody, &response) |
| 112 | + if err != nil { |
| 113 | + return diag.Errorf("cannot unmarshal response: %s", err) |
| 114 | + } |
| 115 | + |
| 116 | + d.SetId(fmt.Sprintf("%f", response["id"])) |
| 117 | + d.Set("id", fmt.Sprintf("%f", response["id"])) |
| 118 | + |
| 119 | + return nil |
| 120 | +} |
| 121 | + |
| 122 | +func resourceReadExtraction(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 123 | + client := m.(*Client) |
| 124 | + |
| 125 | + id := d.Id() |
| 126 | + |
| 127 | + req, err := http.NewRequest("GET", client.HostURL+"/extraction/", nil) |
| 128 | + if err != nil { |
| 129 | + return diag.Errorf("cannot create request: %s", err) |
| 130 | + } |
| 131 | + |
| 132 | + body, err := client.doReq(req) |
| 133 | + if err != nil { |
| 134 | + return diag.Errorf("cannot send request: %s", err) |
| 135 | + } |
| 136 | + |
| 137 | + var response []map[string]interface{} |
| 138 | + err = json.Unmarshal(body, &response) |
| 139 | + if err != nil { |
| 140 | + return diag.Errorf("cannot unmarshal response: %s", err) |
| 141 | + } |
| 142 | + |
| 143 | + idFloat, err := strconv.ParseFloat(id, 64) |
| 144 | + if err != nil { |
| 145 | + return diag.Errorf("cannot parse id: %s", err) |
| 146 | + } |
| 147 | + |
| 148 | + for _, extraction := range response { |
| 149 | + if extraction["id"] == idFloat { |
| 150 | + d.SetId(id) |
| 151 | + d.Set("name", extraction["name"]) |
| 152 | + d.Set("description", extraction["description"]) |
| 153 | + d.Set("priority", extraction["priority"]) |
| 154 | + d.Set("attribute", extraction["attribute"]) |
| 155 | + d.Set("condition", extraction["condition"]) |
| 156 | + d.Set("disabled", extraction["disabled"]) |
| 157 | + d.Set("regex", extraction["regex"]) |
| 158 | + d.Set("pre", extraction["pre"]) |
| 159 | + break |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + return nil |
| 164 | +} |
| 165 | + |
| 166 | +func resourceUpdateExtraction(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 167 | + client := m.(*Client) |
| 168 | + id := d.Id() |
| 169 | + |
| 170 | + // Prepare the payload for the extraction update request |
| 171 | + extractionUpdatePayload := map[string]interface{}{ |
| 172 | + "name": d.Get("name").(string), |
| 173 | + "description": d.Get("description").(string), |
| 174 | + "priority": d.Get("priority").(int), |
| 175 | + "attribute": d.Get("attribute").(string), |
| 176 | + "condition": d.Get("condition").(string), |
| 177 | + "disabled": d.Get("disabled").(bool), |
| 178 | + "regex": d.Get("regex").(string), |
| 179 | + "pre": d.Get("pre").(bool), |
| 180 | + } |
| 181 | + |
| 182 | + if !d.HasChange("name") || !d.HasChange("description") || !d.HasChange("priority") || !d.HasChange("attribute") || !d.HasChange("condition") || !d.HasChange("disabled") || !d.HasChange("regex") || !d.HasChange("pre") { |
| 183 | + return nil |
| 184 | + } |
| 185 | + |
| 186 | + // Marshal the payload |
| 187 | + payload, err := json.Marshal(extractionUpdatePayload) |
| 188 | + if err != nil { |
| 189 | + return diag.Errorf("cannot marshal payload: %s", err) |
| 190 | + } |
| 191 | + |
| 192 | + // Create a new request |
| 193 | + req, err := http.NewRequest("PUT", fmt.Sprintf("%s/extraction/%s", client.HostURL, id), strings.NewReader(string(payload))) |
| 194 | + if err != nil { |
| 195 | + return diag.Errorf("cannot create request: %s", err) |
| 196 | + } |
| 197 | + |
| 198 | + // Do the request |
| 199 | + body, err := client.doReq(req) |
| 200 | + if err != nil { |
| 201 | + return diag.Errorf("cannot send request: %s", err) |
| 202 | + } |
| 203 | + |
| 204 | + // Parse the response |
| 205 | + var response map[string]interface{} |
| 206 | + err = json.Unmarshal(body, &response) |
| 207 | + if err != nil { |
| 208 | + return diag.Errorf("cannot parse response: %s", err) |
| 209 | + } |
| 210 | + |
| 211 | + d.SetId(id) |
| 212 | + |
| 213 | + return nil |
| 214 | +} |
| 215 | + |
| 216 | +func resourceDeleteExtraction(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 217 | + client := m.(*Client) |
| 218 | + |
| 219 | + id := d.Id() |
| 220 | + |
| 221 | + req, err := http.NewRequest("DELETE", client.HostURL+"/extraction/"+id, nil) |
| 222 | + if err != nil { |
| 223 | + return diag.Errorf("cannot create request: %s", err) |
| 224 | + } |
| 225 | + |
| 226 | + _, err = client.doReq(req) |
| 227 | + if err != nil { |
| 228 | + return diag.Errorf("cannot send request: %s", err) |
| 229 | + } |
| 230 | + |
| 231 | + return nil |
| 232 | +} |
0 commit comments