Debian Codegraph @

fastly/ftp.go

raw symbols SCIP
1 package fastly
2
3 import (
4 "fmt"
5 "net/url"
6 "sort"
7 "time"
8 )
9
10 // FTP represents an FTP logging response from the Fastly API.
11 type FTP struct {
12 ServiceID string `mapstructure:"service_id"`
13 Version int `mapstructure:"version"`
14
15 Name string `mapstructure:"name"`
16 Address string `mapstructure:"address"`
17 Port uint `mapstructure:"port"`
18 Username string `mapstructure:"user"`
19 Password string `mapstructure:"password"`
20 Path string `mapstructure:"path"`
21 Period uint `mapstructure:"period"`
22 GzipLevel uint8 `mapstructure:"gzip_level"`
23 Format string `mapstructure:"format"`
24 FormatVersion uint `mapstructure:"format_version"`
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 // ftpsByName is a sortable list of ftps.
34 type ftpsByName []*FTP
35
36 // Len, Swap, and Less implement the sortable interface.
37 func (s ftpsByName) Len() int { return len(s) }
38 func (s ftpsByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
39 func (s ftpsByName) Less(i, j int) bool {
40 return s[i].Name < s[j].Name
41 }
42
43 // ListFTPsInput is used as input to the ListFTPs function.
44 type ListFTPsInput 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 // ListFTPs returns the list of ftps for the configuration version.
53 func (c *Client) ListFTPs(i *ListFTPsInput) ([]*FTP, 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/ftp", i.Service, i.Version)
63 resp, err := c.Get(path, nil)
64 if err != nil {
65 return nil, err
66 }
67
68 var ftps []*FTP
69 if err := decodeJSON(&ftps, resp.Body); err != nil {
70 return nil, err
71 }
72 sort.Stable(ftpsByName(ftps))
73 return ftps, nil
74 }
75
76 // CreateFTPInput is used as input to the CreateFTP function.
77 type CreateFTPInput 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 Address string `form:"address,omitempty"`
85 Port uint `form:"port,omitempty"`
86 Username string `form:"user,omitempty"`
87 Password string `form:"password,omitempty"`
88 Path string `form:"path,omitempty"`
89 Period uint `form:"period,omitempty"`
90 FormatVersion uint `form:"format_version,omitempty"`
91 GzipLevel uint8 `form:"gzip_level,omitempty"`
92 Format string `form:"format,omitempty"`
93 ResponseCondition string `form:"response_condition,omitempty"`
94 TimestampFormat string `form:"timestamp_format,omitempty"`
95 Placement string `form:"placement,omitempty"`
96 }
97
98 // CreateFTP creates a new Fastly FTP.
99 func (c *Client) CreateFTP(i *CreateFTPInput) (*FTP, 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/ftp", i.Service, i.Version)
109 resp, err := c.PostForm(path, i, nil)
110 if err != nil {
111 return nil, err
112 }
113
114 var ftp *FTP
115 if err := decodeJSON(&ftp, resp.Body); err != nil {
116 return nil, err
117 }
118 return ftp, nil
119 }
120
121 // GetFTPInput is used as input to the GetFTP function.
122 type GetFTPInput 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 FTP to fetch.
129 Name string
130 }
131
132 // GetFTP gets the FTP configuration with the given parameters.
133 func (c *Client) GetFTP(i *GetFTPInput) (*FTP, 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/ftp/%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 *FTP
153 if err := decodeJSON(&b, resp.Body); err != nil {
154 return nil, err
155 }
156 return b, nil
157 }
158
159 // UpdateFTPInput is used as input to the UpdateFTP function.
160 type UpdateFTPInput 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 FTP to update.
167 Name string
168
169 NewName string `form:"name,omitempty"`
170 Address string `form:"address,omitempty"`
171 Port uint `form:"port,omitempty"`
172 Username string `form:"user,omitempty"`
173 Password string `form:"password,omitempty"`
174 Path string `form:"path,omitempty"`
175 Period uint `form:"period,omitempty"`
176 FormatVersion uint `form:"format_version,omitempty"`
177 GzipLevel uint8 `form:"gzip_level,omitempty"`
178 Format string `form:"format,omitempty"`
179 ResponseCondition string `form:"response_condition,omitempty"`
180 TimestampFormat string `form:"timestamp_format,omitempty"`
181 Placement string `form:"placement,omitempty"`
182 }
183
184 // UpdateFTP updates a specific FTP.
185 func (c *Client) UpdateFTP(i *UpdateFTPInput) (*FTP, error) {
186 if i.Service == "" {
187 return nil, ErrMissingService
188 }
189
190 if i.Version == 0 {
191 return nil, ErrMissingVersion
192 }
193
194 if i.Name == "" {
195 return nil, ErrMissingName
196 }
197
198 path := fmt.Sprintf("/service/%s/version/%d/logging/ftp/%s", i.Service, i.Version, url.PathEscape(i.Name))
199 resp, err := c.PutForm(path, i, nil)
200 if err != nil {
201 return nil, err
202 }
203
204 var b *FTP
205 if err := decodeJSON(&b, resp.Body); err != nil {
206 return nil, err
207 }
208 return b, nil
209 }
210
211 // DeleteFTPInput is the input parameter to DeleteFTP.
212 type DeleteFTPInput struct {
213 // Service is the ID of the service. Version is the specific configuration
214 // version. Both fields are required.
215 Service string
216 Version int
217
218 // Name is the name of the FTP to delete (required).
219 Name string
220 }
221
222 // DeleteFTP deletes the given FTP version.
223 func (c *Client) DeleteFTP(i *DeleteFTPInput) error {
224 if i.Service == "" {
225 return ErrMissingService
226 }
227
228 if i.Version == 0 {
229 return ErrMissingVersion
230 }
231
232 if i.Name == "" {
233 return ErrMissingName
234 }
235
236 path := fmt.Sprintf("/service/%s/version/%d/logging/ftp/%s", i.Service, i.Version, url.PathEscape(i.Name))
237 resp, err := c.Delete(path, nil)
238 if err != nil {
239 return err
240 }
241
242 var r *statusResp
243 if err := decodeJSON(&r, resp.Body); err != nil {
244 return err
245 }
246 if !r.Ok() {
247 return fmt.Errorf("Not Ok")
248 }
249 return nil
250 }