Skip to content

Populate all relevant attributes during fleet output import #522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Fixed
- Handle nil LastExecutionDate's in Kibana alerting rules. ([#508](https://github.com./elastic/terraform-provider-elasticstack/pull/508))
- Import all relevant attributes during `elasticstack_fleet_output` import ([#522](https://github.com./elastic/terraform-provider-elasticstack/pull/522))

## [0.11.0] - 2023-12-12

Expand Down
35 changes: 19 additions & 16 deletions internal/fleet/output_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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 @@ -405,6 +406,9 @@ func resourceOutputUpdate(ctx context.Context, d *schema.ResourceData, meta inte
}

func resourceOutputReadElasticsearch(d *schema.ResourceData, data fleetapi.OutputCreateRequestElasticsearch) diag.Diagnostics {
if err := d.Set("type", "elasticsearch"); err != nil {
return diag.FromErr(err)
}
if err := d.Set("name", data.Name); err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -441,6 +445,9 @@ func resourceOutputReadElasticsearch(d *schema.ResourceData, data fleetapi.Outpu
}

func resourceOutputReadLogstash(d *schema.ResourceData, data fleetapi.OutputCreateRequestLogstash) diag.Diagnostics {
if err := d.Set("type", "logstash"); err != nil {
return diag.FromErr(err)
}
if err := d.Set("name", data.Name); err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -493,22 +500,18 @@ func resourceOutputRead(ctx context.Context, d *schema.ResourceData, meta interf
return nil
}

outputType := d.Get("type").(string)
switch outputType {
case "elasticsearch":
output, err := rawOutput.AsOutputCreateRequestElasticsearch()
if err != nil {
return diag.FromErr(err)
}

diags = resourceOutputReadElasticsearch(d, output)
case "logstash":
output, err := rawOutput.AsOutputCreateRequestLogstash()
if err != nil {
return diag.FromErr(err)
}

diags = resourceOutputReadLogstash(d, output)
output, err := rawOutput.ValueByDiscriminator()
if err != nil {
return diag.FromErr(err)
}
switch outputType := output.(type) {
case fleetapi.OutputCreateRequestElasticsearch:
diags = resourceOutputReadElasticsearch(d, outputType)
case fleetapi.OutputCreateRequestLogstash:
diags = resourceOutputReadLogstash(d, outputType)
}
if err := d.Set("output_id", d.Id()); err != nil {
return diag.FromErr(err)
}
if diags.HasError() {
return diags
Expand Down
6 changes: 6 additions & 0 deletions internal/fleet/output_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ func TestAccResourceOutputElasticsearch(t *testing.T) {
resource.TestCheckResourceAttr("elasticstack_fleet_output.test_output", "hosts.0", "https://elasticsearch:9200"),
),
},
{
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minVersionOutput),
ResourceName: "elasticstack_fleet_output.test_output",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand Down