forked from PaloAltoNetworks/pango
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from pimielowski/improve_validate_option
Improve validation option in Panorama
- Loading branch information
Showing
3 changed files
with
171 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package commit | ||
|
||
import ( | ||
"encoding/xml" | ||
"github.com/PaloAltoNetworks/pango/util" | ||
) | ||
|
||
// PanoramaValidate is a Panorama Valid structure contained all option for Client.ValidateConfig(). | ||
type PanoramaValidate struct { | ||
Admins []string | ||
DeviceGroups []string | ||
LogCollectors []string | ||
LogCollectorGroups []string | ||
Templates []string | ||
TemplateStacks []string | ||
WildfireAppliances []string | ||
WildfireApplianceClusters []string | ||
ExcludeDeviceAndNetwork bool | ||
ExcludeSharedObjects bool | ||
Full bool | ||
} | ||
|
||
// PanoramaValidateAll it is a shell struct for doing validate all changes in Panorama. | ||
type PanoramaValidateAll struct{} | ||
|
||
// Element this function return an interface ready to be marshalled into XML for validate. | ||
func (o PanoramaValidate) Element() interface{} { | ||
ans := panoValidate{} | ||
|
||
var p *panoValidatePartial | ||
if len(o.Admins) > 0 || len(o.DeviceGroups) > 0 || len(o.Templates) > 0 || | ||
len(o.TemplateStacks) > 0 || len(o.LogCollectors) > 0 || len(o.LogCollectorGroups) > 0 { | ||
p = &panoValidatePartial{ | ||
Admins: util.StrToMem(o.Admins), | ||
DeviceGroups: util.StrToMem(o.DeviceGroups), | ||
Templates: util.StrToMem(o.Templates), | ||
TemplateStacks: util.StrToMem(o.TemplateStacks), | ||
WildfireAppliances: util.StrToMem(o.WildfireAppliances), | ||
WildfireClusters: util.StrToMem(o.WildfireApplianceClusters), | ||
LogCollectors: util.StrToMem(o.LogCollectors), | ||
LogCollectorGroups: util.StrToMem(o.LogCollectorGroups), | ||
} | ||
|
||
if o.ExcludeSharedObjects { | ||
p.ExcludeSharedObjects = "excluded" | ||
} | ||
|
||
if o.ExcludeDeviceAndNetwork { | ||
p.ExcludeDeviceAndNetwork = "excluded" | ||
} | ||
} else { | ||
ans.Full = &panoValidateFull{} | ||
} | ||
|
||
ans.Partial = p | ||
|
||
return ans | ||
} | ||
|
||
// panoValidate main XML structure for validate, it contains main 'validate' option and regarded for the action | ||
// it can be either 'full' or 'partial' | ||
type panoValidate struct { | ||
XMLName xml.Name `xml:"validate"` | ||
Full *panoValidateFull `xml:"full"` | ||
Partial *panoValidatePartial `xml:"partial"` | ||
} | ||
|
||
type panoValidateFull struct{} | ||
|
||
type panoValidatePartial struct { | ||
Admins *util.MemberType `xml:"admin"` | ||
DeviceGroups *util.MemberType `xml:"device-group"` | ||
Templates *util.MemberType `xml:"template"` | ||
TemplateStacks *util.MemberType `xml:"template-stack"` | ||
WildfireAppliances *util.MemberType `xml:"wildfire-appliance"` | ||
WildfireClusters *util.MemberType `xml:"wildfire-appliance-cluster"` | ||
LogCollectors *util.MemberType `xml:"log-collector"` | ||
LogCollectorGroups *util.MemberType `xml:"log-collector-group"` | ||
ExcludeDeviceAndNetwork string `xml:"device-and-network,omitempty"` | ||
ExcludeSharedObjects string `xml:"shared-object,omitempty"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package commit | ||
|
||
import ( | ||
"encoding/xml" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestPanoramaValidate_ValidateElement(t *testing.T) { | ||
testS := []string{ | ||
"<validate>", | ||
"<partial>", | ||
"<admin><member>admin1</member><member>admin2</member></admin>", | ||
"<device-group><member>dg1</member></device-group>", | ||
"<template><member>tmpl1</member></template>", | ||
"<template-stack><member>ts1</member></template-stack>", | ||
"<wildfire-appliance><member>wfa1</member></wildfire-appliance>", | ||
"<wildfire-appliance-cluster><member>wfc1</member></wildfire-appliance-cluster>", | ||
"<log-collector><member>lc1</member></log-collector>", | ||
"<log-collector-group><member>lcg1</member></log-collector-group>", | ||
"<device-and-network>excluded</device-and-network>", | ||
"<shared-object>excluded</shared-object>", | ||
"</partial>", | ||
"</validate>", | ||
} | ||
|
||
expected := strings.Join(testS, "") | ||
|
||
validateStruct := PanoramaValidate{ | ||
Admins: []string{"admin1", "admin2"}, | ||
DeviceGroups: []string{"dg1"}, | ||
LogCollectors: []string{"lc1"}, | ||
LogCollectorGroups: []string{"lcg1"}, | ||
Templates: []string{"tmpl1"}, | ||
TemplateStacks: []string{"ts1"}, | ||
WildfireAppliances: []string{"wfa1"}, | ||
WildfireApplianceClusters: []string{"wfc1"}, | ||
ExcludeDeviceAndNetwork: true, | ||
ExcludeSharedObjects: true, | ||
} | ||
|
||
marshV, _ := xml.Marshal(validateStruct.Element()) | ||
if expected != string(marshV) { | ||
t.Errorf("Expected(%s) got(%s)", expected, marshV) | ||
} | ||
} | ||
|
||
func TestPanoramaValidateAll_ValidateAllElement(t *testing.T) { | ||
testS := []string{ | ||
"<validate>", | ||
"<full>", | ||
"</full>", | ||
"</validate>", | ||
} | ||
|
||
expected := strings.Join(testS, "") | ||
validateStruct := PanoramaValidate{} | ||
|
||
marshV, _ := xml.Marshal(validateStruct.Element()) | ||
if expected != string(marshV) { | ||
t.Errorf("Expected(%s) got(%s)", expected, marshV) | ||
} | ||
} |