Debian Codegraph @

fastly/director.go

raw symbols SCIP
1 package fastly
2
3 import (
4 "fmt"
5 "net/url"
6 "sort"
7 "time"
8 )
9
10 const (
11 // DirectorTypeRandom is a director that does random direction.
12 DirectorTypeRandom DirectorType = 1
13
14 // DirectorTypeRoundRobin is a director that does round-robin direction.
15 DirectorTypeRoundRobin DirectorType = 2
16
17 // DirectorTypeHash is a director that does hash direction.
18 DirectorTypeHash DirectorType = 3
19
20 // DirectorTypeClient is a director that does client direction.
21 DirectorTypeClient DirectorType = 4
22 )
23
24 // DirectorType is a type of director.
25 type DirectorType uint8
26
27 // Director represents a director response from the Fastly API.
28 type Director struct {
29 ServiceID string `mapstructure:"service_id"`
30 Version int `mapstructure:"version"`
31
32 Name string `mapstructure:"name"`
33 Comment string `mapstructure:"comment"`
34 Shield string `mapstructure:"shield"`
35 Quorum uint `mapstructure:"quorum"`
36 Type DirectorType `mapstructure:"type"`
37 Retries uint `mapstructure:"retries"`
38 Capacity uint `mapstructure:"capacity"`
39 CreatedAt *time.Time `mapstructure:"created_at"`
40 UpdatedAt *time.Time `mapstructure:"updated_at"`
41 DeletedAt *time.Time `mapstructure:"deleted_at"`
42 }
43
44 // directorsByName is a sortable list of directors.
45 type directorsByName []*Director
46
47 // Len, Swap, and Less implement the sortable interface.
48 func (s directorsByName) Len() int { return len(s) }
49 func (s directorsByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
50 func (s directorsByName) Less(i, j int) bool {
51 return s[i].Name < s[j].Name
52 }
53
54 // ListDirectorsInput is used as input to the ListDirectors function.
55 type ListDirectorsInput struct {
56 // Service is the ID of the service (required).
57 Service string
58
59 // Version is the specific configuration version (required).
60 Version int
61 }
62
63 // ListDirectors returns the list of directors for the configuration version.
64 func (c *Client) ListDirectors(i *ListDirectorsInput) ([]*Director, error) {
65 if i.Service == "" {
66 return nil, ErrMissingService
67 }
68
69 if i.Version == 0 {
70 return nil, ErrMissingVersion
71 }
72
73 path := fmt.Sprintf("/service/%s/version/%d/director", i.Service, i.Version)
74 resp, err := c.Get(path, nil)
75 if err != nil {
76 return nil, err
77 }
78
79 var ds []*Director
80 if err := decodeJSON(&ds, resp.Body); err != nil {
81 return nil, err
82 }
83 sort.Stable(directorsByName(ds))
84 return ds, nil
85 }
86
87 // CreateDirectorInput is used as input to the CreateDirector function.
88 type CreateDirectorInput struct {
89 // Service is the ID of the service. Version is the specific configuration
90 // version. Both fields are required.
91 Service string
92 Version int
93
94 Name string `form:"name,omitempty"`
95 Comment string `form:"comment,omitempty"`
96 Shield string `form:"shield,omitempty"`
97 Quorum uint `form:"quorum,omitempty"`
98 Type DirectorType `form:"type,omitempty"`
99 Retries uint `form:"retries,omitempty"`
100 Capacity uint `form:"capacity,omitempty"`
101 }
102
103 // CreateDirector creates a new Fastly director.
104 func (c *Client) CreateDirector(i *CreateDirectorInput) (*Director, error) {
105 if i.Service == "" {
106 return nil, ErrMissingService
107 }
108
109 if i.Version == 0 {
110 return nil, ErrMissingVersion
111 }
112
113 path := fmt.Sprintf("/service/%s/version/%d/director", i.Service, i.Version)
114 resp, err := c.PostForm(path, i, nil)
115 if err != nil {
116 return nil, err
117 }
118
119 var d *Director
120 if err := decodeJSON(&d, resp.Body); err != nil {
121 return nil, err
122 }
123 return d, nil
124 }
125
126 // GetDirectorInput is used as input to the GetDirector function.
127 type GetDirectorInput struct {
128 // Service is the ID of the service. Version is the specific configuration
129 // version. Both fields are required.
130 Service string
131 Version int
132
133 // Name is the name of the director to fetch.
134 Name string
135 }
136
137 // GetDirector gets the director configuration with the given parameters.
138 func (c *Client) GetDirector(i *GetDirectorInput) (*Director, error) {
139 if i.Service == "" {
140 return nil, ErrMissingService
141 }
142
143 if i.Version == 0 {
144 return nil, ErrMissingVersion
145 }
146
147 if i.Name == "" {
148 return nil, ErrMissingName
149 }
150
151 path := fmt.Sprintf("/service/%s/version/%d/director/%s", i.Service, i.Version, url.PathEscape(i.Name))
152 resp, err := c.Get(path, nil)
153 if err != nil {
154 return nil, err
155 }
156
157 var d *Director
158 if err := decodeJSON(&d, resp.Body); err != nil {
159 return nil, err
160 }
161 return d, nil
162 }
163
164 // UpdateDirectorInput is used as input to the UpdateDirector function.
165 type UpdateDirectorInput struct {
166 // Service is the ID of the service. Version is the specific configuration
167 // version. Both fields are required.
168 Service string
169 Version int
170
171 // Name is the name of the director to update.
172 Name string
173
174 NewName string `form:"name,omitempty"`
175 Comment string `form:"comment,omitempty"`
176 Shield string `form:"shield,omitempty"`
177 Quorum uint `form:"quorum,omitempty"`
178 Type DirectorType `form:"type,omitempty"`
179 Retries uint `form:"retries,omitempty"`
180 Capacity uint `form:"capacity,omitempty"`
181 }
182
183 // UpdateDirector updates a specific director.
184 func (c *Client) UpdateDirector(i *UpdateDirectorInput) (*Director, error) {
185 if i.Service == "" {
186 return nil, ErrMissingService
187 }
188
189 if i.Version == 0 {
190 return nil, ErrMissingVersion
191 }
192
193 if i.Name == "" {
194 return nil, ErrMissingName
195 }
196
197 path := fmt.Sprintf("/service/%s/version/%d/director/%s", i.Service, i.Version, url.PathEscape(i.Name))
198 resp, err := c.PutForm(path, i, nil)
199 if err != nil {
200 return nil, err
201 }
202
203 var d *Director
204 if err := decodeJSON(&d, resp.Body); err != nil {
205 return nil, err
206 }
207 return d, nil
208 }
209
210 // DeleteDirectorInput is the input parameter to DeleteDirector.
211 type DeleteDirectorInput struct {
212 // Service is the ID of the service. Version is the specific configuration
213 // version. Both fields are required.
214 Service string
215 Version int
216
217 // Name is the name of the director to delete (required).
218 Name string
219 }
220
221 // DeleteDirector deletes the given director version.
222 func (c *Client) DeleteDirector(i *DeleteDirectorInput) error {
223 if i.Service == "" {
224 return ErrMissingService
225 }
226
227 if i.Version == 0 {
228 return ErrMissingVersion
229 }
230
231 if i.Name == "" {
232 return ErrMissingName
233 }
234
235 path := fmt.Sprintf("/service/%s/version/%d/director/%s", i.Service, i.Version, url.PathEscape(i.Name))
236 resp, err := c.Delete(path, nil)
237 if err != nil {
238 return err
239 }
240
241 var r *statusResp
242 if err := decodeJSON(&r, resp.Body); err != nil {
243 return err
244 }
245 if !r.Ok() {
246 return fmt.Errorf("Not Ok")
247 }
248 return nil
249 }