@@ -11,14 +11,15 @@ import (
11
11
"github.com/elastic/terraform-provider-elasticstack/generated/kbapi"
12
12
"github.com/elastic/terraform-provider-elasticstack/internal/models"
13
13
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
14
+ fwdiag "github.com/hashicorp/terraform-plugin-framework/diag"
14
15
"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"
16
17
)
17
18
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 ) {
19
20
body , err := createConnectorRequestBody (connectorOld )
20
21
if err != nil {
21
- return "" , diag . FromErr ( err )
22
+ return "" , fwdiag. Diagnostics { fwdiag . NewErrorDiagnostic ( "Failed to create connector request body" , err . Error ())}
22
23
}
23
24
24
25
resp , err := client .API .PostActionsConnectorIdWithResponse (
@@ -35,40 +36,40 @@ func CreateConnector(ctx context.Context, client *Client, connectorOld models.Ki
35
36
},
36
37
)
37
38
if err != nil {
38
- return "" , diag . FromErr ( err )
39
+ return "" , fwdiag. Diagnostics { fwdiag . NewErrorDiagnostic ( "HTTP request failed" , err . Error ())}
39
40
}
40
41
41
42
switch resp .StatusCode () {
42
43
case http .StatusOK :
43
44
return resp .JSON200 .Id , nil
44
45
default :
45
- return "" , reportUnknownErrorSDK (resp .StatusCode (), resp .Body )
46
+ return "" , reportUnknownError (resp .StatusCode (), resp .Body )
46
47
}
47
48
}
48
49
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 ) {
50
51
body , err := updateConnectorRequestBody (connectorOld )
51
52
if err != nil {
52
- return "" , diag . FromErr ( err )
53
+ return "" , fwdiag. Diagnostics { fwdiag . NewErrorDiagnostic ( "Failed to create update request body" , err . Error ())}
53
54
}
54
55
55
56
resp , err := client .API .PutActionsConnectorIdWithResponse (ctx , connectorOld .SpaceID , connectorOld .ConnectorID , body )
56
57
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 ())}
58
59
}
59
60
60
61
switch resp .StatusCode () {
61
62
case http .StatusOK :
62
63
return resp .JSON200 .Id , nil
63
64
default :
64
- return "" , reportUnknownErrorSDK (resp .StatusCode (), resp .Body )
65
+ return "" , reportUnknownError (resp .StatusCode (), resp .Body )
65
66
}
66
67
}
67
68
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 ) {
69
70
resp , err := client .API .GetActionsConnectorIdWithResponse (ctx , spaceID , connectorID )
70
71
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 ())}
72
73
}
73
74
74
75
switch resp .StatusCode () {
@@ -77,14 +78,14 @@ func GetConnector(ctx context.Context, client *Client, connectorID, spaceID stri
77
78
case http .StatusNotFound :
78
79
return nil , nil
79
80
default :
80
- return nil , reportUnknownErrorSDK (resp .StatusCode (), resp .Body )
81
+ return nil , reportUnknownError (resp .StatusCode (), resp .Body )
81
82
}
82
83
}
83
84
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 ) {
85
86
resp , err := client .API .GetActionsConnectorsWithResponse (ctx , spaceID )
86
87
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 )
88
89
}
89
90
90
91
if resp .StatusCode () != http .StatusOK {
@@ -101,9 +102,9 @@ func SearchConnectors(ctx context.Context, client *Client, connectorName, spaceI
101
102
continue
102
103
}
103
104
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 )
107
108
}
108
109
109
110
foundConnectors = append (foundConnectors , c )
@@ -115,9 +116,9 @@ func SearchConnectors(ctx context.Context, client *Client, connectorName, spaceI
115
116
return foundConnectors , nil
116
117
}
117
118
118
- func ConnectorResponseToModel (spaceID string , connector * kbapi.ConnectorResponse ) (* models.KibanaActionConnector , diag .Diagnostics ) {
119
+ func ConnectorResponseToModel (spaceID string , connector * kbapi.ConnectorResponse ) (* models.KibanaActionConnector , fwdiag .Diagnostics ) {
119
120
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" )}
121
122
}
122
123
123
124
var configJSON []byte
@@ -132,7 +133,7 @@ func ConnectorResponseToModel(spaceID string, connector *kbapi.ConnectorResponse
132
133
var err error
133
134
configJSON , err = json .Marshal (configMap )
134
135
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 ())}
136
137
}
137
138
138
139
// 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
141
142
if ok {
142
143
configJSONString , err := handler .remarshalConfig (string (configJSON ))
143
144
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 ())}
145
146
}
146
147
147
148
configJSON = []byte (configJSONString )
@@ -165,21 +166,21 @@ func ConnectorResponseToModel(spaceID string, connector *kbapi.ConnectorResponse
165
166
return model , nil
166
167
}
167
168
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 {
169
170
resp , err := client .API .DeleteActionsConnectorIdWithResponse (ctx , spaceID , connectorID )
170
171
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 ())}
172
173
}
173
174
174
175
if resp .StatusCode () != http .StatusOK && resp .StatusCode () != http .StatusNoContent {
175
- return reportUnknownErrorSDK (resp .StatusCode (), resp .Body )
176
+ return reportUnknownError (resp .StatusCode (), resp .Body )
176
177
}
177
178
178
179
return nil
179
180
}
180
181
181
182
type connectorConfigHandler struct {
182
- defaults func (plan , backend string ) (string , error )
183
+ defaults func (plan string ) (string , error )
183
184
remarshalConfig func (config string ) (string , error )
184
185
}
185
186
@@ -246,13 +247,13 @@ var connectorConfigHandlers = map[string]connectorConfigHandler{
246
247
},
247
248
}
248
249
249
- func ConnectorConfigWithDefaults (connectorTypeID , plan , backend , state string ) (string , error ) {
250
+ func ConnectorConfigWithDefaults (connectorTypeID , plan string ) (string , error ) {
250
251
handler , ok := connectorConfigHandlers [connectorTypeID ]
251
252
if ! ok {
252
253
return plan , errors .New ("unknown connector type ID: " + connectorTypeID )
253
254
}
254
255
255
- return handler .defaults (plan , backend )
256
+ return handler .defaults (plan )
256
257
}
257
258
258
259
// User can omit optonal fields in config JSON.
@@ -271,7 +272,7 @@ func remarshalConfig[T any](plan string) (string, error) {
271
272
return string (customJSON ), nil
272
273
}
273
274
274
- func connectorConfigWithDefaultsCasesWebhook (plan , _ string ) (string , error ) {
275
+ func connectorConfigWithDefaultsCasesWebhook (plan string ) (string , error ) {
275
276
var custom kbapi.CasesWebhookConfig
276
277
if err := json .Unmarshal ([]byte (plan ), & custom ); err != nil {
277
278
return "" , err
@@ -292,7 +293,7 @@ func connectorConfigWithDefaultsCasesWebhook(plan, _ string) (string, error) {
292
293
return string (customJSON ), nil
293
294
}
294
295
295
- func connectorConfigWithDefaultsEmail (plan , _ string ) (string , error ) {
296
+ func connectorConfigWithDefaultsEmail (plan string ) (string , error ) {
296
297
var custom kbapi.EmailConfig
297
298
if err := json .Unmarshal ([]byte (plan ), & custom ); err != nil {
298
299
return "" , err
@@ -310,11 +311,11 @@ func connectorConfigWithDefaultsEmail(plan, _ string) (string, error) {
310
311
return string (customJSON ), nil
311
312
}
312
313
313
- func connectorConfigWithDefaultsGemini (plan , _ string ) (string , error ) {
314
+ func connectorConfigWithDefaultsGemini (plan string ) (string , error ) {
314
315
return plan , nil
315
316
}
316
317
317
- func connectorConfigWithDefaultsIndex (plan , _ string ) (string , error ) {
318
+ func connectorConfigWithDefaultsIndex (plan string ) (string , error ) {
318
319
var custom kbapi.IndexConfig
319
320
if err := json .Unmarshal ([]byte (plan ), & custom ); err != nil {
320
321
return "" , err
@@ -329,32 +330,28 @@ func connectorConfigWithDefaultsIndex(plan, _ string) (string, error) {
329
330
return string (customJSON ), nil
330
331
}
331
332
332
- func connectorConfigWithDefaultsJira (plan , _ string ) (string , error ) {
333
+ func connectorConfigWithDefaultsJira (plan string ) (string , error ) {
333
334
return remarshalConfig [kbapi.JiraConfig ](plan )
334
335
}
335
336
336
- func connectorConfigWithDefaultsOpsgenie (plan , _ string ) (string , error ) {
337
+ func connectorConfigWithDefaultsOpsgenie (plan string ) (string , error ) {
337
338
return plan , nil
338
339
}
339
340
340
- func connectorConfigWithDefaultsPagerduty (plan , _ string ) (string , error ) {
341
+ func connectorConfigWithDefaultsPagerduty (plan string ) (string , error ) {
341
342
return remarshalConfig [kbapi.PagerdutyConfig ](plan )
342
343
}
343
344
344
- func connectorConfigWithDefaultsResilient (plan , _ string ) (string , error ) {
345
+ func connectorConfigWithDefaultsResilient (plan string ) (string , error ) {
345
346
return plan , nil
346
347
}
347
348
348
- func connectorConfigWithDefaultsServicenow (plan , backend string ) (string , error ) {
349
+ func connectorConfigWithDefaultsServicenow (plan string ) (string , error ) {
349
350
var planConfig kbapi.ServicenowConfig
350
351
if err := json .Unmarshal ([]byte (plan ), & planConfig ); err != nil {
351
352
return "" , err
352
353
}
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 {
358
355
planConfig .IsOAuth = utils .Pointer (false )
359
356
}
360
357
if planConfig .UsesTableApi == nil {
@@ -367,7 +364,7 @@ func connectorConfigWithDefaultsServicenow(plan, backend string) (string, error)
367
364
return string (customJSON ), nil
368
365
}
369
366
370
- func connectorConfigWithDefaultsServicenowItom (plan , _ string ) (string , error ) {
367
+ func connectorConfigWithDefaultsServicenowItom (plan string ) (string , error ) {
371
368
var custom kbapi.ServicenowItomConfig
372
369
if err := json .Unmarshal ([]byte (plan ), & custom ); err != nil {
373
370
return "" , err
@@ -382,11 +379,11 @@ func connectorConfigWithDefaultsServicenowItom(plan, _ string) (string, error) {
382
379
return string (customJSON ), nil
383
380
}
384
381
385
- func connectorConfigWithDefaultsServicenowSir (plan , backend string ) (string , error ) {
386
- return connectorConfigWithDefaultsServicenow (plan , backend )
382
+ func connectorConfigWithDefaultsServicenowSir (plan string ) (string , error ) {
383
+ return connectorConfigWithDefaultsServicenow (plan )
387
384
}
388
385
389
- func connectorConfigWithDefaultsSwimlane (plan , _ string ) (string , error ) {
386
+ func connectorConfigWithDefaultsSwimlane (plan string ) (string , error ) {
390
387
var custom kbapi.SwimlaneConfig
391
388
if err := json .Unmarshal ([]byte (plan ), & custom ); err != nil {
392
389
return "" , err
@@ -444,15 +441,15 @@ func connectorConfigWithDefaultsSwimlane(plan, _ string) (string, error) {
444
441
return string (customJSON ), nil
445
442
}
446
443
447
- func connectorConfigWithDefaultsTines (plan , _ string ) (string , error ) {
444
+ func connectorConfigWithDefaultsTines (plan string ) (string , error ) {
448
445
return plan , nil
449
446
}
450
447
451
- func connectorConfigWithDefaultsWebhook (plan , _ string ) (string , error ) {
448
+ func connectorConfigWithDefaultsWebhook (plan string ) (string , error ) {
452
449
return plan , nil
453
450
}
454
451
455
- func connectorConfigWithDefaultsXmatters (plan , _ string ) (string , error ) {
452
+ func connectorConfigWithDefaultsXmatters (plan string ) (string , error ) {
456
453
var custom kbapi.XmattersConfig
457
454
if err := json .Unmarshal ([]byte (plan ), & custom ); err != nil {
458
455
return "" , err
0 commit comments