Skip to content

Commit e385ba4

Browse files
committed
Add exceptions client helper
1 parent f29832b commit e385ba4

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package kibana_oapi
2+
3+
import (
4+
"context"
5+
"net/http"
6+
7+
"github.com/elastic/terraform-provider-elasticstack/generated/kbapi"
8+
"github.com/elastic/terraform-provider-elasticstack/internal/diagutil"
9+
"github.com/hashicorp/terraform-plugin-framework/diag"
10+
)
11+
12+
// GetExceptionList reads an exception list from the API by ID or list_id
13+
func GetExceptionList(ctx context.Context, client *Client, spaceId string, params *kbapi.ReadExceptionListParams) (*kbapi.ReadExceptionListResponse, diag.Diagnostics) {
14+
resp, err := client.API.ReadExceptionListWithResponse(ctx, kbapi.SpaceId(spaceId), params)
15+
if err != nil {
16+
return nil, diagutil.FrameworkDiagFromError(err)
17+
}
18+
19+
switch resp.StatusCode() {
20+
case http.StatusOK:
21+
return resp, nil
22+
case http.StatusNotFound:
23+
return nil, nil
24+
default:
25+
return nil, reportUnknownError(resp.StatusCode(), resp.Body)
26+
}
27+
}
28+
29+
// CreateExceptionList creates a new exception list.
30+
func CreateExceptionList(ctx context.Context, client *Client, spaceId string, body kbapi.CreateExceptionListJSONRequestBody) (*kbapi.CreateExceptionListResponse, diag.Diagnostics) {
31+
resp, err := client.API.CreateExceptionListWithResponse(ctx, kbapi.SpaceId(spaceId), body)
32+
if err != nil {
33+
return nil, diagutil.FrameworkDiagFromError(err)
34+
}
35+
36+
switch resp.StatusCode() {
37+
case http.StatusOK:
38+
return resp, nil
39+
default:
40+
return nil, reportUnknownError(resp.StatusCode(), resp.Body)
41+
}
42+
}
43+
44+
// UpdateExceptionList updates an existing exception list.
45+
func UpdateExceptionList(ctx context.Context, client *Client, spaceId string, body kbapi.UpdateExceptionListJSONRequestBody) (*kbapi.UpdateExceptionListResponse, diag.Diagnostics) {
46+
resp, err := client.API.UpdateExceptionListWithResponse(ctx, kbapi.SpaceId(spaceId), body)
47+
if err != nil {
48+
return nil, diagutil.FrameworkDiagFromError(err)
49+
}
50+
51+
switch resp.StatusCode() {
52+
case http.StatusOK:
53+
return resp, nil
54+
default:
55+
return nil, reportUnknownError(resp.StatusCode(), resp.Body)
56+
}
57+
}
58+
59+
// DeleteExceptionList deletes an existing exception list.
60+
func DeleteExceptionList(ctx context.Context, client *Client, spaceId string, params *kbapi.DeleteExceptionListParams) diag.Diagnostics {
61+
resp, err := client.API.DeleteExceptionListWithResponse(ctx, kbapi.SpaceId(spaceId), params)
62+
if err != nil {
63+
return diagutil.FrameworkDiagFromError(err)
64+
}
65+
66+
switch resp.StatusCode() {
67+
case http.StatusOK:
68+
return nil
69+
case http.StatusNotFound:
70+
return nil
71+
default:
72+
return reportUnknownError(resp.StatusCode(), resp.Body)
73+
}
74+
}
75+
76+
// GetExceptionListItem reads an exception list item from the API by ID or item_id
77+
func GetExceptionListItem(ctx context.Context, client *Client, spaceId string, params *kbapi.ReadExceptionListItemParams) (*kbapi.ReadExceptionListItemResponse, diag.Diagnostics) {
78+
resp, err := client.API.ReadExceptionListItemWithResponse(ctx, kbapi.SpaceId(spaceId), params)
79+
if err != nil {
80+
return nil, diagutil.FrameworkDiagFromError(err)
81+
}
82+
83+
switch resp.StatusCode() {
84+
case http.StatusOK:
85+
return resp, nil
86+
case http.StatusNotFound:
87+
return nil, nil
88+
default:
89+
return nil, reportUnknownError(resp.StatusCode(), resp.Body)
90+
}
91+
}
92+
93+
// CreateExceptionListItem creates a new exception list item.
94+
func CreateExceptionListItem(ctx context.Context, client *Client, spaceId string, body kbapi.CreateExceptionListItemJSONRequestBody) (*kbapi.CreateExceptionListItemResponse, diag.Diagnostics) {
95+
resp, err := client.API.CreateExceptionListItemWithResponse(ctx, kbapi.SpaceId(spaceId), body)
96+
if err != nil {
97+
return nil, diagutil.FrameworkDiagFromError(err)
98+
}
99+
100+
switch resp.StatusCode() {
101+
case http.StatusOK:
102+
return resp, nil
103+
default:
104+
return nil, reportUnknownError(resp.StatusCode(), resp.Body)
105+
}
106+
}
107+
108+
// UpdateExceptionListItem updates an existing exception list item.
109+
func UpdateExceptionListItem(ctx context.Context, client *Client, spaceId string, body kbapi.UpdateExceptionListItemJSONRequestBody) (*kbapi.UpdateExceptionListItemResponse, diag.Diagnostics) {
110+
resp, err := client.API.UpdateExceptionListItemWithResponse(ctx, kbapi.SpaceId(spaceId), body)
111+
if err != nil {
112+
return nil, diagutil.FrameworkDiagFromError(err)
113+
}
114+
115+
switch resp.StatusCode() {
116+
case http.StatusOK:
117+
return resp, nil
118+
default:
119+
return nil, reportUnknownError(resp.StatusCode(), resp.Body)
120+
}
121+
}
122+
123+
// DeleteExceptionListItem deletes an existing exception list item.
124+
func DeleteExceptionListItem(ctx context.Context, client *Client, spaceId string, params *kbapi.DeleteExceptionListItemParams) diag.Diagnostics {
125+
resp, err := client.API.DeleteExceptionListItemWithResponse(ctx, kbapi.SpaceId(spaceId), params)
126+
if err != nil {
127+
return diagutil.FrameworkDiagFromError(err)
128+
}
129+
130+
switch resp.StatusCode() {
131+
case http.StatusOK:
132+
return nil
133+
case http.StatusNotFound:
134+
return nil
135+
default:
136+
return reportUnknownError(resp.StatusCode(), resp.Body)
137+
}
138+
}

0 commit comments

Comments
 (0)