Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Extend service template #50

Merged
merged 9 commits into from
Apr 9, 2024
57 changes: 57 additions & 0 deletions assets/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,42 @@ func main() {
return
}

// SECURITY POLICY RULE - ADD
securityPolicyRuleName := "codegen_rule"
securityPolicyRuleAction := "allow"
securityPolicyRuleSourceZones := []string{"any"}
securityPolicyRuleDestinationZones := []string{"any"}
securityPolicyRuleEntry := security.Entry{
Name: securityPolicyRuleName,
Action: &securityPolicyRuleAction,
SourceZone: securityPolicyRuleSourceZones,
DestinationZone: securityPolicyRuleDestinationZones,
}

securityPolicyRuleLocation := security.Location{
Vsys: &security.VsysLocation{
NgfwDevice: "localhost.localdomain",
Rulebase: "post-rulebase",
Vsys: "vsys1",
},
}

securityPolicyRuleApi := security.NewService(c)
securityPolicyRuleReply, err := securityPolicyRuleApi.Create(ctx, securityPolicyRuleLocation, securityPolicyRuleEntry)
if err != nil {
log.Printf("Failed to create security policy rule: %s", err)
return
}
log.Printf("Security policy rule '%s:%s' created", *securityPolicyRuleReply.Uuid, securityPolicyRuleReply.Name)

// SECURITY POLICY RULE - DELETE
err = securityPolicyRuleApi.Delete(ctx, securityPolicyRuleLocation, securityPolicyRuleName)
if err != nil {
log.Printf("Failed to delete security policy rule: %s", err)
return
}
log.Printf("Security policy rule '%s' deleted", securityPolicyRuleName)

// TAG - CREATE
tagName := "codegen_color"
tagColor := tag.ColorAzureBlue
Expand Down Expand Up @@ -89,6 +125,16 @@ func main() {
}
log.Printf("Address '%s' deleted", addressName)

// ADDRESS - LIST
addresses, err := addressApi.List(ctx, addressLocation, "get", "name starts-with 'wu'", "'")
if err != nil {
log.Printf("Failed to list object: %s", err)
} else {
for index, item := range addresses {
log.Printf("Address %d: '%s'", index, item.Name)
}
}

// SERVICE - ADD
serviceName := "codegen_service_test1"
servicePort := 8642
Expand Down Expand Up @@ -156,6 +202,17 @@ func main() {
}
log.Printf("Service '%s=%d' renamed", serviceReply.Name, *serviceReply.Protocol.Tcp.DestinationPort)

// SERViCE - LIST
//services, err := serviceApi.List(ctx, serviceLocation, "get", "name starts-with 'test'", "'")
services, err := serviceApi.List(ctx, serviceLocation, "get", "", "")
if err != nil {
log.Printf("Failed to list object: %s", err)
} else {
for index, item := range services {
log.Printf("Service %d: '%s'", index, item.Name)
}
}

// SERVICE - DELETE
err = serviceApi.Delete(ctx, serviceLocation, newServiceName)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions pkg/translate/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ import (
)

