Skip to content

Commit dc6fb13

Browse files
committed
[bugfix] fix child classes to be queried only when apic versions allows
1 parent 35bb5d5 commit dc6fb13

File tree

297 files changed

+12281
-8845
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

297 files changed

+12281
-8845
lines changed

docs/data-sources/bridge_domain.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ data "aci_bridge_domain" "example_tenant" {
161161
* `tags` - (list) A list of Tags (ACI object [tagTag](https://pubhub.devnetcloud.com/media/model-doc-latest/docs/app/index.html#/objects/tagTag/overview)). This attribute is supported in ACI versions: 3.2(1l) and later.
162162
* `key` (key) - (string) The key used to uniquely identify this configuration object.
163163
* `value` (value) - (string) The value of the property.
164-
* `relation_to_dhcp_relay_policy` - (map) A map of Relation From Bridge Domain To DHCP Relay Policy (ACI object [fvRsBDToRelayP](https://pubhub.devnetcloud.com/media/model-doc-latest/docs/app/index.html#/objects/fvRsBDToRelayP/overview)) pointing to DHCP Relay Policy (ACI Object [dhcpRelayP](https://pubhub.devnetcloud.com/media/model-doc-latest/docs/app/index.html#/objects/dhcpRelayP/overview)).
164+
* `relation_to_dhcp_relay_policy` - (map) A map of Relation From Bridge Domain To DHCP Relay Policy (ACI object [fvRsBDToRelayP](https://pubhub.devnetcloud.com/media/model-doc-latest/docs/app/index.html#/objects/fvRsBDToRelayP/overview)) pointing to DHCP Relay Policy (ACI Object [dhcpRelayP](https://pubhub.devnetcloud.com/media/model-doc-latest/docs/app/index.html#/objects/dhcpRelayP/overview)). This attribute is supported in ACI versions: 1.0(1e) to 6.1(1e)
165165
* `annotation` (annotation) - (string) The annotation of the Relation From Bridge Domain To DHCP Relay Policy object. This attribute is supported in ACI versions: 3.2(1l) and later.
166166
* `dhcp_relay_policy_name` (tnDhcpRelayPName) - (string) The name of the DHCP Relay Policy object.
167167
* `annotations` - (list) A list of Annotations (ACI object [tagAnnotation](https://pubhub.devnetcloud.com/media/model-doc-latest/docs/app/index.html#/objects/tagAnnotation/overview)). This attribute is supported in ACI versions: 3.2(1l) and later.

docs/resources/bridge_domain.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ All examples for the Bridge Domain resource can be found in the [examples](https
557557

558558
* `key` (key) - (string) The key used to uniquely identify this configuration object.
559559
* `value` (value) - (string) The value of the property.
560-
* `relation_to_dhcp_relay_policy` - (map) A map of Relation From Bridge Domain To DHCP Relay Policy (ACI object [fvRsBDToRelayP](https://pubhub.devnetcloud.com/media/model-doc-latest/docs/app/index.html#/objects/fvRsBDToRelayP/overview)) pointing to DHCP Relay Policy (ACI Object [dhcpRelayP](https://pubhub.devnetcloud.com/media/model-doc-latest/docs/app/index.html#/objects/dhcpRelayP/overview)) which can be configured using the [aci_dhcp_relay_policy](https://registry.terraform.io/providers/CiscoDevNet/aci/latest/docs/resources/dhcp_relay_policy) resource.
560+
* `relation_to_dhcp_relay_policy` - (map) A map of Relation From Bridge Domain To DHCP Relay Policy (ACI object [fvRsBDToRelayP](https://pubhub.devnetcloud.com/media/model-doc-latest/docs/app/index.html#/objects/fvRsBDToRelayP/overview)) pointing to DHCP Relay Policy (ACI Object [dhcpRelayP](https://pubhub.devnetcloud.com/media/model-doc-latest/docs/app/index.html#/objects/dhcpRelayP/overview)) which can be configured using the [aci_dhcp_relay_policy](https://registry.terraform.io/providers/CiscoDevNet/aci/latest/docs/resources/dhcp_relay_policy) resource. This attribute is supported in ACI versions: 1.0(1e) to 6.1(1e)
561561
#### Optional ####
562562

563563
* `annotation` (annotation) - (string) The annotation of the Relation From Bridge Domain To DHCP Relay Policy object. This attribute is supported in ACI versions: 3.2(1l) and later.

gen/definitions/classes.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,7 @@ fvRsBDToProfile:
10221022

10231023
fvRsBDToRelayP:
10241024
resource_name: "relation_from_bridge_domain_to_dhcp_relay_policy"
1025+
class_version: "1.0(1e)-6.1(1e)"
10251026

10261027
fvRsCtx:
10271028
resource_name: "relation_to_vrf"
@@ -1082,7 +1083,7 @@ commPol:
10821083
sub_category: "Fabric Policies"
10831084
ui_locations:
10841085
- "Fabric -> Fabric Policies -> Policies -> Pod -> Management Access"
1085-
class_version: "6.0(2h)-6.0(8g),6.1(3f)-"
1086+
class_version_tests: "6.0(2h)-6.0(8g),6.1(3f)-"
10861087
contained_by:
10871088
- "polUni"
10881089
rn_prepend: "fabric"

gen/generator.go

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,63 @@ var templateFuncs = template.FuncMap{
155155
"getCustomTestDependency": GetCustomTestDependency,
156156
"getIgnoreInLegacy": GetIgnoreInLegacy,
157157
"isSensitiveAttribute": IsSensitiveAttribute,
158+
"getChildVersion": GetChildVersion,
159+
"getChildClassNames": GetChildClassNames,
160+
"getLegacyAttributeVersion": GetLegacyAttributeVersion,
161+
"getLegacyAttributeVersionInTest": GetLegacyAttributeVersionInTest,
162+
}
163+
164+
func GetChildClassNames(model Model, childClassNames []string) []string {
165+
166+
if childClassNames == nil {
167+
childClassNames = []string{}
168+
}
169+
170+
for _, child := range model.Children {
171+
if !slices.Contains(childClassNames, child.PkgName) {
172+
childClassNames = append(childClassNames, child.PkgName)
173+
}
174+
175+
if child.HasChild {
176+
childClassNames = GetChildClassNames(child, childClassNames)
177+
}
178+
179+
}
180+
sort.Strings(childClassNames)
181+
return childClassNames
182+
}
183+
184+
func GetLegacyAttributeVersion(attributename string, model Model) string {
185+
186+
for _, property := range model.Properties {
187+
if property.Name == attributename {
188+
return property.RawVersion
189+
}
190+
}
191+
192+
for _, child := range model.Children {
193+
if child.ResourceClassName == attributename {
194+
return child.RawVersions
195+
}
196+
}
197+
198+
return "no_version_found"
199+
}
200+
201+
func GetLegacyAttributeVersionInTest(attributename string, versions interface{}) string {
202+
203+
if version, ok := versions.(map[interface{}]interface{})[attributename]; ok {
204+
return version.(string)
205+
}
206+
return "no_version_found"
207+
}
208+
209+
func GetChildVersion(children []interface{}) string {
210+
211+
if versions, ok := children[0].(map[interface{}]interface{})["child_versions"]; ok {
212+
return versions.(string)
213+
}
214+
return ""
158215
}
159216

160217
func IsSensitiveAttribute(attributeName string, properties map[string]Property) bool {
@@ -524,6 +581,7 @@ func GetNewChildAttributes(legacyAttributes map[string]LegacyAttribute, properti
524581
var labels = []string{"dns_provider", "filter_entry"}
525582
var duplicateLabels = []string{}
526583
var resourceNames = map[string]string{}
584+
var classVersions = map[string]string{}
527585
var targetRelationalPropertyClasses = map[string]string{}
528586
var alwaysIncludeChildren = []string{"tag:Annotation", "tag:Tag"}
529587
var excludeChildResourceNamesFromDocs = []string{"", "annotation", "tag"}
@@ -969,6 +1027,8 @@ func renderTemplate(templateName, outputFileName, outputPath string, outputData
9691027
err = tmpl.Execute(&buffer, outputData.([]string))
9701028
} else if strings.Contains(templateName, "custom_type.go.tmpl") {
9711029
err = tmpl.Execute(&buffer, outputData.(Property))
1030+
} else if strings.Contains(templateName, "class_versions.go.tmpl") {
1031+
err = tmpl.Execute(&buffer, outputData.(map[string]string))
9721032
} else {
9731033
err = tmpl.Execute(&buffer, outputData.(Model))
9741034
}
@@ -1277,6 +1337,7 @@ func main() {
12771337
classModels := getClassModels(definitions)
12781338
annotationUnsupported := generateAnnotationUnsupported()
12791339

1340+
renderTemplate("class_versions.go.tmpl", "versions.go", providerPath, classVersions)
12801341
renderTemplate("provider.go.tmpl", "provider.go", providerPath, classModels)
12811342
renderTemplate("index.md.tmpl", "index.md", docsPath, ProviderModel{Example: string(getExampleCode(providerExamplePath))})
12821343
if len(annotationUnsupported) > 0 {
@@ -2027,15 +2088,20 @@ func (m *Model) SetClassComment(classDetails interface{}) {
20272088
}
20282089

20292090
func (m *Model) SetClassVersions(classDetails interface{}) {
2030-
versions, ok := classDetails.(map[string]interface{})["versions"]
2031-
if ok {
2032-
m.RawVersions = versions.(string)
2033-
m.Versions = formatVersion(versions.(string))
2091+
2092+
m.RawVersions = GetOverwriteClassVersion(m.PkgName, m.Definitions, false)
2093+
if m.RawVersions == "" {
2094+
v, ok := classDetails.(map[string]interface{})["versions"]
2095+
if ok {
2096+
m.RawVersions = v.(string)
2097+
}
20342098
}
2099+
m.Versions = formatVersion(m.RawVersions)
2100+
classVersions[m.PkgName] = m.RawVersions
20352101
}
20362102

20372103
func (m *Model) SetTestApplicableFromVersion(classDetails interface{}) {
2038-
m.ClassVersion = GetOverwriteClassVersion(m.PkgName, m.Definitions)
2104+
m.ClassVersion = GetOverwriteClassVersion(m.PkgName, m.Definitions, true)
20392105
if m.ClassVersion == "" {
20402106
versions, ok := classDetails.(map[string]interface{})["versions"]
20412107
if ok {
@@ -2795,10 +2861,16 @@ func GetOverwriteTestType(classPkgName string, definitions Definitions) string {
27952861
return ""
27962862
}
27972863

2798-
func GetOverwriteClassVersion(classPkgName string, definitions Definitions) string {
2864+
func GetOverwriteClassVersion(classPkgName string, definitions Definitions, tests bool) string {
2865+
2866+
matchKey := "class_version"
2867+
if tests {
2868+
matchKey = "class_version_tests"
2869+
}
2870+
27992871
if v, ok := definitions.Classes[classPkgName]; ok {
28002872
for key, value := range v.(map[interface{}]interface{}) {
2801-
if key.(string) == "class_version" {
2873+
if key.(string) == matchKey {
28022874
return value.(string)
28032875
}
28042876
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Code generated by "gen/generator.go"; DO NOT EDIT.
2+
// In order to regenerate this file execute `go generate` from the repository root.
3+
// More details can be found in the [README](https://github.com/CiscoDevNet/terraform-provider-aci/blob/master/README.md).
4+
5+
package provider
6+
7+
8+
func classVersions() map[string]string {
9+
return map[string]string{
10+
{{- range $key, $value := . }}
11+
"{{$key}}": "{{$value}}",
12+
{{- end}}
13+
}
14+
}

gen/templates/provider.go.tmpl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
)
2929

3030
var globalAnnotation string
31+
var apicVersion string
3132
var globalAllowExistingOnCreate bool
3233

3334
// Ensure AciProvider satisfies various provider interfaces.
@@ -199,6 +200,8 @@ func (p *AciProvider) Configure(ctx context.Context, req provider.ConfigureReque
199200
aciClient = client.GetClient(url, username, client.PrivateKey(privateKey), client.AdminCert(certName), client.Insecure(isInsecure), client.ProxyUrl(proxyUrl), client.ProxyCreds(proxyCreds), client.ValidateRelationDn(validateRelationDn), client.MaxRetries(maxRetries))
200201
}
201202

203+
apicVersion = getVersionAPIC(ctx, &resp.Diagnostics, aciClient)
204+
202205
resp.DataSourceData = aciClient
203206
resp.ResourceData = aciClient
204207
}
@@ -354,3 +357,37 @@ func (p *AciProvider) Functions(ctx context.Context) []func() function.Function
354357
NewCompareVersionsFunction,
355358
}
356359
}
360+
361+
func getVersionAPIC(ctx context.Context, diags *diag.Diagnostics, client *client.Client) string {
362+
requestData := DoRestRequest(ctx, diags, client, fmt.Sprintf("/api/node/class/topSystem.json"), "GET", nil)
363+
if diags.HasError() {
364+
return ""
365+
}
366+
367+
if requestData.Search("imdata").Search("topSystem").Data() != nil {
368+
attributes := requestData.Search("imdata").Search("topSystem").Search("attributes").Data().([]interface{})
369+
var versions []string
370+
for _, attributeMap := range attributes {
371+
if role, ok := attributeMap.(map[string]interface{})["role"]; ok && role == "controller" {
372+
if v, ok := attributeMap.(map[string]interface{})["version"]; ok {
373+
versions = append(versions, v.(string))
374+
375+
}
376+
}
377+
}
378+
if len(versions) == 1 {
379+
return versions[0]
380+
}
381+
diags.AddError(
382+
"Controller version mismatch detected",
383+
fmt.Sprintf("The versions of the APIC controllers must all match. Versions found: %s", versions),
384+
)
385+
} else {
386+
diags.AddError(
387+
"Data for topSysytem class could not be retrieved",
388+
fmt.Sprintf("The versions of the APIC controllers could not be determined"),
389+
)
390+
}
391+
392+
return ""
393+
}

gen/templates/resource.go.tmpl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2045,7 +2045,8 @@ func (r *{{.ResourceClassName}}Resource) ImportState(ctx context.Context, req re
20452045

20462046
func getAndSet{{.ResourceClassName}}Attributes(ctx context.Context, diags *diag.Diagnostics, client *client.Client, data *{{.ResourceClassName}}ResourceModel) {
20472047
{{- if .HasChild}}
2048-
requestData := DoRestRequest(ctx, diags, client, fmt.Sprintf("api/mo/%s.json?rsp-subtree=full&rsp-subtree-class=%s", data.Id.ValueString(), "{{- .PkgName}},{{ template "rspSubtreeClassFilter" . }}"), "GET", nil)
2048+
childClasses := getChildClassesForGetRequest([]string{ {{ range (getChildClassNames . nil)}}"{{.}}",{{ end}} })
2049+
requestData := DoRestRequest(ctx, diags, client, fmt.Sprintf("api/mo/%s.json?rsp-subtree=full&rsp-subtree-class=%s", data.Id.ValueString(), strings.Join(childClasses, ",")), "GET", nil)
20492050
{{- else}}
20502051
requestData := DoRestRequest(ctx, diags, client, fmt.Sprintf("api/mo/%s.json", data.Id.ValueString()), "GET", nil)
20512052
{{- end}}

0 commit comments

Comments
 (0)