|
| 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 | +// CreateListIndex creates the .lists and .items data streams for a space if they don't exist. |
| 13 | +// This is required before any list operations can be performed. |
| 14 | +func CreateListIndex(ctx context.Context, client *Client, spaceId string) diag.Diagnostics { |
| 15 | + resp, err := client.API.CreateListIndexWithResponse(ctx, kbapi.SpaceId(spaceId)) |
| 16 | + if err != nil { |
| 17 | + return diagutil.FrameworkDiagFromError(err) |
| 18 | + } |
| 19 | + |
| 20 | + switch resp.StatusCode() { |
| 21 | + case http.StatusOK: |
| 22 | + return nil |
| 23 | + default: |
| 24 | + return reportUnknownError(resp.StatusCode(), resp.Body) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +// 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 | + resp, err := client.API.ReadListWithResponse(ctx, kbapi.SpaceId(spaceId), params) |
| 31 | + if err != nil { |
| 32 | + return nil, diagutil.FrameworkDiagFromError(err) |
| 33 | + } |
| 34 | + |
| 35 | + switch resp.StatusCode() { |
| 36 | + case http.StatusOK: |
| 37 | + return resp, nil |
| 38 | + case http.StatusNotFound: |
| 39 | + return nil, nil |
| 40 | + default: |
| 41 | + return nil, reportUnknownError(resp.StatusCode(), resp.Body) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// CreateList creates a new security list. |
| 46 | +func CreateList(ctx context.Context, client *Client, spaceId string, body kbapi.CreateListJSONRequestBody) (*kbapi.CreateListResponse, diag.Diagnostics) { |
| 47 | + resp, err := client.API.CreateListWithResponse(ctx, kbapi.SpaceId(spaceId), body) |
| 48 | + if err != nil { |
| 49 | + return nil, diagutil.FrameworkDiagFromError(err) |
| 50 | + } |
| 51 | + |
| 52 | + switch resp.StatusCode() { |
| 53 | + case http.StatusOK: |
| 54 | + return resp, nil |
| 55 | + default: |
| 56 | + return nil, reportUnknownError(resp.StatusCode(), resp.Body) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// UpdateList updates an existing security list. |
| 61 | +func UpdateList(ctx context.Context, client *Client, spaceId string, body kbapi.UpdateListJSONRequestBody) (*kbapi.UpdateListResponse, diag.Diagnostics) { |
| 62 | + resp, err := client.API.UpdateListWithResponse(ctx, kbapi.SpaceId(spaceId), body) |
| 63 | + if err != nil { |
| 64 | + return nil, diagutil.FrameworkDiagFromError(err) |
| 65 | + } |
| 66 | + |
| 67 | + switch resp.StatusCode() { |
| 68 | + case http.StatusOK: |
| 69 | + return resp, nil |
| 70 | + default: |
| 71 | + return nil, reportUnknownError(resp.StatusCode(), resp.Body) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// DeleteList deletes an existing security list. |
| 76 | +func DeleteList(ctx context.Context, client *Client, spaceId string, params *kbapi.DeleteListParams) diag.Diagnostics { |
| 77 | + resp, err := client.API.DeleteListWithResponse(ctx, kbapi.SpaceId(spaceId), params) |
| 78 | + if err != nil { |
| 79 | + return diagutil.FrameworkDiagFromError(err) |
| 80 | + } |
| 81 | + |
| 82 | + switch resp.StatusCode() { |
| 83 | + case http.StatusOK: |
| 84 | + return nil |
| 85 | + case http.StatusNotFound: |
| 86 | + return nil |
| 87 | + default: |
| 88 | + return reportUnknownError(resp.StatusCode(), resp.Body) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// 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) { |
| 94 | + resp, err := client.API.ReadListItemWithResponse(ctx, kbapi.SpaceId(spaceId), params) |
| 95 | + if err != nil { |
| 96 | + return nil, diagutil.FrameworkDiagFromError(err) |
| 97 | + } |
| 98 | + |
| 99 | + switch resp.StatusCode() { |
| 100 | + case http.StatusOK: |
| 101 | + return resp, nil |
| 102 | + case http.StatusNotFound: |
| 103 | + return nil, nil |
| 104 | + default: |
| 105 | + return nil, reportUnknownError(resp.StatusCode(), resp.Body) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +// CreateListItem creates a new security list item. |
| 110 | +func CreateListItem(ctx context.Context, client *Client, spaceId string, body kbapi.CreateListItemJSONRequestBody) (*kbapi.CreateListItemResponse, diag.Diagnostics) { |
| 111 | + resp, err := client.API.CreateListItemWithResponse(ctx, kbapi.SpaceId(spaceId), body) |
| 112 | + if err != nil { |
| 113 | + return nil, diagutil.FrameworkDiagFromError(err) |
| 114 | + } |
| 115 | + |
| 116 | + switch resp.StatusCode() { |
| 117 | + case http.StatusOK: |
| 118 | + return resp, nil |
| 119 | + default: |
| 120 | + return nil, reportUnknownError(resp.StatusCode(), resp.Body) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +// UpdateListItem updates an existing security list item. |
| 125 | +func UpdateListItem(ctx context.Context, client *Client, spaceId string, body kbapi.UpdateListItemJSONRequestBody) (*kbapi.UpdateListItemResponse, diag.Diagnostics) { |
| 126 | + resp, err := client.API.UpdateListItemWithResponse(ctx, kbapi.SpaceId(spaceId), body) |
| 127 | + if err != nil { |
| 128 | + return nil, diagutil.FrameworkDiagFromError(err) |
| 129 | + } |
| 130 | + |
| 131 | + switch resp.StatusCode() { |
| 132 | + case http.StatusOK: |
| 133 | + return resp, nil |
| 134 | + default: |
| 135 | + return nil, reportUnknownError(resp.StatusCode(), resp.Body) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +// DeleteListItem deletes an existing security list item. |
| 140 | +func DeleteListItem(ctx context.Context, client *Client, spaceId string, params *kbapi.DeleteListItemParams) diag.Diagnostics { |
| 141 | + resp, err := client.API.DeleteListItemWithResponse(ctx, kbapi.SpaceId(spaceId), params) |
| 142 | + if err != nil { |
| 143 | + return diagutil.FrameworkDiagFromError(err) |
| 144 | + } |
| 145 | + |
| 146 | + switch resp.StatusCode() { |
| 147 | + case http.StatusOK: |
| 148 | + return nil |
| 149 | + case http.StatusNotFound: |
| 150 | + return nil |
| 151 | + default: |
| 152 | + return reportUnknownError(resp.StatusCode(), resp.Body) |
| 153 | + } |
| 154 | +} |
0 commit comments