// GenerateEntryXpathForLocation functions used in location.tmpl to generate XPath for location.
func GenerateEntryXpathForLocation(location, xpath string) (string, error) {
func GenerateEntryXpathForLocation(prefix, suffix, location, xpath string) (string, error) {
if !strings.Contains(xpath, "$") || !strings.Contains(xpath, "}") {
return "", fmt.Errorf("xpath '%s' is missing '$' followed by '}'", xpath)
}
asEntryXpath := generateEntryXpathForLocation(location, xpath)
asEntryXpath := generateEntryXpathForLocation(prefix, suffix, location, xpath)
return asEntryXpath, nil
}

func generateEntryXpathForLocation(location string, xpath string) string {
func generateEntryXpathForLocation(prefix, suffix, location, xpath string) string {
xpathPartWithoutDollar := strings.SplitAfter(xpath, "$")
xpathPartWithoutBrackets := strings.TrimSpace(strings.Trim(xpathPartWithoutDollar[1], "${}"))
xpathPartCamelCase := naming.CamelCase("", xpathPartWithoutBrackets, "", true)
asEntryXpath := fmt.Sprintf("util.AsEntryXpath([]string{o.%s.%s}),", location, xpathPartCamelCase)
asEntryXpath := fmt.Sprintf("%so.%s.%s%s,", prefix, location, xpathPartCamelCase, suffix)
return asEntryXpath
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/translate/funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func TestGenerateEntryXpath(t *testing.T) {
// given

// when
asEntryXpath, _ := GenerateEntryXpathForLocation("DeviceGroup", "{{ Entry $panorama_device }}")
asEntryXpath, _ := GenerateEntryXpathForLocation("util.AsEntryXpath([]string{", "})", "DeviceGroup", "{{ Entry $panorama_device }}")

// then
assert.Equal(t, "util.AsEntryXpath([]string{o.DeviceGroup.PanoramaDevice}),", asEntryXpath)
Expand Down
45 changes: 24 additions & 21 deletions pkg/translate/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,32 @@ package translate
import "github.com/paloaltonetworks/pan-os-codegen/pkg/imports"

// RenderImports render string, which contains import required in entry, location or service template.
func RenderImports(templateType string) (string, error) {
func RenderImports(templateTypes ...string) (string, error) {
manager := imports.NewManager()

switch templateType {
case "entry":
manager.AddStandardImport("encoding/xml", "")
manager.AddStandardImport("fmt", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/filtering", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/generic", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/util", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/version", "")
case "location":
manager.AddStandardImport("fmt", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/errors", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/util", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/version", "")
case "service":
manager.AddStandardImport("context", "")
manager.AddStandardImport("fmt", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/errors", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/filtering", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/util", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/xmlapi", "")
for _, templateType := range templateTypes {
switch templateType {
case "entry":
manager.AddStandardImport("encoding/xml", "")
manager.AddStandardImport("fmt", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/filtering", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/generic", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/util", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/version", "")
case "location":
manager.AddStandardImport("fmt", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/errors", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/util", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/version", "")
case "service":
manager.AddStandardImport("context", "")
manager.AddStandardImport("fmt", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/errors", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/util", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/xmlapi", "")
case "filtering":
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/filtering", "")
}
}

return manager.RenderImports()
Expand Down
7 changes: 6 additions & 1 deletion pkg/translate/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,13 @@ func XmlName(param *properties.SpecParam) string {
func XmlTag(param *properties.SpecParam) string {
if param.Profiles != nil && len(param.Profiles) > 0 {
suffix := ""

if param.Name != nil && param.Name.Underscore == "uuid" {
suffix = suffix + ",attr"
}

if !param.Required {
suffix = ",omitempty"
suffix = suffix + ",omitempty"
}

return fmt.Sprintf("`xml:\"%s%s\"`", XmlName(param), suffix)
Expand Down
20 changes: 18 additions & 2 deletions pkg/translate/structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,28 @@ func TestXmlTag(t *testing.T) {
// given
paramTypeRequiredString := properties.SpecParam{
Type: "string",
Required: false,
Required: true,
Profiles: []*properties.SpecParamProfile{
{
Type: "string",
Xpath: []string{"description"},
},
},
}
paramTypeUuid := properties.SpecParam{
Type: "string",
Required: false,
Name: &properties.NameVariant{
CamelCase: "Uuid",
Underscore: "uuid",
},
Profiles: []*properties.SpecParamProfile{
{
Type: "string",
Xpath: []string{"uuid"},
},
},
}
paramTypeListString := properties.SpecParam{
Type: "list",
Items: &properties.SpecParamItems{
Expand All @@ -175,10 +189,12 @@ func TestXmlTag(t *testing.T) {
// when
calculatedXmlTagRequiredString := XmlTag(&paramTypeRequiredString)
calculatedXmlTagListString := XmlTag(&paramTypeListString)
calculatedXmlTagUuid := XmlTag(&paramTypeUuid)

// then
assert.Equal(t, "`xml:\"description,omitempty\"`", calculatedXmlTagRequiredString)
assert.Equal(t, "`xml:\"description\"`", calculatedXmlTagRequiredString)
assert.Equal(t, "`xml:\"tag,omitempty\"`", calculatedXmlTagListString)
assert.Equal(t, "`xml:\"uuid,attr,omitempty\"`", calculatedXmlTagUuid)
}

func TestNestedSpecs(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions specs/policies/security-policy-rule.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ locations:
xpath:
- 'config'
- 'shared'
- '{{ Entry $rulebase }}'
- '{{ Object $rulebase }}'
vars:
'rulebase':
description: 'The rulebase.'
Expand All @@ -34,7 +34,7 @@ locations:
- '{{ Entry $ngfw_device }}'
- 'vsys'
- '{{ Entry $vsys }}'
- '{{ Entry $rulebase }}'
- 'rulebase'
vars:
'ngfw_device':
description: 'The NGFW device.'
Expand All @@ -60,7 +60,7 @@ locations:
- '{{ Entry $panorama_device }}'
- 'device-group'
- '{{ Entry $device_group }}'
- '{{ Entry $rulebase }}'
- '{{ Object $rulebase }}'
vars:
'panorama_device':
description: 'The panorama device.'
Expand Down
40 changes: 33 additions & 7 deletions templates/sdk/location.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ return fmt.Errorf("multiple paths specified: only one should be specified")
return nil
}

{{- if .Entry}}
func (o Location) Xpath(vn version.Number, name string) ([]string, error) {
{{- else}}
func (o Location) Xpath(vn version.Number) ([]string, error) {
{{- end}}
func (o Location) xpath(vn version.Number) ([]string, error) {

var ans []string

Expand All @@ -61,7 +57,9 @@ switch {
ans = []string{
{{- range $name, $xpath := $location.Xpath}}
{{- if contains $xpath "Entry"}}
{{generateEntryXpath $location.Name.CamelCase $xpath}}
{{generateEntryXpath "util.AsEntryXpath([]string{" "})" $location.Name.CamelCase $xpath}}
{{- else if contains $xpath "Object"}}
{{generateEntryXpath "" "" $location.Name.CamelCase $xpath}}
{{- else}}
"{{$xpath}}",
{{- end}}
Expand All @@ -72,10 +70,38 @@ default:
return nil, errors.NoLocationSpecifiedError
}

return ans, nil
}

{{- if .Entry}}
func (o Location) XpathWithEntryName(vn version.Number, name string) ([]string, error) {
{{- else}}
func (o Location) Xpath(vn version.Number) ([]string, error) {
{{- end}}

ans, err := o.xpath(vn)
if err != nil {
return nil, err
}

{{- if .Entry}}
ans = append(ans, Suffix...)
ans = append(ans, util.AsEntryXpath([]string{name}))
{{- end}}

return ans, nil
}
}

{{- if .Entry}}
func (o Location) XpathWithUuid(vn version.Number, uuid string) ([]string, error) {

ans, err := o.xpath(vn)
if err != nil {
return nil, err
}
ans = append(ans, Suffix...)
ans = append(ans, util.AsUuidXpath(uuid))

return ans, nil
}
{{- end}}
Loading