diff --git a/CHANGELOG.md b/CHANGELOG.md index be8bf6a24..d1d96274f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,5 @@ ## [Unreleased] -### Added - -- Add `advanced_settings` to `elasticstack_fleet_agent_policy` to configure agent logging, CPU limits, and download settings ([#1545](https://github.com/elastic/terraform-provider-elasticstack/pull/1545)) - ### Breaking changes #### `elasticstack_fleet_integration_policy` input block has changed to a map attribute. @@ -70,6 +66,8 @@ inputs = { ### Changes +- Add `advanced_settings` to `elasticstack_fleet_agent_policy` to configure agent logging, CPU limits, and download settings ([#1545](https://github.com/elastic/terraform-provider-elasticstack/pull/1545)) +- Prevent provider panic when importing a non-existant `elasticstack_elasticsearch_ml_datafeed`. ([#1579](https://github.com/elastic/terraform-provider-elasticstack/pull/1579)) - Fix handling of empty `except` attributes in `elasticstack_elasticsearch_security_role` ([#1581](https://github.com/elastic/terraform-provider-elasticstack/pull/1581)) - Fix the enabled property being ignored in `elasticstack_kibana_alerting_rule` ([#1527](https://github.com/elastic/terraform-provider-elasticstack/pull/1527)) - Add `advanced_monitoring_options` to `elasticstack_fleet_agent_policy` to configure HTTP monitoring endpoint and diagnostics settings ([#1537](https://github.com/elastic/terraform-provider-elasticstack/pull/1537)) diff --git a/internal/elasticsearch/ml/datafeed/acc_test.go b/internal/elasticsearch/ml/datafeed/acc_test.go index 226a83de1..5069c4add 100644 --- a/internal/elasticsearch/ml/datafeed/acc_test.go +++ b/internal/elasticsearch/ml/datafeed/acc_test.go @@ -168,3 +168,23 @@ func TestAccResourceDatafeedComprehensive(t *testing.T) { }, }) } + +func TestAccResourceDatafeed_ImportNonExistent(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t) }, + Steps: []resource.TestStep{ + { + ProtoV6ProviderFactories: acctest.Providers, + ConfigDirectory: acctest.NamedTestCaseDirectory("import_missing"), + ConfigVariables: config.Variables{ + "job_id": config.StringVariable("dummy-job"), + "datafeed_id": config.StringVariable("dummy-datafeed"), + }, + ResourceName: "elasticstack_elasticsearch_ml_datafeed.test", + ImportState: true, + ImportStateId: "cluster-id/non-existent-datafeed-id", + ImportStateVerify: false, + }, + }, + }) +} diff --git a/internal/elasticsearch/ml/datafeed/read.go b/internal/elasticsearch/ml/datafeed/read.go index 8e3cf41ee..dc8d50048 100644 --- a/internal/elasticsearch/ml/datafeed/read.go +++ b/internal/elasticsearch/ml/datafeed/read.go @@ -27,6 +27,11 @@ func (r *datafeedResource) read(ctx context.Context, model *Datafeed) (bool, fwd return false, diags } + if apiModel == nil { + // Datafeed not found + return false, diags + } + // Convert API model to TF model convertDiags := model.FromAPIModel(ctx, apiModel) diags.Append(convertDiags...) diff --git a/internal/elasticsearch/ml/datafeed/testdata/TestAccResourceDatafeed_ImportNonExistent/import_missing/datafeed.tf b/internal/elasticsearch/ml/datafeed/testdata/TestAccResourceDatafeed_ImportNonExistent/import_missing/datafeed.tf new file mode 100644 index 000000000..9eca7a600 --- /dev/null +++ b/internal/elasticsearch/ml/datafeed/testdata/TestAccResourceDatafeed_ImportNonExistent/import_missing/datafeed.tf @@ -0,0 +1,23 @@ +variable "job_id" { + type = string +} + +variable "datafeed_id" { + type = string +} + +provider "elasticstack" { + elasticsearch {} +} + +resource "elasticstack_elasticsearch_ml_datafeed" "test" { + datafeed_id = var.datafeed_id + job_id = var.job_id + indices = ["test-index-*"] + + query = jsonencode({ + match_all = { + boost = 1 + } + }) +}