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
2 changes: 2 additions & 0 deletions gen/definitions/access_control_policy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ attributes:
data_source_optional_parameter: true
default_value: "true"
example: "true"
tf_only: true
- model_name: dummy_categories
tf_name: categories
type: List
Expand Down Expand Up @@ -182,6 +183,7 @@ attributes:
data_source_optional_parameter: true
default_value: "true"
example: "true"
tf_only: true
- model_name: dummy_rules
tf_name: rules
type: List
Expand Down
11 changes: 10 additions & 1 deletion gen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ func SnakeCase(s string) string {
return strings.Join(g, "_")
}

// Templating helper function to lowercase the first character of a string
func LowerFirst(s string) string {
if s == "" {
return s
}
return strings.ToLower(s[:1]) + s[1:]
}

// Templating helper function to fail a template mid-way
func Errorf(s string, args ...any) (struct{}, error) {
return struct{}{}, fmt.Errorf(s, args...)
Expand Down Expand Up @@ -445,6 +453,7 @@ var functions = template.FuncMap{
"toGoName": ToGoName,
"camelCase": CamelCase,
"snakeCase": SnakeCase,
"lowerFirst": LowerFirst,
"sprintf": fmt.Sprintf,
"errorf": Errorf,
"toLower": strings.ToLower,
Expand Down Expand Up @@ -654,7 +663,7 @@ func renderTemplate(templatePath, outputPath string, config any) {
matches := endRegex.FindStringSubmatch(line)
if len(matches) > 1 && matches[1] == currentSectionName {
currentSectionName = ""
newSection := getTemplateSection(string(output.Bytes()), matches[1])
newSection := getTemplateSection(output.String(), matches[1])
newContent += newSection
}
}
Expand Down
14 changes: 10 additions & 4 deletions gen/templates/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,12 @@ func (data {{camelCase .Name}}) toBody(ctx context.Context, state {{camelCase .N
func (data *{{camelCase .Name}}) fromBody(ctx context.Context, res gjson.Result) {
{{- define "fromBodyTemplate"}}
{{- range .Attributes}}
{{- if .TfOnly}}{{- continue}}{{- end}}
{{- if .TfOnly}}
{{- if .DefaultValue}}
data.{{toGoName .TfName}} = types.{{.Type}}Value({{.DefaultValue}})
{{- end}}
{{- continue}}
{{- end}}
{{- if and (not .Value) (not .WriteOnly) (not .Reference)}}
{{- if or (eq .Type "String") (eq .Type "Int64") (eq .Type "Float64") (eq .Type "Bool")}}
if value := res.Get("{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}"); value.Exists() {
Expand All @@ -323,7 +328,7 @@ func (data *{{camelCase .Name}}) fromBody(ctx context.Context, res gjson.Result)
}
{{- else if isNestedListSet .}}
if value := res{{if .ModelName}}.Get("{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}"){{end}}; value.Exists() {
data.{{toGoName .TfName}} = make([]{{.GoTypeName}}, 0)
data.{{toGoName .TfName}} = make([]{{.GoTypeName}}, 0, int(value.Get("#").Int()))
value.ForEach(func(k, res gjson.Result) bool {
parent := &data
data := {{.GoTypeName}}{}
Expand Down Expand Up @@ -441,16 +446,17 @@ func (data *{{camelCase .Name}}) fromBody(ctx context.Context, res gjson.Result)
parentRes := &res
res := parentRes.Get(fmt.Sprintf("{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}.%d", i))
{{- else }}
{{- $arrayVar := printf "%sArray" (lowerFirst (toGoName .TfName))}}
{{if .ModelName}}{{$arrayVar}} := res.Get("{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}"){{end}}
for i := 0; i < len(data.{{toGoName .TfName}}); i++ {
keys := [...]string{ {{$noId := not (hasId .Attributes)}}{{range .Attributes}}{{if or .Id (and $noId (not .Value))}}{{if or (eq .Type "Int64") (eq .Type "Bool") (eq .Type "String")}}"{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", {{end}}{{end}}{{end}} }
keyValues := [...]string{ {{$noId := not (hasId .Attributes)}}{{range .Attributes}}{{if or .Id (and $noId (not .Value))}}{{if eq .Type "Int64"}}strconv.FormatInt(data.{{$list}}[i].{{toGoName .TfName}}.ValueInt64(), 10), {{else if eq .Type "Bool"}}strconv.FormatBool(data.{{$list}}[i].{{toGoName .TfName}}.ValueBool()), {{else if eq .Type "String"}}data.{{$list}}[i].{{toGoName .TfName}}.Value{{.Type}}(), {{end}}{{end}}{{end}} }

parent := &data
data := (*parent).{{toGoName .TfName}}[i]
parentRes := &res
var res gjson.Result

parentRes.{{if .ModelName}}Get("{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}").{{end}}ForEach(
{{if .ModelName}}{{$arrayVar}}{{else}}res{{end}}.ForEach(
func(_, v gjson.Result) bool {
found := false
for ik := range keys {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ func TestAccDataSourceFmcAccessControlPolicy(t *testing.T) {
checks = append(checks, resource.TestCheckResourceAttr("data.fmc_access_control_policy.test", "default_action_log_connection_end", "false"))
checks = append(checks, resource.TestCheckResourceAttr("data.fmc_access_control_policy.test", "default_action_send_events_to_fmc", "true"))
checks = append(checks, resource.TestCheckResourceAttr("data.fmc_access_control_policy.test", "default_action_syslog_severity", "DEBUG"))
checks = append(checks, resource.TestCheckResourceAttr("data.fmc_access_control_policy.test", "manage_categories", "true"))
checks = append(checks, resource.TestCheckResourceAttr("data.fmc_access_control_policy.test", "categories.0.name", "category_1"))
checks = append(checks, resource.TestCheckResourceAttr("data.fmc_access_control_policy.test", "manage_rules", "true"))
checks = append(checks, resource.TestCheckResourceAttr("data.fmc_access_control_policy.test", "rules.0.action", "ALLOW"))
checks = append(checks, resource.TestCheckResourceAttr("data.fmc_access_control_policy.test", "rules.0.name", "rule_1"))
checks = append(checks, resource.TestCheckResourceAttr("data.fmc_access_control_policy.test", "rules.0.source_network_literals.0.value", "10.1.1.0/24"))
Expand Down
Loading