Skip to content

Commit

Permalink
Fix fleet objects not having ID set on import
Browse files Browse the repository at this point in the history
- Fixed fleet objects not having an ID set when importing.
- Update fleet documentation to remove kibana space id
  • Loading branch information
taylor-swanson committed Oct 13, 2023
1 parent d2a4fbc commit d84f917
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 48 deletions.
2 changes: 1 addition & 1 deletion docs/resources/fleet_agent_policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <space id>/<policy id>
terraform import elasticstack_kibana_fleet_agent_policy.my_policy <fleet_agent_policy_id>
```
2 changes: 1 addition & 1 deletion docs/resources/fleet_output.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <space id>/<output id>
terraform import elasticstack_fleet_output.my_output <fleet_output_id>
```
2 changes: 1 addition & 1 deletion docs/resources/fleet_server_host.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <space id>/<host id>
terraform import elasticstack_fleet_server_host.my_host <fleet_server_host_id>
```
Original file line number Diff line number Diff line change
@@ -1 +1 @@
terraform import elasticstack_kibana_fleet_agent_policy.my_policy <space id>/<policy id>
terraform import elasticstack_kibana_fleet_agent_policy.my_policy <fleet_agent_policy_id>
2 changes: 1 addition & 1 deletion examples/resources/elasticstack_fleet_output/import.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
terraform import elasticstack_fleet_output.my_output <space id>/<output id>
terraform import elasticstack_fleet_output.my_output <fleet_output_id>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
terraform import elasticstack_fleet_server_host.my_host <space id>/<host id>
terraform import elasticstack_fleet_server_host.my_host <fleet_server_host_id>
19 changes: 7 additions & 12 deletions internal/fleet/agent_policy_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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("")
Expand Down
5 changes: 4 additions & 1 deletion internal/fleet/enrollment_tokens_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
19 changes: 7 additions & 12 deletions internal/fleet/fleet_server_host_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down Expand Up @@ -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 != "" {
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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("")
Expand Down
25 changes: 8 additions & 17 deletions internal/fleet/output_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down Expand Up @@ -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
}
Expand All @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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("")
Expand Down

0 comments on commit d84f917

Please sign in to comment.