Debian Codegraph @

fastly/acl.go

raw symbols SCIP
1 package fastly
2
3 import (
4 "fmt"
5 "net/url"
6 "sort"
7 "time"
8 )
9
10 type ACL struct {
11 ServiceID string `mapstructure:"service_id"`
12 Version int `mapstructure:"version"`
13
14 Name string `mapstructure:"name"`
15 ID string `mapstructure:"id"`
16 CreatedAt *time.Time `mapstructure:"created_at"`
17 UpdatedAt *time.Time `mapstructure:"updated_at"`
18 DeletedAt *time.Time `mapstructure:"deleted_at"`
19 }
20
21 // ACLsByName is a sortable list of ACLs.
22 type ACLsByName []*ACL
23
24 // Len, Swap, and Less implement the sortable interface.
25 func (s ACLsByName) Len() int { return len(s) }
26 func (s ACLsByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
27 func (s ACLsByName) Less(i, j int) bool {
28 return s[i].Name < s[j].Name
29 }
30
31 // ListACLsInput is used as input to the ListACLs function.
32 type ListACLsInput struct {
33 // Service is the ID of the service (required).
34 Service string
35
36 // Version is the specific configuration version (required).
37 Version int
38 }
39
40 // ListACLs returns the list of ACLs for the configuration version.
41 func (c *Client) ListACLs(i *ListACLsInput) ([]*ACL, error) {
42 if i.Service == "" {
43 return nil, ErrMissingService
44 }
45
46 if i.Version == 0 {
47 return nil, ErrMissingVersion
48 }
49
50 path := fmt.Sprintf("/service/%s/version/%d/acl", i.Service, i.Version)
51 resp, err := c.Get(path, nil)
52 if err != nil {
53 return nil, err
54 }
55
56 var as []*ACL
57 if err := decodeJSON(&as, resp.Body); err != nil {
58 return nil, err
59 }
60 sort.Stable(ACLsByName(as))
61 return as, nil
62 }
63
64 // CreateACLInput is used as input to the CreateACL function.
65 type CreateACLInput struct {
66 // Service is the ID of the service. Version is the specific configuration
67 // version. Both fields are required.
68 Service string
69 Version int
70
71 // Name is the name of the ACL to create (required)
72 Name string `form:"name"`
73 }
74
75 func (c *Client) CreateACL(i *CreateACLInput) (*ACL, error) {
76 if i.Service == "" {
77 return nil, ErrMissingService
78 }
79
80 if i.Version == 0 {
81 return nil, ErrMissingVersion
82 }
83
84 path := fmt.Sprintf("/service/%s/version/%d/acl", i.Service, i.Version)
85 resp, err := c.PostForm(path, i, nil)
86 if err != nil {
87 return nil, err
88 }
89
90 var a *ACL
91 if err := decodeJSON(&a, resp.Body); err != nil {
92 return nil, err
93 }
94 return a, nil
95 }
96
97 // DeleteACLInput is the input parameter to DeleteACL function.
98 type DeleteACLInput struct {
99 // Service is the ID of the service. Version is the specific configuration
100 // version. Both fields are required.
101 Service string
102 Version int
103
104 // Name is the name of the ACL to delete (required).
105 Name string
106 }
107
108 // DeleteACL deletes the given ACL version.
109 func (c *Client) DeleteACL(i *DeleteACLInput) error {
110 if i.Service == "" {
111 return ErrMissingService
112 }
113
114 if i.Version == 0 {
115 return ErrMissingVersion
116 }
117
118 if i.Name == "" {
119 return ErrMissingName
120 }
121
122 path := fmt.Sprintf("/service/%s/version/%d/acl/%s", i.Service, i.Version, url.PathEscape(i.Name))
123 resp, err := c.Delete(path, nil)
124 if err != nil {
125 return err
126 }
127
128 var r *statusResp
129 if err := decodeJSON(&r, resp.Body); err != nil {
130 return err
131 }
132 if !r.Ok() {
133 return fmt.Errorf("Not Ok")
134 }
135 return nil
136 }
137
138 // GetACLInput is the input parameter to GetACL function.
139 type GetACLInput struct {
140 // Service is the ID of the service. Version is the specific configuration
141 // version. Both fields are required.
142 Service string
143 Version int
144
145 // Name is the name of the ACL to get (required).
146 Name string
147 }
148
149 // GetACL gets the ACL configuration with the given parameters.
150 func (c *Client) GetACL(i *GetACLInput) (*ACL, error) {
151 if i.Service == "" {
152 return nil, ErrMissingService
153 }
154
155 if i.Version == 0 {
156 return nil, ErrMissingVersion
157 }
158
159 if i.Name == "" {
160 return nil, ErrMissingName
161 }
162
163 path := fmt.Sprintf("/service/%s/version/%d/acl/%s", i.Service, i.Version, url.PathEscape(i.Name))
164 resp, err := c.Get(path, nil)
165 if err != nil {
166 return nil, err
167 }
168
169 var a *ACL
170 if err := decodeJSON(&a, resp.Body); err != nil {
171 return nil, err
172 }
173 return a, nil
174 }
175
176 // UpdateACLInput is the input parameter to UpdateACL function.
177 type UpdateACLInput struct {
178 // Service is the ID of the service. Version is the specific configuration
179 // version. Both fields are required.
180 Service string
181 Version int
182
183 // Name is the name of the ACL to update (required).
184 Name string
185
186 // NewName is the new name of the ACL to update (required).
187 NewName string `form:"name"`
188 }
189
190 // UpdateACL updates the name of the ACL with the given parameters.
191 func (c *Client) UpdateACL(i *UpdateACLInput) (*ACL, error) {
192 if i.Service == "" {
193 return nil, ErrMissingService
194 }
195
196 if i.Version == 0 {
197 return nil, ErrMissingVersion
198 }
199
200 if i.Name == "" {
201 return nil, ErrMissingName
202 }
203
204 if i.NewName == "" {
205 return nil, ErrMissingNewName
206 }
207
208 path := fmt.Sprintf("/service/%s/version/%d/acl/%s", i.Service, i.Version, url.PathEscape(i.Name))
209 resp, err := c.PutForm(path, i, nil)
210
211 if err != nil {
212 return nil, err
213 }
214
215 var a *ACL
216 if err := decodeJSON(&a, resp.Body); err != nil {
217 return nil, err
218 }
219
220 return a, nil
221 }