forked from PaloAltoNetworks/pango
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpano.go
More file actions
212 lines (178 loc) · 5.56 KB
/
pano.go
File metadata and controls
212 lines (178 loc) · 5.56 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package pango
import (
"encoding/xml"
"github.com/inwinstack/pango/util"
// Various namespace imports.
"github.com/inwinstack/pango/dev"
"github.com/inwinstack/pango/objs"
"github.com/inwinstack/pango/poli"
"github.com/inwinstack/pango/netw"
"github.com/inwinstack/pango/pnrm"
"github.com/inwinstack/pango/licen"
"github.com/inwinstack/pango/userid"
)
// Panorama is a panorama specific client, providing version safe functions
// for the PAN-OS Xpath API methods. After creating the object, invoke
// Initialize() to prepare it for use.
//
// It has the following namespaces:
// * Licensing
// * UserId
type Panorama struct {
Client
// Namespaces
Device *dev.PanoDev
Licensing *licen.Licen
UserId *userid.UserId
Panorama *pnrm.Pnrm
Objects *objs.PanoObjs
Policies *poli.PanoPoli
Network *netw.PanoNetw
}
// Initialize does some initial setup of the Panorama connection, retrieves
// the API key if it was not already present, then performs "show system
// info" to get the PAN-OS version. The full results are saved into the
// client's SystemInfo map.
//
// If not specified, the following is assumed:
// * Protocol: https
// * Port: (unspecified)
// * Timeout: 10
// * Logging: LogAction | LogUid
func (c *Panorama) Initialize() error {
if len(c.rb) == 0 {
var e error
if e = c.initCon(); e != nil {
return e
} else if e = c.initApiKey(); e != nil {
return e
} else if e = c.initSystemInfo(); e != nil {
return e
}
c.initPlugins()
} else {
c.Hostname = "localhost"
c.ApiKey = "password"
}
c.initNamespaces()
return nil
}
// CommitAll performs a Panorama commit-all.
//
// Param dg is the device group you want to commit-all on. Note that all other
// params are ignored / unused if the device group is left empty.
//
// Param desc is the optional commit description message you want associated
// with the commit.
//
// Param serials is the list of serial numbers you want to limit the commit-all
// to that are also in the device group dg.
//
// Param tmpl should be true if you want to push template config as well.
//
// Param sync should be true if you want this function to block until the
// commit job completes.
//
// Commits result in a job being submitted to the backend. The job ID and
// if an error was encountered or not are returned from this function.
func (c *Panorama) CommitAll(dg, desc string, serials []string, tmpl, sync bool) (uint, error) {
c.LogAction("(commit-all) %q", desc)
req := panoDgCommit{}
if dg != "" {
sp := sharedPolicy{
Description: desc,
WithTemplate: util.YesNo(tmpl),
Dg: deviceGroup{
Entry: deviceGroupEntry{
Name: dg,
Devices: util.StrToEnt(serials),
},
},
}
req.Policy = &sp
}
job, _, err := c.CommitConfig(req, "all", nil)
if err != nil || !sync || job == 0 {
return job, err
}
return job, c.WaitForJob(job, nil)
}
/** Private functions **/
func (c *Panorama) initNamespaces() {
c.Device = &dev.PanoDev{}
c.Device.Initialize(c)
c.Licensing = &licen.Licen{}
c.Licensing.Initialize(c)
c.UserId = &userid.UserId{}
c.UserId.Initialize(c)
c.Panorama = &pnrm.Pnrm{}
c.Panorama.Initialize(c)
c.Objects = &objs.PanoObjs{}
c.Objects.Initialize(c)
c.Policies = &poli.PanoPoli{}
c.Policies.Initialize(c)
c.Network = &netw.PanoNetw{}
c.Network.Initialize(c)
}
func (c *Panorama) initPlugins() {
c.LogOp("(op) getting plugin info")
type plugin_req struct {
XMLName xml.Name `xml:"show"`
Cmd string `xml:"plugins>packages"`
}
type relNote struct {
ReleaseNoteUrl string `xml:",cdata"`
}
type pkgInfo struct {
Name string `xml:"name"`
Version string `xml:"version"`
ReleaseDate string `xml:"release-date"`
RelNote relNote `xml:"release-note-url"`
PackageFile string `xml:"pkg-file"`
Size string `xml:"size"`
Platform string `xml:"platform"`
Installed string `xml:"installed"`
Downloaded string `xml:"downloaded"`
}
type pluginResp struct {
Answer []pkgInfo `xml:"result>plugins>entry"`
}
req := plugin_req{}
ans := pluginResp{}
_, err := c.Op(req, "", nil, &ans)
if err != nil {
c.LogAction("WARNING: Failed to get plugin info: %s", err)
return
}
c.Plugin = make([]map[string] string, 0, len(ans.Answer))
for _, data := range ans.Answer {
c.Plugin = append(c.Plugin, map[string] string{
"name": data.Name,
"version": data.Version,
"release-date": data.ReleaseDate,
"release-note-url": data.RelNote.ReleaseNoteUrl,
"package-file": data.PackageFile,
"size": data.Size,
"platform": data.Platform,
"installed": data.Installed,
"downloaded": data.Downloaded,
})
}
}
/** Internal structs / functions **/
type panoDgCommit struct {
XMLName xml.Name `xml:"commit-all"`
Policy *sharedPolicy `xml:"shared-policy"`
}
type sharedPolicy struct {
Dg deviceGroup `xml:"device-group"`
Description string `xml:"description,omitempty"`
WithTemplate string `xml:"include-template"`
}
type deviceGroup struct {
Entry deviceGroupEntry `xml:"entry"`
}
type deviceGroupEntry struct {
Name string `xml:"name,attr"`
Devices *util.EntryType `xml:"devices"`
}