Skip to content

Commit

Permalink
feat: Render location.go (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianczech authored Mar 8, 2024
1 parent bd09ee9 commit 65727a4
Show file tree
Hide file tree
Showing 21 changed files with 323 additions and 16 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module github.com/paloaltonetworks/pan-os-codegen

require (
github.com/stretchr/testify v1.8.4
github.com/stretchr/testify v1.9.0
gopkg.in/yaml.v3 v3.0.1
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
8 changes: 7 additions & 1 deletion pkg/generate/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@ func (c *Creator) generateOutputFileFromTemplate(tmpl *template.Template, output
func (c *Creator) parseTemplate(templateName string) (*template.Template, error) {
templatePath := fmt.Sprintf("%s/%s", c.TemplatesDir, templateName)
funcMap := template.FuncMap{
"packageName": translate.PackageName,
"packageName": translate.PackageName,
"locationType": translate.LocationType,
"omitEmpty": translate.OmitEmpty,
"contains": func(full, part string) bool {
return strings.Contains(full, part)
},
"asEntryXpath": translate.AsEntryXpath,
}
tmpl, err := template.New(templateName).Funcs(funcMap).ParseFiles(templatePath)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/generate/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestListOfTemplates(t *testing.T) {
assert.Equal(t, 4, len(templates))
}

func TestParseTemplate(t *testing.T) {
func TestParseTemplateForInterfaces(t *testing.T) {
// given
spec := properties.Normalization{
GoSdkPath: []string{"object", "address"},
Expand Down
36 changes: 33 additions & 3 deletions pkg/properties/normalized.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"github.com/paloaltonetworks/pan-os-codegen/pkg/content"
"github.com/paloaltonetworks/pan-os-codegen/pkg/naming"
"io/fs"
"path/filepath"
"runtime"
Expand All @@ -21,7 +22,13 @@ type Normalization struct {
Spec *Spec `json:"spec" yaml:"spec"`
}

type NameVariant struct {
Underscore string
CamelCase string
}

type Location struct {
Name *NameVariant
Description string `json:"description" yaml:"description"`
Device *LocationDevice `json:"device" yaml:"device"`
Xpath []string `json:"xpath" yaml:"xpath"`
Expand All @@ -35,6 +42,7 @@ type LocationDevice struct {
}

type LocationVar struct {
Name *NameVariant
Description string `json:"description" yaml:"description"`
Required bool `json:"required" yaml:"required"`
Validation *LocationVarValidation `json:"validation" yaml:"validation"`
Expand Down Expand Up @@ -128,9 +136,31 @@ func GetNormalizations() ([]string, error) {
}

func ParseSpec(input []byte) (*Normalization, error) {
var ans Normalization
err := content.Unmarshal(input, &ans)
return &ans, err
var spec Normalization

err := content.Unmarshal(input, &spec)

err = spec.AddNameVariants()

return &spec, err
}

func (spec *Normalization) AddNameVariants() error {
for key, location := range spec.Locations {
location.Name = &NameVariant{
Underscore: key,
CamelCase: naming.CamelCase("", key, "", true),
}

for subkey, variable := range location.Vars {
variable.Name = &NameVariant{
Underscore: subkey,
CamelCase: naming.CamelCase("", subkey, "", true),
}
}
}

return nil
}

func (spec *Normalization) Sanity() error {
Expand Down
24 changes: 24 additions & 0 deletions pkg/properties/normalized_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ xpath_suffix:
- address
locations:
device_group:
name:
underscore: device_group
camelcase: DeviceGroup
description: Located in a specific device group.
device:
panorama: true
Expand All @@ -167,16 +170,25 @@ locations:
read_only: false
vars:
device_group:
name:
underscore: device_group
camelcase: DeviceGroup
description: The device group.
required: true
validation:
not_values:
shared: The device group cannot be "shared". Use the "shared" path instead.
panorama_device:
name:
underscore: panorama_device
camelcase: PanoramaDevice
description: The panorama device.
required: false
validation: null
from_panorama:
name:
underscore: from_panorama
camelcase: FromPanorama
description: Located in the config pushed from Panorama.
device:
panorama: false
Expand All @@ -187,6 +199,9 @@ locations:
read_only: true
vars: {}
shared:
name:
underscore: shared
camelcase: Shared
description: Located in shared.
device:
panorama: true
Expand All @@ -197,6 +212,9 @@ locations:
read_only: false
vars: {}
vsys:
name:
underscore: vsys
camelcase: Vsys
description: Located in a specific vsys.
device:
panorama: true
Expand All @@ -210,10 +228,16 @@ locations:
read_only: false
vars:
ngfw_device:
name:
underscore: ngfw_device
camelcase: NgfwDevice
description: The NGFW device.
required: false
validation: null
vsys:
name:
underscore: vsys
camelcase: Vsys
description: The vsys.
required: false
validation:
Expand Down
16 changes: 16 additions & 0 deletions pkg/translate/funcs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package translate

import (
"errors"
"github.com/paloaltonetworks/pan-os-codegen/pkg/naming"
"strings"
)

func AsEntryXpath(location, xpath string) (string, error) {
if !strings.Contains(xpath, "$") || !strings.Contains(xpath, "}") {
return "", errors.New("$ followed by } should exists in xpath'")
}
xpath = strings.TrimSpace(strings.Split(strings.Split(xpath, "$")[1], "}")[0])
xpath = naming.CamelCase("", xpath, "", true)
return "util.AsEntryXpath([]string{o." + location + "." + xpath + "}),", nil
}
16 changes: 16 additions & 0 deletions pkg/translate/funcs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package translate

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestAsEntryXpath(t *testing.T) {
// given

// when
asEntryXpath, _ := AsEntryXpath("DeviceGroup", "{{ Entry $panorama_device }}")

// then
assert.Equal(t, "util.AsEntryXpath([]string{o.DeviceGroup.PanoramaDevice}),", asEntryXpath)
}
2 changes: 1 addition & 1 deletion pkg/translate/names.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package translate

// Get package name from Go SDK path
// PackageName Get package name from Go SDK path
func PackageName(list []string) string {
if len(list) == 0 {
return ""
Expand Down
25 changes: 25 additions & 0 deletions pkg/translate/structs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package translate

import (
"github.com/paloaltonetworks/pan-os-codegen/pkg/properties"
)

func LocationType(location *properties.Location, pointer bool) string {
prefix := ""
if pointer {
prefix = "*"
}
if location.Vars != nil {
return prefix + location.Name.CamelCase + "Location"
} else {
return "bool"
}
}

func OmitEmpty(location *properties.Location) string {
if location.Vars != nil {
return ",omitempty"
} else {
return ""
}
}
86 changes: 86 additions & 0 deletions pkg/translate/structs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package translate

import (
"github.com/paloaltonetworks/pan-os-codegen/pkg/properties"
"github.com/stretchr/testify/assert"
"testing"
)

const sampleSpec = `name: 'Address'
terraform_provider_suffix: 'address'
go_sdk_path:
- 'objects'
- 'address'
xpath_suffix:
- 'address'
locations:
'shared':
description: 'Located in shared.'
device:
panorama: true
ngfw: true
xpath: ['config', 'shared']
'device_group':
description: 'Located in a specific device group.'
device:
panorama: true
xpath:
- 'config'
- 'devices'
- '{{ Entry $panorama_device }}'
- 'device-group'
- '{{ Entry $device_group }}'
vars:
'panorama_device':
description: 'The panorama device.'
default: 'localhost.localdomain'
'device_group':
description: 'The device group.'
required: true
validation:
not_values:
'shared': 'The device group cannot be "shared". Use the "shared" path instead.'
entry:
name:
description: 'The name of the address object.'
length:
min: 1
max: 63
version: '10.1.0'
`

func TestLocationType(t *testing.T) {
// given
yamlParsedData, _ := properties.ParseSpec([]byte(sampleSpec))
locationKeys := []string{"device_group", "shared"}
locations := yamlParsedData.Locations
var locationTypes []string

// when
for _, locationKey := range locationKeys {
locationTypes = append(locationTypes, LocationType(locations[locationKey], true))
}

// then
assert.NotEmpty(t, locationTypes)
assert.Contains(t, locationTypes, "*DeviceGroupLocation")
assert.Contains(t, locationTypes, "bool")
}

func TestOmitEmpty(t *testing.T) {
// given
yamlParsedData, _ := properties.ParseSpec([]byte(sampleSpec))
locationKeys := []string{"device_group", "shared"}
locations := yamlParsedData.Locations
var omitEmptyLocations []string

// when
for _, locationKey := range locationKeys {
omitEmptyLocations = append(omitEmptyLocations, OmitEmpty(locations[locationKey]))
}

// then
assert.NotEmpty(t, omitEmptyLocations)
assert.Contains(t, omitEmptyLocations, ",omitempty")
assert.Contains(t, omitEmptyLocations, "")
}
3 changes: 2 additions & 1 deletion specs/objects/address-group.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ name: 'Address group'
terraform_provider_suffix: 'address_group'
go_sdk_path:
- 'objects'
- 'address_group'
- 'address'
- 'group'
xpath_suffix:
- 'address-group'
locations:
Expand Down
3 changes: 2 additions & 1 deletion specs/objects/service-group.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ name: 'Service group'
terraform_provider_suffix: 'service_group'
go_sdk_path:
- 'objects'
- 'service_group'
- 'service'
- 'group'
xpath_suffix:
- 'service-group'
locations:
Expand Down
10 changes: 10 additions & 0 deletions specs/panorama/device-group.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ locations:
- '{{ Entry $ngfw_device }}'
- 'vsys'
- '{{ Entry $vsys }}'
vars:
'ngfw_device':
description: 'The NGFW device.'
default: 'localhost.localdomain'
'vsys':
description: 'The vsys.'
default: 'vsys1'
validation:
not_values:
'shared': 'The vsys cannot be "shared". Use the "shared" path instead.'
entry:
name:
description: 'The name of the device group.'
Expand Down
10 changes: 10 additions & 0 deletions specs/panorama/template-stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ locations:
- '{{ Entry $ngfw_device }}'
- 'vsys'
- '{{ Entry $vsys }}'
vars:
'ngfw_device':
description: 'The NGFW device.'
default: 'localhost.localdomain'
'vsys':
description: 'The vsys.'
default: 'vsys1'
validation:
not_values:
'shared': 'The vsys cannot be "shared". Use the "shared" path instead.'
entry:
name:
description: 'The name of the template stack.'
Expand Down
Loading

0 comments on commit 65727a4

Please sign in to comment.