Skip to content

Commit 13fe2ab

Browse files
committed
Migrate elasticstack_kibana_action_connector resource to the plugin framework
1 parent 3f6be4b commit 13fe2ab

File tree

20 files changed

+1173
-1964
lines changed

20 files changed

+1173
-1964
lines changed

docs/resources/kibana_action_connector.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,29 @@ resource "elasticstack_kibana_action_connector" "slack-api-connector" {
6666

6767
- `config` (String) The configuration for the connector. Configuration properties vary depending on the connector type.
6868
- `connector_id` (String) A UUID v1 or v4 to use instead of a randomly generated ID.
69+
- `kibana_connection` (Block List) Kibana connection configuration block. (see [below for nested schema](#nestedblock--kibana_connection))
6970
- `secrets` (String, Sensitive) The secrets configuration for the connector. Secrets configuration properties vary depending on the connector type.
7071
- `space_id` (String) An identifier for the space. If space_id is not provided, the default space is used.
7172

7273
### Read-Only
7374

74-
- `id` (String) The ID of this resource.
75+
- `id` (String) Internal identifier of the resource.
7576
- `is_deprecated` (Boolean) Indicates whether the connector type is deprecated.
7677
- `is_missing_secrets` (Boolean) Indicates whether secrets are missing for the connector.
7778
- `is_preconfigured` (Boolean) Indicates whether it is a preconfigured connector.
7879

80+
<a id="nestedblock--kibana_connection"></a>
81+
### Nested Schema for `kibana_connection`
82+
83+
Optional:
84+
85+
- `api_key` (String, Sensitive) API Key to use for authentication to Kibana
86+
- `ca_certs` (List of String) A list of paths to CA certificates to validate the certificate presented by the Kibana server.
87+
- `endpoints` (List of String, Sensitive) A comma-separated list of endpoints where the terraform provider will point to, this must include the http(s) schema and port number.
88+
- `insecure` (Boolean) Disable TLS certificate validation
89+
- `password` (String, Sensitive) Password to use for API authentication to Kibana.
90+
- `username` (String) Username to use for API authentication to Kibana.
91+
7992
## Import
8093

8194
Import is supported using the following syntax:

internal/clients/kibana_oapi/connector.go

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ import (
1111
"github.com/elastic/terraform-provider-elasticstack/generated/kbapi"
1212
"github.com/elastic/terraform-provider-elasticstack/internal/models"
1313
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
14+
fwdiag "github.com/hashicorp/terraform-plugin-framework/diag"
1415
"github.com/hashicorp/terraform-plugin-log/tflog"
15-
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
16+
sdkdiag "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1617
)
1718

18-
func CreateConnector(ctx context.Context, client *Client, connectorOld models.KibanaActionConnector) (string, diag.Diagnostics) {
19+
func CreateConnector(ctx context.Context, client *Client, connectorOld models.KibanaActionConnector) (string, fwdiag.Diagnostics) {
1920
body, err := createConnectorRequestBody(connectorOld)
2021
if err != nil {
21-
return "", diag.FromErr(err)
22+
return "", fwdiag.Diagnostics{fwdiag.NewErrorDiagnostic("Failed to create connector request body", err.Error())}
2223
}
2324

2425
resp, err := client.API.PostActionsConnectorIdWithResponse(
@@ -35,40 +36,40 @@ func CreateConnector(ctx context.Context, client *Client, connectorOld models.Ki
3536
},
3637
)
3738
if err != nil {
38-
return "", diag.FromErr(err)
39+
return "", fwdiag.Diagnostics{fwdiag.NewErrorDiagnostic("HTTP request failed", err.Error())}
3940
}
4041

4142
switch resp.StatusCode() {
4243
case http.StatusOK:
4344
return resp.JSON200.Id, nil
4445
default:
45-
return "", reportUnknownErrorSDK(resp.StatusCode(), resp.Body)
46+
return "", reportUnknownError(resp.StatusCode(), resp.Body)
4647
}
4748
}
4849

49-
func UpdateConnector(ctx context.Context, client *Client, connectorOld models.KibanaActionConnector) (string, diag.Diagnostics) {
50+
func UpdateConnector(ctx context.Context, client *Client, connectorOld models.KibanaActionConnector) (string, fwdiag.Diagnostics) {
5051
body, err := updateConnectorRequestBody(connectorOld)
5152
if err != nil {
52-
return "", diag.FromErr(err)
53+
return "", fwdiag.Diagnostics{fwdiag.NewErrorDiagnostic("Failed to create update request body", err.Error())}
5354
}
5455

5556
resp, err := client.API.PutActionsConnectorIdWithResponse(ctx, connectorOld.SpaceID, connectorOld.ConnectorID, body)
5657
if err != nil {
57-
return "", diag.Errorf("unable to update connector: [%v]", err)
58+
return "", fwdiag.Diagnostics{fwdiag.NewErrorDiagnostic("Unable to update connector", err.Error())}
5859
}
5960

6061
switch resp.StatusCode() {
6162
case http.StatusOK:
6263
return resp.JSON200.Id, nil
6364
default:
64-
return "", reportUnknownErrorSDK(resp.StatusCode(), resp.Body)
65+
return "", reportUnknownError(resp.StatusCode(), resp.Body)
6566
}
6667
}
6768

68-
func GetConnector(ctx context.Context, client *Client, connectorID, spaceID string) (*models.KibanaActionConnector, diag.Diagnostics) {
69+
func GetConnector(ctx context.Context, client *Client, connectorID, spaceID string) (*models.KibanaActionConnector, fwdiag.Diagnostics) {
6970
resp, err := client.API.GetActionsConnectorIdWithResponse(ctx, spaceID, connectorID)
7071
if err != nil {
71-
return nil, diag.Errorf("unable to get connector: [%v]", err)
72+
return nil, fwdiag.Diagnostics{fwdiag.NewErrorDiagnostic("Unable to get connector", err.Error())}
7273
}
7374

7475
switch resp.StatusCode() {
@@ -77,14 +78,14 @@ func GetConnector(ctx context.Context, client *Client, connectorID, spaceID stri
7778
case http.StatusNotFound:
7879
return nil, nil
7980
default:
80-
return nil, reportUnknownErrorSDK(resp.StatusCode(), resp.Body)
81+
return nil, reportUnknownError(resp.StatusCode(), resp.Body)
8182
}
8283
}
8384

84-
func SearchConnectors(ctx context.Context, client *Client, connectorName, spaceID, connectorTypeID string) ([]*models.KibanaActionConnector, diag.Diagnostics) {
85+
func SearchConnectors(ctx context.Context, client *Client, connectorName, spaceID, connectorTypeID string) ([]*models.KibanaActionConnector, sdkdiag.Diagnostics) {
8586
resp, err := client.API.GetActionsConnectorsWithResponse(ctx, spaceID)
8687
if err != nil {
87-
return nil, diag.Errorf("unable to get connectors: [%v]", err)
88+
return nil, sdkdiag.Errorf("unable to get connectors: [%v]", err)
8889
}
8990

9091
if resp.StatusCode() != http.StatusOK {
@@ -101,9 +102,9 @@ func SearchConnectors(ctx context.Context, client *Client, connectorName, spaceI
101102
continue
102103
}
103104

104-
c, diags := ConnectorResponseToModel(spaceID, &connector)
105-
if diags.HasError() {
106-
return nil, diags
105+
c, fwDiags := ConnectorResponseToModel(spaceID, &connector)
106+
if fwDiags.HasError() {
107+
return nil, utils.SDKDiagsFromFramework(fwDiags)
107108
}
108109

109110
foundConnectors = append(foundConnectors, c)
@@ -115,9 +116,9 @@ func SearchConnectors(ctx context.Context, client *Client, connectorName, spaceI
115116
return foundConnectors, nil
116117
}
117118

118-
func ConnectorResponseToModel(spaceID string, connector *kbapi.ConnectorResponse) (*models.KibanaActionConnector, diag.Diagnostics) {
119+
func ConnectorResponseToModel(spaceID string, connector *kbapi.ConnectorResponse) (*models.KibanaActionConnector, fwdiag.Diagnostics) {
119120
if connector == nil {
120-
return nil, diag.Errorf("connector response is nil")
121+
return nil, fwdiag.Diagnostics{fwdiag.NewErrorDiagnostic("Invalid connector response", "connector response is nil")}
121122
}
122123

123124
var configJSON []byte
@@ -132,7 +133,7 @@ func ConnectorResponseToModel(spaceID string, connector *kbapi.ConnectorResponse
132133
var err error
133134
configJSON, err = json.Marshal(configMap)
134135
if err != nil {
135-
return nil, diag.Errorf("unable to marshal config: %v", err)
136+
return nil, fwdiag.Diagnostics{fwdiag.NewErrorDiagnostic("Unable to marshal config", err.Error())}
136137
}
137138

138139
// If we have a specific config type, marshal into and out of that to
@@ -141,7 +142,7 @@ func ConnectorResponseToModel(spaceID string, connector *kbapi.ConnectorResponse
141142
if ok {
142143
configJSONString, err := handler.remarshalConfig(string(configJSON))
143144
if err != nil {
144-
return nil, diag.Errorf("failed to remarshal config: %v", err)
145+
return nil, fwdiag.Diagnostics{fwdiag.NewErrorDiagnostic("Failed to remarshal config", err.Error())}
145146
}
146147

147148
configJSON = []byte(configJSONString)
@@ -165,21 +166,21 @@ func ConnectorResponseToModel(spaceID string, connector *kbapi.ConnectorResponse
165166
return model, nil
166167
}
167168

168-
func DeleteConnector(ctx context.Context, client *Client, connectorID string, spaceID string) diag.Diagnostics {
169+
func DeleteConnector(ctx context.Context, client *Client, connectorID string, spaceID string) fwdiag.Diagnostics {
169170
resp, err := client.API.DeleteActionsConnectorIdWithResponse(ctx, spaceID, connectorID)
170171
if err != nil {
171-
return diag.Errorf("unable to delete connector: [%v]", err)
172+
return fwdiag.Diagnostics{fwdiag.NewErrorDiagnostic("Unable to delete connector", err.Error())}
172173
}
173174

174175
if resp.StatusCode() != http.StatusOK && resp.StatusCode() != http.StatusNoContent {
175-
return reportUnknownErrorSDK(resp.StatusCode(), resp.Body)
176+
return reportUnknownError(resp.StatusCode(), resp.Body)
176177
}
177178

178179
return nil
179180
}
180181

181182
type connectorConfigHandler struct {
182-
defaults func(plan, backend string) (string, error)
183+
defaults func(plan string) (string, error)
183184
remarshalConfig func(config string) (string, error)
184185
}
185186

@@ -246,13 +247,13 @@ var connectorConfigHandlers = map[string]connectorConfigHandler{
246247
},
247248
}
248249

249-
func ConnectorConfigWithDefaults(connectorTypeID, plan, backend, state string) (string, error) {
250+
func ConnectorConfigWithDefaults(connectorTypeID, plan string) (string, error) {
250251
handler, ok := connectorConfigHandlers[connectorTypeID]
251252
if !ok {
252253
return plan, errors.New("unknown connector type ID: " + connectorTypeID)
253254
}
254255

255-
return handler.defaults(plan, backend)
256+
return handler.defaults(plan)
256257
}
257258

258259
// User can omit optonal fields in config JSON.
@@ -271,7 +272,7 @@ func remarshalConfig[T any](plan string) (string, error) {
271272
return string(customJSON), nil
272273
}
273274

274-
func connectorConfigWithDefaultsCasesWebhook(plan, _ string) (string, error) {
275+
func connectorConfigWithDefaultsCasesWebhook(plan string) (string, error) {
275276
var custom kbapi.CasesWebhookConfig
276277
if err := json.Unmarshal([]byte(plan), &custom); err != nil {
277278
return "", err
@@ -292,7 +293,7 @@ func connectorConfigWithDefaultsCasesWebhook(plan, _ string) (string, error) {
292293
return string(customJSON), nil
293294
}
294295

295-
func connectorConfigWithDefaultsEmail(plan, _ string) (string, error) {
296+
func connectorConfigWithDefaultsEmail(plan string) (string, error) {
296297
var custom kbapi.EmailConfig
297298
if err := json.Unmarshal([]byte(plan), &custom); err != nil {
298299
return "", err
@@ -310,11 +311,11 @@ func connectorConfigWithDefaultsEmail(plan, _ string) (string, error) {
310311
return string(customJSON), nil
311312
}
312313

313-
func connectorConfigWithDefaultsGemini(plan, _ string) (string, error) {
314+
func connectorConfigWithDefaultsGemini(plan string) (string, error) {
314315
return plan, nil
315316
}
316317

317-
func connectorConfigWithDefaultsIndex(plan, _ string) (string, error) {
318+
func connectorConfigWithDefaultsIndex(plan string) (string, error) {
318319
var custom kbapi.IndexConfig
319320
if err := json.Unmarshal([]byte(plan), &custom); err != nil {
320321
return "", err
@@ -329,32 +330,28 @@ func connectorConfigWithDefaultsIndex(plan, _ string) (string, error) {
329330
return string(customJSON), nil
330331
}
331332

332-
func connectorConfigWithDefaultsJira(plan, _ string) (string, error) {
333+
func connectorConfigWithDefaultsJira(plan string) (string, error) {
333334
return remarshalConfig[kbapi.JiraConfig](plan)
334335
}
335336

336-
func connectorConfigWithDefaultsOpsgenie(plan, _ string) (string, error) {
337+
func connectorConfigWithDefaultsOpsgenie(plan string) (string, error) {
337338
return plan, nil
338339
}
339340

340-
func connectorConfigWithDefaultsPagerduty(plan, _ string) (string, error) {
341+
func connectorConfigWithDefaultsPagerduty(plan string) (string, error) {
341342
return remarshalConfig[kbapi.PagerdutyConfig](plan)
342343
}
343344

344-
func connectorConfigWithDefaultsResilient(plan, _ string) (string, error) {
345+
func connectorConfigWithDefaultsResilient(plan string) (string, error) {
345346
return plan, nil
346347
}
347348

348-
func connectorConfigWithDefaultsServicenow(plan, backend string) (string, error) {
349+
func connectorConfigWithDefaultsServicenow(plan string) (string, error) {
349350
var planConfig kbapi.ServicenowConfig
350351
if err := json.Unmarshal([]byte(plan), &planConfig); err != nil {
351352
return "", err
352353
}
353-
var backendConfig kbapi.ServicenowConfig
354-
if err := json.Unmarshal([]byte(backend), &backendConfig); err != nil {
355-
return "", err
356-
}
357-
if planConfig.IsOAuth == nil && backendConfig.IsOAuth != nil && !*backendConfig.IsOAuth {
354+
if planConfig.IsOAuth == nil {
358355
planConfig.IsOAuth = utils.Pointer(false)
359356
}
360357
if planConfig.UsesTableApi == nil {
@@ -367,7 +364,7 @@ func connectorConfigWithDefaultsServicenow(plan, backend string) (string, error)
367364
return string(customJSON), nil
368365
}
369366

370-
func connectorConfigWithDefaultsServicenowItom(plan, _ string) (string, error) {
367+
func connectorConfigWithDefaultsServicenowItom(plan string) (string, error) {
371368
var custom kbapi.ServicenowItomConfig
372369
if err := json.Unmarshal([]byte(plan), &custom); err != nil {
373370
return "", err
@@ -382,11 +379,11 @@ func connectorConfigWithDefaultsServicenowItom(plan, _ string) (string, error) {
382379
return string(customJSON), nil
383380
}
384381

385-
func connectorConfigWithDefaultsServicenowSir(plan, backend string) (string, error) {
386-
return connectorConfigWithDefaultsServicenow(plan, backend)
382+
func connectorConfigWithDefaultsServicenowSir(plan string) (string, error) {
383+
return connectorConfigWithDefaultsServicenow(plan)
387384
}
388385

389-
func connectorConfigWithDefaultsSwimlane(plan, _ string) (string, error) {
386+
func connectorConfigWithDefaultsSwimlane(plan string) (string, error) {
390387
var custom kbapi.SwimlaneConfig
391388
if err := json.Unmarshal([]byte(plan), &custom); err != nil {
392389
return "", err
@@ -444,15 +441,15 @@ func connectorConfigWithDefaultsSwimlane(plan, _ string) (string, error) {
444441
return string(customJSON), nil
445442
}
446443

447-
func connectorConfigWithDefaultsTines(plan, _ string) (string, error) {
444+
func connectorConfigWithDefaultsTines(plan string) (string, error) {
448445
return plan, nil
449446
}
450447

451-
func connectorConfigWithDefaultsWebhook(plan, _ string) (string, error) {
448+
func connectorConfigWithDefaultsWebhook(plan string) (string, error) {
452449
return plan, nil
453450
}
454451

455-
func connectorConfigWithDefaultsXmatters(plan, _ string) (string, error) {
452+
func connectorConfigWithDefaultsXmatters(plan string) (string, error) {
456453
var custom kbapi.XmattersConfig
457454
if err := json.Unmarshal([]byte(plan), &custom); err != nil {
458455
return "", err

internal/clients/kibana_oapi/connector_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
1212
"github.com/elastic/terraform-provider-elasticstack/internal/clients/kibana_oapi"
1313
"github.com/elastic/terraform-provider-elasticstack/internal/models"
14-
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
14+
fwdiag "github.com/hashicorp/terraform-plugin-framework/diag"
1515
"github.com/stretchr/testify/require"
1616
)
1717

@@ -21,15 +21,15 @@ func Test_connectorResponseToModel(t *testing.T) {
2121
spaceId string
2222
response *kbapi.ConnectorResponse
2323
expectedModel *models.KibanaActionConnector
24-
expectedError diag.Diagnostics
24+
expectedError fwdiag.Diagnostics
2525
}
2626
tests := []testCase{
2727
{
2828
name: "should return an error diag when response is nil",
2929
spaceId: "default",
3030
response: nil,
3131
expectedModel: nil,
32-
expectedError: diag.Errorf("connector response is nil"),
32+
expectedError: fwdiag.Diagnostics{fwdiag.NewErrorDiagnostic("Invalid connector response", "connector response is nil")},
3333
},
3434
{
3535
name: "should map valid connector response to model",

0 commit comments

Comments
 (0)