/
pano.go
255 lines (205 loc) · 5.95 KB
/
pano.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package template
import (
"encoding/xml"
"fmt"
"github.com/PaloAltoNetworks/pango/util"
"github.com/PaloAltoNetworks/pango/version"
)
// Template is the client.Panorama.Template namespace.
type Template struct {
con util.XapiClient
}
// Initialize is invoked by client.Initialize().
func (c *Template) Initialize(con util.XapiClient) {
c.con = con
}
/*
SetDeviceVsys performs a SET to add specific vsys from a device to
template t.
If you want all vsys to be included, or the device is a virtual firewall, then
leave the vsys list empty.
The template can be either a string or an Entry object.
*/
func (c *Template) SetDeviceVsys(t interface{}, d string, vsys []string) error {
var name string
switch v := t.(type) {
case string:
name = v
case Entry:
name = v.Name
default:
return fmt.Errorf("Unknown type sent to set device vsys: %s", v)
}
c.con.LogAction("(set) device vsys in template: %s", name)
m := util.MapToVsysEnt(map[string][]string{d: vsys})
path := c.xpath([]string{name})
path = append(path, "devices")
_, err := c.con.Set(path, m.Entries[0], nil, nil)
return err
}
/*
EditDeviceVsys performs an EDIT to add specific vsys from a device to
template t.
If you want all vsys to be included, or the device is a virtual firewall, then
leave the vsys list empty.
The template can be either a string or an Entry object.
*/
func (c *Template) EditDeviceVsys(t interface{}, d string, vsys []string) error {
var name string
switch v := t.(type) {
case string:
name = v
case Entry:
name = v.Name
default:
return fmt.Errorf("Unknown type sent to edit device vsys: %s", v)
}
c.con.LogAction("(edit) device vsys in template: %s", name)
m := util.MapToVsysEnt(map[string][]string{d: vsys})
path := c.xpath([]string{name})
path = append(path, "devices", util.AsEntryXpath([]string{d}))
_, err := c.con.Edit(path, m.Entries[0], nil, nil)
return err
}
/*
DeleteDeviceVsys performs a DELETE to remove specific vsys from device d from
template t.
If you want all vsys to be removed, or the device is a virtual firewall, then
leave the vsys list empty.
The template can be either a string or an Entry object.
*/
func (c *Template) DeleteDeviceVsys(t interface{}, d string, vsys []string) error {
var name string
switch v := t.(type) {
case string:
name = v
case Entry:
name = v.Name
default:
return fmt.Errorf("Unknown type sent to delete device vsys: %s", v)
}
c.con.LogAction("(delete) device vsys from template: %s", name)
path := make([]string, 0, 9)
path = append(path, c.xpath([]string{name})...)
path = append(path, "devices", util.AsEntryXpath([]string{d}))
if len(vsys) > 0 {
path = append(path, "vsys", util.AsEntryXpath(vsys))
}
_, err := c.con.Delete(path, nil, nil)
return err
}
// ShowList performs SHOW to retrieve a list of templates.
func (c *Template) ShowList() ([]string, error) {
c.con.LogQuery("(show) list of templates")
path := c.xpath(nil)
return c.con.EntryListUsing(c.con.Show, path[:len(path)-1])
}
// GetList performs GET to retrieve a list of templates.
func (c *Template) GetList() ([]string, error) {
c.con.LogQuery("(get) list of templates")
path := c.xpath(nil)
return c.con.EntryListUsing(c.con.Get, path[:len(path)-1])
}
// Get performs GET to retrieve information for the given template.
func (c *Template) Get(name string) (Entry, error) {
c.con.LogQuery("(get) template %q", name)
return c.details(c.con.Get, name)
}
// Show performs SHOW to retrieve information for the given template.
func (c *Template) Show(name string) (Entry, error) {
c.con.LogQuery("(show) template %q", name)
return c.details(c.con.Show, name)
}
// Set performs SET to create / update one or more templates.
func (c *Template) Set(e ...Entry) error {
var err error
if len(e) == 0 {
return nil
}
_, fn := c.versioning()
names := make([]string, len(e))
// Build up the struct with the given configs.
d := util.BulkElement{XMLName: xml.Name{Local: "template"}}
for i := range e {
e[i].SetConfTree()
d.Data = append(d.Data, fn(e[i]))
names[i] = e[i].Name
}
c.con.LogAction("(set) templates: %v", names)
// Set xpath.
path := c.xpath(names)
if len(e) == 1 {
path = path[:len(path)-1]
} else {
path = path[:len(path)-2]
}
// Create the templates.
_, err = c.con.Set(path, d.Config(), nil, nil)
return err
}
// Edit performs EDIT to create / update a template.
func (c *Template) Edit(e Entry) error {
var err error
_, fn := c.versioning()
c.con.LogAction("(edit) template %q", e.Name)
e.SetConfTree()
// Set xpath.
path := c.xpath([]string{e.Name})
// Edit the template.
_, err = c.con.Edit(path, fn(e), nil, nil)
return err
}
// Delete removes the given templates from the firewall.
//
// Templates can be a string or an Entry object.
func (c *Template) Delete(e ...interface{}) error {
var err error
if len(e) == 0 {
return nil
}
names := make([]string, len(e))
for i := range e {
switch v := e[i].(type) {
case string:
names[i] = v
case Entry:
names[i] = v.Name
default:
return fmt.Errorf("Unknown type sent to delete: %s", v)
}
}
c.con.LogAction("(delete) templates: %v", names)
// Remove the templates.
path := c.xpath(names)
_, err = c.con.Delete(path, nil, nil)
return err
}
/** Internal functions for this namespace struct **/
func (c *Template) versioning() (normalizer, func(Entry) interface{}) {
v := c.con.Versioning()
if v.Gte(version.Number{8, 1, 0, ""}) {
return &container_v3{}, specify_v3
} else if v.Gte(version.Number{7, 0, 0, ""}) {
return &container_v2{}, specify_v2
} else {
return &container_v1{}, specify_v1
}
}
func (c *Template) details(fn util.Retriever, name string) (Entry, error) {
path := c.xpath([]string{name})
obj, _ := c.versioning()
if _, err := fn(path, nil, obj); err != nil {
return Entry{}, err
}
ans := obj.Normalize()
return ans, nil
}
func (c *Template) xpath(vals []string) []string {
return []string{
"config",
"devices",
util.AsEntryXpath([]string{"localhost.localdomain"}),
"template",
util.AsEntryXpath(vals),
}
}