Debian Codegraph @

fastly/blobstorage.go

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