Skip to content

Commit 0f40f27

Browse files
committed
[bugfix] fix child classes to be queried only when apic versions allows
1 parent eddcc49 commit 0f40f27

File tree

318 files changed

+12794
-10194
lines changed

Some content is hidden

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

318 files changed

+12794
-10194
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/commPol.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class_version: 6.0(2h)-6.0(8g),6.1(3f)-
1+
class_version_tests: 6.0(2h)-6.0(8g),6.1(3f)-
22
contained_by:
33
- polUni
44
resource_name: management_access_policy
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
resource_name: relation_from_bridge_domain_to_dhcp_relay_policy
2+
class_version: "1.0(1e)-6.1(1e)"

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
}
@@ -1293,6 +1353,7 @@ func main() {
12931353
classModels := getClassModels(definitions)
12941354
annotationUnsupported := generateAnnotationUnsupported()
12951355

1356+
renderTemplate("class_versions.go.tmpl", "versions.go", providerPath, classVersions)
12961357
renderTemplate("provider.go.tmpl", "provider.go", providerPath, classModels)
12971358
renderTemplate("index.md.tmpl", "index.md", docsPath, ProviderModel{Example: string(getExampleCode(providerExamplePath))})
12981359
if len(annotationUnsupported) > 0 {
@@ -2055,15 +2116,20 @@ func (m *Model) SetClassComment(classDetails interface{}) {
20552116
}
20562117

20572118
func (m *Model) SetClassVersions(classDetails interface{}) {
2058-
versions, ok := classDetails.(map[string]interface{})["versions"]
2059-
if ok {
2060-
m.RawVersions = versions.(string)
2061-
m.Versions = formatVersion(versions.(string))
2119+
2120+
m.RawVersions = GetOverwriteClassVersion(m.PkgName, m.Definitions, false)
2121+
if m.RawVersions == "" {
2122+
v, ok := classDetails.(map[string]interface{})["versions"]
2123+
if ok {
2124+
m.RawVersions = v.(string)
2125+
}
20622126
}
2127+
m.Versions = formatVersion(m.RawVersions)
2128+
classVersions[m.PkgName] = m.RawVersions
20632129
}
20642130

20652131
func (m *Model) SetTestApplicableFromVersion(classDetails interface{}) {
2066-
m.ClassVersion = GetOverwriteClassVersion(m.PkgName, m.Definitions)
2132+
m.ClassVersion = GetOverwriteClassVersion(m.PkgName, m.Definitions, true)
20672133
if m.ClassVersion == "" {
20682134
versions, ok := classDetails.(map[string]interface{})["versions"]
20692135
if ok {
@@ -2823,10 +2889,16 @@ func GetOverwriteTestType(classPkgName string, definitions Definitions) string {
28232889
return ""
28242890
}
28252891

2826-
func GetOverwriteClassVersion(classPkgName string, definitions Definitions) string {
2892+
func GetOverwriteClassVersion(classPkgName string, definitions Definitions, tests bool) string {
2893+
2894+
matchKey := "class_version"
2895+
if tests {
2896+
matchKey = "class_version_tests"
2897+
}
2898+
28272899
if v, ok := definitions.Classes[classPkgName]; ok {
28282900
for key, value := range v.(map[string]interface{}) {
2829-
if key == "class_version" {
2901+
if key == matchKey {
28302902
return value.(string)
28312903
}
28322904
}
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)