Skip to content

[Bug] Provider crashes when attempting to import non-existent datafeed from anomaly detection job #1570

@edsavage

Description

@edsavage

Describe the bug
When an attempt is made to import a datafeed from an anomaly detection job that doesn't have a datafeed defined, the terraform provider crashes with a null pointer exception.

To Reproduce
Steps to reproduce the behavior:

  1. Create a job with no corresponding datafeed
    Create a job similar to the following using e.g. the anomaly detection API
{
  "job_id": "eqtg",
  "description": "",
  "analysis_config": {
    "bucket_span": "15m",
    "categorization_field_name": "useragent",
    "categorization_analyzer": {
      "char_filter": [
        "first_line_with_letters",
        {
          "pattern": "IP:.*",
          "type": "pattern_replace"
        }
      ],
      "tokenizer": "ml_standard",
      "filter": [
        {
          "type": "stop",
          "stopwords": [
            "Monday",
            "Tuesday",
            "Wednesday",
            "Thursday",
            "Friday",
            "Saturday",
            "Sunday",
            "Mon",
            "Tue",
            "Wed",
            "Thu",
            "Fri",
            "Sat",
            "Sun",
            "January",
            "February",
            "March",
            "April",
            "May",
            "June",
            "July",
            "August",
            "September",
            "October",
            "November",
            "December",
            "Jan",
            "Feb",
            "Mar",
            "Apr",
            "May",
            "Jun",
            "Jul",
            "Aug",
            "Sep",
            "Oct",
            "Nov",
            "Dec",
            "GMT",
            "UTC"
          ]
        },
        {
          "type": "limit",
          "max_token_count": "100"
        }
      ]
    },
    "per_partition_categorization": {
      "enabled": false
    },
    "detectors": [
      {
        "detector_description": "count by mlcategory",
        "function": "count",
        "by_field_name": "mlcategory",
        "detector_index": 0
      }
    ],
    "influencers": [
      "mlcategory"
    ],
    "model_prune_window": "30d"
  },
  "analysis_limits": {
    "model_memory_limit": "61mb",
    "categorization_examples_limit": 4
  },
  "data_description": {
    "time_field": "@timestamp",
    "time_format": "epoch_ms"
  },
  "model_plot_config": {
    "enabled": false,
    "annotations_enabled": false
  },
  "model_snapshot_retention_days": 10,
  "daily_model_snapshot_retention_after_days": 1,
  "results_index_name": "shared",
  "allow_lazy_open": false
}
  1. Define a minimal terraform resource for the non-existent datafeed
resource "elasticstack_elasticsearch_ml_datafeed" "eqtg" {
  # (resource arguments)
  datafeed_id = "datafeed-eqtg"
}
  1. Attempt to import the datafeed
> terraform import elasticstack_elasticsearch_ml_datafeed.eqtg i0YWc1YkQASar4qCfQL9-g/datafeed-eqtg

elasticstack_elasticsearch_ml_datafeed.eqtg: Importing from ID "i0YWc1YkQASar4qCfQL9-g/datafeed-eqtg"...
elasticstack_elasticsearch_ml_datafeed.eqtg: Import prepared!
  Prepared elasticstack_elasticsearch_ml_datafeed for import
elasticstack_elasticsearch_ml_datafeed.eqtg: Refreshing state... [id=i0YWc1YkQASar4qCfQL9-g/datafeed-eqtg]
╷
│ Error: Plugin did not respond
│ 
│ The plugin encountered an error, and failed to respond to the plugin6.(*GRPCProvider).ReadResource call. The plugin logs may contain more details.
╵
  1. See the error in the output from the provider
{"@caller":"/usr/local/go/src/runtime/panic.go:783","@level":"trace","@message":"Served request","@module":"sdk.proto","@timestamp":"2025-12-18T16:03:02.444671+13:00","tf_proto_version":"6.10","tf_provider_addr":"registry.terraform.io/elastic/elasticstack","tf_req_id":"7aea9310-118f-a3c7-95c3-f6707ee7bc2e","tf_resource_type":"elasticstack_elasticsearch_ml_datafeed","tf_rpc":"ReadResource"}
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x10375b040]

