Debian Codegraph @

fastly/acl_entry.go

raw symbols SCIP
1 package fastly
2
3 import (
4 "fmt"
5 "sort"
6 "time"
7 )
8
9 type ACLEntry struct {
10 ServiceID string `mapstructure:"service_id"`
11 ACLID string `mapstructure:"acl_id"`
12
13 ID string `mapstructure:"id"`
14 IP string `mapstructure:"ip"`
15 Subnet string `mapstructure:"subnet"`
16 Negated bool `mapstructure:"negated"`
17 Comment string `mapstructure:"comment"`
18 CreatedAt *time.Time `mapstructure:"created_at"`
19 UpdatedAt *time.Time `mapstructure:"updated_at"`
20 DeletedAt *time.Time `mapstructure:"deleted_at"`
21 }
22
23 // entriesById is a sortable list of ACL entries.
24 type entriesById []*ACLEntry
25
26 // Len, Swap, and Less implements the sortable interface.
27 func (s entriesById) Len() int { return len(s) }
28 func (s entriesById) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
29 func (s entriesById) Less(i, j int) bool {
30 return s[i].ID < s[j].ID
31 }
32
33 // ListACLEntriesInput is the input parameter to ListACLEntries function.
34 type ListACLEntriesInput struct {
35 Service string
36 ACL string
37 }
38
39 // ListACLEntries return a list of entries for an ACL
40 func (c *Client) ListACLEntries(i *ListACLEntriesInput) ([]*ACLEntry, error) {
41 if i.Service == "" {
42 return nil, ErrMissingService
43 }
44
45 if i.ACL == "" {
46 return nil, ErrMissingACL
47 }
48
49 path := fmt.Sprintf("/service/%s/acl/%s/entries", i.Service, i.ACL)
50
51 resp, err := c.Get(path, nil)
52 if err != nil {
53 return nil, err
54 }
55
56 var es []*ACLEntry
57 if err := decodeJSON(&es, resp.Body); err != nil {
58 return nil, err
59 }
60
61 sort.Stable(entriesById(es))
62
63 return es, nil
64 }
65
66 // GetACLEntryInput is the input parameter to GetACLEntry function.
67 type GetACLEntryInput struct {
68 Service string
69 ACL string
70 ID string
71 }
72
73 // GetACLEntry returns a single ACL entry based on its ID.
74 func (c *Client) GetACLEntry(i *GetACLEntryInput) (*ACLEntry, error) {
75 if i.Service == "" {
76 return nil, ErrMissingService
77 }
78
79 if i.ACL == "" {
80 return nil, ErrMissingACL
81 }
82
83 if i.ID == "" {
84 return nil, ErrMissingID
85 }
86
87 path := fmt.Sprintf("/service/%s/acl/%s/entry/%s", i.Service, i.ACL, i.ID)
88
89 resp, err := c.Get(path, nil)
90 if err != nil {
91 return nil, err
92 }
93
94 var e *ACLEntry
95 if err := decodeJSON(&e, resp.Body); err != nil {
96 return nil, err
97 }
98
99 return e, nil
100 }
101
102 // CreateACLEntryInput the input parameter to CreateACLEntry function.
103 type CreateACLEntryInput struct {
104 // Required fields
105 Service string
106 ACL string
107 IP string `form:"ip"`
108
109 // Optional fields
110 Subnet string `form:"subnet,omitempty"`
111 Negated bool `form:"negated,omitempty"`
112 Comment string `form:"comment,omitempty"`
113 }
114
115 // CreateACLEntry creates and returns a new ACL entry.
116 func (c *Client) CreateACLEntry(i *CreateACLEntryInput) (*ACLEntry, error) {
117 if i.Service == "" {
118 return nil, ErrMissingService
119 }
120
121 if i.ACL == "" {
122 return nil, ErrMissingACL
123 }
124
125 if i.IP == "" {
126 return nil, ErrMissingIP
127 }
128
129 path := fmt.Sprintf("/service/%s/acl/%s/entry", i.Service, i.ACL)
130
131 resp, err := c.PostForm(path, i, nil)
132 if err != nil {
133 return nil, err
134 }
135
136 var e *ACLEntry
137 if err := decodeJSON(&e, resp.Body); err != nil {
138 return nil, err
139 }
140
141 return e, nil
142 }
143
144 // DeleteACLEntryInput the input parameter to DeleteACLEntry function.
145 type DeleteACLEntryInput struct {
146 Service string
147 ACL string
148 ID string
149 }
150
151 // DeleteACLEntry deletes an entry from an ACL based on its ID
152 func (c *Client) DeleteACLEntry(i *DeleteACLEntryInput) error {
153 if i.Service == "" {
154 return ErrMissingService
155 }
156
157 if i.ACL == "" {
158 return ErrMissingACL
159 }
160
161 if i.ID == "" {
162 return ErrMissingID
163 }
164
165 path := fmt.Sprintf("/service/%s/acl/%s/entry/%s", i.Service, i.ACL, i.ID)
166
167 resp, err := c.Delete(path, nil)
168 if err != nil {
169 return err
170 }
171
172 var r *statusResp
173 if err := decodeJSON(&r, resp.Body); err != nil {
174 return err
175 }
176
177 if !r.Ok() {
178 return fmt.Errorf("Not OK")
179 }
180
181 return nil
182
183 }
184
185 // UpdateACLEntryInput is the input parameter to UpdateACLEntry function.
186 type UpdateACLEntryInput struct {
187 // Required fields
188 Service string
189 ACL string
190 ID string
191
192 // Optional fields
193 IP string `form:"ip,omitempty"`
194 Subnet string `form:"subnet,omitempty"`
195 Negated bool `form:"negated,omitempty"`
196 Comment string `form:"comment,omitempty"`
197 }
198
199 // UpdateACLEntry updates an ACL entry
200 func (c *Client) UpdateACLEntry(i *UpdateACLEntryInput) (*ACLEntry, error) {
201 if i.Service == "" {
202 return nil, ErrMissingService
203 }
204
205 if i.ACL == "" {
206 return nil, ErrMissingACL
207 }
208
209 if i.ID == "" {
210 return nil, ErrMissingID
211 }
212
213 path := fmt.Sprintf("/service/%s/acl/%s/entry/%s", i.Service, i.ACL, i.ID)
214
215 resp, err := c.RequestForm("PATCH", path, i, nil)
216 if err != nil {
217 return nil, err
218 }
219
220 var e *ACLEntry
221 if err := decodeJSON(&e, resp.Body); err != nil {
222 return nil, err
223 }
224
225 return e, nil
226 }
227
228 type BatchModifyACLEntriesInput struct {
229 Service string `json:"-,"`
230 ACL string `json:"-,"`
231
232 Entries []*BatchACLEntry `json:"entries"`
233 }
234
235 type BatchACLEntry struct {
236 Operation BatchOperation `json:"op"`
237 ID string `json:"id,omitempty"`
238 IP string `json:"ip,omitempty"`
239 Subnet string `json:"subnet,omitempty"`
240 Negated bool `json:"negated,omitempty"`
241 Comment string `json:"comment,omitempty"`
242 }
243
244 func (c *Client) BatchModifyACLEntries(i *BatchModifyACLEntriesInput) error {
245
246 if i.Service == "" {
247 return ErrMissingService
248 }
249
250 if i.ACL == "" {
251 return ErrMissingACL
252 }
253
254 if len(i.Entries) > BatchModifyMaximumOperations {
255 return ErrBatchUpdateMaximumOperationsExceeded
256 }
257
258 path := fmt.Sprintf("/service/%s/acl/%s/entries", i.Service, i.ACL)
259 resp, err := c.PatchJSON(path, i, nil)
260 if err != nil {
261 return err
262 }
263
264 var batchModifyResult map[string]string
265 if err := decodeJSON(&batchModifyResult, resp.Body); err != nil {
266 return err
267 }
268
269 return nil
270 }