Skip to content

Commit 0901eee

Browse files
committed
standalone output resource
1 parent ba9abc9 commit 0901eee

File tree

15 files changed

+781
-604
lines changed

15 files changed

+781
-604
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- Migrate `elasticstack_fleet_agent_policy`, `elasticstack_fleet_integration` (both), and `elasticstack_fleet_server_host` to terraform-plugin-framework ([#785](https://github.com/elastic/terraform-provider-elasticstack/pull/785))
55
- Fix for synthetics http/tcp monitor produces inconsistent result after apply ([#801](https://github.com/elastic/terraform-provider-elasticstack/pull/801))
66
- Migrate `elasticstack_fleet_integration_policy` to terraform-plugin-framework. Fix drift in integration policy secrets. ([#797](https://github.com/elastic/terraform-provider-elasticstack/pull/797))
7+
- Migrate `elasticstack_fleet_output` to terraform-plugin-framework. ([#811](https://github.com/elastic/terraform-provider-elasticstack/pull/811))
78

89
## [0.11.7] - 2024-09-20
910

docs/resources/fleet_output.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ resource "elasticstack_fleet_output" "test_output" {
4848
- `default_monitoring` (Boolean) Make this output the default for agent monitoring.
4949
- `hosts` (List of String) A list of hosts.
5050
- `output_id` (String) Unique identifier of the output.
51-
- `ssl` (Block List, Max: 1) SSL configuration. (see [below for nested schema](#nestedblock--ssl))
51+
- `ssl` (Block List) SSL configuration. (see [below for nested schema](#nestedblock--ssl))
5252

5353
### Read-Only
5454

internal/clients/fleet/fleet.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,10 @@ func DeleteAgentPolicy(ctx context.Context, client *Client, id string) fwdiag.Di
125125
}
126126

127127
// ReadOutput reads a specific output from the API.
128-
func ReadOutput(ctx context.Context, client *Client, id string) (*fleetapi.OutputCreateRequest, diag.Diagnostics) {
128+
func ReadOutput(ctx context.Context, client *Client, id string) (*fleetapi.OutputCreateRequest, fwdiag.Diagnostics) {
129129
resp, err := client.API.GetOutputWithResponse(ctx, id)
130130
if err != nil {
131-
return nil, diag.FromErr(err)
131+
return nil, fromErr(err)
132132
}
133133

134134
switch resp.StatusCode() {
@@ -137,45 +137,45 @@ func ReadOutput(ctx context.Context, client *Client, id string) (*fleetapi.Outpu
137137
case http.StatusNotFound:
138138
return nil, nil
139139
default:
140-
return nil, reportUnknownError(resp.StatusCode(), resp.Body)
140+
return nil, reportUnknownErrorFw(resp.StatusCode(), resp.Body)
141141
}
142142
}
143143

144144
// CreateOutput creates a new output.
145-
func CreateOutput(ctx context.Context, client *Client, req fleetapi.PostOutputsJSONRequestBody) (*fleetapi.OutputCreateRequest, diag.Diagnostics) {
145+
func CreateOutput(ctx context.Context, client *Client, req fleetapi.PostOutputsJSONRequestBody) (*fleetapi.OutputCreateRequest, fwdiag.Diagnostics) {
146146
resp, err := client.API.PostOutputsWithResponse(ctx, req)
147147
if err != nil {
148-
return nil, diag.FromErr(err)
148+
return nil, fromErr(err)
149149
}
150150

151151
switch resp.StatusCode() {
152152
case http.StatusOK:
153153
return resp.JSON200.Item, nil
154154
default:
155-
return nil, reportUnknownError(resp.StatusCode(), resp.Body)
155+
return nil, reportUnknownErrorFw(resp.StatusCode(), resp.Body)
156156
}
157157
}
158158

159159
// UpdateOutput updates an existing output.
160-
func UpdateOutput(ctx context.Context, client *Client, id string, req fleetapi.UpdateOutputJSONRequestBody) (*fleetapi.OutputUpdateRequest, diag.Diagnostics) {
160+
func UpdateOutput(ctx context.Context, client *Client, id string, req fleetapi.UpdateOutputJSONRequestBody) (*fleetapi.OutputUpdateRequest, fwdiag.Diagnostics) {
161161
resp, err := client.API.UpdateOutputWithResponse(ctx, id, req)
162162
if err != nil {
163-
return nil, diag.FromErr(err)
163+
return nil, fromErr(err)
164164
}
165165

166166
switch resp.StatusCode() {
167167
case http.StatusOK:
168168
return resp.JSON200.Item, nil
169169
default:
170-
return nil, reportUnknownError(resp.StatusCode(), resp.Body)
170+
return nil, reportUnknownErrorFw(resp.StatusCode(), resp.Body)
171171
}
172172
}
173173

174174
// DeleteOutput deletes an existing output
175-
func DeleteOutput(ctx context.Context, client *Client, id string) diag.Diagnostics {
175+
func DeleteOutput(ctx context.Context, client *Client, id string) fwdiag.Diagnostics {
176176
resp, err := client.API.DeleteOutputWithResponse(ctx, id)
177177
if err != nil {
178-
return diag.FromErr(err)
178+
return fromErr(err)
179179
}
180180

181181
switch resp.StatusCode() {
@@ -184,7 +184,7 @@ func DeleteOutput(ctx context.Context, client *Client, id string) diag.Diagnosti
184184
case http.StatusNotFound:
185185
return nil
186186
default:
187-
return reportUnknownError(resp.StatusCode(), resp.Body)
187+
return reportUnknownErrorFw(resp.StatusCode(), resp.Body)
188188
}
189189
}
190190

internal/fleet/output/create.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package output
2+
3+
import (
4+
"context"
5+
6+
"github.com/elastic/terraform-provider-elasticstack/internal/clients/fleet"
7+
"github.com/hashicorp/terraform-plugin-framework/resource"
8+
)
9+
10+
func (r *outputResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
11+
var planModel outputModel
12+
13+
diags := req.Plan.Get(ctx, &planModel)
14+
resp.Diagnostics.Append(diags...)
15+
if resp.Diagnostics.HasError() {
16+
return
17+
}
18+
19+
client, err := r.client.GetFleetClient()
20+
if err != nil {
21+
resp.Diagnostics.AddError(err.Error(), "")
22+
return
23+
}
24+
25+
body, diags := planModel.toAPICreateModel(ctx)
26+
resp.Diagnostics.Append(diags...)
27+
if resp.Diagnostics.HasError() {
28+
return
29+
}
30+
31+
output, diags := fleet.CreateOutput(ctx, client, body)
32+
resp.Diagnostics.Append(diags...)
33+
if resp.Diagnostics.HasError() {
34+
return
35+
}
36+
37+
diags = planModel.populateFromAPICreate(ctx, output)
38+
resp.Diagnostics.Append(diags...)
39+
if resp.Diagnostics.HasError() {
40+
return
41+
}
42+
43+
diags = resp.State.Set(ctx, planModel)
44+
resp.Diagnostics.Append(diags...)
45+
}

internal/fleet/output/delete.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package output
2+
3+
import (
4+
"context"
5+
6+
"github.com/elastic/terraform-provider-elasticstack/internal/clients/fleet"
7+
"github.com/hashicorp/terraform-plugin-framework/resource"
8+
)
9+
10+
func (r *outputResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
11+
var stateModel outputModel
12+
13+
diags := req.State.Get(ctx, &stateModel)
14+
resp.Diagnostics.Append(diags...)
15+
if resp.Diagnostics.HasError() {
16+
return
17+
}
18+
19+
client, err := r.client.GetFleetClient()
20+
if err != nil {
21+
resp.Diagnostics.AddError(err.Error(), "")
22+
return
23+
}
24+
25+
outputID := stateModel.OutputID.ValueString()
26+
diags = fleet.DeleteOutput(ctx, client, outputID)
27+
resp.Diagnostics.Append(diags...)
28+
}

0 commit comments

Comments
 (0)