forked from PaloAltoNetworks/pango
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiconf.go
98 lines (83 loc) · 2.38 KB
/
multiconf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package pango
import (
"encoding/xml"
"fmt"
"strings"
"github.com/PaloAltoNetworks/pango/util"
)
// MultiConfigure is a container object for making a type=multi-config call.
type MultiConfigure struct {
XMLName xml.Name `xml:"multi-configure-request"`
Reqs []MultiConfigureRequest
}
// IncrementalIds assigns incremental ID numbers to all requests.
//
// Any request that already has an ID is skipped, and the number is discarded.
func (m *MultiConfigure) IncrementalIds() {
for i := range m.Reqs {
if m.Reqs[i].Id == "" {
m.Reqs[i].Id = fmt.Sprintf("%d", i+1)
}
}
}
// MultiConfigureRequest is an individual request in a MultiConfigure instance.
//
// These are built up automatically when invoking Client.Set / Client.Edit after
// Client.PrepareMultiConfigure is invoked.
type MultiConfigureRequest struct {
XMLName xml.Name
Id string `xml:"id,attr,omitempty"`
Xpath string `xml:"xpath,attr"`
Data interface{}
}
// MultiConfigureResponse is a struct to handle the response from multi-config
// commands.
type MultiConfigureResponse struct {
XMLName xml.Name `xml:"response"`
Status string `xml:"status,attr"`
Code int `xml:"code,attr"`
Results []MultiConfigResponseElement `xml:"response"`
}
// Ok returns if there was an error or not.
func (m *MultiConfigureResponse) Ok() bool {
return m.Status == "success"
}
// Error returns the error if there was one.
func (m *MultiConfigureResponse) Error() string {
if len(m.Results) == 0 {
return ""
}
r := m.Results[len(m.Results)-1]
if r.Ok() {
return ""
}
return r.Message()
}
// MultiConfigResponseElement is a single response from a multi-config request.
type MultiConfigResponseElement struct {
XMLName xml.Name `xml:"response"`
Status string `xml:"status,attr"`
Code int `xml:"code,attr"`
Id string `xml:"id,attr,omitempty"`
Msg McreMsg `xml:"msg"`
}
type McreMsg struct {
Line []util.CdataText `xml:"line"`
Message string `xml:",chardata"`
}
func (m *MultiConfigResponseElement) Ok() bool {
return m.Status == "success"
}
func (m *MultiConfigResponseElement) Message() string {
if len(m.Msg.Line) > 0 {
var b strings.Builder
for i := range m.Msg.Line {
if i != 0 {
b.WriteString(" | ")
}
b.WriteString(strings.TrimSpace(m.Msg.Line[i].Text))
}
return b.String()
}
return m.Msg.Message
}