Skip to content

Commit 3ea2662

Browse files
Fix fleet resources not having ID set on import (#447)
* Fix fleet objects not having ID set on import - Fixed fleet objects not having an ID set when importing. - Update fleet documentation to remove kibana space id * Update changelog
1 parent 9d620f2 commit 3ea2662

File tree

11 files changed

+33
-48
lines changed

11 files changed

+33
-48
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### Added
44
- Introduce `elasticstack_kibana_import_saved_objects` resource as an additive only way to manage Kibana saved objects ([#343](https://github.com/elastic/terraform-provider-elasticstack/pull/343)).
55
- Add support for Terraform Plugin Framework ([#343](https://github.com/elastic/terraform-provider-elasticstack/pull/343)).
6+
- Fix fleet resources not having ID set on import ([#447](https://github.com/elastic/terraform-provider-elasticstack/pull/447))
67

78
## [0.9.0] - 2023-10-09
89

docs/resources/fleet_agent_policy.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ resource "elasticstack_fleet_agent_policy" "test_policy" {
5757
Import is supported using the following syntax:
5858

5959
```shell
60-
terraform import elasticstack_kibana_fleet_agent_policy.my_policy <space id>/<policy id>
60+
terraform import elasticstack_kibana_fleet_agent_policy.my_policy <fleet_agent_policy_id>
6161
```

docs/resources/fleet_output.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,5 @@ resource "elasticstack_fleet_output" "test_output" {
5858
Import is supported using the following syntax:
5959

6060
```shell
61-
terraform import elasticstack_fleet_output.my_output <space id>/<output id>
61+
terraform import elasticstack_fleet_output.my_output <fleet_output_id>
6262
```

docs/resources/fleet_server_host.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,5 @@ resource "elasticstack_fleet_server_host" "test_host" {
4848
Import is supported using the following syntax:
4949

5050
```shell
51-
terraform import elasticstack_fleet_server_host.my_host <space id>/<host id>
51+
terraform import elasticstack_fleet_server_host.my_host <fleet_server_host_id>
5252
```
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
terraform import elasticstack_kibana_fleet_agent_policy.my_policy <space id>/<policy id>
1+
terraform import elasticstack_kibana_fleet_agent_policy.my_policy <fleet_agent_policy_id>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
terraform import elasticstack_fleet_output.my_output <space id>/<output id>
1+
terraform import elasticstack_fleet_output.my_output <fleet_output_id>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
terraform import elasticstack_fleet_server_host.my_host <space id>/<host id>
1+
terraform import elasticstack_fleet_server_host.my_host <fleet_server_host_id>

internal/fleet/agent_policy_resource.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ func resourceAgentPolicyCreate(ctx context.Context, d *schema.ResourceData, meta
103103
return diags
104104
}
105105

106+
if id := d.Get("policy_id").(string); id != "" {
107+
d.SetId(id)
108+
}
109+
106110
req := fleetapi.AgentPolicyCreateRequest{
107111
Name: d.Get("name").(string),
108112
Namespace: d.Get("namespace").(string),
@@ -157,9 +161,6 @@ func resourceAgentPolicyUpdate(ctx context.Context, d *schema.ResourceData, meta
157161
return diags
158162
}
159163

160-
id := d.Get("policy_id").(string)
161-
d.SetId(id)
162-
163164
req := fleetapi.AgentPolicyUpdateRequest{
164165
Name: d.Get("name").(string),
165166
Namespace: d.Get("namespace").(string),
@@ -191,7 +192,7 @@ func resourceAgentPolicyUpdate(ctx context.Context, d *schema.ResourceData, meta
191192
if len(monitoringValues) > 0 {
192193
req.MonitoringEnabled = &monitoringValues
193194
}
194-
_, diags = fleet.UpdateAgentPolicy(ctx, fleetClient, id, req)
195+
_, diags = fleet.UpdateAgentPolicy(ctx, fleetClient, d.Id(), req)
195196
if diags.HasError() {
196197
return diags
197198
}
@@ -205,10 +206,7 @@ func resourceAgentPolicyRead(ctx context.Context, d *schema.ResourceData, meta i
205206
return diags
206207
}
207208

208-
id := d.Get("policy_id").(string)
209-
d.SetId(id)
210-
211-
agentPolicy, diags := fleet.ReadAgentPolicy(ctx, fleetClient, id)
209+
agentPolicy, diags := fleet.ReadAgentPolicy(ctx, fleetClient, d.Id())
212210
if diags.HasError() {
213211
return diags
214212
}
@@ -280,10 +278,7 @@ func resourceAgentPolicyDelete(ctx context.Context, d *schema.ResourceData, meta
280278
return diags
281279
}
282280

283-
id := d.Get("policy_id").(string)
284-
d.SetId(id)
285-
286-
if diags = fleet.DeleteAgentPolicy(ctx, fleetClient, id); diags.HasError() {
281+
if diags = fleet.DeleteAgentPolicy(ctx, fleetClient, d.Id()); diags.HasError() {
287282
return diags
288283
}
289284
d.SetId("")

internal/fleet/enrollment_tokens_data_source.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ func dataSourceEnrollmentTokensRead(ctx context.Context, d *schema.ResourceData,
7878
return diags
7979
}
8080

81-
policyID := d.Get("policy_id").(string)
81+
if d.Id() == "" {
82+
d.SetId(d.Get("policy_id").(string))
83+
}
84+
policyID := d.Id()
8285

8386
allTokens, diags := fleet.AllEnrollmentTokens(ctx, fleetClient)
8487
if diags.HasError() {

internal/fleet/fleet_server_host_resource.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ func resourceFleetServerHostCreate(ctx context.Context, d *schema.ResourceData,
6060
return diags
6161
}
6262

63+
if id := d.Get("host_id").(string); id != "" {
64+
d.SetId(id)
65+
}
66+
6367
req := fleetapi.PostFleetServerHostsJSONRequestBody{
6468
Name: d.Get("name").(string),
6569
}
@@ -94,9 +98,6 @@ func resourceFleetServerHostUpdate(ctx context.Context, d *schema.ResourceData,
9498
return diags
9599
}
96100

97-
id := d.Get("host_id").(string)
98-
d.SetId(id)
99-
100101
req := fleetapi.UpdateFleetServerHostsJSONRequestBody{}
101102

102103
if value, ok := d.Get("name").(string); ok && value != "" {
@@ -117,7 +118,7 @@ func resourceFleetServerHostUpdate(ctx context.Context, d *schema.ResourceData,
117118
req.IsDefault = &value
118119
}
119120

120-
_, diags = fleet.UpdateFleetServerHost(ctx, fleetClient, id, req)
121+
_, diags = fleet.UpdateFleetServerHost(ctx, fleetClient, d.Id(), req)
121122
if diags.HasError() {
122123
return diags
123124
}
@@ -131,10 +132,7 @@ func resourceFleetServerHostRead(ctx context.Context, d *schema.ResourceData, me
131132
return diags
132133
}
133134

134-
id := d.Get("host_id").(string)
135-
d.SetId(id)
136-
137-
host, diags := fleet.ReadFleetServerHost(ctx, fleetClient, id)
135+
host, diags := fleet.ReadFleetServerHost(ctx, fleetClient, d.Id())
138136
if diags.HasError() {
139137
return diags
140138
}
@@ -166,10 +164,7 @@ func resourceFleetServerHostDelete(ctx context.Context, d *schema.ResourceData,
166164
return diags
167165
}
168166

169-
id := d.Get("host_id").(string)
170-
d.SetId(id)
171-
172-
if diags = fleet.DeleteFleetServerHost(ctx, fleetClient, id); diags.HasError() {
167+
if diags = fleet.DeleteFleetServerHost(ctx, fleetClient, d.Id()); diags.HasError() {
173168
return diags
174169
}
175170
d.SetId("")

internal/fleet/output_resource.go

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package fleet
22

33
import (
44
"context"
5-
65
fleetapi "github.com/elastic/terraform-provider-elasticstack/generated/fleet"
76
"github.com/elastic/terraform-provider-elasticstack/internal/clients/fleet"
87
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
@@ -206,6 +205,10 @@ func resourceOutputCreate(ctx context.Context, d *schema.ResourceData, meta inte
206205
outputType := d.Get("type").(string)
207206
var diags diag.Diagnostics
208207

208+
if id := d.Get("output_id").(string); id != "" {
209+
d.SetId(id)
210+
}
211+
209212
switch outputType {
210213
case "elasticsearch":
211214
diags = resourceOutputCreateElasticsearch(ctx, d, meta)
@@ -225,9 +228,6 @@ func resourceOutputUpdateElasticsearch(ctx context.Context, d *schema.ResourceDa
225228
return diags
226229
}
227230

228-
id := d.Get("output_id").(string)
229-
d.SetId(id)
230-
231231
reqData := fleetapi.OutputUpdateRequestElasticsearch{
232232
Name: d.Get("name").(string),
233233
Type: fleetapi.OutputUpdateRequestElasticsearchTypeElasticsearch,
@@ -260,7 +260,7 @@ func resourceOutputUpdateElasticsearch(ctx context.Context, d *schema.ResourceDa
260260
return diag.FromErr(err)
261261
}
262262

263-
_, diags = fleet.UpdateOutput(ctx, fleetClient, id, req)
263+
_, diags = fleet.UpdateOutput(ctx, fleetClient, d.Id(), req)
264264
if diags.HasError() {
265265
return diags
266266
}
@@ -274,9 +274,6 @@ func resourceOutputUpdateLogstash(ctx context.Context, d *schema.ResourceData, m
274274
return diags
275275
}
276276

277-
id := d.Get("output_id").(string)
278-
d.SetId(id)
279-
280277
reqData := fleetapi.OutputUpdateRequestLogstash{
281278
Name: d.Get("name").(string),
282279
Type: fleetapi.OutputUpdateRequestLogstashTypeLogstash,
@@ -311,7 +308,7 @@ func resourceOutputUpdateLogstash(ctx context.Context, d *schema.ResourceData, m
311308
return diag.FromErr(err)
312309
}
313310

314-
_, diags = fleet.UpdateOutput(ctx, fleetClient, id, req)
311+
_, diags = fleet.UpdateOutput(ctx, fleetClient, d.Id(), req)
315312
if diags.HasError() {
316313
return diags
317314
}
@@ -412,10 +409,7 @@ func resourceOutputRead(ctx context.Context, d *schema.ResourceData, meta interf
412409
return diags
413410
}
414411

415-
id := d.Get("output_id").(string)
416-
d.SetId(id)
417-
418-
rawOutput, diags := fleet.ReadOutput(ctx, fleetClient, id)
412+
rawOutput, diags := fleet.ReadOutput(ctx, fleetClient, d.Id())
419413
if diags.HasError() {
420414
return diags
421415
}
@@ -455,10 +449,7 @@ func resourceOutputDelete(ctx context.Context, d *schema.ResourceData, meta inte
455449
return diags
456450
}
457451

458-
id := d.Get("output_id").(string)
459-
d.SetId(id)
460-
461-
if diags = fleet.DeleteOutput(ctx, fleetClient, id); diags.HasError() {
452+
if diags = fleet.DeleteOutput(ctx, fleetClient, d.Id()); diags.HasError() {
462453
return diags
463454
}
464455
d.SetId("")

0 commit comments

Comments
 (0)