@@ -2,6 +2,7 @@ package kibana_oapi
22
33import (
44 "context"
5+ "encoding/json"
56 "net/http"
67
78 "github.com/elastic/terraform-provider-elasticstack/generated/kbapi"
@@ -26,15 +27,20 @@ func CreateListIndex(ctx context.Context, client *Client, spaceId string) diag.D
2627}
2728
2829// GetList reads a security list from the API by ID
29- func GetList (ctx context.Context , client * Client , spaceId string , params * kbapi.ReadListParams ) (* kbapi.ReadListResponse , diag.Diagnostics ) {
30+ func GetList (ctx context.Context , client * Client , spaceId string , params * kbapi.ReadListParams ) (* kbapi.SecurityListsAPIList , diag.Diagnostics ) {
3031 resp , err := client .API .ReadListWithResponse (ctx , kbapi .SpaceId (spaceId ), params )
3132 if err != nil {
3233 return nil , diagutil .FrameworkDiagFromError (err )
3334 }
3435
3536 switch resp .StatusCode () {
3637 case http .StatusOK :
37- return resp , nil
38+ if resp .JSON200 == nil {
39+ return nil , diag.Diagnostics {
40+ diag .NewErrorDiagnostic ("Failed to parse list response" , "API returned 200 but JSON200 is nil" ),
41+ }
42+ }
43+ return resp .JSON200 , nil
3844 case http .StatusNotFound :
3945 return nil , nil
4046 default :
@@ -43,30 +49,40 @@ func GetList(ctx context.Context, client *Client, spaceId string, params *kbapi.
4349}
4450
4551// CreateList creates a new security list.
46- func CreateList (ctx context.Context , client * Client , spaceId string , body kbapi.CreateListJSONRequestBody ) (* kbapi.CreateListResponse , diag.Diagnostics ) {
52+ func CreateList (ctx context.Context , client * Client , spaceId string , body kbapi.CreateListJSONRequestBody ) (* kbapi.SecurityListsAPIList , diag.Diagnostics ) {
4753 resp , err := client .API .CreateListWithResponse (ctx , kbapi .SpaceId (spaceId ), body )
4854 if err != nil {
4955 return nil , diagutil .FrameworkDiagFromError (err )
5056 }
5157
5258 switch resp .StatusCode () {
5359 case http .StatusOK :
54- return resp , nil
60+ if resp .JSON200 == nil {
61+ return nil , diag.Diagnostics {
62+ diag .NewErrorDiagnostic ("Failed to parse list response" , "API returned 200 but JSON200 is nil" ),
63+ }
64+ }
65+ return resp .JSON200 , nil
5566 default :
5667 return nil , reportUnknownError (resp .StatusCode (), resp .Body )
5768 }
5869}
5970
6071// UpdateList updates an existing security list.
61- func UpdateList (ctx context.Context , client * Client , spaceId string , body kbapi.UpdateListJSONRequestBody ) (* kbapi.UpdateListResponse , diag.Diagnostics ) {
72+ func UpdateList (ctx context.Context , client * Client , spaceId string , body kbapi.UpdateListJSONRequestBody ) (* kbapi.SecurityListsAPIList , diag.Diagnostics ) {
6273 resp , err := client .API .UpdateListWithResponse (ctx , kbapi .SpaceId (spaceId ), body )
6374 if err != nil {
6475 return nil , diagutil .FrameworkDiagFromError (err )
6576 }
6677
6778 switch resp .StatusCode () {
6879 case http .StatusOK :
69- return resp , nil
80+ if resp .JSON200 == nil {
81+ return nil , diag.Diagnostics {
82+ diag .NewErrorDiagnostic ("Failed to parse list response" , "API returned 200 but JSON200 is nil" ),
83+ }
84+ }
85+ return resp .JSON200 , nil
7086 default :
7187 return nil , reportUnknownError (resp .StatusCode (), resp .Body )
7288 }
@@ -90,15 +106,24 @@ func DeleteList(ctx context.Context, client *Client, spaceId string, params *kba
90106}
91107
92108// GetListItem reads a security list item from the API by ID or list_id and value
93- func GetListItem (ctx context.Context , client * Client , spaceId string , params * kbapi.ReadListItemParams ) (* kbapi.ReadListItemResponse , diag.Diagnostics ) {
109+ // The response can be a single item or an array, so we unmarshal from the body.
110+ // When querying by ID, we expect a single item.
111+ func GetListItem (ctx context.Context , client * Client , spaceId string , params * kbapi.ReadListItemParams ) (* kbapi.SecurityListsAPIListItem , diag.Diagnostics ) {
94112 resp , err := client .API .ReadListItemWithResponse (ctx , kbapi .SpaceId (spaceId ), params )
95113 if err != nil {
96114 return nil , diagutil .FrameworkDiagFromError (err )
97115 }
98116
99117 switch resp .StatusCode () {
100118 case http .StatusOK :
101- return resp , nil
119+ var listItem kbapi.SecurityListsAPIListItem
120+ if err := json .Unmarshal (resp .Body , & listItem ); err != nil {
121+ return nil , diag.Diagnostics {
122+ diag .NewErrorDiagnostic ("Failed to parse list item response" , err .Error ()),
123+ }
124+ }
125+
126+ return & listItem , nil
102127 case http .StatusNotFound :
103128 return nil , nil
104129 default :
@@ -107,30 +132,40 @@ func GetListItem(ctx context.Context, client *Client, spaceId string, params *kb
107132}
108133
109134// CreateListItem creates a new security list item.
110- func CreateListItem (ctx context.Context , client * Client , spaceId string , body kbapi.CreateListItemJSONRequestBody ) (* kbapi.CreateListItemResponse , diag.Diagnostics ) {
135+ func CreateListItem (ctx context.Context , client * Client , spaceId string , body kbapi.CreateListItemJSONRequestBody ) (* kbapi.SecurityListsAPIListItem , diag.Diagnostics ) {
111136 resp , err := client .API .CreateListItemWithResponse (ctx , kbapi .SpaceId (spaceId ), body )
112137 if err != nil {
113138 return nil , diagutil .FrameworkDiagFromError (err )
114139 }
115140
116141 switch resp .StatusCode () {
117142 case http .StatusOK :
118- return resp , nil
143+ if resp .JSON200 == nil {
144+ return nil , diag.Diagnostics {
145+ diag .NewErrorDiagnostic ("Failed to parse list item response" , "API returned 200 but JSON200 is nil" ),
146+ }
147+ }
148+ return resp .JSON200 , nil
119149 default :
120150 return nil , reportUnknownError (resp .StatusCode (), resp .Body )
121151 }
122152}
123153
124154// UpdateListItem updates an existing security list item.
125- func UpdateListItem (ctx context.Context , client * Client , spaceId string , body kbapi.UpdateListItemJSONRequestBody ) (* kbapi.UpdateListItemResponse , diag.Diagnostics ) {
155+ func UpdateListItem (ctx context.Context , client * Client , spaceId string , body kbapi.UpdateListItemJSONRequestBody ) (* kbapi.SecurityListsAPIListItem , diag.Diagnostics ) {
126156 resp , err := client .API .UpdateListItemWithResponse (ctx , kbapi .SpaceId (spaceId ), body )
127157 if err != nil {
128158 return nil , diagutil .FrameworkDiagFromError (err )
129159 }
130160
131161 switch resp .StatusCode () {
132162 case http .StatusOK :
133- return resp , nil
163+ if resp .JSON200 == nil {
164+ return nil , diag.Diagnostics {
165+ diag .NewErrorDiagnostic ("Failed to parse list item response" , "API returned 200 but JSON200 is nil" ),
166+ }
167+ }
168+ return resp .JSON200 , nil
134169 default :
135170 return nil , reportUnknownError (resp .StatusCode (), resp .Body )
136171 }
0 commit comments