/
vlanrange.go
85 lines (54 loc) · 2.16 KB
/
vlanrange.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
package aci
import (
"bytes"
"fmt"
)
func nameVR(from, to string) string {
return "from-[vlan-" + from + "]-to-[vlan-" + to + "]"
}
// VlanRangeAdd creates a new VLAN range for a VLAN pool.
func (c *Client) VlanRangeAdd(vlanpoolName, vlanpoolMode, from, to string) error {
pool := nameVP(vlanpoolName, vlanpoolMode)
rang := nameVR(from, to)
api := "/api/node/mo/uni/infra/" + pool + "/" + rang + ".json"
j := fmt.Sprintf(`{"fvnsEncapBlk":{"attributes":{"dn":"uni/infra/%s/%s","from":"vlan-%s","to":"vlan-%s","rn":"%s","status":"created"}}}`,
pool, rang, from, to, rang)
url := c.getURL(api)
c.debugf("VlanRangeAdd: url=%s json=%s", url, j)
body, errPost := c.post(url, contentTypeJSON, bytes.NewBufferString(j))
if errPost != nil {
return errPost
}
c.debugf("VlanRangeAdd: reply: %s", string(body))
return parseJSONError(body)
}
// VlanRangeDel deletes an existing VLAN range from a VLAN pool.
func (c *Client) VlanRangeDel(vlanpoolName, vlanpoolMode, from, to string) error {
pool := nameVP(vlanpoolName, vlanpoolMode)
rang := nameVR(from, to)
api := "/api/node/mo/uni/infra/" + pool + ".json"
j := fmt.Sprintf(`{"fvnsVlanInstP":{"attributes":{"dn":"uni/infra/%s","status":"modified"},"children":[{"fvnsEncapBlk":{"attributes":{"dn":"uni/infra/%s/%s","status":"deleted"}}}]}}`,
pool, pool, rang)
url := c.getURL(api)
c.debugf("VlanRangeAdd: url=%s json=%s", url, j)
body, errPost := c.post(url, contentTypeJSON, bytes.NewBufferString(j))
if errPost != nil {
return errPost
}
c.debugf("VlanRangeDel: reply: %s", string(body))
return parseJSONError(body)
}
// VlanRangeList retrieves the list of VLAN ranges from a VLAN pool.
func (c *Client) VlanRangeList(vlanpoolName, vlanpoolMode string) ([]map[string]interface{}, error) {
pool := nameVP(vlanpoolName, vlanpoolMode)
key := "fvnsEncapBlk"
api := "/api/node/mo/uni/infra/" + pool + ".json?query-target=children&target-subtree-class=" + key
url := c.getURL(api)
c.debugf("VlanRangeList: url=%s", url)
body, errGet := c.get(url)
if errGet != nil {
return nil, errGet
}
c.debugf("VlanRangeList: reply: %s", string(body))
return jsonImdataAttributes(c, body, key, "VlanRangeList")
}