/
node.go
89 lines (57 loc) · 1.89 KB
/
node.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
package aci
import (
"bytes"
"fmt"
)
func rnNode(serial string) string {
return "nodep-" + serial
}
func dnNode(serial string) string {
return "controller/nodeidentpol/" + rnNode(serial)
}
// NodeAdd creates a new fabric membership node.
func (c *Client) NodeAdd(name, ID, serial string) error {
me := "NodeAdd"
rn := rnNode(serial)
dn := dnNode(serial)
api := "/api/node/mo/uni/" + dn + ".json"
url := c.getURL(api)
j := fmt.Sprintf(`{"fabricNodeIdentP":{"attributes":{"dn":"uni/%s","serial":"%s","nodeId":"%s","name":"%s","rn":"%s","status":"created"},"children":[]}}`,
dn, serial, ID, name, rn)
c.debugf("%s: url=%s json=%s", me, url, j)
body, errPost := c.post(url, contentTypeJSON, bytes.NewBufferString(j))
if errPost != nil {
return fmt.Errorf("%s: %v", me, errPost)
}
c.debugf("%s: reply: %s", me, string(body))
return parseJSONError(body)
}
// NodeDel deletes an existing fabric membership node.
func (c *Client) NodeDel(serial string) error {
me := "NodeDel"
dn := dnNode(serial)
api := "/api/node/mo/uni/controller/nodeidentpol.json"
url := c.getURL(api)
j := fmt.Sprintf(`{"fabricNodeIdentP":{"attributes":{"dn":"uni/%s","status":"deleted"},"children":[]}}`,
dn)
c.debugf("%s: url=%s json=%s", me, url, j)
body, errPost := c.post(url, contentTypeJSON, bytes.NewBufferString(j))
if errPost != nil {
return fmt.Errorf("%s: %v", me, errPost)
}
c.debugf("%s: reply: %s", me, string(body))
return parseJSONError(body)
}
// NodeList retrieves the list of top level system elements (APICs, spines, leaves).
func (c *Client) NodeList() ([]map[string]interface{}, error) {
key := "topSystem"
api := "/api/class/" + key + ".json"
url := c.getURL(api)
c.debugf("NodeList: url=%s", url)
body, errGet := c.get(url)
if errGet != nil {
return nil, errGet
}
c.debugf("NodeList: reply: %s", string(body))
return jsonImdataAttributes(c, body, key, "NodeList")
}