Skip to content

Commit

Permalink
[ignore] add support for null to plan when state is null and plan unk…
Browse files Browse the repository at this point in the history
…nown attributes inside of sets
  • Loading branch information
akinross committed Jan 23, 2025
1 parent 7702431 commit 46f1696
Show file tree
Hide file tree
Showing 60 changed files with 1,166 additions and 11 deletions.
12 changes: 12 additions & 0 deletions gen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,18 @@ var templateFuncs = template.FuncMap{
"addToChild": AddToChildInTestTemplate,
"checkDeletableChild": CheckDeletableChild,
"emptyChild": EmptyChild,
"excludeForNullInSetCheck": ExcludeForNullInSetCheck,
}

func ExcludeForNullInSetCheck(resourceClassName string) bool {
// Function to exclude TagTag and TagAnnotation from the null check in the Set function
// Done to reduce the amount of functions created which are not needed for these classes
// During refactor to struct per class which is reused in children this is not needed anymore
var childClasses []string
for _, child := range alwaysIncludeChildren {
childClasses = append(childClasses, Capitalize(strings.ReplaceAll(child, ":", "")))
}
return !slices.Contains(childClasses, resourceClassName)
}

func ContainsRequired(properties map[string]Property) bool {
Expand Down
44 changes: 42 additions & 2 deletions gen/templates/resource.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -864,8 +864,8 @@ func (r *{{.ResourceClassName}}Resource) ModifyPlan(ctx context.Context, req res
if GetMOName(stateData.Deprecated{{$SetName}}.ValueString()) == attributeValues.{{ .Name }}.ValueString() && !attributeValues.{{ .Name }}.IsNull() {
planData.Deprecated{{$SetName}} = stateData.Deprecated{{$SetName}}
}
{{- end}}
{{- end}}
{{- end}}
}
} else if !configData.Deprecated{{ $SetName }}.IsNull() {
var newAttributeValues {{ $SetName }}{{.ParentHierarchy}}ResourceModel
Expand Down Expand Up @@ -1623,7 +1623,7 @@ func (r *{{.ResourceClassName}}Resource) Schema(ctx context.Context, req resourc
{{- if not .ReadOnly }}
PlanModifiers: []planmodifier.Set{
setplanmodifier.UseStateForUnknown(),
SetToSetNullWhenStateIsNullPlanIsUnknownDuringUpdate(),
SetToSetNullWhenStateIsNullPlanIsUnknownDuringUpdate(nil),

},
{{- end}}
Expand Down Expand Up @@ -2660,6 +2660,7 @@ func getEmpty{{.ResourceClassName}}{{.ParentHierarchy}}ResourceModel() {{.Resour
{{- end}}
}
}

{{- if or (not .IdentifiedBy) .MaxOneClassAllowed}}
var {{.ResourceClassName}}{{.ParentHierarchy}}Type = map[string]attr.Type{
{{- range .Properties}}
Expand Down Expand Up @@ -2698,6 +2699,40 @@ var {{.ResourceClassName}}{{.ParentHierarchy}}Type = types.ObjectType{
{{- end}}
},
}

{{- if excludeForNullInSetCheck .ResourceClassName}}
func {{.ResourceClassName}}{{.ParentHierarchy}}SetToSetNullWhenStateIsNullPlanIsUnknownDuringUpdate(ctx context.Context, planValue, stateValue types.Set) basetypes.SetValue {
var planSetValues, stateSetValues []{{.ResourceClassName}}{{.ParentHierarchy}}ResourceModel
stateValue.ElementsAs(ctx, &stateSetValues, false)
planValue.ElementsAs(ctx, &planSetValues, false)

// If the length of the state and plan values are different a change is already detected thus reflection can be skipped
if len(stateSetValues) == len(planSetValues) {
for index, stateValue := range stateSetValues {
nullInStateFound := false
{{- range .Properties}}
if stateValue.{{ .Name }}.IsNull() {
nullInStateFound = true
{{- if eq .ValueType "bitmask"}}
planSetValues[index].{{ .Name }} = basetypes.NewSetNull(types.StringType)
{{- else if .HasCustomType}}
planSetValues[index].{{ .Name }} = customTypes.New{{.ResourceClassName}}{{.Name}}StringNull()
{{- else}}
planSetValues[index].{{ .Name }} = basetypes.NewStringNull()
{{- end}}
}
{{- end}}
if !nullInStateFound {
// when there are no null fields we can conclude the version supports all attributes in set
break
}
}
}
planSet, _ := types.SetValueFrom(ctx, {{.ResourceClassName}}{{.ParentHierarchy}}Type, planSetValues)
return planSet

}
{{- end}}
{{- end}}
{{- range .Children}}
{{- template "childStructsAndAttributeTypes" . }}
Expand Down Expand Up @@ -2780,6 +2815,11 @@ var {{.ResourceClassName}}{{.ParentHierarchy}}Type = types.ObjectType{
Computed: true,
PlanModifiers: []planmodifier.Set{
setplanmodifier.UseStateForUnknown(),
{{- if excludeForNullInSetCheck .ResourceClassName}}
SetToSetNullWhenStateIsNullPlanIsUnknownDuringUpdate({{.ResourceClassName}}{{.ParentHierarchy}}SetToSetNullWhenStateIsNullPlanIsUnknownDuringUpdate),
{{- else}}
SetToSetNullWhenStateIsNullPlanIsUnknownDuringUpdate(nil),
{{- end}}
},
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
Expand Down
6 changes: 6 additions & 0 deletions internal/provider/resource_aci_access_interface_override.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 46f1696

Please sign in to comment.