Skip to content

Commit 3999676

Browse files
authored
feat: added extraction resource (#30)
* feat: added extraction resource
1 parent 02caef5 commit 3999676

8 files changed

+277
-8
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
!Makefile
2727
!.goreleaser.yml
2828
!.pre-commit-config.yaml
29+
!assets/**
2930

3031
# ...even if they are in subdirectories
3132
!*/

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<picture>
2-
<img align="right" height="54" src="https://assets-global.website-files.com/651fbba3d2d2f809dffbe9c5/6524099c11a532d90a7576c2_Keep%20Logo.png">
2+
<img align="right" height="54" src="assets/keep-logo.png">
33
</picture>
44

55
# terraform-provider-keep

assets/keep-logo.png

8.97 KB
Loading

docs/resources/extraction.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "keep_extraction Resource - terraform-provider-keep"
4+
subcategory: ""
5+
description: |-
6+
7+
---
8+
9+
# keep_extraction (Resource)
10+
11+
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `attribute` (String) Attribute of the extraction
21+
- `disabled` (Boolean)
22+
- `name` (String) Name of the extraction
23+
- `pre` (Boolean) Pre of the extraction
24+
- `regex` (String) Regex of the extraction
25+
26+
### Optional
27+
28+
- `condition` (String) Condition of the extraction
29+
- `description` (String) Description of the extraction
30+
- `priority` (Number) Priority of the extraction
31+
32+
### Read-Only
33+
34+
- `id` (String) ID of the extraction

docs/resources/mapping.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ description: |-
2020
- `mapping_file_path` (String) Path of the mapping file
2121
- `matchers` (Set of String) List of matchers
2222
- `name` (String) Name of the mapping
23+
- `priority` (Number) Priority of the mapping
2324

2425
### Optional
2526

2627
- `description` (String) Description of the mapping
27-
- `priority` (Number) Priority of the mapping
2828

2929
### Read-Only
3030

31-
- `id` (String) The ID of this resource.
31+
- `id` (String) ID of the mapping

keep/provider.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ func Provider() *schema.Provider {
2222
},
2323
},
2424
ResourcesMap: map[string]*schema.Resource{
25-
"keep_provider": resourceProvider(),
26-
"keep_workflow": resourceWorkflow(),
27-
"keep_mapping": resourceMapping(),
25+
"keep_provider": resourceProvider(),
26+
"keep_workflow": resourceWorkflow(),
27+
"keep_mapping": resourceMapping(),
28+
"keep_extraction": resourceExtraction(),
2829
},
2930
DataSourcesMap: map[string]*schema.Resource{
3031
"keep_workflow": dataSourceWorkflows(),

keep/resource_extraction.go

+232
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
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+
}

keep/resource_mapping.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ func resourceMapping() *schema.Resource {
4848
},
4949
"priority": {
5050
Type: schema.TypeInt,
51-
Optional: true,
51+
Required: true,
5252
Description: "Priority of the mapping",
53+
Default: 0,
5354
},
5455
"mapping_file_path": {
5556
Type: schema.TypeString,
@@ -115,7 +116,7 @@ func resourceCreateMapping(ctx context.Context, d *schema.ResourceData, m interf
115116
// marshal body
116117
bodyBytes, err := json.Marshal(body)
117118
if err != nil {
118-
return diag.Errorf("cannot request marshal body: %s", err)
119+
return diag.Errorf("cannot marshal mapping body: %s", err)
119120
}
120121

121122
// create mapping

0 commit comments

Comments
 (0)