Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 16, 2025

Addresses code review feedback to eliminate duplicated logic for handling untyped zero-value lists that appear during import operations. The pattern was repeated identically in three locations within the anomaly detection job model conversion.

Changes

  • Added EnsureTypedList helper in internal/utils/typeutils/list.go

    • Converts untyped zero-value lists (common during import) to properly typed null lists
    • Handles both nil element types and DynamicPseudoType elements
    • Returns list unchanged if already properly typed
  • Refactored duplicate logic in models_tf.go for:

    • CategorizationFilters (StringType lists)
    • Influencers (StringType lists)
    • CustomRules (ObjectType lists)
  • Removed unused import from resource.go

Before

if analysisConfigTF.CategorizationFilters.ElementType(ctx) == nil {
    analysisConfigTF.CategorizationFilters = types.ListNull(types.StringType)
} else if _, ok := analysisConfigTF.CategorizationFilters.ElementType(ctx).(basetypes.DynamicType); ok {
    analysisConfigTF.CategorizationFilters = types.ListNull(types.StringType)
}

After

analysisConfigTF.CategorizationFilters = typeutils.EnsureTypedList(ctx, analysisConfigTF.CategorizationFilters, types.StringType)

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…function

- Created EnsureTypedList helper in typeutils package
- Replaced three instances of duplicated logic for handling untyped zero-value lists
- Removed unused strings import from resource.go
- All three instances (CategorizationFilters, Influencers, CustomRules) now use the centralized helper

Co-authored-by: neiljbrookes <[email protected]>
Copilot AI changed the title [WIP] Fix anomaly detection job import/refresh mismatch issue Extract duplicated list type handling logic into reusable helper Dec 16, 2025
Copilot AI requested a review from neiljbrookes December 16, 2025 11:24
@neiljbrookes neiljbrookes marked this pull request as ready for review December 16, 2025 12:57
@neiljbrookes neiljbrookes merged commit 0185df6 into bugfix/ml-anomaly-job-import-sparse-state Dec 16, 2025
76 checks passed
@neiljbrookes neiljbrookes deleted the copilot/sub-pr-1550 branch December 16, 2025 13:25
neiljbrookes added a commit that referenced this pull request Dec 17, 2025
* fix(ml): make anomaly_detection_job import resilient

Fixes terraform import/refresh failures for elasticstack_elasticsearch_ml_anomaly_detection_job by keeping ImportState sparse (id/job_id only) and allowing analysis_config to be null during import before Read populates it.

Also ensures empty nested lists in analysis_config (e.g. categorization_filters/influencers/custom_rules) are always typed to avoid DynamicPseudoType conversion errors.

AI assistance: This change was implemented with the help of an AI coding assistant (Cursor + GPT).

* Update internal/elasticsearch/ml/anomaly_detection_job/resource.go

Co-authored-by: Toby Brain <[email protected]>

* Remove unused import in anomaly_detection_job resource file

* Add ImportState testing for anomaly_detection_job acceptance tests

This update introduces ImportState testing for the elasticstack_elasticsearch_ml_anomaly_detection_job resource, ensuring that the job can be imported correctly with the necessary configuration variables. The test verifies the import functionality and enhances the robustness of the acceptance tests.

* Extract duplicated list type handling logic into reusable helper (#1559)

* Initial plan

* Refactor: extract duplicated list type handling into reusable helper function

- Created EnsureTypedList helper in typeutils package
- Replaced three instances of duplicated logic for handling untyped zero-value lists
- Removed unused strings import from resource.go
- All three instances (CategorizationFilters, Influencers, CustomRules) now use the centralized helper

Co-authored-by: neiljbrookes <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: neiljbrookes <[email protected]>

* Update CHANGELOG.md to include a fix for `elasticstack_elasticsearch_ml_anomaly_detection_job` import, enhancing resilience to sparse state values.

---------

Co-authored-by: Toby Brain <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: neiljbrookes <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants