Skip to content

Commit

Permalink
[ignore] fix test dependencies for templates with legacy attributes, …
Browse files Browse the repository at this point in the history
…and add support for reference attributes of dependencies in tests
  • Loading branch information
akinross committed Jan 23, 2025
1 parent 46f1696 commit 174fc57
Show file tree
Hide file tree
Showing 12 changed files with 486 additions and 129 deletions.
61 changes: 61 additions & 0 deletions gen/definitions/properties.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,9 @@ fvRsScope:
parent_dependency: "fvAp"
- class_name: "fvAEPg"
parent_dependency: "fvAp"
test_values:
all:
vrf_name: "default"

fvRsCons:
ignores:
Expand Down Expand Up @@ -1463,6 +1466,9 @@ fvRsABDPolMonPol:
relation_from_bridge_domain_to_monitoring_policy: "relation_to_monitor_policy"
documentation:
tnMonEPGPolName: "The name of the Monitoring Policy object."
test_values:
all:
monitoring_policy_name: "default"

fvRsBdToEpRet:
overwrites:
Expand All @@ -1471,18 +1477,27 @@ fvRsBdToEpRet:
documentation:
tnFvEpRetPolName: "The name of the Endpoint Retention Policy object."
resolveAct: "The action to take for resolving the Endpoint Retention Policy object."
test_values:
all:
end_point_retention_policy_name: "default"

fvRsBDToFhs:
overwrites:
relation_from_bridge_domain_to_first_hop_security_policy: "relation_to_first_hop_security_policy"
documentation:
tnFhsBDPolName: "The name of the First Hop Security Policy object."
test_values:
all:
first_hop_security_policy_name: "default"

fvRsBDToNdP:
overwrites:
relation_from_bridge_domain_to_neighbor_discovery_interface_policy: "relation_to_neighbor_discovery_interface_policy"
documentation:
tnNdIfPolName: "The name of the Neighbor Discovery Interface Policy object."
test_values:
all:
neighbor_discovery_interface_policy_name: "default"

fvRsBDToNetflowMonitorPol:
overwrites:
Expand Down Expand Up @@ -1518,12 +1533,52 @@ fvRsBDToOut:
fvRsBDToProfile:
overwrites:
relation_from_bridge_domain_to_route_control_profile: "relation_to_route_control_profile"
targets:
- class_name: "fvCtx"
parent_dependency: "fvTenant"
overwrite_parent_dn_key: "tenant_dn"
target_dn: "uni/tn-test_tenant/ctx-vrf_name_1"
relation_resource_name: "vrf"
properties:
name: "vrf_name_1"
- class_name: "l3extOut"
parent_dependency: "fvTenant"
overwrite_parent_dn_key: "tenant_dn"
target_dn: "uni/tn-test_tenant/out-abr_l3_outside_name_1"
relation_resource_name: "l3_outside"
properties:
name: "l3_outside_name_1"
relation_l3ext_rs_ectx: aci_vrf.test_vrf_0.id
- class_name: "l3extOut"
parent_dependency: "fvTenant"
overwrite_parent_dn_key: "tenant_dn"
target_dn: "uni/tn-test_tenant/out-abr_l3_outside_name_0"
relation_resource_name: "l3_outside"
properties:
name: "l3_outside_name_0"
relation_l3ext_rs_ectx: aci_vrf.test_vrf_0.id
- class_name: "rtctrlProfile"
parent_dependency: "l3extOut"
target_dn: "uni/tn-test_tenant/out-abr_l3_outside_name_0/prof-route_control_profile_name_1"
parent_dependency_dn_ref: aci_l3_outside.test_l3_outside_1.id
relation_resource_name: "route_control_profile"
properties:
name: "route_control_profile_name_1"


fvRsBDToRelayP:
overwrites:
relation_from_bridge_domain_to_dhcp_relay_policy: "relation_to_dhcp_relay_policy"
documentation:
tnDhcpRelayPName: "The name of the DHCP Relay Policy object."
targets:
- class_name: "dhcpRelayP"
parent_dependency: "fvTenant"
overwrite_parent_dn_key: "tenant_dn"
target_dn: "uni/tn-test_tenant/relayp-dhcp_relay_policy_name_1"
relation_resource_name: "dhcp_relay_policy"
properties:
name: "dhcp_relay_policy_name_1"

fvRsCtx:
documentation:
Expand All @@ -1532,10 +1587,16 @@ fvRsCtx:
fvRsIgmpsn:
documentation:
tnIgmpSnoopPolName: "The name of the IGMP Snooping Policy object."
test_values:
all:
igmp_snooping_policy_name: "default"

fvRsMldsn:
documentation:
tnMldSnoopPolName: "The name of the MLD Snooping Policy object."
test_values:
all:
mld_snooping_policy_name: "default"

