Skip to content

Commit

Permalink
[ignore] Made changes to resource template to include child attribute…
Browse files Browse the repository at this point in the history
…s in the child payload only when defined in the config
  • Loading branch information
shrsr committed Apr 23, 2024
1 parent 711e5dc commit d9a11c3
Show file tree
Hide file tree
Showing 13 changed files with 234 additions and 219 deletions.
70 changes: 36 additions & 34 deletions gen/templates/resource.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -491,16 +491,15 @@ func getAndSet{{.ResourceClassName}}Attributes(ctx context.Context, diags *diag.
{{- end}}
{{- end}}
{{- end}}
{{/* $enteredGrandChildGetAndSetAttributesZone is used here because the presence of grand children is unknown while rendering the template unlike .HasChild used for the immediate children. It is also used because there can be more than one grandchild in the same level*/}}
{{/* $enteredGrandChildGetAndSetAttributesZone is used here because the presence of grand children is unknown while rendering the template unlike .HasChild used for the immediate children. It is also used because there can be more than one grandchild in the same level. */}}
{{- $enteredGrandChildGetAndSetAttributesZone := false }}
{{- range .Children}}
{{- if not $enteredGrandChildGetAndSetAttributesZone }}
{{- $enteredGrandChildGetAndSetAttributesZone = true }}
}
_, childrenOf{{.ParentName}}Exist:= childClassDetails.(map[string]interface{})["children"]
childrenOf{{.ParentName}}, childrenOf{{.ParentName}}Exist:= childClassDetails.(map[string]interface{})["children"]
if childrenOf{{.ParentName}}Exist {
childClassesOf{{.ParentName}} := childClassDetails.(map[string]interface{})["children"].([]interface{})
for _, child{{.ParentName}} := range childClassesOf{{.ParentName}} {
for _, child{{.ParentName}} := range childrenOf{{.ParentName}}.([]interface{}) {
for childClassName{{.ParentName}}, childClassDetails{{.ParentName}} := range child{{.ParentName}}.(map[string]interface{}) {
{{- end}}
{{- template "getAndSetGrandChildren" . }}
Expand All @@ -510,12 +509,8 @@ func getAndSet{{.ResourceClassName}}Attributes(ctx context.Context, diags *diag.
}
}
{{- range .Children}}
if len({{.ResourceClassName}}{{.ParentName}}List) > 0 {
{{.ResourceClassName}}Set,_:=types.SetValueFrom(ctx,{{.ResourceClassName}}{{.ParentName}}ResourceModelElementType(),{{.ResourceClassName}}{{.ParentName}}List)
{{.ParentName}}{{.GrandParentName}}.{{.ResourceClassName}}={{.ResourceClassName}}Set
} else{
{{.ParentName}}{{.GrandParentName}}.{{.ResourceClassName}}=types.SetNull({{.ResourceClassName}}{{.ParentName}}ResourceModelElementType())
}
{{- end}}
{{- else}}
}
Expand Down Expand Up @@ -592,7 +587,7 @@ func set{{.ResourceClassName}}Id(ctx context.Context, data *{{.ResourceClassName

{{ range .Children}}
func get{{$.ResourceClassName}}{{ .ResourceClassName }}ChildPayloads(ctx context.Context, diags *diag.Diagnostics, data *{{$.ResourceClassName}}ResourceModel, {{.PkgName}}Plan, {{.PkgName}}State []{{.ResourceClassName}}{{$.ResourceClassName}}ResourceModel) []map[string]interface{} {
childMap := newAciObjectType()
childMap := NewAciObject()
childPayloads := []map[string]interface{}{}
{{- $enteredGrandChildDeclarationZone := false }}
{{- range .Children}}
Expand All @@ -605,13 +600,13 @@ func get{{$.ResourceClassName}}{{ .ResourceClassName }}ChildPayloads(ctx context
{{.ParentName}}Children := make([]map[string]interface{}, 0)
{{- end}}
{{- end}}
if !data.{{.ResourceClassName}}.IsUnknown() {
if !data.{{.ResourceClassName}}.IsNull() && !data.{{.ResourceClassName}}.IsUnknown() {
{{- if .IdentifiedBy}}
{{ .PkgName }}Identifiers := []{{.ResourceClassName}}Identifier{}
{{- end}}
for _, {{ .PkgName }} := range {{ .PkgName }}Plan {
{{- range .Properties}}
if !{{ .PkgName }}.{{- if eq .Name "Id"}}{{.ResourceClassName}}{{ .Name }}{{- else}}{{.Name}}{{- end}}.IsUnknown(){
if !{{ .PkgName }}.{{- if eq .Name "Id"}}{{.ResourceClassName}}{{ .Name }}{{- else}}{{.Name}}{{- end}}.IsNull() && !{{ .PkgName }}.{{- if eq .Name "Id"}}{{.ResourceClassName}}{{ .Name }}{{- else}}{{.Name}}{{- end}}.IsUnknown(){
{{- if eq .ValueType "bitmask"}}
var tmp{{ .Name }} []string
{{ .PkgName }}.{{ .Name }}.ElementsAs(ctx, &tmp{{ .Name }}, false)
Expand Down Expand Up @@ -662,7 +657,7 @@ func get{{$.ResourceClassName}}{{ .ResourceClassName }}ChildPayloads(ctx context
}
}
if delete {
{{.PkgName}}ChildMapForDelete := newAciObjectType()
{{.PkgName}}ChildMapForDelete := NewAciObject()
{{.PkgName}}ChildMapForDelete.Attributes["status"] = "deleted"
{{- range .Properties}}
{{- if .IsNaming }}
Expand All @@ -675,7 +670,7 @@ func get{{$.ResourceClassName}}{{ .ResourceClassName }}ChildPayloads(ctx context
{{- else}}
if len({{ .PkgName }}Plan) == 0 && len({{ .PkgName }}State) == 1 {
{{- if .AllowDelete}}
{{.PkgName}}ChildMapForDelete := newAciObjectType()
{{.PkgName}}ChildMapForDelete := NewAciObject()
{{.PkgName}}ChildMapForDelete.Attributes["status"] = "deleted"
childPayloads = append(childPayloads, map[string]interface{}{"{{ .PkgName }}": {{.PkgName}}ChildMapForDelete})
{{- else}}
Expand Down Expand Up @@ -746,6 +741,9 @@ func get{{.ResourceClassName}}CreateJsonPayload(ctx context.Context, diags *diag
return jsonPayload
}


{{/* Sub Templates */}}

{{/* A sub template for the child structs used in the structs definiton section. */}}
{{- define "childStructsAndAttributeTypes" }}
// {{.ResourceClassName}}{{.ParentName}}ResourceModel describes the resource data model for the children without relation ships.
Expand All @@ -763,6 +761,7 @@ type {{.ResourceClassName}}{{.ParentName}}ResourceModel struct {
{{ .ResourceClassName }} types.Set `tfsdk:"{{- .ResourceName}}"`
{{- end}}
}

func {{.ResourceClassName}}{{.ParentName}}ResourceModelAttributeTypes() map[string]attr.Type {
return map[string]attr.Type{
{{- range .Properties}}
Expand All @@ -779,6 +778,7 @@ func {{.ResourceClassName}}{{.ParentName}}ResourceModelAttributeTypes() map[stri
{{- end}}
}
}

func {{.ResourceClassName}}{{.ParentName}}ResourceModelElementType() attr.TypeWithAttributeTypes {
return basetypes.ObjectType.WithAttributeTypes(basetypes.ObjectType{},{{.ResourceClassName}}{{.ParentName}}ResourceModelAttributeTypes())
}
Expand All @@ -787,6 +787,7 @@ func {{.ResourceClassName}}{{.ParentName}}ResourceModelElementType() attr.TypeWi
{{- end}}
{{- end}}


{{/* A sub template for the child attributes used in the Attributes block. */}}
{{- define "childAttributes" }}
"{{- .ResourceName }}": schema.SetNestedAttribute{
Expand Down Expand Up @@ -851,14 +852,16 @@ func {{.ResourceClassName}}{{.ParentName}}ResourceModelElementType() attr.TypeWi
{{- end }}
{{- end }}


{{/* A sub template for the grand child attributes used in the Payload function. */}}
{{- define "grandChildPayload" }}
var {{.PkgName}}Plan []{{.ResourceClassName}}{{.ParentName}}ResourceModel
var {{.PkgName}}Plan, {{.PkgName}}State []{{.ResourceClassName}}{{.ParentName}}ResourceModel
{{.ParentName | lowerFirstCharacter}}.{{.ResourceClassName}}.ElementsAs(ctx, &{{.PkgName}}Plan, false)
for _, {{.ParentName | lowerFirstCharacter}}{{.ResourceClassName | lowerFirstCharacter}} := range {{.ParentName | lowerFirstCharacter}}State {
if len({{ .PkgName }}Plan) == 0 && len({{.ParentName | lowerFirstCharacter}}{{.ResourceClassName | lowerFirstCharacter}}.{{.ResourceClassName}}.Elements()) == 1 {
for _, {{.ParentName | lowerFirstCharacter}}{{.ResourceClassName | lowerFirstCharacter}}State := range {{.ParentName | lowerFirstCharacter}}State {
{{.ParentName | lowerFirstCharacter}}{{.ResourceClassName | lowerFirstCharacter}}State.{{.ResourceClassName}}.ElementsAs(ctx, &{{.PkgName}}State, false)
if len({{ .PkgName }}Plan) == 0 && len({{.PkgName}}State) == 1 {
{{- if .AllowDelete}}
{{.PkgName}}ChildMap := newAciObjectType()
{{.PkgName}}ChildMap := NewAciObject()
{{.PkgName}}ChildMap.Attributes["status"] = "deleted"
{{ .ParentName }}Children = append({{ .ParentName }}Children, map[string]interface{}{"{{.PkgName}}": {{.PkgName}}ChildMap})
{{- else}}
Expand All @@ -870,10 +873,10 @@ if len({{ .PkgName }}Plan) == 0 && len({{.ParentName | lowerFirstCharacter}}{{.R
{{- end }}
}
}
if !{{ .ParentName | lowerFirstCharacter}}.{{.ResourceClassName}}.IsUnknown(){
if !{{ .ParentName | lowerFirstCharacter}}.{{.ResourceClassName}}.IsNull() && !{{ .ParentName | lowerFirstCharacter}}.{{.ResourceClassName}}.IsUnknown(){
for _, {{ .PkgName }} := range {{.PkgName}}Plan {
{{- range .Properties}}
if !{{ .PkgName }}.{{- if eq .Name "Id"}}{{.ResourceClassName}}{{ .Name }}{{- else}}{{.Name}}{{- end}}.IsUnknown(){
if !{{ .PkgName }}.{{- if eq .Name "Id"}}{{.ResourceClassName}}{{ .Name }}{{- else}}{{.Name}}{{- end}}.IsNull() && !{{ .PkgName }}.{{- if eq .Name "Id"}}{{.ResourceClassName}}{{ .Name }}{{- else}}{{.Name}}{{- end}}.IsUnknown(){
{{- if eq .ValueType "bitmask"}}
var tmp{{ .Name }} []string
{{ .PkgName }}.{{ .Name }}.ElementsAs(ctx, &tmp{{ .Name }}, false)
Expand All @@ -896,7 +899,7 @@ for _, {{ .PkgName }} := range {{.PkgName}}Plan {
{{- end }}



{{/* A sub template to declare child lists in the get and set attributes function. */}}
{{- define "declareChildListsInGetandSetAttributesFunction" }}
{{.ResourceClassName}}{{.ParentName}} := {{ .ResourceClassName}}{{.ParentName}}ResourceModel{}
{{- if .IdentifiedBy}}
Expand All @@ -910,6 +913,7 @@ for _, {{ .PkgName }} := range {{.PkgName}}Plan {
{{- end}}


{{/* A sub template for the grand child attributes used in the get and set attributes function. */}}
{{- define "getAndSetGrandChildren" }}
if childClassName{{.ParentName}} == "{{.PkgName}}"{
{{.PkgName}}childAttributeValue:= childClassDetails{{.ParentName}}.(map[string]interface{})["attributes"].(map[string]interface{})
Expand Down Expand Up @@ -959,10 +963,9 @@ for childAttributeName, childAttributeValue := range {{.PkgName}}childAttributeV
{{- if not $enteredGrandChildGetAndSetAttributesZone }}
{{- $enteredGrandChildGetAndSetAttributesZone = true }}
}
_, childrenOf{{.ParentName}}:= childClassDetails.(map[string]interface{})["children"]
if childrenOf{{.ParentName}} {
childClassesOf{{.ParentName}} := childClassDetails.(map[string]interface{})["children"].([]interface{})
for _, child{{.ParentName}} := range childClassesOf{{.ParentName}} {
childrenOf{{.ParentName}}, childrenOf{{.ParentName}}Exist:= childClassDetails.(map[string]interface{})["children"]
if childrenOf{{.ParentName}}Exist {
for _, child{{.ParentName}} := range childrenOf{{.ParentName}}.([]interface{}) {
for childClassName{{.ParentName}}, childClassDetails{{.ParentName}} := range child{{.ParentName}}.(map[string]interface{}) {
{{- end}}
{{- template "getAndSetGrandChildren" . }}
Expand All @@ -972,12 +975,8 @@ for childAttributeName, childAttributeValue := range {{.PkgName}}childAttributeV
}
}
{{- range .Children}}
if len({{.ResourceClassName}}{{.ParentName}}List) > 0 {
{{.ResourceClassName}}Set,_:=types.SetValueFrom(ctx,{{.ResourceClassName}}{{.ParentName}}ResourceModelElementType(),{{.ResourceClassName}}{{.ParentName}}List)
{{.ParentName}}{{.GrandParentName}}.{{.ResourceClassName}}={{.ResourceClassName}}Set
} else{
{{.ParentName}}{{.GrandParentName}}.{{.ResourceClassName}}=types.SetNull({{.ResourceClassName}}{{.ParentName}}ResourceModelElementType())
}
{{- end}}
{{- else}}
}
Expand All @@ -986,21 +985,25 @@ for childAttributeName, childAttributeValue := range {{.PkgName}}childAttributeV
}
{{- end }}


{{/* A sub template for grand child declaration in the child payload function. */}}
{{- define "grandChildDeclarationInChildPayloadFunction" }}
{{.PkgName}}ChildMap := newAciObjectType()
{{.PkgName}}ChildMap := NewAciObject()
{{- $enteredGrandChildDeclarationZone := false }}
{{- range .Children}}
{{- $enteredGrandChildDeclarationZone = true }}
{{- $enteredGrandChildDeclarationZone = true }}
{{- template "grandChildDeclarationInChildPayloadFunction" . }}
{{- end }}
{{- range .Children}}
{{- if $enteredGrandChildDeclarationZone }}
{{- $enteredGrandChildDeclarationZone = false }}
{{- if $enteredGrandChildDeclarationZone }}
{{- $enteredGrandChildDeclarationZone = false }}
{{.ParentName}}Children := make([]map[string]interface{}, 0)
{{- end}}
{{- end}}
{{- end}}
{{- end}}


{{/* A sub template for joining all the class names as a single string. */}}
{{- define "rspSubtreeClassFilter" -}}
{{- $currentClasses := listToString .ChildClasses -}}
{{- $currentClasses -}}
Expand All @@ -1010,4 +1013,3 @@ for childAttributeName, childAttributeValue := range {{.PkgName}}childAttributeV
{{- end -}}
{{- end -}}
{{- end -}}

Loading

0 comments on commit d9a11c3

Please sign in to comment.