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
|
|
|
)
|
|
|
|
|
|
|
|
// Version represents a distinct configuration version.
|
|
|
|
type Version struct {
|
2016-05-09 20:08:13 +02:00
|
|
|
Number string `mapstructure:"number"`
|
|
|
|
Comment string `mapstructure:"comment"`
|
|
|
|
ServiceID string `mapstructure:"service_id"`
|
|
|
|
Active bool `mapstructure:"active"`
|
|
|
|
Locked bool `mapstructure:"locked"`
|
|
|
|
Deployed bool `mapstructure:"deployed"`
|
|
|
|
Staging bool `mapstructure:"staging"`
|
|
|
|
Testing bool `mapstructure:"testing"`
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// versionsByNumber is a sortable list of versions. This is used by the version
|
|
|
|
// `List()` function to sort the API responses.
|
|
|
|
type versionsByNumber []*Version
|
|
|
|
|
|
|
|
// Len, Swap, and Less implement the sortable interface.
|
|
|
|
func (s versionsByNumber) Len() int { return len(s) }
|
|
|
|
func (s versionsByNumber) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
|
|
func (s versionsByNumber) Less(i, j int) bool {
|
2016-04-20 20:43:54 +02:00
|
|
|
return s[i].Number < s[j].Number
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListVersionsInput is the input to the ListVersions function.
|
|
|
|
type ListVersionsInput struct {
|
2016-04-20 20:43:54 +02:00
|
|
|
// Service is the ID of the service (required).
|
|
|
|
Service string
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListVersions returns the full list of all versions of the given service.
|
|
|
|
func (c *Client) ListVersions(i *ListVersionsInput) ([]*Version, error) {
|
2016-04-20 20:43:54 +02:00
|
|
|
if i.Service == "" {
|
|
|
|
return nil, ErrMissingService
|
|
|
|
}
|
|
|
|
|
|
|
|
path := fmt.Sprintf("/service/%s/version", i.Service)
|
|
|
|
resp, err := c.Get(path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var e []*Version
|
|
|
|
if err := decodeJSON(&e, resp.Body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sort.Sort(versionsByNumber(e))
|
|
|
|
|
|
|
|
return e, nil
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// LatestVersionInput is the input to the LatestVersion function.
|
|
|
|
type LatestVersionInput struct {
|
2016-04-20 20:43:54 +02:00
|
|
|
// Service is the ID of the service (required).
|
|
|
|
Service string
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// LatestVersion fetches the latest version. If there are no versions, this
|
|
|
|
// function will return nil (but not an error).
|
|
|
|
func (c *Client) LatestVersion(i *LatestVersionInput) (*Version, error) {
|
2016-04-20 20:43:54 +02:00
|
|
|
if i.Service == "" {
|
|
|
|
return nil, ErrMissingService
|
|
|
|
}
|
|
|
|
|
|
|
|
list, err := c.ListVersions(&ListVersionsInput{Service: i.Service})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(list) < 1 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
e := list[len(list)-1]
|
|
|
|
return e, nil
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateVersionInput is the input to the CreateVersion function.
|
|
|
|
type CreateVersionInput struct {
|
2016-04-20 20:43:54 +02:00
|
|
|
// Service is the ID of the service (required).
|
|
|
|
Service string
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateVersion constructs a new version. There are no request parameters, but
|
|
|
|
// you should consult the resulting version number. Note that `CloneVersion` is
|
|
|
|
// preferred in almost all scenarios, since `Create()` creates a _blank_
|
|
|
|
// configuration where `Clone()` builds off of an existing configuration.
|
|
|
|
func (c *Client) CreateVersion(i *CreateVersionInput) (*Version, error) {
|
2016-04-20 20:43:54 +02:00
|
|
|
if i.Service == "" {
|
|
|
|
return nil, ErrMissingService
|
|
|
|
}
|
|
|
|
|
|
|
|
path := fmt.Sprintf("/service/%s/version", i.Service)
|
|
|
|
resp, err := c.Post(path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var e *Version
|
|
|
|
if err := decodeJSON(&e, resp.Body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return e, nil
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetVersionInput is the input to the GetVersion function.
|
|
|
|
type GetVersionInput struct {
|
2016-04-20 20:43:54 +02:00
|
|
|
// Service is the ID of the service (required).
|
|
|
|
Service string
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
// Version is the version number to fetch (required).
|
|
|
|
Version string
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetVersion fetches a version with the given information.
|
|
|
|
func (c *Client) GetVersion(i *GetVersionInput) (*Version, 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", i.Service, i.Version)
|
|
|
|
resp, err := c.Get(path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var e *Version
|
|
|
|
if err := decodeJSON(&e, resp.Body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return e, nil
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateVersionInput is the input to the UpdateVersion function.
|
|
|
|
type UpdateVersionInput 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
|
|
|
Comment string `form:"comment,omitempty"`
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateVersion updates the given version
|
|
|
|
func (c *Client) UpdateVersion(i *UpdateVersionInput) (*Version, 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", i.Service, i.Version)
|
|
|
|
resp, err := c.PutForm(path, i, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var e *Version
|
|
|
|
if err := decodeJSON(&e, resp.Body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return e, nil
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ActivateVersionInput is the input to the ActivateVersion function.
|
|
|
|
type ActivateVersionInput 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
|
|
|
}
|
|
|
|
|
|
|
|
// ActivateVersion activates the given version.
|
|
|
|
func (c *Client) ActivateVersion(i *ActivateVersionInput) (*Version, 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/activate", i.Service, i.Version)
|
|
|
|
resp, err := c.Put(path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var e *Version
|
|
|
|
if err := decodeJSON(&e, resp.Body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return e, nil
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeactivateVersionInput is the input to the DeactivateVersion function.
|
|
|
|
type DeactivateVersionInput 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
|
|
|
}
|
|
|
|
|
|
|
|
// DeactivateVersion deactivates the given version.
|
|
|
|
func (c *Client) DeactivateVersion(i *DeactivateVersionInput) (*Version, 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/deactivate", i.Service, i.Version)
|
|
|
|
resp, err := c.Put(path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var e *Version
|
|
|
|
if err := decodeJSON(&e, resp.Body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return e, nil
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CloneVersionInput is the input to the CloneVersion function.
|
|
|
|
type CloneVersionInput 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
|
|
|
}
|
|
|
|
|
|
|
|
// CloneVersion creates a clone of the version with and returns a new
|
|
|
|
// configuration version with all the same configuration options, but an
|
|
|
|
// incremented number.
|
|
|
|
func (c *Client) CloneVersion(i *CloneVersionInput) (*Version, 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/clone", i.Service, i.Version)
|
|
|
|
resp, err := c.Put(path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var e *Version
|
|
|
|
if err := decodeJSON(&e, resp.Body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return e, nil
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateVersionInput is the input to the ValidateVersion function.
|
|
|
|
type ValidateVersionInput 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
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateVersion validates if the given version is okay.
|
|
|
|
func (c *Client) ValidateVersion(i *ValidateVersionInput) (bool, string, error) {
|
2016-04-20 20:43:54 +02:00
|
|
|
var msg string
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
if i.Service == "" {
|
|
|
|
return false, msg, ErrMissingService
|
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
if i.Version == "" {
|
|
|
|
return false, msg, ErrMissingVersion
|
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
path := fmt.Sprintf("/service/%s/version/%s/validate", i.Service, i.Version)
|
|
|
|
resp, err := c.Get(path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, msg, err
|
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
var r *statusResp
|
|
|
|
if err := decodeJSON(&r, resp.Body); err != nil {
|
|
|
|
return false, msg, err
|
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
msg = r.Msg
|
|
|
|
return r.Ok(), msg, nil
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// LockVersionInput is the input to the LockVersion function.
|
|
|
|
type LockVersionInput 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
|
|
|
}
|
|
|
|
|
|
|
|
// LockVersion locks the specified version.
|
|
|
|
func (c *Client) LockVersion(i *LockVersionInput) (*Version, 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/lock", i.Service, i.Version)
|
|
|
|
resp, err := c.Put(path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var e *Version
|
|
|
|
if err := decodeJSON(&e, resp.Body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return e, nil
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|