forked from fastly/go-fastly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordpress.go
214 lines (176 loc) · 5.14 KB
/
wordpress.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package fastly
import (
"fmt"
"sort"
)
// Wordpress represents a wordpress response from the Fastly API.
type Wordpress struct {
ServiceID string `mapstructure:"service_id"`
Version string `mapstructure:"version"`
Name string `mapstructure:"name"`
Path string `mapstructure:"path"`
Comment string `mapstructure:"comment"`
}
// wordpressesByName is a sortable list of wordpresses.
type wordpressesByName []*Wordpress
// Len, Swap, and Less implement the sortable interface.
func (s wordpressesByName) Len() int { return len(s) }
func (s wordpressesByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s wordpressesByName) Less(i, j int) bool {
return s[i].Name < s[j].Name
}
// ListWordpressesInput is used as input to the ListWordpresses function.
type ListWordpressesInput struct {
// Service is the ID of the service. Version is the specific configuration
// version. Both fields are required.
Service string
Version string
}
// ListWordpresses returns the list of wordpresses for the configuration version.
func (c *Client) ListWordpresses(i *ListWordpressesInput) ([]*Wordpress, error) {
if i.Service == "" {
return nil, ErrMissingService
}
if i.Version == "" {
return nil, ErrMissingVersion
}
path := fmt.Sprintf("/service/%s/version/%s/wordpress", i.Service, i.Version)
resp, err := c.Get(path, nil)
if err != nil {
return nil, err
}
var bs []*Wordpress
if err := decodeJSON(&bs, resp.Body); err != nil {
return nil, err
}
sort.Stable(wordpressesByName(bs))
return bs, nil
}
// CreateWordpressInput is used as input to the CreateWordpress function.
type CreateWordpressInput struct {
// Service is the ID of the service. Version is the specific configuration
// version. Both fields are required.
Service string
Version string
Name string `form:"name,omitempty"`
Path string `form:"path,omitempty"`
Comment string `form:"comment,omitempty"`
}
// CreateWordpress creates a new Fastly wordpress.
func (c *Client) CreateWordpress(i *CreateWordpressInput) (*Wordpress, error) {
if i.Service == "" {
return nil, ErrMissingService
}
if i.Version == "" {
return nil, ErrMissingVersion
}
path := fmt.Sprintf("/service/%s/version/%s/wordpress", i.Service, i.Version)
resp, err := c.PostForm(path, i, nil)
if err != nil {
return nil, err
}
var b *Wordpress
if err := decodeJSON(&b, resp.Body); err != nil {
return nil, err
}
return b, nil
}
// GetWordpressInput is used as input to the GetWordpress function.
type GetWordpressInput struct {
// Service is the ID of the service. Version is the specific configuration
// version. Both fields are required.
Service string
Version string
// Name is the name of the wordpress to fetch.
Name string
}
// GetWordpress gets the wordpress configuration with the given parameters.
func (c *Client) GetWordpress(i *GetWordpressInput) (*Wordpress, error) {
if i.Service == "" {
return nil, ErrMissingService
}
if i.Version == "" {
return nil, ErrMissingVersion
}
if i.Name == "" {
return nil, ErrMissingName
}
path := fmt.Sprintf("/service/%s/version/%s/wordpress/%s", i.Service, i.Version, i.Name)
resp, err := c.Get(path, nil)
if err != nil {
return nil, err
}
var b *Wordpress
if err := decodeJSON(&b, resp.Body); err != nil {
return nil, err
}
return b, nil
}
// UpdateWordpressInput is used as input to the UpdateWordpress function.
type UpdateWordpressInput struct {
// Service is the ID of the service. Version is the specific configuration
// version. Both fields are required.
Service string
Version string
// Name is the name of the wordpress to update.
Name string
NewName string `form:"name,omitempty"`
Path string `form:"path,omitempty"`
Comment string `form:"comment,omitempty"`
}
// UpdateWordpress updates a specific wordpress.
func (c *Client) UpdateWordpress(i *UpdateWordpressInput) (*Wordpress, error) {
if i.Service == "" {
return nil, ErrMissingService
}
if i.Version == "" {
return nil, ErrMissingVersion
}
if i.Name == "" {
return nil, ErrMissingName
}
path := fmt.Sprintf("/service/%s/version/%s/wordpress/%s", i.Service, i.Version, i.Name)
resp, err := c.PutForm(path, i, nil)
if err != nil {
return nil, err
}
var b *Wordpress
if err := decodeJSON(&b, resp.Body); err != nil {
return nil, err
}
return b, nil
}
// DeleteWordpressInput is the input parameter to DeleteWordpress.
type DeleteWordpressInput struct {
// Service is the ID of the service. Version is the specific configuration
// version. Both fields are required.
Service string
Version string
// Name is the name of the wordpress to delete (required).
Name string
}
// DeleteWordpress deletes the given wordpress version.
func (c *Client) DeleteWordpress(i *DeleteWordpressInput) error {
if i.Service == "" {
return ErrMissingService
}
if i.Version == "" {
return ErrMissingVersion
}
if i.Name == "" {
return ErrMissingName
}
path := fmt.Sprintf("/service/%s/version/%s/wordpress/%s", i.Service, i.Version, i.Name)
resp, err := c.Delete(path, nil)
if err != nil {
return err
}
var r *statusResp
if err := decodeJSON(&r, resp.Body); err != nil {
return err
}
if !r.Ok() {
return fmt.Errorf("Not Ok")
}
return nil
}