goroutine 163 [running]:
github.com/elastic/terraform-provider-elasticstack/internal/elasticsearch/ml/datafeed.(*Datafeed).FromAPIModel(0x1400036cec0?, {0x1041d0718?, 0x140003bb740?}, 0x1400079ade0?)
    /Users/eds/src/elasticsearch/terraform-provider-elasticstack/internal/elasticsearch/ml/datafeed/models.go:203 +0x30
github.com/elastic/terraform-provider-elasticstack/internal/elasticsearch/ml/datafeed.(*datafeedResource).read(0x140001920f0, {0x1041d0718, 0x140003bb740}, 0x14000702540)
    /Users/eds/src/elasticsearch/terraform-provider-elasticstack/internal/elasticsearch/ml/datafeed/read.go:31 +0x160
github.com/elastic/terraform-provider-elasticstack/internal/elasticsearch/ml/datafeed.(*datafeedResource).Read(0x140001920f0, {0x1041d0718, 0x140003bb740}, {{{{0x1041d92d8, 0x140006d3da0}, {0x103e8aae0, 0x140006d3b60}}, {0x1041ddb88, 0x140005917c0}}, 0x0, ...}, ...)
    /Users/eds/src/elasticsearch/terraform-provider-elasticstack/internal/elasticsearch/ml/datafeed/resource.go:43 +0x10c
github.com/hashicorp/terraform-plugin-framework/internal/fwserver.(*Server).ReadResource(0x140003fa308, {0x1041d0718, 0x140003bb740}, 0x1400015ac80, 0x1400036d530)
    /Users/eds/go/pkg/mod/github.com/hashicorp/[email protected]/internal/fwserver/server_readresource.go:156 +0x86c
github.com/hashicorp/terraform-plugin-framework/internal/proto6server.(*Server).ReadResource(0x140003fa308, {0x1041d0718?, 0x140003bb650?}, 0x1400015abe0)
    /Users/eds/go/pkg/mod/github.com/hashicorp/[email protected]/internal/proto6server/server_readresource.go:71 +0x3f8
github.com/hashicorp/terraform-plugin-mux/tf6muxserver.(*muxServer).ReadResource(0x140003f83f0, {0x1041d0718?, 0x140003bb350?}, 0x1400015abe0)
    /Users/eds/go/pkg/mod/github.com/hashicorp/[email protected]/tf6muxserver/mux_server_ReadResource.go:35 +0x178
github.com/hashicorp/terraform-plugin-go/tfprotov6/tf6server.(*server).ReadResource(0x1400049f2c0, {0x1041d0718?, 0x140003ba570?}, 0x1400005a150)
    /Users/eds/go/pkg/mod/github.com/hashicorp/[email protected]/tfprotov6/tf6server/server.go:863 +0x1f8
github.com/hashicorp/terraform-plugin-go/tfprotov6/internal/tfplugin6._Provider_ReadResource_Handler({0x10412e8a0, 0x1400049f2c0}, {0x1041d0718, 0x140003ba570}, 0x14000159a00, 0x0)
    /Users/eds/go/pkg/mod/github.com/hashicorp/[email protected]/tfprotov6/internal/tfplugin6/tfplugin6_grpc.pb.go:753 +0x1b8
google.golang.org/grpc.(*Server).processUnaryRPC(0x140003aca00, {0x1041d0718, 0x140003ba4e0}, 0x140000b0420, 0x140004f5590, 0x1052ac538, 0x0)
    /Users/eds/go/pkg/mod/google.golang.org/[email protected]/server.go:1431 +0xbd0
google.golang.org/grpc.(*Server).handleStream(0x140003aca00, {0x1041d17d8, 0x140004c6d00}, 0x140000b0420)
    /Users/eds/go/pkg/mod/google.golang.org/[email protected]/server.go:1842 +0x858
google.golang.org/grpc.(*Server).serveStreams.func2.1()
    /Users/eds/go/pkg/mod/google.golang.org/[email protected]/server.go:1061 +0x74
created by google.golang.org/grpc.(*Server).serveStreams.func2 in goroutine 42
    /Users/eds/go/pkg/mod/google.golang.org/[email protected]/server.go:1072 +0x120
exit status 2

Expected behavior
The null pointer to be handled gracefully and the provider not to crash.

Versions (please complete the following information):

  • OS: macOS
  • Terraform Version 1.13.5
  • Provider version main 1215a4a
  • Elasticsearch Version 9.3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions