-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathcluster_sdn_fabrics.go
More file actions
198 lines (182 loc) · 5.91 KB
/
Copy pathcluster_sdn_fabrics.go
File metadata and controls
198 lines (182 loc) · 5.91 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
package proxmox
import (
"context"
"errors"
"fmt"
"net/url"
)
// SDNFabricsIndex returns the directory entries under /cluster/sdn/fabrics
// (currently {"fabric", "node", "all"} subdirs).
//
// GET /cluster/sdn/fabrics
func (cl *Cluster) SDNFabricsIndex(ctx context.Context) (entries []map[string]any, err error) {
err = cl.client.Get(ctx, "/cluster/sdn/fabrics", &entries)
return
}
// SDNFabricsAll returns the combined view of all fabrics and their member
// nodes — useful for rendering the whole SDN underlay in one call.
//
// GET /cluster/sdn/fabrics/all
func (cl *Cluster) SDNFabricsAll(ctx context.Context) (all *SDNFabricsAll, err error) {
all = &SDNFabricsAll{}
err = cl.client.Get(ctx, "/cluster/sdn/fabrics/all", all)
if err != nil {
return nil, err
}
for _, f := range all.Fabrics {
f.client = cl.client
}
for _, n := range all.Nodes {
n.client = cl.client
}
return
}
// SDNFabrics lists configured fabrics. pending/running toggle the
// returned configuration (PVE distinguishes pending changes from running config).
//
// GET /cluster/sdn/fabrics/fabric
func (cl *Cluster) SDNFabrics(ctx context.Context, pending, running bool) (fabrics []*SDNFabric, err error) {
path := "/cluster/sdn/fabrics/fabric"
q := url.Values{}
if pending {
q.Set("pending", "1")
}
if running {
q.Set("running", "1")
}
if len(q) > 0 {
path = path + "?" + q.Encode()
}
if err = cl.client.Get(ctx, path, &fabrics); err != nil {
return nil, err
}
for _, f := range fabrics {
f.client = cl.client
}
return
}
// SDNFabric returns a handle for a single fabric. No API call is made.
//
// GET /cluster/sdn/fabrics/fabric/{id}
func (cl *Cluster) SDNFabric(id string) *SDNFabric {
return &SDNFabric{client: cl.client, ID: id}
}
// NewSDNFabric creates a new fabric. opts.ID and opts.Protocol are required.
//
// POST /cluster/sdn/fabrics/fabric
func (cl *Cluster) NewSDNFabric(ctx context.Context, opts *SDNFabricOptions) error {
if opts == nil || opts.ID == "" {
return errors.New("sdn fabric id is required")
}
if opts.Protocol == "" {
return errors.New("sdn fabric protocol is required")
}
return cl.client.Post(ctx, "/cluster/sdn/fabrics/fabric", opts, nil)
}
// Read populates the receiver with the current fabric configuration.
//
// GET /cluster/sdn/fabrics/fabric/{id}
func (f *SDNFabric) Read(ctx context.Context) error {
if f.ID == "" {
return errors.New("sdn fabric id is required")
}
return f.client.Get(ctx, fmt.Sprintf("/cluster/sdn/fabrics/fabric/%s", f.ID), f)
}
// Update mutates a fabric configuration.
//
// PUT /cluster/sdn/fabrics/fabric/{id}
func (f *SDNFabric) Update(ctx context.Context, opts *SDNFabricOptions) error {
if f.ID == "" {
return errors.New("sdn fabric id is required")
}
if opts == nil {
opts = &SDNFabricOptions{}
}
return f.client.Put(ctx, fmt.Sprintf("/cluster/sdn/fabrics/fabric/%s", f.ID), opts, nil)
}
// Delete removes the fabric.
//
// DELETE /cluster/sdn/fabrics/fabric/{id}
func (f *SDNFabric) Delete(ctx context.Context) error {
if f.ID == "" {
return errors.New("sdn fabric id is required")
}
return f.client.Delete(ctx, fmt.Sprintf("/cluster/sdn/fabrics/fabric/%s", f.ID), nil)
}
// Nodes lists nodes participating in this fabric.
//
// GET /cluster/sdn/fabrics/node/{fabric_id}
func (f *SDNFabric) Nodes(ctx context.Context) (nodes []*SDNFabricNode, err error) {
if f.ID == "" {
return nil, errors.New("sdn fabric id is required")
}
if err = f.client.Get(ctx, fmt.Sprintf("/cluster/sdn/fabrics/node/%s", f.ID), &nodes); err != nil {
return nil, err
}
for _, n := range nodes {
n.client = f.client
n.FabricID = f.ID
}
return
}
// Node returns a handle for a single node in this fabric. No API call is made.
//
// GET /cluster/sdn/fabrics/node/{fabric_id}/{node_id}
func (f *SDNFabric) Node(nodeID string) *SDNFabricNode {
return &SDNFabricNode{client: f.client, FabricID: f.ID, NodeID: nodeID}
}
// AddNode adds a node to this fabric. opts.NodeID is required.
//
// POST /cluster/sdn/fabrics/node/{fabric_id}
func (f *SDNFabric) AddNode(ctx context.Context, opts *SDNFabricNodeOptions) error {
if f.ID == "" {
return errors.New("sdn fabric id is required")
}
if opts == nil || opts.NodeID == "" {
return errors.New("sdn fabric node id is required")
}
return f.client.Post(ctx, fmt.Sprintf("/cluster/sdn/fabrics/node/%s", f.ID), opts, nil)
}
// SDNFabricNodes lists all SDN fabric/node pairs across every fabric — the
// flat alternative to per-fabric iteration via SDNFabric.Nodes.
//
// GET /cluster/sdn/fabrics/node
func (cl *Cluster) SDNFabricNodes(ctx context.Context) (nodes []*SDNFabricNode, err error) {
if err = cl.client.Get(ctx, "/cluster/sdn/fabrics/node", &nodes); err != nil {
return nil, err
}
for _, n := range nodes {
n.client = cl.client
}
return
}
// Read populates the receiver with the current fabric-node configuration.
//
// GET /cluster/sdn/fabrics/node/{fabric_id}/{node_id}
func (n *SDNFabricNode) Read(ctx context.Context) error {
if n.FabricID == "" || n.NodeID == "" {
return errors.New("sdn fabric and node id are required")
}
return n.client.Get(ctx, fmt.Sprintf("/cluster/sdn/fabrics/node/%s/%s", n.FabricID, n.NodeID), n)
}
// Update mutates a fabric-node configuration.
//
// PUT /cluster/sdn/fabrics/node/{fabric_id}/{node_id}
func (n *SDNFabricNode) Update(ctx context.Context, opts *SDNFabricNodeOptions) error {
if n.FabricID == "" || n.NodeID == "" {
return errors.New("sdn fabric and node id are required")
}
if opts == nil {
opts = &SDNFabricNodeOptions{}
}
return n.client.Put(ctx, fmt.Sprintf("/cluster/sdn/fabrics/node/%s/%s", n.FabricID, n.NodeID), opts, nil)
}
// Delete removes the node from the fabric.
//
// DELETE /cluster/sdn/fabrics/node/{fabric_id}/{node_id}
func (n *SDNFabricNode) Delete(ctx context.Context) error {
if n.FabricID == "" || n.NodeID == "" {
return errors.New("sdn fabric and node id are required")
}
return n.client.Delete(ctx, fmt.Sprintf("/cluster/sdn/fabrics/node/%s/%s", n.FabricID, n.NodeID), nil)
}