Skip to content

Commit

Permalink
adding the new address object namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
shinmog committed Feb 22, 2024
1 parent a21074f commit d5a937c
Show file tree
Hide file tree
Showing 4 changed files with 556 additions and 0 deletions.
130 changes: 130 additions & 0 deletions objects/address/entry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package address

import (
"fmt"
"encoding/xml"

"github.com/PaloAltoNetworks/pango/generic"
"github.com/PaloAltoNetworks/pango/util"
"github.com/PaloAltoNetworks/pango/version"
)

var (
Suffix = []string{"address"}
)

type Entry struct {
Name string
Description *string
Tags []string // ordered
IpNetmask *string
IpRange *string
Fqdn *string
IpWildcard *string // PAN-OS 9.0

Misc map[string] []generic.Xml
}

func (e *Entry) CopyMiscFrom(v *Entry) {
if v == nil || len(v.Misc) == 0 {
return
}

e.Misc = make(map[string] []generic.Xml)
for key := range v.Misc {
e.Misc[key] = append([]generic.Xml(nil), v.Misc[key]...)
}
}

func Versioning(vn version.Number) (Specifier, Normalizer, error) {
return Entry1Specify, &Entry1Container{}, nil
}

func Entry1Specify(o Entry) (any, error) {
ans := Entry1{}
ans.Name = o.Name
ans.Description = o.Description
ans.Tags = util.StrToMem(o.Tags)
ans.IpNetmask = o.IpNetmask
ans.IpRange = o.IpRange
ans.Fqdn = o.Fqdn
ans.IpWildcard = o.IpWildcard

ans.Misc = o.Misc[fmt.Sprintf("%s\n%s", "Entry", o.Name)]

return ans, nil
}

type Entry1Container struct {
Answer []Entry1 `xml:"entry"`
}

func (c *Entry1Container) Normalize() ([]Entry, error) {
ans := make([]Entry, 0, len(c.Answer))
for _, var0 := range c.Answer {
var1 := Entry{
Misc: make(map[string] []generic.Xml),
}
var1.Name = var0.Name
var1.Description = var0.Description
var1.IpNetmask = var0.IpNetmask
var1.IpRange = var0.IpRange
var1.Fqdn = var0.Fqdn
var1.IpWildcard = var0.IpWildcard

var1.Misc[fmt.Sprintf("%s\n%s", "Entry", var0.Name)] = var0.Misc

ans = append(ans, var1)
}

return ans, nil
}

type Entry1 struct {
XMLName xml.Name `xml:"entry"`
Name string `xml:"name,attr"`
IpNetmask *string `xml:"ip-netmask"`
IpRange *string `xml:"ip-range"`
Fqdn *string `xml:"fqdn"`
IpWildcard *string `xml:"ip-wildcard"`
Description *string `xml:"description,omitempty"`
Tags *util.MemberType `xml:"tag"`

Misc []generic.Xml `xml:",any"`
}

func SpecMatches(a, b *Entry) bool {
if a == nil && b != nil || a != nil && b == nil {
return false
} else if a == nil && b == nil {
return true
}

// Don't compare Name.

if !util.OptionalStringsMatch(a.Description, b.Description) {
return false
}

if !util.OrderedListsMatch(a.Tags, b.Tags) {
return false
}

if !util.OptionalStringsMatch(a.IpNetmask, b.IpNetmask) {
return false
}

if !util.OptionalStringsMatch(a.IpRange, b.IpRange) {
return false
}

if !util.OptionalStringsMatch(a.Fqdn, b.Fqdn) {
return false
}

if !util.OptionalStringsMatch(a.IpWildcard, b.IpWildcard) {
return false
}

return true
}
7 changes: 7 additions & 0 deletions objects/address/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package address

type Specifier func(Entry) (any, error)

type Normalizer interface {
Normalize() ([]Entry, error)
}
75 changes: 75 additions & 0 deletions objects/address/location.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package address

import (
"fmt"

"github.com/PaloAltoNetworks/pango/errors"
"github.com/PaloAltoNetworks/pango/util"
"github.com/PaloAltoNetworks/pango/version"
)

type Location struct {
Shared bool
Vsys *VsysLocation
DeviceGroup *DeviceGroupLocation
FromPanorama bool
}

func (o Location) Xpath(vn version.Number, name string) ([]string, error) {
var ans []string

switch {
case o.Shared:
ans = []string{
"config",
"shared",
}
case o.Vsys != nil:
if o.Vsys.NgfwDevice == "" {
return nil, fmt.Errorf("NgfwDevice is unspecified")
}
if o.Vsys.Vsys == "" {
return nil, fmt.Errorf("Vsys is unspecified")
}
ans = []string{
"config",
"devices",
util.AsEntryXpath([]string{o.Vsys.NgfwDevice}),
"vsys",
util.AsEntryXpath([]string{o.Vsys.Vsys}),
}
case o.DeviceGroup != nil:
if o.DeviceGroup.PanoramaDevice == "" {
return nil, fmt.Errorf("PanoramaDevice is unspecified")
}
if o.DeviceGroup.DeviceGroup == "" {
return nil, fmt.Errorf("DeviceGroup is unspecified")
}
ans = []string{
"config",
"devices",
util.AsEntryXpath([]string{o.DeviceGroup.PanoramaDevice}),
"device-group",
util.AsEntryXpath([]string{o.DeviceGroup.DeviceGroup}),
}
case o.FromPanorama:
ans = []string{"config", "panorama"}
default:
return nil, errors.NoLocationSpecifiedError
}

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

return ans, nil
}

type VsysLocation struct {
NgfwDevice string
Vsys string
}

type DeviceGroupLocation struct {
PanoramaDevice string
DeviceGroup string
}
Loading

0 comments on commit d5a937c

Please sign in to comment.