2016-03-23 20:53:09 +01:00
|
|
|
package fastly
|
|
|
|
|
|
|
|
import (
|
2016-04-20 20:43:54 +02:00
|
|
|
"fmt"
|
|
|
|
"sort"
|
2016-03-23 20:53:09 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Wordpress represents a wordpress response from the Fastly API.
|
|
|
|
type Wordpress struct {
|
2016-04-20 20:43:54 +02:00
|
|
|
ServiceID string `mapstructure:"service_id"`
|
|
|
|
Version string `mapstructure:"version"`
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
Name string `mapstructure:"name"`
|
|
|
|
Path string `mapstructure:"path"`
|
|
|
|
Comment string `mapstructure:"comment"`
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2016-04-20 20:43:54 +02:00
|
|
|
return s[i].Name < s[j].Name
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListWordpressesInput is used as input to the ListWordpresses function.
|
|
|
|
type ListWordpressesInput struct {
|
2016-04-20 20:43:54 +02:00
|
|
|
// Service is the ID of the service. Version is the specific configuration
|
|
|
|
// version. Both fields are required.
|
|
|
|
Service string
|
|
|
|
Version string
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListWordpresses returns the list of wordpresses for the configuration version.
|
|
|
|
func (c *Client) ListWordpresses(i *ListWordpressesInput) ([]*Wordpress, error) {
|
2016-04-20 20:43:54 +02:00
|
|
|
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
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateWordpressInput is used as input to the CreateWordpress function.
|
|
|
|
type CreateWordpressInput struct {
|
2016-04-20 20:43:54 +02:00
|
|
|
// 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"`
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateWordpress creates a new Fastly wordpress.
|
|
|
|
func (c *Client) CreateWordpress(i *CreateWordpressInput) (*Wordpress, error) {
|
2016-04-20 20:43:54 +02:00
|
|
|
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
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetWordpressInput is used as input to the GetWordpress function.
|
|
|
|
type GetWordpressInput struct {
|
2016-04-20 20:43:54 +02:00
|
|
|
// Service is the ID of the service. Version is the specific configuration
|
|
|
|
// version. Both fields are required.
|
|
|
|
Service string
|
|
|
|
Version string
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
// Name is the name of the wordpress to fetch.
|
|
|
|
Name string
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetWordpress gets the wordpress configuration with the given parameters.
|
|
|
|
func (c *Client) GetWordpress(i *GetWordpressInput) (*Wordpress, error) {
|
2016-04-20 20:43:54 +02:00
|
|
|
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
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateWordpressInput is used as input to the UpdateWordpress function.
|
|
|
|
type UpdateWordpressInput struct {
|
2016-04-20 20:43:54 +02:00
|
|
|
// Service is the ID of the service. Version is the specific configuration
|
|
|
|
// version. Both fields are required.
|
|
|
|
Service string
|
|
|
|
Version string
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
// Name is the name of the wordpress to update.
|
|
|
|
Name string
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
NewName string `form:"name,omitempty"`
|
|
|
|
Path string `form:"path,omitempty"`
|
|
|
|
Comment string `form:"comment,omitempty"`
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateWordpress updates a specific wordpress.
|
|
|
|
func (c *Client) UpdateWordpress(i *UpdateWordpressInput) (*Wordpress, error) {
|
2016-04-20 20:43:54 +02:00
|
|
|
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
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteWordpressInput is the input parameter to DeleteWordpress.
|
|
|
|
type DeleteWordpressInput struct {
|
2016-04-20 20:43:54 +02:00
|
|
|
// Service is the ID of the service. Version is the specific configuration
|
|
|
|
// version. Both fields are required.
|
|
|
|
Service string
|
|
|
|
Version string
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
// Name is the name of the wordpress to delete (required).
|
|
|
|
Name string
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteWordpress deletes the given wordpress version.
|
|
|
|
func (c *Client) DeleteWordpress(i *DeleteWordpressInput) error {
|
2016-04-20 20:43:54 +02:00
|
|
|
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
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|