Skip to content

chore: Remove 'spec' prefix from pango type and func names #115

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

Merged
merged 2 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
88 changes: 44 additions & 44 deletions assets/pango/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ func checkTemplate(c *pango.Client, ctx context.Context) {
Name: "codegen_template",
Description: util.String("This is a template created by codegen."),
DefaultVsys: util.String("vsys1"),
Config: &template.SpecConfig{
Devices: []template.SpecConfigDevices{
Config: &template.Config{
Devices: []template.ConfigDevices{
{
Name: "localhost.localdomain",
Vsys: []template.SpecConfigDevicesVsys{
Vsys: []template.ConfigDevicesVsys{
{
Name: "vsys1",
},
Expand Down Expand Up @@ -182,41 +182,41 @@ func checkSharedObjects(c *pango.Client, ctx context.Context) {
func checkVr(c *pango.Client, ctx context.Context) {
entry := virtual_router.Entry{
Name: "codegen_vr",
Protocol: &virtual_router.SpecProtocol{
Bgp: &virtual_router.SpecProtocolBgp{
Protocol: &virtual_router.Protocol{
Bgp: &virtual_router.ProtocolBgp{
Enable: util.Bool(false),
},
Ospf: &virtual_router.SpecProtocolOspf{
Ospf: &virtual_router.ProtocolOspf{
Enable: util.Bool(false),
},
Ospfv3: &virtual_router.SpecProtocolOspfv3{
Ospfv3: &virtual_router.ProtocolOspfv3{
Enable: util.Bool(false),
},
Rip: &virtual_router.SpecProtocolRip{
Rip: &virtual_router.ProtocolRip{
Enable: util.Bool(false),
},
},
RoutingTable: &virtual_router.SpecRoutingTable{
// Ip: &virtual_router.SpecRoutingTableIp{
// StaticRoutes: []virtual_router.SpecRoutingTableIpStaticRoutes{
RoutingTable: &virtual_router.RoutingTable{
// Ip: &virtual_router.RoutingTableIp{
// StaticRoutes: []virtual_router.RoutingTableIpStaticRoutes{
// {
// Name: "default",
// Destination: util.String("0.0.0.0/0"),
// Interface: util.String("ethernet1/2"),
// NextHop: &virtual_router.SpecRoutingTableIpStaticRoutesNextHop{
// NextHop: &virtual_router.RoutingTableIpStaticRoutesNextHop{
// IpAddress: util.String("1.1.1.1"),
// },
// Metric: util.Int(64),
// AdminDist: util.Int(120),
// },
// },
// },
// Ipv6: &virtual_router.SpecRoutingTableIpv6{
// StaticRoutes: []virtual_router.SpecRoutingTableIpv6StaticRoutes{
// Ipv6: &virtual_router.RoutingTableIpv6{
// StaticRoutes: []virtual_router.RoutingTableIpv6StaticRoutes{
// {
// Name: "default",
// Destination: util.String("0.0.0.0/0"),
// NextHop: &virtual_router.SpecRoutingTableIpv6StaticRoutesNextHop{
// NextHop: &virtual_router.RoutingTableIpv6StaticRoutesNextHop{
// Ipv6Address: util.String("2001:0000:130F:0000:0000:09C0:876A:230D"),
// },
// Metric: util.Int(24),
Expand All @@ -225,18 +225,18 @@ func checkVr(c *pango.Client, ctx context.Context) {
// },
// },
},
Ecmp: &virtual_router.SpecEcmp{
Ecmp: &virtual_router.Ecmp{
Enable: util.Bool(true),
SymmetricReturn: util.Bool(true),
MaxPaths: util.Int(3),
Algorithm: &virtual_router.SpecEcmpAlgorithm{
// IpHash: &virtual_router.SpecEcmpAlgorithmIpHash{
Algorithm: &virtual_router.EcmpAlgorithm{
// IpHash: &virtual_router.EcmpAlgorithmIpHash{
// HashSeed: util.Int(1234),
// UsePort: util.Bool(true),
// SrcOnly: util.Bool(true),
// },
// WeightedRoundRobin: &virtual_router.SpecEcmpAlgorithmWeightedRoundRobin{
// Interfaces: []virtual_router.SpecEcmpAlgorithmWeightedRoundRobinInterfaces{
// WeightedRoundRobin: &virtual_router.EcmpAlgorithmWeightedRoundRobin{
// Interfaces: []virtual_router.EcmpAlgorithmWeightedRoundRobinInterfaces{
// {
// Name: "ethernet1/2",
// Weight: util.Int(1),
Expand All @@ -249,7 +249,7 @@ func checkVr(c *pango.Client, ctx context.Context) {
// },
},
},
AdministrativeDistances: &virtual_router.SpecAdministrativeDistances{
AdministrativeDistances: &virtual_router.AdministrativeDistances{
OspfInt: util.Int(77),
OspfExt: util.Int(88),
},
Expand All @@ -275,18 +275,18 @@ func checkEthernetLayer3Static(c *pango.Client, ctx context.Context) {
entry := ethernet.Entry{
Name: "ethernet1/2",
Comment: util.String("This is a ethernet1/2"),
Layer3: &ethernet.SpecLayer3{
Layer3: &ethernet.Layer3{
NdpProxy: util.Bool(true),
Lldp: util.Bool(true),
AdjustTcpMss: &ethernet.SpecLayer3AdjustTcpMss{
AdjustTcpMss: &ethernet.Layer3AdjustTcpMss{
Enable: util.Bool(true),
Ipv4MssAdjustment: util.Int(250),
Ipv6MssAdjustment: util.Int(250),
},
Mtu: util.Int(1280),
Ips: []string{"11.11.11.11", "22.22.22.22"},
Ipv6: &ethernet.SpecLayer3Ipv6{
Addresses: []ethernet.SpecLayer3Ipv6Addresses{
Ipv6: &ethernet.Layer3Ipv6{
Addresses: []ethernet.Layer3Ipv6Addresses{
{
EnableOnInterface: util.Bool(false),
Name: "2001:0000:130F:0000:0000:09C0:876A:230B",
Expand Down Expand Up @@ -321,13 +321,13 @@ func checkEthernetLayer3Dhcp(c *pango.Client, ctx context.Context) {
entry := ethernet.Entry{
Name: "ethernet1/3",
Comment: util.String("This is a ethernet1/3"),
Layer3: &ethernet.SpecLayer3{
Layer3: &ethernet.Layer3{
InterfaceManagementProfile: util.String("codegen_mgmt_profile"),
DhcpClient: &ethernet.SpecLayer3DhcpClient{
DhcpClient: &ethernet.Layer3DhcpClient{
CreateDefaultRoute: util.Bool(false),
DefaultRouteMetric: util.Int(64),
Enable: util.Bool(true),
SendHostname: &ethernet.SpecLayer3DhcpClientSendHostname{
SendHostname: &ethernet.Layer3DhcpClientSendHostname{
Enable: util.Bool(true),
Hostname: util.String("codegen_dhcp"),
},
Expand Down Expand Up @@ -355,7 +355,7 @@ func checkEthernetHa(c *pango.Client, ctx context.Context) {
entry := ethernet.Entry{
Name: "ethernet1/10",
Comment: util.String("This is a ethernet1/10"),
Ha: &ethernet.SpecHa{},
Ha: &ethernet.Ha{},
}
var location *ethernet.Location
if ok, _ := c.IsPanorama(); ok {
Expand All @@ -377,16 +377,16 @@ func checkEthernetHa(c *pango.Client, ctx context.Context) {
func checkLoopback(c *pango.Client, ctx context.Context) {
entry := loopback.Entry{
Name: "loopback.123",
AdjustTcpMss: &loopback.SpecAdjustTcpMss{
AdjustTcpMss: &loopback.AdjustTcpMss{
Enable: util.Bool(true),
Ipv4MssAdjustment: util.Int(250),
Ipv6MssAdjustment: util.Int(250),
},
Comment: util.String("This is a loopback entry"),
Mtu: util.Int(1280),
Ips: []string{"1.1.1.1", "2.2.2.2"},
Ipv6: &loopback.SpecIpv6{
Addresses: []loopback.SpecIpv6Addresses{
Ipv6: &loopback.Ipv6{
Addresses: []loopback.Ipv6Addresses{
{
EnableOnInterface: util.Bool(false),
Name: "2001:0000:130F:0000:0000:09C0:876A:130B",
Expand Down Expand Up @@ -420,14 +420,14 @@ func checkZone(c *pango.Client, ctx context.Context) {
entry := zone.Entry{
Name: "codegen_zone",
EnableUserIdentification: util.Bool(true),
Network: &zone.SpecNetwork{
Network: &zone.Network{
EnablePacketBufferProtection: util.Bool(false),
Layer3: []string{},
},
DeviceAcl: &zone.SpecDeviceAcl{
DeviceAcl: &zone.DeviceAcl{
IncludeList: []string{"1.2.3.4"},
},
UserAcl: &zone.SpecUserAcl{
UserAcl: &zone.UserAcl{
ExcludeList: []string{"1.2.3.4"},
},
}
Expand Down Expand Up @@ -516,7 +516,7 @@ func checkVrZoneWithEthernet(c *pango.Client, ctx context.Context) {
}
log.Printf("Zone %s read\n", replyZone.Name)

replyZone.Network = &zone.SpecNetwork{
replyZone.Network = &zone.Network{
EnablePacketBufferProtection: util.Bool(false),
Layer3: []string{"ethernet1/2", "ethernet1/3"},
}
Expand All @@ -539,7 +539,7 @@ func checkVrZoneWithEthernet(c *pango.Client, ctx context.Context) {
log.Printf("VR %s updated with %s\n", replyVr.Name, replyVr.Interfaces)

// DELETE INTERFACES FROM ZONE
replyZone.Network = &zone.SpecNetwork{
replyZone.Network = &zone.Network{
EnablePacketBufferProtection: util.Bool(false),
Layer3: []string{},
}
Expand Down Expand Up @@ -878,10 +878,10 @@ func checkService(c *pango.Client, ctx context.Context) {
serviceObject := service.Entry{
Name: "codegen_service_test1",
Description: util.String("test description"),
Protocol: &service.SpecProtocol{
Tcp: &service.SpecProtocolTcp{
Protocol: &service.Protocol{
Tcp: &service.ProtocolTcp{
DestinationPort: util.Int(8642),
Override: &service.SpecProtocolTcpOverride{
Override: &service.ProtocolTcpOverride{
HalfcloseTimeout: util.Int(124),
Timeout: util.Int(125),
TimewaitTimeout: util.Int(127),
Expand Down Expand Up @@ -1035,8 +1035,8 @@ func checkService(c *pango.Client, ctx context.Context) {
func checkNtp(c *pango.Client, ctx context.Context) {
// NTP - ADD
ntpConfig := ntp.Config{
NtpServers: &ntp.SpecNtpServers{
PrimaryNtpServer: &ntp.SpecNtpServersPrimaryNtpServer{
NtpServers: &ntp.NtpServers{
PrimaryNtpServer: &ntp.NtpServersPrimaryNtpServer{
NtpServerAddress: util.String("11.12.13.14"),
},
},
Expand Down Expand Up @@ -1071,8 +1071,8 @@ func checkNtp(c *pango.Client, ctx context.Context) {
func checkDns(c *pango.Client, ctx context.Context) {
// DNS - ADD
dnsConfig := dns.Config{
DnsSetting: &dns.SpecDnsSetting{
Servers: &dns.SpecDnsSettingServers{
DnsSetting: &dns.DnsSetting{
Servers: &dns.DnsSettingServers{
Primary: util.String("8.8.8.8"),
Secondary: util.String("4.4.4.4"),
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/translate/assignments.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (
// in entry.tmpl/config.tmpl template. If param contains nested specs, then recursively are executed
// internal functions, which are creating entry assignment.
func NormalizeAssignment(objectType string, param *properties.SpecParam, version string) string {
return prepareAssignment(objectType, param, "util.MemToStr", "util.EntToStr", "util.AsBool", "Spec", "", version)
return prepareAssignment(objectType, param, "util.MemToStr", "util.EntToStr", "util.AsBool", "", "", version)
}

// SpecifyEntryAssignment generates a string, which contains entry/config assignment in SpecifyEntry() function
// in entry.tmpl/config.tmpl template. If param contains nested specs, then recursively are executed
// internal functions, which are creating entry assignment.
func SpecifyEntryAssignment(objectType string, param *properties.SpecParam, version string) string {
return prepareAssignment(objectType, param, "util.StrToMem", "util.StrToEnt", "util.YesNo", "spec", "Xml", version)
return prepareAssignment(objectType, param, "util.StrToMem", "util.StrToEnt", "util.YesNo", "", "Xml", version)
}

func prepareAssignment(objectType string, param *properties.SpecParam, listFunction, entryFunction, boolFunction, prefix, suffix string, version string) string {
Expand Down
9 changes: 5 additions & 4 deletions pkg/translate/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package translate

import (
"fmt"
"strings"

"github.com/paloaltonetworks/pan-os-codegen/pkg/naming"
"github.com/paloaltonetworks/pan-os-codegen/pkg/properties"
"strings"
)

// GenerateEntryXpath functions used in location.tmpl to generate XPath for location.
Expand Down Expand Up @@ -46,7 +47,7 @@ func specMatchFunctionName(parent []string, param *properties.SpecParam) string
} else if param.Type == "int64" {
return "util.Ints64Match"
} else {
return fmt.Sprintf("specMatch%s%s", strings.Join(parent, ""), param.Name.CamelCase)
return fmt.Sprintf("match%s%s", strings.Join(parent, ""), param.Name.CamelCase)
}
}

Expand Down Expand Up @@ -83,7 +84,7 @@ func defineSpecMatchesFunction(parent []string, params map[string]*properties.Sp

func renderSpecMatchesFunctionSignature(parent []string, builder *strings.Builder, param *properties.SpecParam) {
prefix := determinePrefix(param, false)
builder.WriteString(fmt.Sprintf("func specMatch%s%s(a %s%s, b %s%s) bool {",
builder.WriteString(fmt.Sprintf("func match%s%s(a %s%s, b %s%s) bool {",
strings.Join(parent, ""), param.Name.CamelCase,
prefix, argumentTypeForSpecMatchesFunction(parent, param),
prefix, argumentTypeForSpecMatchesFunction(parent, param)))
Expand All @@ -95,7 +96,7 @@ func argumentTypeForSpecMatchesFunction(parent []string, param *properties.SpecP
} else if param.Type == "int" {
return "int"
} else {
return fmt.Sprintf("Spec%s%s",
return fmt.Sprintf("%s%s",
strings.Join(parent, ""), param.Name.CamelCase)
}
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/translate/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package translate

import (
"fmt"
"strings"

"github.com/paloaltonetworks/pan-os-codegen/pkg/naming"
"github.com/paloaltonetworks/pan-os-codegen/pkg/properties"
"github.com/paloaltonetworks/pan-os-codegen/pkg/version"
"strings"
)

// LocationType function used in template location.tmpl to generate location type name.
Expand All @@ -21,7 +22,7 @@ func LocationType(location *properties.Location, pointer bool) string {
}
}

// NestedSpecs go through all params and one of (recursively) and return map of all nested specs.
// NestedSpecs goes through all params and one ofs (recursively) and returns map of all nested specs.
func NestedSpecs(spec *properties.Spec) (map[string]*properties.Spec, error) {
nestedSpecs := make(map[string]*properties.Spec)

Expand Down Expand Up @@ -65,7 +66,7 @@ func addNameAsParamForNestedSpec(parent []string, nestedSpecs map[string]*proper
}
}

// SpecParamType return param type (it can be nested spec) (for struct based on spec from YAML files).
// SpecParamType returns param type (it can be a nested spec) for structs based on spec from YAML files.
func SpecParamType(parent string, param *properties.SpecParam) string {
prefix := determinePrefix(param, false)

Expand Down Expand Up @@ -122,11 +123,11 @@ func determineListType(param *properties.SpecParam) string {
}

func calculateNestedSpecType(parent string, param *properties.SpecParam) string {
return fmt.Sprintf("Spec%s%s", parent, naming.CamelCase("", param.Name.CamelCase, "", true))
return fmt.Sprintf("%s%s", parent, naming.CamelCase("", param.Name.CamelCase, "", true))
}

func calculateNestedXmlSpecType(parent string, param *properties.SpecParam) string {
return fmt.Sprintf("spec%s%sXml", parent, naming.CamelCase("", param.Name.CamelCase, "", true))
return fmt.Sprintf("%s%sXml", parent, naming.CamelCase("", param.Name.CamelCase, "", true))
}

// XmlName creates a string with xml name (e.g. `description`).
Expand Down
4 changes: 2 additions & 2 deletions templates/sdk/config.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}

{{- range $name, $spec := nestedSpecs $.Spec }}
type Spec{{$name}}{{createGoSuffixFromVersion ""}} struct {
type {{$name}}{{createGoSuffixFromVersion ""}} struct {
{{- range $_, $param := $spec.Params}}
{{$param.Name.CamelCase}} {{specParamType $name $param}}
{{- end}}
Expand Down Expand Up @@ -64,7 +64,7 @@

{{- range $version := .SupportedVersions }}
{{- range $name, $spec := nestedSpecs $.Spec }}
type spec{{$name}}Xml{{createGoSuffixFromVersion $version}} struct {
type {{$name}}Xml{{createGoSuffixFromVersion $version}} struct {
{{- range $_, $param := $spec.Params}}
{{- if paramSupportedInVersion $param $version}}
{{- if $param.Spec}}
Expand Down
Loading
Loading