Skip to content
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
24 changes: 6 additions & 18 deletions internal/elasticsearch/ml/anomaly_detection_job/models_tf.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,23 +362,15 @@ func (tfModel *AnomalyDetectionJobTFModel) convertAnalysisConfigFromAPI(ctx cont
var categorizationFiltersDiags diag.Diagnostics
analysisConfigTF.CategorizationFilters, categorizationFiltersDiags = typeutils.NonEmptyListOrDefault(ctx, analysisConfigTF.CategorizationFilters, types.StringType, apiConfig.CategorizationFilters)
diags.Append(categorizationFiltersDiags...)
// If the existing value was an untyped zero-value list (common during import), force a typed null list.
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)
}
// Ensure the list is properly typed (handles untyped zero-value lists from import)
analysisConfigTF.CategorizationFilters = typeutils.EnsureTypedList(ctx, analysisConfigTF.CategorizationFilters, types.StringType)

// Convert influencers
var influencersDiags diag.Diagnostics
analysisConfigTF.Influencers, influencersDiags = typeutils.NonEmptyListOrDefault(ctx, analysisConfigTF.Influencers, types.StringType, apiConfig.Influencers)
diags.Append(influencersDiags...)
// If the existing value was an untyped zero-value list (common during import), force a typed null list.
if analysisConfigTF.Influencers.ElementType(ctx) == nil {
analysisConfigTF.Influencers = types.ListNull(types.StringType)
} else if _, ok := analysisConfigTF.Influencers.ElementType(ctx).(basetypes.DynamicType); ok {
analysisConfigTF.Influencers = types.ListNull(types.StringType)
}
// Ensure the list is properly typed (handles untyped zero-value lists from import)
analysisConfigTF.Influencers = typeutils.EnsureTypedList(ctx, analysisConfigTF.Influencers, types.StringType)

// Convert detectors
if len(apiConfig.Detectors) > 0 {
Expand Down Expand Up @@ -449,12 +441,8 @@ func (tfModel *AnomalyDetectionJobTFModel) convertAnalysisConfigFromAPI(ctx cont
var customRulesDiags diag.Diagnostics
detectorsTF[i].CustomRules, customRulesDiags = typeutils.NonEmptyListOrDefault(ctx, originalDetector.CustomRules, types.ObjectType{AttrTypes: getCustomRuleAttrTypes()}, apiConfig.Detectors[i].CustomRules)
diags.Append(customRulesDiags...)
// If the existing value was an untyped zero-value list (common during import), force a typed null list.
if detectorsTF[i].CustomRules.ElementType(ctx) == nil {
detectorsTF[i].CustomRules = types.ListNull(types.ObjectType{AttrTypes: getCustomRuleAttrTypes()})
} else if _, ok := detectorsTF[i].CustomRules.ElementType(ctx).(basetypes.DynamicType); ok {
detectorsTF[i].CustomRules = types.ListNull(types.ObjectType{AttrTypes: getCustomRuleAttrTypes()})
}
// Ensure the list is properly typed (handles untyped zero-value lists from import)
detectorsTF[i].CustomRules = typeutils.EnsureTypedList(ctx, detectorsTF[i].CustomRules, types.ObjectType{AttrTypes: getCustomRuleAttrTypes()})
}
analysisConfigTF.Detectors = detectorsTF
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package anomaly_detection_job

import (
"context"
"strings"

"github.com/elastic/terraform-provider-elasticstack/internal/clients"
fwdiags "github.com/hashicorp/terraform-plugin-framework/diag"
Expand Down
20 changes: 20 additions & 0 deletions internal/utils/typeutils/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
)

func NonEmptyListOrDefault[T any](ctx context.Context, original types.List, elemType attr.Type, slice []T) (types.List, diag.Diagnostics) {
Expand All @@ -15,3 +16,22 @@ func NonEmptyListOrDefault[T any](ctx context.Context, original types.List, elem

return types.ListValueFrom(ctx, elemType, slice)
}

// EnsureTypedList converts untyped zero-value lists to properly typed null lists.
// This is commonly needed during import operations where the framework may create
// untyped lists with DynamicPseudoType elements, which causes type conversion errors.
// If the list already has a proper type, it is returned unchanged.
func EnsureTypedList(ctx context.Context, list types.List, elemType attr.Type) types.List {
// Check if the list has no element type (nil)
if list.ElementType(ctx) == nil {
return types.ListNull(elemType)
}

// Check if the list has a dynamic pseudo type
if _, ok := list.ElementType(ctx).(basetypes.DynamicType); ok {
return types.ListNull(elemType)
}

// List is already properly typed, return as-is
return list
}