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
|
|
|
)
|
|
|
|
|
|
|
|
// Domain represents the the domain name Fastly will serve content for.
|
|
|
|
type Domain 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"`
|
|
|
|
Comment string `mapstructure:"comment"`
|
|
|
|
Locked bool `mapstructure:"locked"`
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// domainsByName is a sortable list of backends.
|
|
|
|
type domainsByName []*Domain
|
|
|
|
|
|
|
|
// Len, Swap, and Less implement the sortable interface.
|
|
|
|
func (s domainsByName) Len() int { return len(s) }
|
|
|
|
func (s domainsByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
|
|
func (s domainsByName) 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
|
|
|
}
|
|
|
|
|
|
|
|
// ListDomainsInput is used as input to the ListDomains function.
|
|
|
|
type ListDomainsInput 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
|
|
|
// ListDomains returns the list of domains for this Service.
|
2016-03-23 20:53:09 +01:00
|
|
|
func (c *Client) ListDomains(i *ListDomainsInput) ([]*Domain, 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/domain", i.Service, i.Version)
|
|
|
|
resp, err := c.Get(path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ds []*Domain
|
|
|
|
if err := decodeJSON(&ds, resp.Body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sort.Stable(domainsByName(ds))
|
|
|
|
return ds, nil
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateDomainInput is used as input to the CreateDomain function.
|
|
|
|
type CreateDomainInput 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 domain that the service will respond to (required).
|
|
|
|
Name string `form:"name"`
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
// Comment is a personal, freeform descriptive note.
|
|
|
|
Comment string `form:"comment,omitempty"`
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateDomain creates a new domain with the given information.
|
|
|
|
func (c *Client) CreateDomain(i *CreateDomainInput) (*Domain, 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/domain", i.Service, i.Version)
|
|
|
|
resp, err := c.PostForm(path, i, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var d *Domain
|
|
|
|
if err := decodeJSON(&d, resp.Body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return d, nil
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetDomainInput is used as input to the GetDomain function.
|
|
|
|
type GetDomainInput 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 domain to fetch.
|
|
|
|
Name string `form:"name"`
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetDomain retrieves information about the given domain name.
|
|
|
|
func (c *Client) GetDomain(i *GetDomainInput) (*Domain, 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/domain/%s", i.Service, i.Version, i.Name)
|
|
|
|
resp, err := c.Get(path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var d *Domain
|
|
|
|
if err := decodeJSON(&d, resp.Body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return d, nil
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateDomainInput is used as input to the UpdateDomain function.
|
|
|
|
type UpdateDomainInput 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 domain that the service will respond to (required).
|
|
|
|
Name string
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
// NewName is the updated name of the domain
|
|
|
|
NewName string `form:"name"`
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
// Comment is a personal, freeform descriptive note.
|
|
|
|
Comment string `form:"comment,omitempty"`
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateDomain updates a single domain for the current service. The only allowed
|
|
|
|
// parameters are `Name` and `Comment`.
|
|
|
|
func (c *Client) UpdateDomain(i *UpdateDomainInput) (*Domain, 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/domain/%s", i.Service, i.Version, i.Name)
|
|
|
|
resp, err := c.PutForm(path, i, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var d *Domain
|
|
|
|
if err := decodeJSON(&d, resp.Body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return d, nil
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteDomainInput is used as input to the DeleteDomain function.
|
|
|
|
type DeleteDomainInput 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 domain that the service will respond to (required).
|
|
|
|
Name string `form:"name"`
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteDomain removes a single domain by the given name.
|
|
|
|
func (c *Client) DeleteDomain(i *DeleteDomainInput) 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/domain/%s", i.Service, i.Version, i.Name)
|
|
|
|
_, err := c.Delete(path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|