From ff2e90533a70d7dfc6a52dcba265194122f81b43 Mon Sep 17 00:00:00 2001 From: Sean Conroy Date: Wed, 7 Aug 2024 10:54:01 +0100 Subject: [PATCH] Resolve issue #267 --- .../profile_parcels/transport_routing_bgp.yaml | 4 ++-- gen/generator.go | 5 +++++ gen/schema/schema.yaml | 1 + gen/templates/profile_parcels/model.go | 8 ++++++++ .../model_sdwan_transport_routing_bgp_profile_parcel.go | 8 ++++++-- 5 files changed, 22 insertions(+), 4 deletions(-) diff --git a/gen/definitions/profile_parcels/transport_routing_bgp.yaml b/gen/definitions/profile_parcels/transport_routing_bgp.yaml index 1b266ca8..413058f5 100644 --- a/gen/definitions/profile_parcels/transport_routing_bgp.yaml +++ b/gen/definitions/profile_parcels/transport_routing_bgp.yaml @@ -250,7 +250,7 @@ attributes: - model_name: refId tf_name: ipv4_table_map_route_policy_id data_path: [addressFamily, name] - # exclude_null: true + always_include_parent: true exclude_test: true example: a509839f-33aa-492a-ab8e-6a6fe70d7d79 # # test_value: @@ -305,7 +305,7 @@ attributes: - model_name: refId tf_name: ipv6_table_map_route_policy_id data_path: [ipv6AddressFamily, name] - # exclude_null: true + always_include_parent: true exclude_test: true example: a509839f-33aa-492a-ab8e-6a6fe70d7d79 # test_value: diff --git a/gen/generator.go b/gen/generator.go index 2aed2e2f..c0d48ac9 100644 --- a/gen/generator.go +++ b/gen/generator.go @@ -257,6 +257,7 @@ type YamlConfigAttribute struct { TestValue string `yaml:"test_value"` MinimumTestValue string `yaml:"minimum_test_value"` AlwaysInclude bool `yaml:"always_include"` + AlwaysIncludeParent bool `yaml:"always_include_parent"` Attributes []YamlConfigAttribute `yaml:"attributes"` ConditionalAttribute YamlConfigConditionalAttribute `yaml:"conditional_attribute"` ConditionalListLength string `yaml:"conditional_list_length"` @@ -720,6 +721,10 @@ func parseProfileParcelAttribute(attr *YamlConfigAttribute, model gjson.Result, attr.TfName = SnakeCase(attr.ModelName) } + if attr.AlwaysIncludeParent { + fmt.Println(path) + } + if r.Get("type").String() == "object" || !r.Get("type").Exists() { noGlobal := false diff --git a/gen/schema/schema.yaml b/gen/schema/schema.yaml index b2996383..e3759b99 100644 --- a/gen/schema/schema.yaml +++ b/gen/schema/schema.yaml @@ -70,6 +70,7 @@ attribute: test_value: str(required=False) # Value used for acceptance test minimum_test_value: any(str(), int(), bool(), num(), required=False) # Value used for "minimum" resource acceptance test always_include: bool(required=False) # Include attribute always in payload + always_include_parent: bool(required=false) # Include attributes parent always in payload attributes: list(include('attribute'), required=False) # List of attributes, only relevant if type is "List" or "Set" conditional_attribute: include('conditional_attribute', required=False) # Add attribute to payload only if condition is true conditional_list_length: str(required=False) # Add attribute to payload only if referenced list has a length greater than 0 diff --git a/gen/templates/profile_parcels/model.go b/gen/templates/profile_parcels/model.go index 73b0aea3..94ff7d42 100644 --- a/gen/templates/profile_parcels/model.go +++ b/gen/templates/profile_parcels/model.go @@ -171,6 +171,8 @@ func (data {{camelCase .Name}}) toBody(ctx context.Context) string { } else {{end}}{{if and .DefaultValuePresent (not .ExcludeNull)}}if data.{{toGoName .TfName}}.IsNull() { body, _ = sjson.Set(body, path+"{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}.optionType", "default") {{if or .DefaultValue .DefaultValueEmptyString}}body, _ = sjson.Set(body, path+"{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}.value", {{if eq .Type "String"}}"{{end}}{{.DefaultValue}}{{if eq .Type "String"}}"{{end}}){{end}} + } else {{else if .AlwaysIncludeParent }}if data.{{toGoName .TfName}}.IsNull() { + body, _ = sjson.Set(body, path+"{{range .DataPath}}{{.}}.{{end}}optionType", "default") } else {{else}}if !data.{{toGoName .TfName}}.IsNull(){{end}} { body, _ = sjson.Set(body, path+"{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}.optionType", "global") {{- if isListSet .}} @@ -197,6 +199,8 @@ func (data {{camelCase .Name}}) toBody(ctx context.Context) string { } else {{end}}{{if and .DefaultValuePresent (not .ExcludeNull)}}if item.{{toGoName .TfName}}.IsNull() { itemBody, _ = sjson.Set(itemBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}.optionType", "default") {{if or .DefaultValue .DefaultValueEmptyString}}itemBody, _ = sjson.Set(itemBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}.value", {{if eq .Type "String"}}"{{end}}{{.DefaultValue}}{{if eq .Type "String"}}"{{end}}){{end}} + } else {{else if .AlwaysIncludeParent }}if data.{{toGoName .TfName}}.IsNull() { + body, _ = sjson.Set(body, path+"{{range .DataPath}}{{.}}.{{end}}optionType", "default") } else {{else}}if !item.{{toGoName .TfName}}.IsNull(){{end}} { itemBody, _ = sjson.Set(itemBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}.optionType", "global") {{- if isListSet .}} @@ -223,6 +227,8 @@ func (data {{camelCase .Name}}) toBody(ctx context.Context) string { } else {{end}}{{if and .DefaultValuePresent (not .ExcludeNull)}}if childItem.{{toGoName .TfName}}.IsNull() { itemChildBody, _ = sjson.Set(itemChildBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}.optionType", "default") {{if or .DefaultValue .DefaultValueEmptyString}}itemChildBody, _ = sjson.Set(itemChildBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}.value", {{if eq .Type "String"}}"{{end}}{{.DefaultValue}}{{if eq .Type "String"}}"{{end}}){{end}} + } else {{else if .AlwaysIncludeParent }}if data.{{toGoName .TfName}}.IsNull() { + body, _ = sjson.Set(body, path+"{{range .DataPath}}{{.}}.{{end}}optionType", "default") } else {{else}}if !childItem.{{toGoName .TfName}}.IsNull(){{end}} { itemChildBody, _ = sjson.Set(itemChildBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}.optionType", "global") {{- if isListSet .}} @@ -249,6 +255,8 @@ func (data {{camelCase .Name}}) toBody(ctx context.Context) string { } else {{end}}{{if and .DefaultValuePresent (not .ExcludeNull)}}if childChildItem.{{toGoName .TfName}}.IsNull() { itemChildChildBody, _ = sjson.Set(itemChildChildBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}.optionType", "default") {{if or .DefaultValue .DefaultValueEmptyString}}itemChildChildBody, _ = sjson.Set(itemChildChildBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}.value", {{if eq .Type "String"}}"{{end}}{{.DefaultValue}}{{if eq .Type "String"}}"{{end}}){{end}} + } else {{else if .AlwaysIncludeParent }}if data.{{toGoName .TfName}}.IsNull() { + body, _ = sjson.Set(body, path+"{{range .DataPath}}{{.}}.{{end}}optionType", "default") } else {{else}}if !childChildItem.{{toGoName .TfName}}.IsNull(){{end}} { itemChildChildBody, _ = sjson.Set(itemChildChildBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}.optionType", "global") {{- if isListSet .}} diff --git a/internal/provider/model_sdwan_transport_routing_bgp_profile_parcel.go b/internal/provider/model_sdwan_transport_routing_bgp_profile_parcel.go index 597d309f..60395747 100644 --- a/internal/provider/model_sdwan_transport_routing_bgp_profile_parcel.go +++ b/internal/provider/model_sdwan_transport_routing_bgp_profile_parcel.go @@ -937,7 +937,9 @@ func (data TransportRoutingBGP) toBody(ctx context.Context) string { body, _ = sjson.Set(body, path+"addressFamily.originate.optionType", "global") body, _ = sjson.Set(body, path+"addressFamily.originate.value", data.Ipv4Originate.ValueBool()) } - if !data.Ipv4TableMapRoutePolicyId.IsNull() { + if data.Ipv4TableMapRoutePolicyId.IsNull() { + body, _ = sjson.Set(body, path+"addressFamily.name.optionType", "default") + } else { body, _ = sjson.Set(body, path+"addressFamily.name.refId.optionType", "global") body, _ = sjson.Set(body, path+"addressFamily.name.refId.value", data.Ipv4TableMapRoutePolicyId.ValueString()) } @@ -1039,7 +1041,9 @@ func (data TransportRoutingBGP) toBody(ctx context.Context) string { body, _ = sjson.Set(body, path+"ipv6AddressFamily.originate.optionType", "global") body, _ = sjson.Set(body, path+"ipv6AddressFamily.originate.value", data.Ipv6Originate.ValueBool()) } - if !data.Ipv6TableMapRoutePolicyId.IsNull() { + if data.Ipv6TableMapRoutePolicyId.IsNull() { + body, _ = sjson.Set(body, path+"ipv6AddressFamily.name.optionType", "default") + } else { body, _ = sjson.Set(body, path+"ipv6AddressFamily.name.refId.optionType", "global") body, _ = sjson.Set(body, path+"ipv6AddressFamily.name.refId.value", data.Ipv6TableMapRoutePolicyId.ValueString()) }