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
|
|
|
)
|
|
|
|
|
|
|
|
// VCL represents a response about VCL from the Fastly API.
|
|
|
|
type VCL 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"`
|
|
|
|
Main bool `mapstructure:"main"`
|
|
|
|
Content string `mapstructure:"content"`
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// vclsByName is a sortable list of VCLs.
|
|
|
|
type vclsByName []*VCL
|
|
|
|
|
|
|
|
// Len, Swap, and Less implement the sortable interface.
|
|
|
|
func (s vclsByName) Len() int { return len(s) }
|
|
|
|
func (s vclsByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
|
|
func (s vclsByName) 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
|
|
|
}
|
|
|
|
|
|
|
|
// ListVCLsInput is used as input to the ListVCLs function.
|
|
|
|
type ListVCLsInput 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 specific configuration version (required).
|
|
|
|
Version string
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListVCLs returns the list of VCLs for the configuration version.
|
|
|
|
func (c *Client) ListVCLs(i *ListVCLsInput) ([]*VCL, 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/vcl", i.Service, i.Version)
|
|
|
|
resp, err := c.Get(path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var vcls []*VCL
|
|
|
|
if err := decodeJSON(&vcls, resp.Body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sort.Stable(vclsByName(vcls))
|
|
|
|
return vcls, nil
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetVCLInput is used as input to the GetVCL function.
|
|
|
|
type GetVCLInput 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 VCL to fetch.
|
|
|
|
Name string
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetVCL gets the VCL configuration with the given parameters.
|
|
|
|
func (c *Client) GetVCL(i *GetVCLInput) (*VCL, 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/vcl/%s", i.Service, i.Version, i.Name)
|
|
|
|
resp, err := c.Get(path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var vcl *VCL
|
|
|
|
if err := decodeJSON(&vcl, resp.Body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return vcl, nil
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetGeneratedVCLInput is used as input to the GetGeneratedVCL function.
|
|
|
|
type GetGeneratedVCLInput 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
|
|
|
}
|
|
|
|
|
|
|
|
// GetGeneratedVCL gets the VCL configuration with the given parameters.
|
|
|
|
func (c *Client) GetGeneratedVCL(i *GetGeneratedVCLInput) (*VCL, 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/generated_vcl", i.Service, i.Version)
|
|
|
|
resp, err := c.Get(path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var vcl *VCL
|
|
|
|
if err := decodeJSON(&vcl, resp.Body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return vcl, nil
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateVCLInput is used as input to the CreateVCL function.
|
|
|
|
type CreateVCLInput 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 string `form:"name,omitempty"`
|
|
|
|
Content string `form:"content,omitempty"`
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateVCL creates a new Fastly VCL.
|
|
|
|
func (c *Client) CreateVCL(i *CreateVCLInput) (*VCL, 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/vcl", i.Service, i.Version)
|
|
|
|
resp, err := c.PostForm(path, i, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var vcl *VCL
|
|
|
|
if err := decodeJSON(&vcl, resp.Body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return vcl, nil
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateVCLInput is used as input to the UpdateVCL function.
|
|
|
|
type UpdateVCLInput 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 VCL to update (required).
|
|
|
|
Name string
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
NewName string `form:"name,omitempty"`
|
|
|
|
Content string `form:"content,omitempty"`
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateVCL creates a new Fastly VCL.
|
|
|
|
func (c *Client) UpdateVCL(i *UpdateVCLInput) (*VCL, 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/vcl/%s", i.Service, i.Version, i.Name)
|
|
|
|
resp, err := c.PutForm(path, i, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var vcl *VCL
|
|
|
|
if err := decodeJSON(&vcl, resp.Body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return vcl, nil
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ActivateVCLInput is used as input to the ActivateVCL function.
|
|
|
|
type ActivateVCLInput 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 VCL to mark as main (required).
|
|
|
|
Name string
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ActivateVCL creates a new Fastly VCL.
|
|
|
|
func (c *Client) ActivateVCL(i *ActivateVCLInput) (*VCL, 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/vcl/%s/main", i.Service, i.Version, i.Name)
|
|
|
|
resp, err := c.Put(path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var vcl *VCL
|
|
|
|
if err := decodeJSON(&vcl, resp.Body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return vcl, nil
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteVCLInput is the input parameter to DeleteVCL.
|
|
|
|
type DeleteVCLInput 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 VCL to delete (required).
|
|
|
|
Name string
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteVCL deletes the given VCL version.
|
|
|
|
func (c *Client) DeleteVCL(i *DeleteVCLInput) 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/vcl/%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
|
|
|
}
|