Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions gen/definitions/properties/fvBD.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,9 @@ test_values:
link_local_ipv6_address: fe80::1
unicast_routing: "yes"
virtual_mac_address: 00:22:BD:F8:19:FB
custom_type:
link_local_ipv6_address: fe80::0002
type_overwrites:
epMoveDetectMode: string
static_custom_type:
llAddr: ipv6_address
4 changes: 4 additions & 0 deletions gen/definitions/properties/fvEpIpTag.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ test_values:
ip: 10.0.0.1
resource_required:
ip: 10.0.0.2
custom_type:
ip: fe80::0001
static_custom_type:
ip: ipv6_address
4 changes: 4 additions & 0 deletions gen/definitions/properties/fvFBRMember.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ test_values:
test_values_for_parent:
- fallback_member: 2.2.2.2
- fallback_member: 2.2.2.3
custom_type:
fallback_member: fe80::0001
static_custom_type:
rnhAddr: ipv6_address
4 changes: 4 additions & 0 deletions gen/definitions/properties/fvIpAttr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ test_values:
resource_required:
ip: 131.107.1.200
name: "131"
custom_type:
ip: fe80::0002
static_custom_type:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you check for all other attributes in other resources where ipv6 types should be used?

ip: ipv6_address
4 changes: 4 additions & 0 deletions gen/definitions/properties/mplsNodeSidP.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ test_values:
segment_id: "1"
resource_required:
segment_id: "1"
custom_type:
loopback_address: fe80::0002
static_custom_type:
loopbackAddr: ipv6_address
6 changes: 6 additions & 0 deletions gen/definitions/properties/netflowExporterPol.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,9 @@ test_values:
destination_port: https
name: netfow_exporter
source_ip_address: 1.1.1.1/10
custom_type:
source_ip_address: 2001:db8::1/116
destination_ip_address: 2001:db8::2
static_custom_type:
srcAddr: ipv6_address
dstAddr: ipv6_address
8 changes: 8 additions & 0 deletions gen/definitions/properties/pimRouteMapEntry.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,11 @@ test_values:
source_ip: 0.0.0.0
resource_required:
order: "1"
custom_type:
group_ip: ff0e::0101/128
rendezvous_point_ip: 2001:0db8:ffff::1/128
source_ip: 2001:0db8:100::10/128
static_custom_type:
grp: ipv6_address
rp: ipv6_address
src: ipv6_address
119 changes: 90 additions & 29 deletions gen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ const (
const providerName = "aci"
const pubhupDevnetBaseUrl = "https://pubhub.devnetcloud.com/media/model-doc-latest/docs"

var staticCustomTypeMap = map[string]string{
"rounded_percentage": "RoundedPercentage",
"ipv6_address": "IPv6Address",
}

// Function map used during template rendering in order to call functions from the template
// The map contains a key which is the name of the function used in the template and a value which is the function itself
// The functions itself are defined in the current file
Expand Down Expand Up @@ -1164,7 +1169,7 @@ func cleanDirectories() {
cleanDirectory(resourcesDocsPath, []string{})
cleanDirectory(datasourcesDocsPath, []string{"system.md"})
cleanDirectory(testVarsPath, []string{})
cleanDirectory("./internal/custom_types", []string{})
cleanDirectory("./internal/custom_types", []string{"roundedPercentage.go", "ipv6Address.go"})

// The *ExamplesPath directories are removed and recreated to ensure all previously rendered files are removed
// The provider example file is not removed because it contains static provider configuration
Expand Down Expand Up @@ -1341,10 +1346,11 @@ func main() {
}
model.TestVars = testVarsMap
for propertyName, property := range model.Properties {
if property.HasCustomType {
if property.HasCustomType && property.StaticCustomType == "" {
renderTemplate("custom_type.go.tmpl", fmt.Sprintf("%s_%s.go", model.PkgName, propertyName), "./internal/custom_types", property)
}
}

renderTemplate("resource.go.tmpl", fmt.Sprintf("resource_%s_%s.go", providerName, model.ResourceName), providerPath, model)
renderTemplate("datasource.go.tmpl", fmt.Sprintf("data_source_%s_%s.go", providerName, model.ResourceName), providerPath, model)

Expand Down Expand Up @@ -1509,14 +1515,16 @@ type LegacyBlock struct {
}

type LegacyAttribute struct {
Name string
AttributeName string
ValueType []string
ReplacedBy ReplacementAttribute
Optional bool
Computed bool
Required bool
NeedsCustomType bool
Name string
AttributeName string
ValueType []string
ReplacedBy ReplacementAttribute
Optional bool
Computed bool
Required bool
NeedsCustomType bool
StaticCustomType string
ValidateAsIPv4OrIPv6 bool
}

type ReplacementAttribute struct {
Expand Down Expand Up @@ -1547,12 +1555,15 @@ type Property struct {
Validators []interface{}
IdentifyProperties []Property
// Below booleans are used during template rendering to determine correct rendering the go code
IsNaming bool
CreateOnly bool
IsRequired bool
IgnoreInTest bool
ReadOnly bool
HasCustomType bool
IsNaming bool
CreateOnly bool
IsRequired bool
IgnoreInTest bool
ReadOnly bool
HasCustomType bool
IncludeInCustomTypeTest bool
StaticCustomType string
ValidateAsIPv4OrIPv6 bool
}

// A Definitions represents the ACI class and property definitions as defined in the definitions YAML files
Expand Down Expand Up @@ -2132,6 +2143,10 @@ func (m *Model) SetClassProperties(classDetails interface{}) {
HasCustomType: false,
}

if propertyValue.(map[string]interface{})["validateAsIPv4OrIPv6"] != nil {
property.ValidateAsIPv4OrIPv6 = propertyValue.(map[string]interface{})["validateAsIPv4OrIPv6"].(bool)
}

if requiredProperty(GetOverwriteAttributeName(m.PkgName, propertyName, m.Definitions), m.PkgName, m.Definitions) || property.IsNaming {
property.IsRequired = true
requiredCount += 1
Expand Down Expand Up @@ -2206,6 +2221,14 @@ func (m *Model) SetClassProperties(classDetails interface{}) {
}
}

staticCustomType := GetStaticCustomType(m.PkgName, propertyName, m.Definitions)
if staticCustomType != "" {
property.StaticCustomType = staticCustomTypeMap[staticCustomType]
property.HasCustomType = true
m.HasCustomTypeProperties = true
}
property.IncludeInCustomTypeTest = IncludeInCustomTypeTest(m.PkgName, propertyName, m.Definitions)

defaultValueOverwrite := GetDefaultValues(m.PkgName, propertyName, m.Definitions)
if defaultValueOverwrite != "" {
property.DefaultValue = defaultValueOverwrite
Expand Down Expand Up @@ -2432,7 +2455,6 @@ func (m *Model) SetLegacyAttributes(definitions Definitions) {
legacyResource := definitions.Migration["provider_schemas"].(map[string]interface{})["registry.terraform.io/ciscodevnet/aci"].(map[string]interface{})["resource_schemas"].(map[string]interface{})[resourceName]

if legacyResource != nil {

attributeNames := []string{}
for _, property := range m.Properties {
attributeNames = append(attributeNames, property.SnakeCaseName)
Expand Down Expand Up @@ -2549,23 +2571,32 @@ func (m *Model) GetLegacyAttribute(attributeName, className string, attributeVal
}
}

needsCustomType := false
// needsCustomType := false
var validateAsIPv4OrIPv6, needsCustomType bool
var staticCustomType string
for _, property := range m.Properties {
if propertyName == property.Name && len(property.ValidValuesMap) > 0 && len(property.Validators) > 0 {
needsCustomType = true
break
if propertyName == property.Name {
if property.StaticCustomType != "" {
staticCustomType = property.StaticCustomType
validateAsIPv4OrIPv6 = property.ValidateAsIPv4OrIPv6
}
if len(property.ValidValuesMap) > 0 && len(property.Validators) > 0 {
needsCustomType = true
break
}
}

}

legacyAttribute := LegacyAttribute{
Name: propertyName,
AttributeName: attributeName,
ValueType: valueType,
Optional: optional,
Computed: computed,
Required: required,
NeedsCustomType: needsCustomType,
Name: propertyName,
AttributeName: attributeName,
ValueType: valueType,
Optional: optional,
Computed: computed,
Required: required,
NeedsCustomType: needsCustomType,
StaticCustomType: staticCustomType,
ValidateAsIPv4OrIPv6: validateAsIPv4OrIPv6,
}

if replacedBy != nil {
Expand Down Expand Up @@ -3442,6 +3473,36 @@ func GetDefaultValues(classPkgName, propertyName string, definitions Definitions
return ""
}

func GetStaticCustomType(classPkgName, propertyName string, definitions Definitions) string {
if classDetails, ok := definitions.Properties[classPkgName]; ok {
for key, value := range classDetails.(map[string]interface{}) {
if key == "static_custom_type" {
for k, v := range value.(map[interface{}]interface{}) {
if k.(string) == propertyName {
return v.(string)
}
}
}
}
}
return ""
}

func IncludeInCustomTypeTest(classPkgName, propertyName string, definitions Definitions) bool {
if classDetails, ok := definitions.Properties[classPkgName]; ok {
for key, value := range classDetails.(map[string]interface{}) {
if key == "required_by_custom_type_in_test" {
for _, v := range value.([]interface{}) {
if v.(string) == propertyName {
return true
}
}
}
}
}
return false
}

func IsInterfaceSlice(input interface{}) bool {
_, ok := input.([]interface{})
return ok
Expand Down
9 changes: 6 additions & 3 deletions gen/templates/datasource.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ func (d *{{.ResourceClassName}}DataSource) Schema(ctx context.Context, req datas
{{- if ne .ReplacedBy.AttributeName "" }}{{$ReplacedAttribute := overwriteProperty .ReplacedBy.ClassName (getConflictingAttributeName .ReplacedBy.AttributeName) $.Definitions}}
{{- if eq (getMigrationType .ValueType) "String"}}
"{{.AttributeName}}": schema.StringAttribute{
{{- if ne .StaticCustomType "" }}
CustomType: customTypes.{{- if .StaticCustomType}}{{.StaticCustomType}}{{else}}{{.ResourceClassName}}{{.Name}}{{- end}}StringType{},
{{- end}}
Computed: true,
DeprecationMessage: "Attribute '{{.AttributeName}}' is deprecated, please refer to '{{$ReplacedAttribute}}' instead. The attribute will be removed in the next major version of the provider.",
},
Expand Down Expand Up @@ -116,7 +119,7 @@ func (d *{{.ResourceClassName}}DataSource) Schema(ctx context.Context, req datas
{{- if .IsNaming}}
"{{overwriteProperty .PkgName .SnakeCaseName $.Definitions}}": schema.StringAttribute{
{{- if .HasCustomType}}
CustomType: customTypes.{{.ResourceClassName}}{{.Name}}StringType{},
CustomType: customTypes.{{- if .StaticCustomType}}{{.StaticCustomType}}{{else}}{{.ResourceClassName}}{{.Name}}{{- end}}StringType{},
{{- end}}
Required: true,
{{- if .ValidValues}}
Expand All @@ -133,7 +136,7 @@ func (d *{{.ResourceClassName}}DataSource) Schema(ctx context.Context, req datas
},{{else}}
"{{overwriteProperty .PkgName .SnakeCaseName $.Definitions}}": schema.StringAttribute{
{{- if .HasCustomType}}
CustomType: customTypes.{{.ResourceClassName}}{{.Name}}StringType{},
CustomType: customTypes.{{- if .StaticCustomType}}{{.StaticCustomType}}{{else}}{{.ResourceClassName}}{{.Name}}{{- end}}StringType{},
{{- end}}
Computed: true,
MarkdownDescription: `{{.Comment}}`,
Expand Down Expand Up @@ -279,7 +282,7 @@ func (d *{{.ResourceClassName}}DataSource) Read(ctx context.Context, req datasou
},{{else}}
"{{overwriteProperty .PkgName .SnakeCaseName $.Definitions}}": schema.StringAttribute{
{{- if .HasCustomType}}
CustomType: customTypes.{{.ResourceClassName}}{{.Name}}StringType{},
CustomType: customTypes.{{- if .StaticCustomType}}{{.StaticCustomType}}{{else}}{{.ResourceClassName}}{{.Name}}{{- end}}StringType{},
{{- end}}
Computed: true,
MarkdownDescription: `{{.Comment}}`,
Expand Down
Loading
Loading