forked from PaloAltoNetworks/pango
/
pano.go
243 lines (201 loc) · 6.58 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
package imp
import (
"fmt"
"encoding/xml"
"strings"
"github.com/inwinstack/pango/util"
"github.com/inwinstack/pango/version"
)
// PanoImp is the client.Network.BgpImport namespace.
type PanoImp struct {
con util.XapiClient
}
// Initialize is invoked by client.Initialize().
func (c *PanoImp) Initialize(con util.XapiClient) {
c.con = con
}
// ShowList performs SHOW to retrieve a list of values.
func (c *PanoImp) ShowList(tmpl, ts, vr string) ([]string, error) {
c.con.LogQuery("(show) list of %s", plural)
path := c.xpath(tmpl, ts, vr, nil)
return c.con.EntryListUsing(c.con.Show, path[:len(path) - 1])
}
// GetList performs GET to retrieve a list of values.
func (c *PanoImp) GetList(tmpl, ts, vr string) ([]string, error) {
c.con.LogQuery("(get) list of %s", plural)
path := c.xpath(tmpl, ts, vr, nil)
return c.con.EntryListUsing(c.con.Get, path[:len(path) - 1])
}
// Get performs GET to retrieve information for the given uid.
func (c *PanoImp) Get(tmpl, ts, vr, name string) (Entry, error) {
c.con.LogQuery("(get) %s %q", singular, name)
return c.details(c.con.Get, tmpl, ts, vr, name)
}
// Show performs SHOW to retrieve information for the given uid.
func (c *PanoImp) Show(tmpl, ts, vr, name string) (Entry, error) {
c.con.LogQuery("(show) %s %q", singular, name)
return c.details(c.con.Show, tmpl, ts, vr, name)
}
// Set performs SET to create / update one or more objects.
func (c *PanoImp) Set(tmpl, ts, vr string, e ...Entry) error {
var err error
if len(e) == 0 {
return nil
} else if tmpl == "" && ts == "" {
return fmt.Errorf("tmpl or ts must be specified")
} else if vr == "" {
return fmt.Errorf("vr must be specified")
} else {
// Make sure rule names are unique.
m := make(map[string] int)
for i := range e {
m[e[i].Name] = m[e[i].Name] + 1
if m[e[i].Name] > 1 {
return fmt.Errorf("%s is defined multiple times: %s", singular, e[i].Name)
}
}
}
_, fn := c.versioning()
names := make([]string, len(e))
// Build up the struct.
d := util.BulkElement{XMLName: xml.Name{Local: "rules"}}
for i := range e {
d.Data = append(d.Data, fn(e[i]))
names[i] = e[i].Name
}
c.con.LogAction("(set) %s: %v", plural, names)
// Set xpath.
path := c.xpath(tmpl, ts, vr, names)
if len(e) == 1 {
path = path[:len(path) - 1]
} else {
path = path[:len(path) - 2]
}
// Create the objects.
_, err = c.con.Set(path, d.Config(), nil, nil)
// On error: find the rule that's causing the error if multiple rules
// were given.
if err != nil && strings.Contains(err.Error(), "rules is invalid") {
for i := 0; i < len(e); i++ {
if e2 := c.Set(tmpl, ts, vr, e[i]); e2 != nil {
return fmt.Errorf("Error with rule %d: %s", i + 1, e2)
} else {
_ = c.Delete(tmpl, ts, vr, e[i])
}
}
// Couldn't find it, just return the original error.
return err
}
return err
}
// Edit performs EDIT to create / update one object.
func (c *PanoImp) Edit(tmpl, ts, vr string, e Entry) error {
var err error
if tmpl == "" && ts == "" {
return fmt.Errorf("tmpl or ts must be specified")
} else if vr == "" {
return fmt.Errorf("vr must be specified")
}
_, fn := c.versioning()
c.con.LogAction("(edit) %s %q", singular, e.Name)
// Set xpath.
path := c.xpath(tmpl, ts, vr, []string{e.Name})
// Edit the object.
_, err = c.con.Edit(path, fn(e), nil, nil)
return err
}
// Delete removes the given objects.
//
// Objects can be a string or an Entry object.
func (c *PanoImp) Delete(tmpl, ts, vr string, e ...interface{}) error {
var err error
if len(e) == 0 {
return nil
} else if tmpl == "" && ts == "" {
return fmt.Errorf("tmpl or ts must be specified")
} else if vr == "" {
return fmt.Errorf("vr must be specified")
}
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) %s: %v", plural, names)
// Remove the objects.
path := c.xpath(tmpl, ts, vr, names)
_, err = c.con.Delete(path, nil, nil)
return err
}
// MoveGroup moves a logical group of BGP import rules somewhere in relation
// to another rule.
func (c *PanoImp) MoveGroup(tmpl, ts, vr string, mvt int, rule string, e ...Entry) error {
var err error
c.con.LogAction("(move) %s group", singular)
if tmpl == "" && ts == "" {
return fmt.Errorf("tmpl or ts must be specified")
} else if len(e) < 1 {
return fmt.Errorf("Requires at least one rule")
}
path := c.xpath(tmpl, ts, vr, []string{e[0].Name})
list, err := c.GetList(tmpl, ts, vr)
if err != nil {
return err
}
// Set the first entity's position.
if err = c.con.PositionFirstEntity(mvt, rule, e[0].Name, path, list); err != nil {
return err
}
// Move all the rest under it.
li := len(path) - 1
for i := 1; i < len(e); i++ {
path[li] = util.AsEntryXpath([]string{e[i].Name})
if _, err = c.con.Move(path, "after", e[i - 1].Name, nil, nil); err != nil {
return err
}
}
return nil
}
/** Internal functions for this namespace struct **/
func (c *PanoImp) versioning() (normalizer, func(Entry) (interface{})) {
v := c.con.Versioning()
if v.Gte(version.Number{8, 0, 0, ""}) {
return &container_v2{}, specify_v2
} else {
return &container_v1{}, specify_v1
}
}
func (c *PanoImp) details(fn util.Retriever, tmpl, ts, vr, name string) (Entry, error) {
path := c.xpath(tmpl, ts, vr, []string{name})
obj, _ := c.versioning()
if _, err := fn(path, nil, obj); err != nil {
return Entry{}, err
}
ans := obj.Normalize()
return ans, nil
}
func (c *PanoImp) xpath(tmpl, ts, vr string, vals []string) []string {
ans := make([]string, 0, 17)
ans = append(ans, util.TemplateXpathPrefix(tmpl, ts)...)
ans = append(ans,
"config",
"devices",
util.AsEntryXpath([]string{"localhost.localdomain"}),
"network",
"virtual-router",
util.AsEntryXpath([]string{vr}),
"protocol",
"bgp",
"policy",
"import",
"rules",
util.AsEntryXpath(vals),
)
return ans
}