fvRogueExceptionMac:
test_values:
Expand Down
34 changes: 33 additions & 1 deletion gen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,30 @@ var templateFuncs = template.FuncMap{
"checkDeletableChild": CheckDeletableChild,
"emptyChild": EmptyChild,
"excludeForNullInSetCheck": ExcludeForNullInSetCheck,
"getTestTargetValue": GetTestTargetValue,
"isReference": IsReference,
}

func GetTestTargetValue(targets []interface{}, key, value string) string {

for index, target := range targets {

var resourceName string
if targetResourceName, ok := target.(map[interface{}]interface{})["target_resource_name"]; ok {
resourceName = targetResourceName.(string)
}

if properties, ok := target.(map[interface{}]interface{})["properties"]; ok {

if key == fmt.Sprintf("%s_name", resourceName) {
key = "name"
}
if result, ok := properties.(map[interface{}]interface{})[key]; ok && value == result {
return fmt.Sprintf(`aci_%s.test_%s_%d.%s`, resourceName, resourceName, index%2, key)
}
}
}
return value
}

func ExcludeForNullInSetCheck(resourceClassName string) bool {
Expand Down Expand Up @@ -421,6 +445,10 @@ func ContainsString(s, sub string) bool {
return strings.Contains(s, sub)
}

func IsReference(s string) bool {
return strings.HasPrefix(s, "aci_") || strings.HasPrefix(s, "data.aci_")
}

// Reused from https://github.com/buxizhizhoum/inflection/blob/master/inflection.go#L8 to avoid importing the whole package
func Underscore(s string) string {
for _, reStr := range []string{`([A-Z]+)([A-Z][a-z])`, `([a-z\d])([A-Z])`} {
Expand Down Expand Up @@ -2755,7 +2783,11 @@ func getTestDependency(className string, targetMap map[interface{}]interface{},

if parentDependency, ok := targetMap["parent_dependency"]; ok {
testDependency.ParentDependency = parentDependency.(string)
testDependency.ParentDependencyDnRef = fmt.Sprintf("%s_%s.test.id", providerName, GetResourceName(parentDependency.(string), definitions))
if parentDependencyDn, ok := targetMap["parent_dependency_dn_ref"]; ok {
testDependency.ParentDependencyDnRef = parentDependencyDn.(string)
} else {
testDependency.ParentDependencyDnRef = fmt.Sprintf("%s_%s.test.id", providerName, GetResourceName(parentDependency.(string), definitions))
}
}

if targetDn, ok := targetMap["target_dn"]; ok {
Expand Down
13 changes: 12 additions & 1 deletion gen/templates/resource.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,16 @@ func (r *{{.ResourceClassName}}Resource) ModifyPlan(ctx context.Context, req res
}
{{- end}}
{{- end}}
var stateAttributeValue, planAttributeValue {{$SetName}}{{$.ResourceClassName}}ResourceModel
stateData.{{$SetName}}.As(ctx, &stateAttributeValue, basetypes.ObjectAsOptions{})
planData.{{$SetName}}.As(ctx, &planAttributeValue, basetypes.ObjectAsOptions{})
{{- range .Children}}
if stateAttributeValue.{{ .ResourceClassName }}.IsNull() && attributeValues.{{ .ResourceClassName }}.IsNull() {
planAttributeValue.{{ .ResourceClassName }} = basetypes.NewSetUnknown({{ .ResourceClassName }}{{$SetName}}{{$.ResourceClassName}}Type)
}
{{- end}}
{{$SetName}}Object, _ := types.ObjectValueFrom(ctx, {{$SetName}}{{$.ResourceClassName}}Type, planAttributeValue)
planData.{{$SetName}} = {{$SetName}}Object
}
} else if !configData.Deprecated{{ $SetName }}.IsNull() {
var newAttributeValues {{ $SetName }}{{.ParentHierarchy}}ResourceModel
Expand Down Expand Up @@ -1113,7 +1123,7 @@ func (r *{{.ResourceClassName}}Resource) ModifyPlan(ctx context.Context, req res
{{$HasNewNamedClassAttributeWithTargetDN = true}}
var newAttributeValues []{{ .ResourceClassName }}{{$.ResourceClassName}}ResourceModelV1
if stateData != nil {
stateData.{{.ResourceClassName}}.ElementsAs(ctx, &newAttributeValues, false)
stateData.Deprecated{{.ResourceClassName}}.ElementsAs(ctx, &newAttributeValues, false)
}
{{- end}}
{{- end}}
Expand Down Expand Up @@ -1543,6 +1553,7 @@ func (r *{{.ResourceClassName}}Resource) Schema(ctx context.Context, req resourc
DeprecationMessage: "Attribute `{{.AttributeName}}` is deprecated. The attribute will be removed in the next major version of the provider.",
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
SetToStringNullWhenStateIsNullPlanIsUnknownDuringUpdate(),
},
},
{{- end}}
Expand Down
Loading

0 comments on commit 174fc57

Please sign in to comment.