@@ -8,15 +8,15 @@ import (
8
8
"net/http"
9
9
10
10
fleetapi "github.com/elastic/terraform-provider-elasticstack/generated/fleet"
11
- fwdiag "github.com/hashicorp/terraform-plugin-framework/diag"
11
+ "github.com/hashicorp/terraform-plugin-framework/diag"
12
12
)
13
13
14
14
var (
15
15
ErrPackageNotFound = errors .New ("package not found" )
16
16
)
17
17
18
18
// AllEnrollmentTokens reads all enrollment tokens from the API.
19
- func AllEnrollmentTokens (ctx context.Context , client * Client ) ([]fleetapi.EnrollmentApiKey , fwdiag .Diagnostics ) {
19
+ func AllEnrollmentTokens (ctx context.Context , client * Client ) ([]fleetapi.EnrollmentApiKey , diag .Diagnostics ) {
20
20
resp , err := client .API .GetEnrollmentApiKeysWithResponse (ctx )
21
21
if err != nil {
22
22
return nil , fromErr (err )
@@ -25,11 +25,11 @@ func AllEnrollmentTokens(ctx context.Context, client *Client) ([]fleetapi.Enroll
25
25
if resp .StatusCode () == http .StatusOK {
26
26
return resp .JSON200 .Items , nil
27
27
}
28
- return nil , reportUnknownErrorFw (resp .StatusCode (), resp .Body )
28
+ return nil , reportUnknownError (resp .StatusCode (), resp .Body )
29
29
}
30
30
31
31
// GetEnrollmentTokensByPolicy Get enrollment tokens by given policy ID
32
- func GetEnrollmentTokensByPolicy (ctx context.Context , client * Client , policyID string ) ([]fleetapi.EnrollmentApiKey , fwdiag .Diagnostics ) {
32
+ func GetEnrollmentTokensByPolicy (ctx context.Context , client * Client , policyID string ) ([]fleetapi.EnrollmentApiKey , diag .Diagnostics ) {
33
33
resp , err := client .API .GetEnrollmentApiKeysWithResponse (ctx , func (ctx context.Context , req * http.Request ) error {
34
34
q := req .URL .Query ()
35
35
q .Set ("kuery" , "policy_id:" + policyID )
@@ -44,11 +44,11 @@ func GetEnrollmentTokensByPolicy(ctx context.Context, client *Client, policyID s
44
44
if resp .StatusCode () == http .StatusOK {
45
45
return resp .JSON200 .Items , nil
46
46
}
47
- return nil , reportUnknownErrorFw (resp .StatusCode (), resp .Body )
47
+ return nil , reportUnknownError (resp .StatusCode (), resp .Body )
48
48
}
49
49
50
50
// ReadAgentPolicy reads a specific agent policy from the API.
51
- func ReadAgentPolicy (ctx context.Context , client * Client , id string ) (* fleetapi.AgentPolicy , fwdiag .Diagnostics ) {
51
+ func ReadAgentPolicy (ctx context.Context , client * Client , id string ) (* fleetapi.AgentPolicy , diag .Diagnostics ) {
52
52
resp , err := client .API .AgentPolicyInfoWithResponse (ctx , id )
53
53
if err != nil {
54
54
return nil , fromErr (err )
@@ -60,12 +60,12 @@ func ReadAgentPolicy(ctx context.Context, client *Client, id string) (*fleetapi.
60
60
case http .StatusNotFound :
61
61
return nil , nil
62
62
default :
63
- return nil , reportUnknownErrorFw (resp .StatusCode (), resp .Body )
63
+ return nil , reportUnknownError (resp .StatusCode (), resp .Body )
64
64
}
65
65
}
66
66
67
67
// CreateAgentPolicy creates a new agent policy.
68
- func CreateAgentPolicy (ctx context.Context , client * Client , req fleetapi.AgentPolicyCreateRequest , sysMonitoring bool ) (* fleetapi.AgentPolicy , fwdiag .Diagnostics ) {
68
+ func CreateAgentPolicy (ctx context.Context , client * Client , req fleetapi.AgentPolicyCreateRequest , sysMonitoring bool ) (* fleetapi.AgentPolicy , diag .Diagnostics ) {
69
69
resp , err := client .API .CreateAgentPolicyWithResponse (ctx , req , func (ctx context.Context , req * http.Request ) error {
70
70
if sysMonitoring {
71
71
qs := req .URL .Query ()
@@ -83,12 +83,12 @@ func CreateAgentPolicy(ctx context.Context, client *Client, req fleetapi.AgentPo
83
83
case http .StatusOK :
84
84
return resp .JSON200 .Item , nil
85
85
default :
86
- return nil , reportUnknownErrorFw (resp .StatusCode (), resp .Body )
86
+ return nil , reportUnknownError (resp .StatusCode (), resp .Body )
87
87
}
88
88
}
89
89
90
90
// UpdateAgentPolicy updates an existing agent policy.
91
- func UpdateAgentPolicy (ctx context.Context , client * Client , id string , req fleetapi.AgentPolicyUpdateRequest ) (* fleetapi.AgentPolicy , fwdiag .Diagnostics ) {
91
+ func UpdateAgentPolicy (ctx context.Context , client * Client , id string , req fleetapi.AgentPolicyUpdateRequest ) (* fleetapi.AgentPolicy , diag .Diagnostics ) {
92
92
resp , err := client .API .UpdateAgentPolicyWithResponse (ctx , id , req )
93
93
if err != nil {
94
94
return nil , fromErr (err )
@@ -98,12 +98,12 @@ func UpdateAgentPolicy(ctx context.Context, client *Client, id string, req fleet
98
98
case http .StatusOK :
99
99
return & resp .JSON200 .Item , nil
100
100
default :
101
- return nil , reportUnknownErrorFw (resp .StatusCode (), resp .Body )
101
+ return nil , reportUnknownError (resp .StatusCode (), resp .Body )
102
102
}
103
103
}
104
104
105
105
// DeleteAgentPolicy deletes an existing agent policy
106
- func DeleteAgentPolicy (ctx context.Context , client * Client , id string ) fwdiag .Diagnostics {
106
+ func DeleteAgentPolicy (ctx context.Context , client * Client , id string ) diag .Diagnostics {
107
107
body := fleetapi.DeleteAgentPolicyJSONRequestBody {
108
108
AgentPolicyId : id ,
109
109
}
@@ -119,12 +119,12 @@ func DeleteAgentPolicy(ctx context.Context, client *Client, id string) fwdiag.Di
119
119
case http .StatusNotFound :
120
120
return nil
121
121
default :
122
- return reportUnknownErrorFw (resp .StatusCode (), resp .Body )
122
+ return reportUnknownError (resp .StatusCode (), resp .Body )
123
123
}
124
124
}
125
125
126
126
// ReadOutput reads a specific output from the API.
127
- func ReadOutput (ctx context.Context , client * Client , id string ) (* fleetapi.OutputCreateRequest , fwdiag .Diagnostics ) {
127
+ func ReadOutput (ctx context.Context , client * Client , id string ) (* fleetapi.OutputCreateRequest , diag .Diagnostics ) {
128
128
resp , err := client .API .GetOutputWithResponse (ctx , id )
129
129
if err != nil {
130
130
return nil , fromErr (err )
@@ -136,12 +136,12 @@ func ReadOutput(ctx context.Context, client *Client, id string) (*fleetapi.Outpu
136
136
case http .StatusNotFound :
137
137
return nil , nil
138
138
default :
139
- return nil , reportUnknownErrorFw (resp .StatusCode (), resp .Body )
139
+ return nil , reportUnknownError (resp .StatusCode (), resp .Body )
140
140
}
141
141
}
142
142
143
143
// CreateOutput creates a new output.
144
- func CreateOutput (ctx context.Context , client * Client , req fleetapi.PostOutputsJSONRequestBody ) (* fleetapi.OutputCreateRequest , fwdiag .Diagnostics ) {
144
+ func CreateOutput (ctx context.Context , client * Client , req fleetapi.PostOutputsJSONRequestBody ) (* fleetapi.OutputCreateRequest , diag .Diagnostics ) {
145
145
resp , err := client .API .PostOutputsWithResponse (ctx , req )
146
146
if err != nil {
147
147
return nil , fromErr (err )
@@ -151,12 +151,12 @@ func CreateOutput(ctx context.Context, client *Client, req fleetapi.PostOutputsJ
151
151
case http .StatusOK :
152
152
return resp .JSON200 .Item , nil
153
153
default :
154
- return nil , reportUnknownErrorFw (resp .StatusCode (), resp .Body )
154
+ return nil , reportUnknownError (resp .StatusCode (), resp .Body )
155
155
}
156
156
}
157
157
158
158
// UpdateOutput updates an existing output.
159
- func UpdateOutput (ctx context.Context , client * Client , id string , req fleetapi.UpdateOutputJSONRequestBody ) (* fleetapi.OutputUpdateRequest , fwdiag .Diagnostics ) {
159
+ func UpdateOutput (ctx context.Context , client * Client , id string , req fleetapi.UpdateOutputJSONRequestBody ) (* fleetapi.OutputUpdateRequest , diag .Diagnostics ) {
160
160
resp , err := client .API .UpdateOutputWithResponse (ctx , id , req )
161
161
if err != nil {
162
162
return nil , fromErr (err )
@@ -166,12 +166,12 @@ func UpdateOutput(ctx context.Context, client *Client, id string, req fleetapi.U
166
166
case http .StatusOK :
167
167
return resp .JSON200 .Item , nil
168
168
default :
169
- return nil , reportUnknownErrorFw (resp .StatusCode (), resp .Body )
169
+ return nil , reportUnknownError (resp .StatusCode (), resp .Body )
170
170
}
171
171
}
172
172
173
173
// DeleteOutput deletes an existing output
174
- func DeleteOutput (ctx context.Context , client * Client , id string ) fwdiag .Diagnostics {
174
+ func DeleteOutput (ctx context.Context , client * Client , id string ) diag .Diagnostics {
175
175
resp , err := client .API .DeleteOutputWithResponse (ctx , id )
176
176
if err != nil {
177
177
return fromErr (err )
@@ -183,12 +183,12 @@ func DeleteOutput(ctx context.Context, client *Client, id string) fwdiag.Diagnos
183
183
case http .StatusNotFound :
184
184
return nil
185
185
default :
186
- return reportUnknownErrorFw (resp .StatusCode (), resp .Body )
186
+ return reportUnknownError (resp .StatusCode (), resp .Body )
187
187
}
188
188
}
189
189
190
190
// ReadFleetServerHost reads a specific fleet server host from the API.
191
- func ReadFleetServerHost (ctx context.Context , client * Client , id string ) (* fleetapi.FleetServerHost , fwdiag .Diagnostics ) {
191
+ func ReadFleetServerHost (ctx context.Context , client * Client , id string ) (* fleetapi.FleetServerHost , diag .Diagnostics ) {
192
192
resp , err := client .API .GetOneFleetServerHostsWithResponse (ctx , id )
193
193
if err != nil {
194
194
return nil , fromErr (err )
@@ -200,12 +200,12 @@ func ReadFleetServerHost(ctx context.Context, client *Client, id string) (*fleet
200
200
case http .StatusNotFound :
201
201
return nil , nil
202
202
default :
203
- return nil , reportUnknownErrorFw (resp .StatusCode (), resp .Body )
203
+ return nil , reportUnknownError (resp .StatusCode (), resp .Body )
204
204
}
205
205
}
206
206
207
207
// CreateFleetServerHost creates a new fleet server host.
208
- func CreateFleetServerHost (ctx context.Context , client * Client , req fleetapi.PostFleetServerHostsJSONRequestBody ) (* fleetapi.FleetServerHost , fwdiag .Diagnostics ) {
208
+ func CreateFleetServerHost (ctx context.Context , client * Client , req fleetapi.PostFleetServerHostsJSONRequestBody ) (* fleetapi.FleetServerHost , diag .Diagnostics ) {
209
209
resp , err := client .API .PostFleetServerHostsWithResponse (ctx , req )
210
210
if err != nil {
211
211
return nil , fromErr (err )
@@ -215,12 +215,12 @@ func CreateFleetServerHost(ctx context.Context, client *Client, req fleetapi.Pos
215
215
case http .StatusOK :
216
216
return resp .JSON200 .Item , nil
217
217
default :
218
- return nil , reportUnknownErrorFw (resp .StatusCode (), resp .Body )
218
+ return nil , reportUnknownError (resp .StatusCode (), resp .Body )
219
219
}
220
220
}
221
221
222
222
// UpdateFleetServerHost updates an existing fleet server host.
223
- func UpdateFleetServerHost (ctx context.Context , client * Client , id string , req fleetapi.UpdateFleetServerHostsJSONRequestBody ) (* fleetapi.FleetServerHost , fwdiag .Diagnostics ) {
223
+ func UpdateFleetServerHost (ctx context.Context , client * Client , id string , req fleetapi.UpdateFleetServerHostsJSONRequestBody ) (* fleetapi.FleetServerHost , diag .Diagnostics ) {
224
224
resp , err := client .API .UpdateFleetServerHostsWithResponse (ctx , id , req )
225
225
if err != nil {
226
226
return nil , fromErr (err )
@@ -230,12 +230,12 @@ func UpdateFleetServerHost(ctx context.Context, client *Client, id string, req f
230
230
case http .StatusOK :
231
231
return & resp .JSON200 .Item , nil
232
232
default :
233
- return nil , reportUnknownErrorFw (resp .StatusCode (), resp .Body )
233
+ return nil , reportUnknownError (resp .StatusCode (), resp .Body )
234
234
}
235
235
}
236
236
237
237
// DeleteFleetServerHost deletes an existing fleet server host.
238
- func DeleteFleetServerHost (ctx context.Context , client * Client , id string ) fwdiag .Diagnostics {
238
+ func DeleteFleetServerHost (ctx context.Context , client * Client , id string ) diag .Diagnostics {
239
239
resp , err := client .API .DeleteFleetServerHostsWithResponse (ctx , id )
240
240
if err != nil {
241
241
return fromErr (err )
@@ -247,12 +247,12 @@ func DeleteFleetServerHost(ctx context.Context, client *Client, id string) fwdia
247
247
case http .StatusNotFound :
248
248
return nil
249
249
default :
250
- return reportUnknownErrorFw (resp .StatusCode (), resp .Body )
250
+ return reportUnknownError (resp .StatusCode (), resp .Body )
251
251
}
252
252
}
253
253
254
254
// ReadPackagePolicy reads a specific package policy from the API.
255
- func ReadPackagePolicy (ctx context.Context , client * Client , id string ) (* fleetapi.PackagePolicy , fwdiag .Diagnostics ) {
255
+ func ReadPackagePolicy (ctx context.Context , client * Client , id string ) (* fleetapi.PackagePolicy , diag .Diagnostics ) {
256
256
format := fleetapi .GetPackagePolicyParamsFormatSimplified
257
257
params := fleetapi.GetPackagePolicyParams {
258
258
Format : & format ,
@@ -269,12 +269,12 @@ func ReadPackagePolicy(ctx context.Context, client *Client, id string) (*fleetap
269
269
case http .StatusNotFound :
270
270
return nil , nil
271
271
default :
272
- return nil , reportUnknownErrorFw (resp .StatusCode (), resp .Body )
272
+ return nil , reportUnknownError (resp .StatusCode (), resp .Body )
273
273
}
274
274
}
275
275
276
276
// CreatePackagePolicy creates a new package policy.
277
- func CreatePackagePolicy (ctx context.Context , client * Client , req fleetapi.CreatePackagePolicyJSONRequestBody ) (* fleetapi.PackagePolicy , fwdiag .Diagnostics ) {
277
+ func CreatePackagePolicy (ctx context.Context , client * Client , req fleetapi.CreatePackagePolicyJSONRequestBody ) (* fleetapi.PackagePolicy , diag .Diagnostics ) {
278
278
format := fleetapi .CreatePackagePolicyParamsFormatSimplified
279
279
params := fleetapi.CreatePackagePolicyParams {
280
280
Format : & format ,
@@ -289,12 +289,12 @@ func CreatePackagePolicy(ctx context.Context, client *Client, req fleetapi.Creat
289
289
case http .StatusOK :
290
290
return & resp .JSON200 .Item , nil
291
291
default :
292
- return nil , reportUnknownErrorFw (resp .StatusCode (), resp .Body )
292
+ return nil , reportUnknownError (resp .StatusCode (), resp .Body )
293
293
}
294
294
}
295
295
296
296
// UpdatePackagePolicy updates an existing package policy.
297
- func UpdatePackagePolicy (ctx context.Context , client * Client , id string , req fleetapi.UpdatePackagePolicyJSONRequestBody ) (* fleetapi.PackagePolicy , fwdiag .Diagnostics ) {
297
+ func UpdatePackagePolicy (ctx context.Context , client * Client , id string , req fleetapi.UpdatePackagePolicyJSONRequestBody ) (* fleetapi.PackagePolicy , diag .Diagnostics ) {
298
298
format := fleetapi .UpdatePackagePolicyParamsFormatSimplified
299
299
params := fleetapi.UpdatePackagePolicyParams {
300
300
Format : & format ,
@@ -309,12 +309,12 @@ func UpdatePackagePolicy(ctx context.Context, client *Client, id string, req fle
309
309
case http .StatusOK :
310
310
return & resp .JSON200 .Item , nil
311
311
default :
312
- return nil , reportUnknownErrorFw (resp .StatusCode (), resp .Body )
312
+ return nil , reportUnknownError (resp .StatusCode (), resp .Body )
313
313
}
314
314
}
315
315
316
316
// DeletePackagePolicy deletes an existing package policy.
317
- func DeletePackagePolicy (ctx context.Context , client * Client , id string , force bool ) fwdiag .Diagnostics {
317
+ func DeletePackagePolicy (ctx context.Context , client * Client , id string , force bool ) diag .Diagnostics {
318
318
params := fleetapi.DeletePackagePolicyParams {Force : & force }
319
319
resp , err := client .API .DeletePackagePolicyWithResponse (ctx , id , & params )
320
320
if err != nil {
@@ -327,12 +327,12 @@ func DeletePackagePolicy(ctx context.Context, client *Client, id string, force b
327
327
case http .StatusNotFound :
328
328
return nil
329
329
default :
330
- return reportUnknownErrorFw (resp .StatusCode (), resp .Body )
330
+ return reportUnknownError (resp .StatusCode (), resp .Body )
331
331
}
332
332
}
333
333
334
334
// ReadPackage reads a specific package from the API.
335
- func ReadPackage (ctx context.Context , client * Client , name , version string ) fwdiag .Diagnostics {
335
+ func ReadPackage (ctx context.Context , client * Client , name , version string ) diag .Diagnostics {
336
336
params := fleetapi.GetPackageParams {}
337
337
338
338
resp , err := client .API .GetPackage (ctx , name , version , & params )
@@ -352,12 +352,12 @@ func ReadPackage(ctx context.Context, client *Client, name, version string) fwdi
352
352
return fromErr (err )
353
353
}
354
354
355
- return reportUnknownErrorFw (resp .StatusCode , errData )
355
+ return reportUnknownError (resp .StatusCode , errData )
356
356
}
357
357
}
358
358
359
359
// InstallPackage installs a package.
360
- func InstallPackage (ctx context.Context , client * Client , name , version string , force bool ) fwdiag .Diagnostics {
360
+ func InstallPackage (ctx context.Context , client * Client , name , version string , force bool ) diag .Diagnostics {
361
361
params := fleetapi.InstallPackageParams {}
362
362
body := fleetapi.InstallPackageJSONRequestBody {
363
363
Force : & force ,
@@ -379,12 +379,12 @@ func InstallPackage(ctx context.Context, client *Client, name, version string, f
379
379
return fromErr (err )
380
380
}
381
381
382
- return reportUnknownErrorFw (resp .StatusCode , errData )
382
+ return reportUnknownError (resp .StatusCode , errData )
383
383
}
384
384
}
385
385
386
386
// Uninstall uninstalls a package.
387
- func Uninstall (ctx context.Context , client * Client , name , version string , force bool ) fwdiag .Diagnostics {
387
+ func Uninstall (ctx context.Context , client * Client , name , version string , force bool ) diag .Diagnostics {
388
388
params := fleetapi.DeletePackageParams {}
389
389
body := fleetapi.DeletePackageJSONRequestBody {
390
390
Force : & force ,
@@ -401,12 +401,12 @@ func Uninstall(ctx context.Context, client *Client, name, version string, force
401
401
case http .StatusNotFound :
402
402
return nil
403
403
default :
404
- return reportUnknownErrorFw (resp .StatusCode (), resp .Body )
404
+ return reportUnknownError (resp .StatusCode (), resp .Body )
405
405
}
406
406
}
407
407
408
408
// AllPackages returns information about the latest packages known to Fleet.
409
- func AllPackages (ctx context.Context , client * Client , prerelease bool ) ([]fleetapi.SearchResult , fwdiag .Diagnostics ) {
409
+ func AllPackages (ctx context.Context , client * Client , prerelease bool ) ([]fleetapi.SearchResult , diag .Diagnostics ) {
410
410
params := fleetapi.ListAllPackagesParams {
411
411
Prerelease : & prerelease ,
412
412
}
@@ -420,23 +420,23 @@ func AllPackages(ctx context.Context, client *Client, prerelease bool) ([]fleeta
420
420
case http .StatusOK :
421
421
return resp .JSON200 .Items , nil
422
422
default :
423
- return nil , reportUnknownErrorFw (resp .StatusCode (), resp .Body )
423
+ return nil , reportUnknownError (resp .StatusCode (), resp .Body )
424
424
}
425
425
}
426
426
427
427
// fromErr recreates the sdkdiag.FromErr functionality.
428
- func fromErr (err error ) fwdiag .Diagnostics {
428
+ func fromErr (err error ) diag .Diagnostics {
429
429
if err == nil {
430
430
return nil
431
431
}
432
- return fwdiag .Diagnostics {
433
- fwdiag .NewErrorDiagnostic (err .Error (), "" ),
432
+ return diag .Diagnostics {
433
+ diag .NewErrorDiagnostic (err .Error (), "" ),
434
434
}
435
435
}
436
436
437
- func reportUnknownErrorFw (statusCode int , body []byte ) fwdiag .Diagnostics {
438
- return fwdiag .Diagnostics {
439
- fwdiag .NewErrorDiagnostic (
437
+ func reportUnknownError (statusCode int , body []byte ) diag .Diagnostics {
438
+ return diag .Diagnostics {
439
+ diag .NewErrorDiagnostic (
440
440
fmt .Sprintf ("Unexpected status code from server: got HTTP %d" , statusCode ),
441
441
string (body ),
442
442
),
0 commit comments