Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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/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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done!

ip: ipv6_address
66 changes: 58 additions & 8 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,7 +1346,7 @@ 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)
}
}
Expand Down Expand Up @@ -1547,12 +1552,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 +2140,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 +2218,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 @@ -3442,6 +3462,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
4 changes: 4 additions & 0 deletions gen/templates/custom_type.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Code generated by "gen/generator.go"; DO NOT EDIT.
Copy link
Collaborator

Choose a reason for hiding this comment

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

split this change to split PR to reduce changes and quick review

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done!

// In order to regenerate this file execute `go generate` from the repository root.
// More details can be found in the [README](https://github.com/CiscoDevNet/terraform-provider-aci/blob/master/README.md).

package customTypes

import (
Expand Down
6 changes: 3 additions & 3 deletions gen/templates/datasource.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,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 +133,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 +279,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
40 changes: 22 additions & 18 deletions gen/templates/resource.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type {{.ResourceClassName}}ResourceModel struct {
{{- else if eq .Name "Id"}}
{{.ResourceClassName}}{{ .Name }} types.String `tfsdk:"{{overwriteProperty .PkgName .SnakeCaseName $.Definitions}}"`
{{- else if .HasCustomType}}
{{ .Name }} customTypes.{{.ResourceClassName}}{{.Name}}StringValue `tfsdk:"{{overwriteProperty .PkgName .SnakeCaseName $.Definitions}}"`
{{ .Name }} customTypes.{{- if .StaticCustomType}}{{.StaticCustomType}}{{else}}{{.ResourceClassName}}{{.Name}}{{- end}}StringValue `tfsdk:"{{overwriteProperty .PkgName .SnakeCaseName $.Definitions}}"`
Copy link
Collaborator

Choose a reason for hiding this comment

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

missing legacy attribute support and changes

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done!

{{- else}}
{{ .Name }} types.String `tfsdk:"{{overwriteProperty .PkgName .SnakeCaseName $.Definitions}}"`
{{- end}}
Expand Down Expand Up @@ -95,7 +95,7 @@ func getEmpty{{.ResourceClassName}}ResourceModel() *{{.ResourceClassName}}Resour
{{- else if eq .Name "Id"}}
{{.ResourceClassName}}{{ .Name }}: basetypes.NewStringNull(),
{{- else if .HasCustomType}}
{{ .Name }}: customTypes.New{{.ResourceClassName}}{{.Name}}StringNull(),
{{ .Name }}: customTypes.New{{- if .StaticCustomType}}{{.StaticCustomType}}{{else}}{{.ResourceClassName}}{{.Name}}{{- end}}StringNull(),
{{- else}}
{{ .Name }}: basetypes.NewStringNull(),
{{- end}}
Expand Down Expand Up @@ -299,7 +299,7 @@ func (r *{{.ResourceClassName}}Resource) UpgradeState(ctx context.Context) map[i
{{- range .Properties}}
{{- if isLegacyAttribute .Name $.LegacyAttributes}}
{{- if .HasCustomType}}
{{ .Name }}: customTypes.{{.ResourceClassName}}{{.Name}}StringValue{ StringValue: priorStateData.{{ .Name }} },
{{ .Name }}: customTypes.{{- if .StaticCustomType}}{{.StaticCustomType}}{{else}}{{.ResourceClassName}}{{.Name}}{{- end}}StringValue{ StringValue: priorStateData.{{ .Name }} },
{{- else}}
{{ .Name }}: priorStateData.{{ .Name }},
{{- end}}
Expand Down Expand Up @@ -457,7 +457,7 @@ func (r *{{.ResourceClassName}}Resource) UpgradeState(ctx context.Context) map[i
{{- if eq $AttributeName ""}}
{{ .Name }}: basetypes.NewStringNull(),
{{- else if .HasCustomType}}
{{ .Name }}: customTypes.{{.ResourceClassName}}{{.Name}}StringValue{StringValue: priorStateData{{.ResourceClassName}}.{{ $AttributeName }}},
{{ .Name }}: customTypes.{{- if .StaticCustomType}}{{.StaticCustomType}}{{else}}{{.ResourceClassName}}{{.Name}}{{- end}}StringValue{StringValue: priorStateData{{.ResourceClassName}}.{{ $AttributeName }}},
{{- else}}
{{ .Name }}: priorStateData{{.ResourceClassName}}.{{ $AttributeName }},
{{- end}}
Expand Down Expand Up @@ -516,7 +516,7 @@ func (r *{{.ResourceClassName}}Resource) UpgradeState(ctx context.Context) map[i
{{- $AttributeName := getLegacyChildAttribute $PkgName $Overwrite . $.LegacyAttributes $.LegacyBlocks}}
{{- if eq $AttributeName ""}}
{{- if .HasCustomType}}
{{ .Name }}: customTypes.New{{.ResourceClassName}}{{.Name}}StringNull(),
{{ .Name }}: customTypes.New{{- if .StaticCustomType}}{{.StaticCustomType}}{{else}}{{.ResourceClassName}}{{.Name}}{{- end}}StringNull(),
{{- else}}
{{ .Name }}: basetypes.NewStringNull(),
{{- end}}
Expand Down Expand Up @@ -1043,7 +1043,7 @@ func (r *{{.ResourceClassName}}Resource) ModifyPlan(ctx context.Context, req res
{{ .ResourceClassName }}.{{ .Name }} = planData.Annotation
{{- else if ne .DefaultValue ""}}
{{- if .HasCustomType}}
{{ .ResourceClassName }}.{{ .Name }} = customTypes.New{{.ResourceClassName}}{{.Name}}StringUnknown()
{{ .ResourceClassName }}.{{ .Name }} = customTypes.New{{- if .StaticCustomType}}{{.StaticCustomType}}{{else}}{{.ResourceClassName}}{{.Name}}{{- end}}StringUnknown()
{{- else}}
{{ .ResourceClassName }}.{{ .Name }} = basetypes.NewStringUnknown()
{{- end}}
Expand Down Expand Up @@ -1122,7 +1122,7 @@ func (r *{{.ResourceClassName}}Resource) ModifyPlan(ctx context.Context, req res
{{ .Name }}: planData.{{ .Name }},
{{- else if and (ne $AttributeName "") (not (isNewNamedClassAttribute $Overwrite)) }}
{{- if .HasCustomType}}
{{ .Name }}: customTypes.{{.ResourceClassName}}{{.Name}}StringValue{StringValue: attributeValue.{{ .Name }}},
{{ .Name }}: customTypes.{{- if .StaticCustomType}}{{.StaticCustomType}}{{else}}{{.ResourceClassName}}{{.Name}}{{- end}}StringValue{StringValue: attributeValue.{{ .Name }}},
{{- else}}
{{ .Name }}: attributeValue.{{ .Name }},
{{- end}}
Expand All @@ -1136,13 +1136,13 @@ func (r *{{.ResourceClassName}}Resource) ModifyPlan(ctx context.Context, req res
{{- $AttributeName := getLegacyChildAttribute .PkgName $Overwrite . $.LegacyAttributes $.LegacyBlocks}}
if !attributeValue.{{ .Name }}.IsNull() {
{{- if .HasCustomType}}
{{ .ResourceClassName }}.{{ .Name }} = customTypes.{{.ResourceClassName}}{{.Name}}StringValue{StringValue: attributeValue.{{ .Name }}}
{{ .ResourceClassName }}.{{ .Name }} = customTypes.{{- if .StaticCustomType}}{{.StaticCustomType}}{{else}}{{.ResourceClassName}}{{.Name}}{{- end}}StringValue{StringValue: attributeValue.{{ .Name }}}
{{- else}}
{{ .ResourceClassName }}.{{ .Name }} = attributeValue.{{ .Name }}
{{- end}}
} else {
{{- if .HasCustomType}}
{{ .ResourceClassName }}.{{ .Name }} = customTypes.{{.ResourceClassName}}{{.Name}}StringValue{StringValue: basetypes.NewStringUnknown()}
{{ .ResourceClassName }}.{{ .Name }} = customTypes.{{- if .StaticCustomType}}{{.StaticCustomType}}{{else}}{{.ResourceClassName}}{{.Name}}{{- end}}StringValue{StringValue: basetypes.NewStringUnknown()}
{{- else}}
{{ .ResourceClassName }}.{{ .Name }} = basetypes.NewStringUnknown()
{{- end}}
Expand Down Expand Up @@ -1602,7 +1602,7 @@ func (r *{{.ResourceClassName}}Resource) Schema(ctx context.Context, req resourc
{{- else}}
"{{$OverwritePropertyName}}": schema.StringAttribute{
{{- if .HasCustomType}}
CustomType: customTypes.{{.ResourceClassName}}{{.Name}}StringType{},
CustomType: customTypes.{{- if .StaticCustomType}}{{.StaticCustomType}}{{else}}{{.ResourceClassName}}{{.Name}}{{- end}}StringType{},
{{- end}}
{{- if or .IsNaming .IsRequired}}
{{- if ne $LegacyAttributeName ""}}
Expand Down Expand Up @@ -1641,10 +1641,12 @@ func (r *{{.ResourceClassName}}Resource) Schema(ctx context.Context, req resourc
{{- if eq .Name "Annotation"}}
Default: stringdefault.StaticString(globalAnnotation),
{{- end}}
{{- if .HasCustomType}}
{{- if and .HasCustomType (not .ValidateAsIPv4OrIPv6) }}
Validators: []validator.String{
stringvalidator.Any(
{{- if .ValidValues}}
stringvalidator.OneOf({{- validatorStringCustomType .ValidValues .ValidValuesMap}}),
{{- end}}
validators.InBetweenFromString({{- index .Validators 0 "min"}}, {{- index .Validators 0 "max"}}),
),
},
Expand Down Expand Up @@ -2091,7 +2093,7 @@ func getAndSet{{.ResourceClassName}}Attributes(ctx context.Context, diags *diag.
{{- else}}
if attributeName == "{{.PropertyName}}" {
{{- if .HasCustomType}}
readData.{{.Name}} = customTypes.New{{.ResourceClassName}}{{.Name}}StringValue(attributeValue.(string))
readData.{{.Name}} = customTypes.New{{- if .StaticCustomType}}{{.StaticCustomType}}{{else}}{{.ResourceClassName}}{{.Name}}{{- end}}StringValue(attributeValue.(string))
{{- else}}
readData.{{- if eq .Name "Id"}}{{.ResourceClassName}}{{ .Name }}{{- else}}{{.Name}}{{- end}} = basetypes.NewStringValue(attributeValue.(string))
{{- end}}
Expand Down Expand Up @@ -2161,7 +2163,7 @@ func getAndSet{{.ResourceClassName}}Attributes(ctx context.Context, diags *diag.
{{- else}}
if childAttributeName == "{{.PropertyName}}" {
{{- if .HasCustomType}}
{{.ResourceClassName}}{{$.ResourceClassName}}.{{.Name}} = customTypes.New{{.ResourceClassName}}{{.Name}}StringValue(childAttributeValue.(string))
{{.ResourceClassName}}{{$.ResourceClassName}}.{{.Name}} = customTypes.New{{- if .StaticCustomType}}{{.StaticCustomType}}{{else}}{{.ResourceClassName}}{{.Name}}{{- end}}StringValue(childAttributeValue.(string))
{{- else}}
{{.ResourceClassName}}{{$.ResourceClassName}}.{{- if eq .Name "Id"}}{{.ResourceClassName}}{{ .Name }}{{- else}}{{.Name}}{{- end}} = basetypes.NewStringValue(childAttributeValue.(string))
{{- end}}
Expand Down Expand Up @@ -2586,7 +2588,7 @@ type {{.ResourceClassName}}{{.ParentHierarchy}}ResourceModel struct {
{{- else if eq .Name "Id"}}
{{.ResourceClassName}}{{ .Name }} types.String `tfsdk:"{{overwriteProperty .PkgName .SnakeCaseName $.Definitions}}"`
{{- else if .HasCustomType}}
{{ .Name }} customTypes.{{.ResourceClassName}}{{.Name}}StringValue `tfsdk:"{{overwriteProperty .PkgName .SnakeCaseName $.Definitions}}"`
{{ .Name }} customTypes.{{- if .StaticCustomType}}{{.StaticCustomType}}{{else}}{{.ResourceClassName}}{{.Name}}{{- end}}StringValue `tfsdk:"{{overwriteProperty .PkgName .SnakeCaseName $.Definitions}}"`
{{- else}}
{{ .Name }} types.String `tfsdk:"{{overwriteProperty .PkgName .SnakeCaseName $.Definitions}}"`
{{- end}}
Expand All @@ -2608,7 +2610,7 @@ func getEmpty{{.ResourceClassName}}{{.ParentHierarchy}}ResourceModel() {{.Resour
{{- else if eq .Name "Id"}}
{{.ResourceClassName}}{{ .Name }}: basetypes.NewStringNull(),
{{- else if .HasCustomType}}
{{ .Name }}: customTypes.New{{.ResourceClassName}}{{.Name}}StringNull(),
{{ .Name }}: customTypes.New{{- if .StaticCustomType}}{{.StaticCustomType}}{{else}}{{.ResourceClassName}}{{.Name}}{{- end}}StringNull(),
{{- else}}
{{ .Name }}: basetypes.NewStringNull(),
{{- end}}
Expand Down Expand Up @@ -2717,7 +2719,7 @@ func {{.ResourceClassName}}{{.ParentHierarchy}}SetToSetNullWhenStateIsNullPlanIs
{{- if eq .ValueType "bitmask"}}
planSetValues[index].{{ .Name }} = basetypes.NewSetNull(types.StringType)
{{- else if .HasCustomType}}
planSetValues[index].{{ .Name }} = customTypes.New{{.ResourceClassName}}{{.Name}}StringNull()
planSetValues[index].{{ .Name }} = customTypes.New{{- if .StaticCustomType}}{{.StaticCustomType}}{{else}}{{.ResourceClassName}}{{.Name}}{{- end}}StringNull()
{{- else}}
planSetValues[index].{{ .Name }} = basetypes.NewStringNull()
{{- end}}
Expand Down Expand Up @@ -2864,7 +2866,7 @@ func {{.ResourceClassName}}{{.ParentHierarchy}}SetToSetNullWhenStateIsNullPlanIs
{{- 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}}
{{- if and (or .IsNaming .IsRequired) (not $.TemplateProperties.HasReadOnlyProperties)}}
Required: true,
Expand All @@ -2884,7 +2886,9 @@ func {{.ResourceClassName}}{{.ParentHierarchy}}SetToSetNullWhenStateIsNullPlanIs
{{- if .HasCustomType}}
Validators: []validator.String{
stringvalidator.Any(
stringvalidator.OneOf({{- validatorStringCustomType .ValidValues .ValidValuesMap}}),
{{- if .ValidValues}}
stringvalidator.OneOf({{- validatorStringCustomType .ValidValues .ValidValuesMap}}),
{{- end}}
validators.InBetweenFromString({{- index .Validators 0 "min"}}, {{- index .Validators 0 "max"}}),
),
},
Expand Down
Loading
Loading