Skip to content

Commit

Permalink
feat: Extend support for device groups specification (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
kklimonda-cl authored Aug 16, 2024
1 parent dfa889a commit 1b483a1
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 35 deletions.
10 changes: 6 additions & 4 deletions pkg/generate/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,19 @@ func (c *Creator) processTemplate(templateName, filePath string) error {
// If no data was rendered from the template, skip creating an empty file.
dataLength := len(bytes.TrimSpace(data.Bytes()))
if dataLength > 0 {
formattedCode, err := format.Source(data.Bytes())
var formattedCode []byte
formattedCode, err = format.Source(data.Bytes())
if err != nil {
return fmt.Errorf("error formatting code %w", err)
log.Printf("Failed to format source code: %s", err.Error())
formattedCode = data.Bytes()
}
formattedBuf := bytes.NewBuffer(formattedCode)

if err := c.createAndWriteFile(filePath, formattedBuf); err != nil {
if writeErr := c.createAndWriteFile(filePath, formattedBuf); writeErr != nil {
return fmt.Errorf("error creating and writing to file %s: %w", filePath, err)
}
}
return nil
return err
}

// writeFormattedContentToFile formats the content and writes it to a file.
Expand Down
116 changes: 91 additions & 25 deletions pkg/translate/assignments.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package translate

import (
"fmt"
"log"
"strings"

"github.com/paloaltonetworks/pan-os-codegen/pkg/properties"
Expand All @@ -25,10 +26,14 @@ func prepareAssignment(objectType string, param *properties.SpecParam, listFunct
var builder strings.Builder

if ParamSupportedInVersion(param, version) {
var isNestedListHack bool
if param.Type == "list" {
isNestedListHack = true
}
switch {
case param.Spec != nil:
appendSpecObjectAssignment(param, nil, objectType, paramVersionInAssignment(suffix, version),
listFunction, entryFunction, boolFunction, prefix, suffix, &builder)
listFunction, entryFunction, boolFunction, prefix, suffix, &builder, isNestedListHack)
case isParamListAndProfileTypeIsMember(param):
appendFunctionAssignment(param, objectType, listFunction, "", &builder)
case isParamListAndProfileTypeIsSingleEntry(param):
Expand Down Expand Up @@ -82,13 +87,28 @@ func appendFunctionAssignment(param *properties.SpecParam, objectType string, fu
}
}

func appendSpecObjectAssignment(param, parentParam *properties.SpecParam, objectType string, version, listFunction, entryFunction, boolFunction, prefix, suffix string, builder *strings.Builder) {
func appendSpecObjectAssignment(param, parentParam *properties.SpecParam, objectType string, version, listFunction, entryFunction, boolFunction, prefix, suffix string, builder *strings.Builder, isNestedListHack bool) {
defineNestedObject([]*properties.SpecParam{param}, param, parentParam, objectType, version, listFunction, entryFunction, boolFunction, prefix, suffix, builder)
builder.WriteString(fmt.Sprintf("%s.%s = nested%s\n", objectType, param.Name.CamelCase, param.Name.CamelCase))

if parentParam == nil && param.Type == "list" && param.Items.Type == "entry" {
builder.WriteString(fmt.Sprintf("%s.%s = nested%sCol\n", objectType, param.Name.CamelCase, param.Name.CamelCase))
} else {
builder.WriteString(fmt.Sprintf("%s.%s = nested%s\n", objectType, param.Name.CamelCase, param.Name.CamelCase))
}

if isNestedListHack {
builder.WriteString("}\n")
}
}

func defineNestedObject(parent []*properties.SpecParam, param, parentParam *properties.SpecParam, objectType string, version, listFunction, entryFunction, boolFunction, prefix, suffix string, builder *strings.Builder) {
declareRootOfNestedObject(parent, builder, version, prefix, suffix)

var isNestedListHack bool
if parentParam == nil && param.Type == "list" && param.Items.Type == "entry" {
isNestedListHack = true
}

declareRootOfNestedObject(parent, builder, version, prefix, suffix, isNestedListHack)

if ParamSupportedInVersion(param, version) {
startIfBlockForParamNotNil(parent, param, parentParam, builder)
Expand All @@ -114,31 +134,56 @@ func defineNestedObject(parent []*properties.SpecParam, param, parentParam *prop
}

func startIfBlockForParamNotNil(parent []*properties.SpecParam, param *properties.SpecParam, parentParam *properties.SpecParam, builder *strings.Builder) {
if isParamName(param) {
builder.WriteString(fmt.Sprintf("if o%s != \"\" {\n",
renderNestedVariableName(parent, true, true, true)))
if len(parent) == 2 && parent[0].Type == "list" && parent[0].Items.Type == "entry" {
if isParamName(param) {
builder.WriteString(fmt.Sprintf("if o%s != \"\" {\n",
renderNestedVariableName(parent, true, true, false)))
} else {
builder.WriteString(fmt.Sprintf("if o%s != nil {\n",
renderNestedVariableName(parent, true, true, false)))
}
} else {
builder.WriteString(fmt.Sprintf("if o%s != nil {\n",
renderNestedVariableName(parent, true, true, true)))
if isParamName(param) {
builder.WriteString(fmt.Sprintf("if o%s != \"\" {\n",
renderNestedVariableName(parent, true, true, true)))
} else {
builder.WriteString(fmt.Sprintf("if o%s != nil {\n",
renderNestedVariableName(parent, true, true, true)))
}
}
}

func finishNestedObjectIfBlock(parent []*properties.SpecParam, param *properties.SpecParam, builder *strings.Builder) {
if isParamListAndProfileTypeIsExtendedEntry(param) {
builder.WriteString(fmt.Sprintf("nested%s = append(nested%s, nested%s)\n",
renderNestedVariableName(parent, true, true, false),
renderNestedVariableName(parent, true, true, false),
renderNestedVariableName(parent, false, false, false)))
if len(parent) == 1 && parent[0].Type == "list" && parent[0].Items.Type == "entry" {
if isParamListAndProfileTypeIsExtendedEntry(param) {
builder.WriteString(fmt.Sprintf("nested%sCol = append(nested%sCol, nested%s)\n",
renderNestedVariableName(parent, true, true, false),
renderNestedVariableName(parent, true, true, false),
renderNestedVariableName(parent, false, false, false)))
}
} else {
if isParamListAndProfileTypeIsExtendedEntry(param) {
builder.WriteString(fmt.Sprintf("nested%s = append(nested%s, nested%s)\n",
renderNestedVariableName(parent, true, true, false),
renderNestedVariableName(parent, true, true, false),
renderNestedVariableName(parent, false, false, false)))
}
}

builder.WriteString("}\n")
}

func isParamName(param *properties.SpecParam) bool {
return param.Name.CamelCase == "Name"
}

func declareRootOfNestedObject(parent []*properties.SpecParam, builder *strings.Builder, version, prefix, suffix string) {
if len(parent) == 1 {
func declareRootOfNestedObject(parent []*properties.SpecParam, builder *strings.Builder, version, prefix, suffix string, isNestedListHack bool) {
if isNestedListHack {
builder.WriteString(fmt.Sprintf("var nested%sCol []%s%s%s%s\n",
renderNestedVariableName(parent, true, true, false), prefix,
renderNestedVariableName(parent, false, false, false), suffix,
CreateGoSuffixFromVersion(version)))
} else if len(parent) == 1 {
builder.WriteString(fmt.Sprintf("var nested%s *%s%s%s%s\n",
renderNestedVariableName(parent, true, true, false), prefix,
renderNestedVariableName(parent, false, false, false), suffix,
Expand All @@ -165,10 +210,17 @@ func createStructForParamWithSpec(parent []*properties.SpecParam, builder *strin
}

func createListAndLoopForNestedEntry(parent []*properties.SpecParam, builder *strings.Builder, prefix string, suffix string, version string) {
builder.WriteString(fmt.Sprintf("nested%s = []%s%s%s%s{}\n",
renderNestedVariableName(parent, true, true, false), prefix,
renderNestedVariableName(parent, false, false, false), suffix,
CreateGoSuffixFromVersion(version)))
if len(parent) == 1 && parent[0].Type == "list" && parent[0].Items.Type == "entry" {
builder.WriteString(fmt.Sprintf("nested%sCol = []%s%s%s%s{}\n",
renderNestedVariableName(parent, true, true, false), prefix,
renderNestedVariableName(parent, false, false, false), suffix,
CreateGoSuffixFromVersion(version)))
} else {
builder.WriteString(fmt.Sprintf("nested%s = []%s%s%s%s{}\n",
renderNestedVariableName(parent, true, true, false), prefix,
renderNestedVariableName(parent, false, false, false), suffix,
CreateGoSuffixFromVersion(version)))
}

builder.WriteString(fmt.Sprintf("for _, o%s := range o%s {\n",
renderNestedVariableName(parent, false, false, false),
Expand Down Expand Up @@ -215,21 +267,35 @@ func miscForUnknownXmlWithExtendedEntry(parent []*properties.SpecParam, builder
}
}

var _ = log.Printf

func assignValueForNestedObject(parent []*properties.SpecParam, builder *strings.Builder, param, parentParam *properties.SpecParam) {
builder.WriteString(fmt.Sprintf("nested%s = o%s\n",
renderNestedVariableName(parent, true, true, false),
renderNestedVariableName(parent, true, true, true)))
if len(parent) == 2 && parent[0].Type == "list" && parent[0].Items.Type == "entry" {
builder.WriteString(fmt.Sprintf("nested%s = o%s\n",
renderNestedVariableName(parent, true, true, false),
renderNestedVariableName(parent, true, true, false)))
} else {
builder.WriteString(fmt.Sprintf("nested%s = o%s\n",
renderNestedVariableName(parent, true, true, false),
renderNestedVariableName(parent, true, true, true)))
}
}

func assignFunctionForNestedObject(parent []*properties.SpecParam, functionName, additionalArguments string, builder *strings.Builder, param, parentParam *properties.SpecParam) {
var startWithDot bool
if len(parent) == 2 && parent[0].Type == "list" && parent[0].Items.Type == "entry" {
startWithDot = false
} else {
startWithDot = true
}
if additionalArguments != "" {
builder.WriteString(fmt.Sprintf("nested%s = %s(o%s, %s)\n",
renderNestedVariableName(parent, true, true, false), functionName,
renderNestedVariableName(parent, true, true, true), additionalArguments))
renderNestedVariableName(parent, true, true, startWithDot), additionalArguments))
} else {
builder.WriteString(fmt.Sprintf("nested%s = %s(o%s)\n",
renderNestedVariableName(parent, true, true, false), functionName,
renderNestedVariableName(parent, true, true, true)))
renderNestedVariableName(parent, true, true, startWithDot)))
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/translate/terraform_provider/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ const copyToPangoTmpl = `
{{- define "terraformListElementsAs" }}
{{- with .Parameter }}
{{- $pangoType := printf "%s%s" $.Spec.PangoType .Name.CamelCase }}
{{- $terraformType := printf "%s%s%s" $.Spec.TerraformType .Name.CamelCase $.Spec.ModelOrObject }}
{{- $terraformType := printf "%s%sObject" $.Spec.TerraformType .Name.CamelCase }}
{{- $pangoEntries := printf "%s_pango_entries" .Name.LowerCamelCase }}
{{- $tfEntries := printf "%s_tf_entries" .Name.LowerCamelCase }}
{{- if eq .ItemsType "entry" }}
Expand Down Expand Up @@ -344,7 +344,7 @@ var {{ .Name.LowerCamelCase }}_list types.List
{{- define "terraformListElementsAsParam" }}
{{- with .Parameter }}
{{- $pangoType := printf "%s%s" $.Spec.PangoType .Name.CamelCase }}
{{- $terraformType := printf "%s%s%s" $.Spec.TerraformType .Name.CamelCase $.Spec.ModelOrObject }}
{{- $terraformType := printf "%s%sObject" $.Spec.TerraformType .Name.CamelCase }}
{{- $terraformList := printf "%s_list" .Name.LowerCamelCase }}
{{- $pangoEntries := printf "%s_pango_entries" .Name.LowerCamelCase }}
{{- $tfEntries := printf "%s_tf_entries" .Name.LowerCamelCase }}
Expand Down
18 changes: 14 additions & 4 deletions specs/panorama/device-group.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,22 @@ spec:
- name: devices
description: "List of devices"
type: list
profiles:
- type: entry
xpath: ["devices", "entry"]
spec:
items:
type: string
profiles:
- type: member
xpath: ["devices"]
type: object
spec:
params:
- name: "vsys"
type: list
profiles:
- type: member
xpath: [vsys]
spec:
items:
type: string
- name: authorization_code
type: string
description: "Authorization code"
Expand Down

0 comments on commit 1b483a1

Please sign in to comment.