-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathcluster_sdn_prefix_lists.go
More file actions
161 lines (148 loc) · 4.91 KB
/
Copy pathcluster_sdn_prefix_lists.go
File metadata and controls
161 lines (148 loc) · 4.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
package proxmox
import (
"context"
"errors"
"fmt"
"net/url"
"strconv"
)
// SDNPrefixLists lists configured prefix-lists. pending/running toggle which
// configuration is returned; verbose=false returns just IDs.
//
// GET /cluster/sdn/prefix-lists
func (cl *Cluster) SDNPrefixLists(ctx context.Context, pending, running, verbose bool) (lists []*SDNPrefixList, err error) {
path := "/cluster/sdn/prefix-lists"
q := url.Values{}
if pending {
q.Set("pending", "1")
}
if running {
q.Set("running", "1")
}
if verbose {
q.Set("verbose", "1")
}
if len(q) > 0 {
path = path + "?" + q.Encode()
}
if err = cl.client.Get(ctx, path, &lists); err != nil {
return nil, err
}
for _, l := range lists {
l.client = cl.client
}
return
}
// SDNPrefixList returns a handle for a single prefix-list. No API call is made.
//
// GET /cluster/sdn/prefix-lists/{id}
func (cl *Cluster) SDNPrefixList(id string) *SDNPrefixList {
return &SDNPrefixList{client: cl.client, ID: id}
}
// NewSDNPrefixList creates a new prefix-list. opts.ID is required.
//
// POST /cluster/sdn/prefix-lists
func (cl *Cluster) NewSDNPrefixList(ctx context.Context, opts *SDNPrefixListOptions) error {
if opts == nil || opts.ID == "" {
return errors.New("sdn prefix-list id is required")
}
return cl.client.Post(ctx, "/cluster/sdn/prefix-lists", opts, nil)
}
// Read populates the receiver with the prefix-list configuration including
// entries.
//
// GET /cluster/sdn/prefix-lists/{id}
func (l *SDNPrefixList) Read(ctx context.Context) error {
if l.ID == "" {
return errors.New("sdn prefix-list id is required")
}
return l.client.Get(ctx, fmt.Sprintf("/cluster/sdn/prefix-lists/%s", l.ID), l)
}
// Update mutates the prefix-list. Pass a fresh Entries slice to replace the
// current set.
//
// PUT /cluster/sdn/prefix-lists/{id}
func (l *SDNPrefixList) Update(ctx context.Context, opts *SDNPrefixListOptions) error {
if l.ID == "" {
return errors.New("sdn prefix-list id is required")
}
if opts == nil {
opts = &SDNPrefixListOptions{}
}
return l.client.Put(ctx, fmt.Sprintf("/cluster/sdn/prefix-lists/%s", l.ID), opts, nil)
}
// Delete removes the prefix-list.
//
// DELETE /cluster/sdn/prefix-lists/{id}
func (l *SDNPrefixList) Delete(ctx context.Context) error {
if l.ID == "" {
return errors.New("sdn prefix-list id is required")
}
return l.client.Delete(ctx, fmt.Sprintf("/cluster/sdn/prefix-lists/%s", l.ID), nil)
}
// ListEntries lists the entries within this prefix-list.
//
// GET /cluster/sdn/prefix-lists/{id}/entries
func (l *SDNPrefixList) ListEntries(ctx context.Context) (entries []*SDNPrefixListEntry, err error) {
if l.ID == "" {
return nil, errors.New("sdn prefix-list id is required")
}
if err = l.client.Get(ctx, fmt.Sprintf("/cluster/sdn/prefix-lists/%s/entries", l.ID), &entries); err != nil {
return nil, err
}
for _, e := range entries {
e.client = l.client
e.ID = l.ID
}
return
}
// Entry returns a handle for a single entry in this prefix-list keyed by seq.
// No API call is made.
//
// GET /cluster/sdn/prefix-lists/{id}/entries/{url_seq}
func (l *SDNPrefixList) Entry(seq uint32) *SDNPrefixListEntry {
return &SDNPrefixListEntry{client: l.client, ID: l.ID, Seq: seq}
}
// AddEntry creates a new entry in this prefix-list. opts.Action and opts.Prefix
// are required.
//
// POST /cluster/sdn/prefix-lists/{id}/entries
func (l *SDNPrefixList) AddEntry(ctx context.Context, opts *SDNPrefixListEntryOptions) error {
if l.ID == "" {
return errors.New("sdn prefix-list id is required")
}
if opts == nil || opts.Action == "" || opts.Prefix == "" {
return errors.New("sdn prefix-list entry action and prefix are required")
}
return l.client.Post(ctx, fmt.Sprintf("/cluster/sdn/prefix-lists/%s/entries", l.ID), opts, nil)
}
// Read populates the receiver with the entry configuration.
//
// GET /cluster/sdn/prefix-lists/{id}/entries/{url_seq}
func (e *SDNPrefixListEntry) Read(ctx context.Context) error {
if e.ID == "" {
return errors.New("sdn prefix-list id is required")
}
return e.client.Get(ctx, fmt.Sprintf("/cluster/sdn/prefix-lists/%s/entries/%s", e.ID, strconv.FormatUint(uint64(e.Seq), 10)), e)
}
// Update mutates the prefix-list entry.
//
// PUT /cluster/sdn/prefix-lists/{id}/entries/{url_seq}
func (e *SDNPrefixListEntry) Update(ctx context.Context, opts *SDNPrefixListEntryOptions) error {
if e.ID == "" {
return errors.New("sdn prefix-list id is required")
}
if opts == nil {
opts = &SDNPrefixListEntryOptions{}
}
return e.client.Put(ctx, fmt.Sprintf("/cluster/sdn/prefix-lists/%s/entries/%s", e.ID, strconv.FormatUint(uint64(e.Seq), 10)), opts, nil)
}
// Delete removes the prefix-list entry.
//
// DELETE /cluster/sdn/prefix-lists/{id}/entries/{url_seq}
func (e *SDNPrefixListEntry) Delete(ctx context.Context) error {
if e.ID == "" {
return errors.New("sdn prefix-list id is required")
}
return e.client.Delete(ctx, fmt.Sprintf("/cluster/sdn/prefix-lists/%s/entries/%s", e.ID, strconv.FormatUint(uint64(e.Seq), 10)), nil)
}