diff --git a/CHANGELOG.md b/CHANGELOG.md index 99ba9ab42..a8f4f2f4d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Added - 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)). - Add support for Terraform Plugin Framework ([#343](https://github.com/elastic/terraform-provider-elasticstack/pull/343)). +- Fix fleet resources not having ID set on import ([#447](https://github.com/elastic/terraform-provider-elasticstack/pull/447)) ## [0.9.0] - 2023-10-09 diff --git a/docs/resources/fleet_agent_policy.md b/docs/resources/fleet_agent_policy.md index d1a36ae24..df090d0a6 100644 --- a/docs/resources/fleet_agent_policy.md +++ b/docs/resources/fleet_agent_policy.md @@ -57,5 +57,5 @@ resource "elasticstack_fleet_agent_policy" "test_policy" { Import is supported using the following syntax: ```shell -terraform import elasticstack_kibana_fleet_agent_policy.my_policy / +terraform import elasticstack_kibana_fleet_agent_policy.my_policy ``` diff --git a/docs/resources/fleet_output.md b/docs/resources/fleet_output.md index 8a095da0a..221de9616 100644 --- a/docs/resources/fleet_output.md +++ b/docs/resources/fleet_output.md @@ -58,5 +58,5 @@ resource "elasticstack_fleet_output" "test_output" { Import is supported using the following syntax: ```shell -terraform import elasticstack_fleet_output.my_output / +terraform import elasticstack_fleet_output.my_output ``` diff --git a/docs/resources/fleet_server_host.md b/docs/resources/fleet_server_host.md index 47b9ee24e..bf97de69b 100644 --- a/docs/resources/fleet_server_host.md +++ b/docs/resources/fleet_server_host.md @@ -48,5 +48,5 @@ resource "elasticstack_fleet_server_host" "test_host" { Import is supported using the following syntax: ```shell -terraform import elasticstack_fleet_server_host.my_host / +terraform import elasticstack_fleet_server_host.my_host ``` diff --git a/examples/resources/elasticstack_fleet_agent_policy/import.sh b/examples/resources/elasticstack_fleet_agent_policy/import.sh index 7037f48de..3235f94e1 100644 --- a/examples/resources/elasticstack_fleet_agent_policy/import.sh +++ b/examples/resources/elasticstack_fleet_agent_policy/import.sh @@ -1 +1 @@ -terraform import elasticstack_kibana_fleet_agent_policy.my_policy / +terraform import elasticstack_kibana_fleet_agent_policy.my_policy diff --git a/examples/resources/elasticstack_fleet_output/import.sh b/examples/resources/elasticstack_fleet_output/import.sh index 8d64683c5..acbd65b83 100644 --- a/examples/resources/elasticstack_fleet_output/import.sh +++ b/examples/resources/elasticstack_fleet_output/import.sh @@ -1 +1 @@ -terraform import elasticstack_fleet_output.my_output / +terraform import elasticstack_fleet_output.my_output diff --git a/examples/resources/elasticstack_fleet_server_host/import.sh b/examples/resources/elasticstack_fleet_server_host/import.sh index 8c590ce7a..b08cdfa3b 100644 --- a/examples/resources/elasticstack_fleet_server_host/import.sh +++ b/examples/resources/elasticstack_fleet_server_host/import.sh @@ -1 +1 @@ -terraform import elasticstack_fleet_server_host.my_host / +terraform import elasticstack_fleet_server_host.my_host diff --git a/internal/fleet/agent_policy_resource.go b/internal/fleet/agent_policy_resource.go index 3badf20aa..9896c8b55 100644 --- a/internal/fleet/agent_policy_resource.go +++ b/internal/fleet/agent_policy_resource.go @@ -103,6 +103,10 @@ func resourceAgentPolicyCreate(ctx context.Context, d *schema.ResourceData, meta return diags } + if id := d.Get("policy_id").(string); id != "" { + d.SetId(id) + } + req := fleetapi.AgentPolicyCreateRequest{ Name: d.Get("name").(string), Namespace: d.Get("namespace").(string), @@ -157,9 +161,6 @@ func resourceAgentPolicyUpdate(ctx context.Context, d *schema.ResourceData, meta return diags } - id := d.Get("policy_id").(string) - d.SetId(id) - req := fleetapi.AgentPolicyUpdateRequest{ Name: d.Get("name").(string), Namespace: d.Get("namespace").(string), @@ -191,7 +192,7 @@ func resourceAgentPolicyUpdate(ctx context.Context, d *schema.ResourceData, meta if len(monitoringValues) > 0 { req.MonitoringEnabled = &monitoringValues } - _, diags = fleet.UpdateAgentPolicy(ctx, fleetClient, id, req) + _, diags = fleet.UpdateAgentPolicy(ctx, fleetClient, d.Id(), req) if diags.HasError() { return diags } @@ -205,10 +206,7 @@ func resourceAgentPolicyRead(ctx context.Context, d *schema.ResourceData, meta i return diags } - id := d.Get("policy_id").(string) - d.SetId(id) - - agentPolicy, diags := fleet.ReadAgentPolicy(ctx, fleetClient, id) + agentPolicy, diags := fleet.ReadAgentPolicy(ctx, fleetClient, d.Id()) if diags.HasError() { return diags } @@ -280,10 +278,7 @@ func resourceAgentPolicyDelete(ctx context.Context, d *schema.ResourceData, meta return diags } - id := d.Get("policy_id").(string) - d.SetId(id) - - if diags = fleet.DeleteAgentPolicy(ctx, fleetClient, id); diags.HasError() { + if diags = fleet.DeleteAgentPolicy(ctx, fleetClient, d.Id()); diags.HasError() { return diags } d.SetId("") diff --git a/internal/fleet/enrollment_tokens_data_source.go b/internal/fleet/enrollment_tokens_data_source.go index ccf3272a7..5f0fe851a 100644 --- a/internal/fleet/enrollment_tokens_data_source.go +++ b/internal/fleet/enrollment_tokens_data_source.go @@ -78,7 +78,10 @@ func dataSourceEnrollmentTokensRead(ctx context.Context, d *schema.ResourceData, return diags } - policyID := d.Get("policy_id").(string) + if d.Id() == "" { + d.SetId(d.Get("policy_id").(string)) + } + policyID := d.Id() allTokens, diags := fleet.AllEnrollmentTokens(ctx, fleetClient) if diags.HasError() { diff --git a/internal/fleet/fleet_server_host_resource.go b/internal/fleet/fleet_server_host_resource.go index 582d883bc..10979fe91 100644 --- a/internal/fleet/fleet_server_host_resource.go +++ b/internal/fleet/fleet_server_host_resource.go @@ -60,6 +60,10 @@ func resourceFleetServerHostCreate(ctx context.Context, d *schema.ResourceData, return diags } + if id := d.Get("host_id").(string); id != "" { + d.SetId(id) + } + req := fleetapi.PostFleetServerHostsJSONRequestBody{ Name: d.Get("name").(string), } @@ -94,9 +98,6 @@ func resourceFleetServerHostUpdate(ctx context.Context, d *schema.ResourceData, return diags } - id := d.Get("host_id").(string) - d.SetId(id) - req := fleetapi.UpdateFleetServerHostsJSONRequestBody{} if value, ok := d.Get("name").(string); ok && value != "" { @@ -117,7 +118,7 @@ func resourceFleetServerHostUpdate(ctx context.Context, d *schema.ResourceData, req.IsDefault = &value } - _, diags = fleet.UpdateFleetServerHost(ctx, fleetClient, id, req) + _, diags = fleet.UpdateFleetServerHost(ctx, fleetClient, d.Id(), req) if diags.HasError() { return diags } @@ -131,10 +132,7 @@ func resourceFleetServerHostRead(ctx context.Context, d *schema.ResourceData, me return diags } - id := d.Get("host_id").(string) - d.SetId(id) - - host, diags := fleet.ReadFleetServerHost(ctx, fleetClient, id) + host, diags := fleet.ReadFleetServerHost(ctx, fleetClient, d.Id()) if diags.HasError() { return diags } @@ -166,10 +164,7 @@ func resourceFleetServerHostDelete(ctx context.Context, d *schema.ResourceData, return diags } - id := d.Get("host_id").(string) - d.SetId(id) - - if diags = fleet.DeleteFleetServerHost(ctx, fleetClient, id); diags.HasError() { + if diags = fleet.DeleteFleetServerHost(ctx, fleetClient, d.Id()); diags.HasError() { return diags } d.SetId("") diff --git a/internal/fleet/output_resource.go b/internal/fleet/output_resource.go index 565c80372..aecc225d6 100644 --- a/internal/fleet/output_resource.go +++ b/internal/fleet/output_resource.go @@ -2,7 +2,6 @@ package fleet import ( "context" - fleetapi "github.com/elastic/terraform-provider-elasticstack/generated/fleet" "github.com/elastic/terraform-provider-elasticstack/internal/clients/fleet" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -206,6 +205,10 @@ func resourceOutputCreate(ctx context.Context, d *schema.ResourceData, meta inte outputType := d.Get("type").(string) var diags diag.Diagnostics + if id := d.Get("output_id").(string); id != "" { + d.SetId(id) + } + switch outputType { case "elasticsearch": diags = resourceOutputCreateElasticsearch(ctx, d, meta) @@ -225,9 +228,6 @@ func resourceOutputUpdateElasticsearch(ctx context.Context, d *schema.ResourceDa return diags } - id := d.Get("output_id").(string) - d.SetId(id) - reqData := fleetapi.OutputUpdateRequestElasticsearch{ Name: d.Get("name").(string), Type: fleetapi.OutputUpdateRequestElasticsearchTypeElasticsearch, @@ -260,7 +260,7 @@ func resourceOutputUpdateElasticsearch(ctx context.Context, d *schema.ResourceDa return diag.FromErr(err) } - _, diags = fleet.UpdateOutput(ctx, fleetClient, id, req) + _, diags = fleet.UpdateOutput(ctx, fleetClient, d.Id(), req) if diags.HasError() { return diags } @@ -274,9 +274,6 @@ func resourceOutputUpdateLogstash(ctx context.Context, d *schema.ResourceData, m return diags } - id := d.Get("output_id").(string) - d.SetId(id) - reqData := fleetapi.OutputUpdateRequestLogstash{ Name: d.Get("name").(string), Type: fleetapi.OutputUpdateRequestLogstashTypeLogstash, @@ -311,7 +308,7 @@ func resourceOutputUpdateLogstash(ctx context.Context, d *schema.ResourceData, m return diag.FromErr(err) } - _, diags = fleet.UpdateOutput(ctx, fleetClient, id, req) + _, diags = fleet.UpdateOutput(ctx, fleetClient, d.Id(), req) if diags.HasError() { return diags } @@ -412,10 +409,7 @@ func resourceOutputRead(ctx context.Context, d *schema.ResourceData, meta interf return diags } - id := d.Get("output_id").(string) - d.SetId(id) - - rawOutput, diags := fleet.ReadOutput(ctx, fleetClient, id) + rawOutput, diags := fleet.ReadOutput(ctx, fleetClient, d.Id()) if diags.HasError() { return diags } @@ -455,10 +449,7 @@ func resourceOutputDelete(ctx context.Context, d *schema.ResourceData, meta inte return diags } - id := d.Get("output_id").(string) - d.SetId(id) - - if diags = fleet.DeleteOutput(ctx, fleetClient, id); diags.HasError() { + if diags = fleet.DeleteOutput(ctx, fleetClient, d.Id()); diags.HasError() { return diags } d.SetId("")