Debian Codegraph @

fastly/gcs.go

raw symbols SCIP
1 package fastly
2
3 import (
4 "fmt"
5 "net/url"
6 "sort"
7 "time"
8 )
9
10 // GCS represents an GCS logging response from the Fastly API.
11 type GCS struct {
12 ServiceID string `mapstructure:"service_id"`
13 Version int `mapstructure:"version"`
14
15 Name string `mapstructure:"name"`
16 Bucket string `mapstructure:"bucket_name"`
17 User string `mapstructure:"user"`
18 SecretKey string `mapstructure:"secret_key"`
19 Path string `mapstructure:"path"`
20 Period uint `mapstructure:"period"`
21 GzipLevel uint8 `mapstructure:"gzip_level"`
22 Format string `mapstructure:"format"`
23 FormatVersion uint `mapstructure:"format_version"`
24 MessageType string `mapstructure:"message_type"`
25 ResponseCondition string `mapstructure:"response_condition"`
26 TimestampFormat string `mapstructure:"timestamp_format"`
27 Placement string `mapstructure:"placement"`
28 CreatedAt *time.Time `mapstructure:"created_at"`
29 UpdatedAt *time.Time `mapstructure:"updated_at"`
30 DeletedAt *time.Time `mapstructure:"deleted_at"`
31 }
32
33 // gcsesByName is a sortable list of gcses.
34 type gcsesByName []*GCS
35
36 // Len, Swap, and Less implement the sortable interface.
37 func (s gcsesByName) Len() int { return len(s) }
38 func (s gcsesByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
39 func (s gcsesByName) Less(i, j int) bool {
40 return s[i].Name < s[j].Name
41 }
42
43 // ListGCSsInput is used as input to the ListGCSs function.
44 type ListGCSsInput struct {
45 // Service is the ID of the service (required).
46 Service string
47
48 // Version is the specific configuration version (required).
49 Version int
50 }
51
52 // ListGCSs returns the list of gcses for the configuration version.
53 func (c *Client) ListGCSs(i *ListGCSsInput) ([]*GCS, error) {
54 if i.Service == "" {
55 return nil, ErrMissingService
56 }
57
58 if i.Version == 0 {
59 return nil, ErrMissingVersion
60 }
61
62 path := fmt.Sprintf("/service/%s/version/%d/logging/gcs", i.Service, i.Version)
63 resp, err := c.Get(path, nil)
64 if err != nil {
65 return nil, err
66 }
67
68 var gcses []*GCS
69 if err := decodeJSON(&gcses, resp.Body); err != nil {
70 return nil, err
71 }
72 sort.Stable(gcsesByName(gcses))
73 return gcses, nil
74 }
75
76 // CreateGCSInput is used as input to the CreateGCS function.
77 type CreateGCSInput struct {
78 // Service is the ID of the service. Version is the specific configuration
79 // version. Both fields are required.
80 Service string
81 Version int
82
83 Name string `form:"name,omitempty"`
84 Bucket string `form:"bucket_name,omitempty"`
85 User string `form:"user,omitempty"`
86 SecretKey string `form:"secret_key,omitempty"`
87 Path string `form:"path,omitempty"`
88 Period uint `form:"period,omitempty"`
89 FormatVersion uint `form:"format_version,omitempty"`
90 GzipLevel uint8 `form:"gzip_level,omitempty"`
91 Format string `form:"format,omitempty"`
92 MessageType string `form:"message_type,omitempty"`
93 ResponseCondition string `form:"response_condition,omitempty"`
94 TimestampFormat string `form:"timestamp_format,omitempty"`
95 Placement string `form:"placement,omitempty"`
96 }
97
98 // CreateGCS creates a new Fastly GCS.
99 func (c *Client) CreateGCS(i *CreateGCSInput) (*GCS, error) {
100 if i.Service == "" {
101 return nil, ErrMissingService
102 }
103
104 if i.Version == 0 {
105 return nil, ErrMissingVersion
106 }
107
108 path := fmt.Sprintf("/service/%s/version/%d/logging/gcs", i.Service, i.Version)
109 resp, err := c.PostForm(path, i, nil)
110 if err != nil {
111 return nil, err
112 }
113
114 var gcs *GCS
115 if err := decodeJSON(&gcs, resp.Body); err != nil {
116 return nil, err
117 }
118 return gcs, nil
119 }
120
121 // GetGCSInput is used as input to the GetGCS function.
122 type GetGCSInput struct {
123 // Service is the ID of the service. Version is the specific configuration
124 // version. Both fields are required.
125 Service string
126 Version int
127
128 // Name is the name of the GCS to fetch.
129 Name string
130 }
131
132 // GetGCS gets the GCS configuration with the given parameters.
133 func (c *Client) GetGCS(i *GetGCSInput) (*GCS, error) {
134 if i.Service == "" {
135 return nil, ErrMissingService
136 }
137
138 if i.Version == 0 {
139 return nil, ErrMissingVersion
140 }
141
142 if i.Name == "" {
143 return nil, ErrMissingName
144 }
145
146 path := fmt.Sprintf("/service/%s/version/%d/logging/gcs/%s", i.Service, i.Version, url.PathEscape(i.Name))
147 resp, err := c.Get(path, nil)
148 if err != nil {
149 return nil, err
150 }
151
152 var b *GCS
153 if err := decodeJSON(&b, resp.Body); err != nil {
154 return nil, err
155 }
156 return b, nil
157 }
158
159 // UpdateGCSInput is used as input to the UpdateGCS function.
160 type UpdateGCSInput struct {
161 // Service is the ID of the service. Version is the specific configuration
162 // version. Both fields are required.
163 Service string
164 Version int
165
166 // Name is the name of the GCS to update.
167 Name string
168
169 NewName string `form:"name,omitempty"`
170 Bucket string `form:"bucket_name,omitempty"`
171 User string `form:"user,omitempty"`
172 SecretKey string `form:"secret_key,omitempty"`
173 Path string `form:"path,omitempty"`
174 Period uint `form:"period,omitempty"`
175 FormatVersion uint `form:"format_version,omitempty"`
176 GzipLevel uint8 `form:"gzip_level,omitempty"`
177 Format string `form:"format,omitempty"`
178 ResponseCondition string `form:"response_condition,omitempty"`
179 TimestampFormat string `form:"timestamp_format,omitempty"`
180 Placement string `form:"placement,omitempty"`
181 }
182
183 // UpdateGCS updates a specific GCS.
184 func (c *Client) UpdateGCS(i *UpdateGCSInput) (*GCS, 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/logging/gcs/%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 b *GCS
204 if err := decodeJSON(&b, resp.Body); err != nil {
205 return nil, err
206 }
207 return b, nil
208 }
209
210 // DeleteGCSInput is the input parameter to DeleteGCS.
211 type DeleteGCSInput 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 GCS to delete (required).
218 Name string
219 }
220
221 // DeleteGCS deletes the given GCS version.
222 func (c *Client) DeleteGCS(i *DeleteGCSInput) 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/logging/gcs/%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 }