openstack cdn services operations
diff --git a/openstack/cdn/v1/services/errors.go b/openstack/cdn/v1/services/errors.go
new file mode 100644
index 0000000..359584c
--- /dev/null
+++ b/openstack/cdn/v1/services/errors.go
@@ -0,0 +1,7 @@
+package services
+
+import "fmt"
+
+func no(str string) error {
+ return fmt.Errorf("Required parameter %s not provided", str)
+}
diff --git a/openstack/cdn/v1/services/requests.go b/openstack/cdn/v1/services/requests.go
new file mode 100644
index 0000000..b58ca9c
--- /dev/null
+++ b/openstack/cdn/v1/services/requests.go
@@ -0,0 +1,280 @@
+package services
+
+import (
+ "github.com/racker/perigee"
+ "github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/pagination"
+)
+
+// ListOptsBuilder allows extensions to add additional parameters to the
+// List request.
+type ListOptsBuilder interface {
+ ToCDNServiceListQuery() (string, error)
+}
+
+// ListOpts allows the filtering and sorting of paginated collections through
+// the API. Marker and Limit are used for pagination.
+type ListOpts struct {
+ Marker string `q:"marker"`
+ Limit int `q:"limit"`
+}
+
+// ToCDNServiceListQuery formats a ListOpts into a query string.
+func (opts ListOpts) ToCDNServiceListQuery() (string, error) {
+ q, err := gophercloud.BuildQueryString(opts)
+ if err != nil {
+ return "", err
+ }
+ return q.String(), nil
+}
+
+// List returns a Pager which allows you to iterate over a collection of
+// CDN services. It accepts a ListOpts struct, which allows for pagination via
+// marker and limit.
+func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
+ url := listURL(c)
+ if opts != nil {
+ query, err := opts.ToCDNServiceListQuery()
+ if err != nil {
+ return pagination.Pager{Err: err}
+ }
+ url += query
+ }
+
+ createPage := func(r pagination.PageResult) pagination.Page {
+ p := ServicePage{pagination.MarkerPageBase{PageResult: r}}
+ p.MarkerPageBase.Owner = p
+ return p
+ }
+
+ pager := pagination.NewPager(c, url, createPage)
+ return pager
+}
+
+// CreateOptsBuilder is the interface options structs have to satisfy in order
+// to be used in the main Create operation in this package. Since many
+// extensions decorate or modify the common logic, it is useful for them to
+// satisfy a basic interface in order for them to be used.
+type CreateOptsBuilder interface {
+ ToCDNServiceCreateMap() (map[string]interface{}, error)
+}
+
+// CreateOpts is the common options struct used in this package's Create
+// operation.
+type CreateOpts struct {
+ // REQUIRED. Specifies the name of the service. The minimum length for name is
+ // 3. The maximum length is 256.
+ Name string
+ // REQUIRED. Specifies a list of domains used by users to access their website.
+ Domains []Domain
+ // REQUIRED. Specifies a list of origin domains or IP addresses where the
+ // original assets are stored.
+ Origins []Origin
+ // REQUIRED. Specifies the CDN provider flavor ID to use. For a list of
+ // flavors, see the operation to list the available flavors. The minimum
+ // length for flavor_id is 1. The maximum length is 256.
+ FlavorID string
+ // OPTIONAL. Specifies the TTL rules for the assets under this service. Supports wildcards for fine-grained control.
+ Caching []Cache
+ // OPTIONAL. Specifies the restrictions that define who can access assets (content from the CDN cache).
+ Restrictions []Restriction
+}
+
+// ToCDNServiceCreateMap casts a CreateOpts struct to a map.
+func (opts CreateOpts) ToCDNServiceCreateMap() (map[string]interface{}, error) {
+ s := make(map[string]interface{})
+
+ if opts.Name == "" {
+ return nil, no("Name")
+ }
+ s["name"] = opts.Name
+
+ if opts.Domains == nil {
+ return nil, no("Domains")
+ }
+ for _, domain := range opts.Domains {
+ if domain.Domain == "" {
+ return nil, no("Domains[].Domain")
+ }
+ }
+ s["domains"] = opts.Domains
+
+ if opts.Origins == nil {
+ return nil, no("Origins")
+ }
+ for _, origin := range opts.Origins {
+ if origin.Origin == "" {
+ return nil, no("Origins[].Origin")
+ }
+ if origin.Rules == nil {
+ return nil, no("Origins[].Rules")
+ }
+ for _, rule := range origin.Rules {
+ if rule.Name == "" {
+ return nil, no("Origins[].Rules[].Name")
+ }
+ if rule.RequestURL == "" {
+ return nil, no("Origins[].Rules[].RequestURL")
+ }
+ }
+ }
+ s["origins"] = opts.Origins
+
+ if opts.FlavorID == "" {
+ return nil, no("FlavorID")
+ }
+ s["flavor_id"] = opts.FlavorID
+
+ if opts.Caching != nil {
+ for _, cache := range opts.Caching {
+ if cache.Name == "" {
+ return nil, no("Caching[].Name")
+ }
+ if cache.Rules != nil {
+ for _, rule := range cache.Rules {
+ if rule.Name == "" {
+ return nil, no("Caching[].Rules[].Name")
+ }
+ if rule.RequestURL == "" {
+ return nil, no("Caching[].Rules[].RequestURL")
+ }
+ }
+ }
+ }
+ s["caching"] = opts.Caching
+ }
+
+ if opts.Restrictions != nil {
+ for _, restriction := range opts.Restrictions {
+ if restriction.Name == "" {
+ return nil, no("Restrictions[].Name")
+ }
+ if restriction.Rules != nil {
+ for _, rule := range restriction.Rules {
+ if rule.Name == "" {
+ return nil, no("Restrictions[].Rules[].Name")
+ }
+ }
+ }
+ }
+ s["restrictions"] = opts.Restrictions
+ }
+
+ return s, nil
+}
+
+// Create accepts a CreateOpts struct and creates a new CDN service using the
+// values provided.
+func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
+ var res CreateResult
+
+ reqBody, err := opts.ToCDNServiceCreateMap()
+ if err != nil {
+ res.Err = err
+ return res
+ }
+
+ // Send request to API
+ _, res.Err = perigee.Request("POST", createURL(c), perigee.Options{
+ MoreHeaders: c.AuthenticatedHeaders(),
+ ReqBody: &reqBody,
+ OkCodes: []int{202},
+ })
+ return res
+}
+
+// Get retrieves a specific service based on its unique ID.
+func Get(c *gophercloud.ServiceClient, id string) GetResult {
+ var res GetResult
+ _, res.Err = perigee.Request("GET", getURL(c, id), perigee.Options{
+ MoreHeaders: c.AuthenticatedHeaders(),
+ Results: &res.Body,
+ OkCodes: []int{200},
+ })
+ return res
+}
+
+// UpdateOptsBuilder is the interface options structs have to satisfy in order
+// to be used in the main Update operation in this package. Since many
+// extensions decorate or modify the common logic, it is useful for them to
+// satisfy a basic interface in order for them to be used.
+type UpdateOptsBuilder interface {
+ ToCDNServiceUpdateMap() (map[string]interface{}, error)
+}
+
+// Op represents an update operation.
+type Op string
+
+var (
+ // Add is a constant used for performing a "add" operation when updating.
+ Add Op = "add"
+ // Remove is a constant used for performing a "remove" operation when updating.
+ Remove Op = "remove"
+ // Replace is a constant used for performing a "replace" operation when updating.
+ Replace Op = "replace"
+)
+
+// UpdateOpts represents the attributes used when updating an existing CDN service.
+type UpdateOpts []UpdateOpt
+
+// UpdateOpt represents a single update to an existing service. Multiple updates
+// to a service can be submitted at the same time. See UpdateOpts.
+type UpdateOpt struct {
+ // Specifies the update operation to perform.
+ Op Op
+ // Specifies the JSON Pointer location within the service's JSON representation
+ // of the service parameter being added, replaced or removed.
+ Path string
+ // Specifies the actual value to be added or replaced. It is not required for
+ // the remove operation.
+ Value map[string]interface{}
+}
+
+// ToCDNServiceUpdateMap casts an UpdateOpts struct to a map.
+func (opts UpdateOpts) ToCDNServiceUpdateMap() ([]UpdateOpt, error) {
+ s := make([]UpdateOpt, len(opts))
+
+ for i, opt := range opts {
+ if opt.Op == "" {
+ return nil, no("Op")
+ }
+ if opt.Path == "" {
+ return nil, no("Path")
+ }
+ if opt.Op != Remove && opt.Value == nil {
+ return nil, no("Value")
+ }
+ s[i] = opt
+ }
+
+ return s, nil
+}
+
+// Update accepts a UpdateOpts struct and updates an existing CDN service using
+// the values provided.
+func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
+ var res UpdateResult
+
+ reqBody, err := opts.ToCDNServiceUpdateMap()
+ if err != nil {
+ res.Err = err
+ return res
+ }
+
+ _, res.Err = perigee.Request("PATCH", updateURL(c, id), perigee.Options{
+ MoreHeaders: c.AuthenticatedHeaders(),
+ ReqBody: &reqBody,
+ OkCodes: []int{202},
+ })
+ return res
+}
+
+// Delete accepts a unique ID and deletes the CDN service associated with it.
+func Delete(c *gophercloud.ServiceClient, id string) DeleteResult {
+ var res DeleteResult
+ _, res.Err = perigee.Request("DELETE", deleteURL(c, id), perigee.Options{
+ MoreHeaders: c.AuthenticatedHeaders(),
+ OkCodes: []int{202},
+ })
+ return res
+}
diff --git a/openstack/cdn/v1/services/results.go b/openstack/cdn/v1/services/results.go
new file mode 100644
index 0000000..f739102
--- /dev/null
+++ b/openstack/cdn/v1/services/results.go
@@ -0,0 +1,188 @@
+package services
+
+import (
+ "github.com/mitchellh/mapstructure"
+ "github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/pagination"
+)
+
+// Domain represents a domain used by users to access their website.
+type Domain struct {
+ // Specifies the domain used to access the assets on their website, for which
+ // a CNAME is given to the CDN provider.
+ Domain string `mapstructure:"domain"`
+ // Specifies the protocol used to access the assets on this domain. Only "http"
+ // or "https" are currently allowed. The default is "http".
+ Protocol string `mapstructure:"protocol"`
+}
+
+// OriginRule represents a rule that defines when an origin should be accessed.
+type OriginRule struct {
+ // Specifies the name of this rule.
+ Name string `mapstructure:"name"`
+ // Specifies the request URL this rule should match for this origin to be used. Regex is supported.
+ RequestURL string `mapstructure:"request_url"`
+}
+
+// Origin specifies a list of origin domains or IP addresses where the original assets are stored.
+type Origin struct {
+ // Specifies the URL or IP address to pull origin content from.
+ Origin string `mapstructure:"origin"`
+ // Specifies the port used to access the origin. The default is port 80.
+ Port int `mapstructure:"port"`
+ // Specifies whether or not to use HTTPS to access the origin. The default
+ // is false.
+ SSL bool `mapstructure:"ssl"`
+ // Specifies a collection of rules that define the conditions when this origin
+ // should be accessed. If there is more than one origin, the rules parameter is required.
+ Rules []OriginRule `mapstructure:"rules"`
+}
+
+// CacheRule specifies a rule that determines if a TTL should be applied to an asset.
+type CacheRule struct {
+ // Specifies the name of this rule.
+ Name string `mapstructure:"name"`
+ // Specifies the request URL this rule should match for this TTL to be used. Regex is supported.
+ RequestURL string `mapstructure:"request_url"`
+}
+
+// Cache specifies the TTL rules for the assets under this service.
+type Cache struct {
+ // Specifies the name of this caching rule. Note: 'default' is a reserved name used for the default TTL setting.
+ Name string `mapstructure:"name"`
+ // Specifies the TTL to apply.
+ TTL int `mapstructure:"ttl"`
+ // Specifies a collection of rules that determine if this TTL should be applied to an asset.
+ Rules []CacheRule `mapstructure:"rules"`
+}
+
+// RestrictionRule specifies a rule that determines if this restriction should be applied to an asset.
+type RestrictionRule struct {
+ // Specifies the name of this rule.
+ Name string `mapstructure:"name"`
+ // Specifies the http host that requests must come from.
+ Referrer string `mapstructure:"referrer"`
+}
+
+// Restriction specifies a restriction that defines who can access assets (content from the CDN cache).
+type Restriction struct {
+ // Specifies the name of this restriction.
+ Name string `mapstructure:"name"`
+ // Specifies a collection of rules that determine if this TTL should be applied to an asset.
+ Rules []RestrictionRule `mapstructure:"rules"`
+}
+
+// Error specifies an error that occurred during the previous service action.
+type Error struct {
+ // Specifies an error message detailing why there is an error.
+ Message string `mapstructure:"message"`
+}
+
+// Service represents a CDN service resource.
+type Service struct {
+ // Specifies the service ID that represents distributed content. The value is
+ // a UUID, such as 96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0, that is generated by the server.
+ ID string `mapstructure:"id"`
+ // Specifies the name of the service.
+ Name string `mapstructure:"name"`
+ // Specifies a list of domains used by users to access their website.
+ Domains []Domain `mapstructure:"domains"`
+ // Specifies a list of origin domains or IP addresses where the original assets are stored.
+ Origins []Origin `mapstructure:"origins"`
+ // Specifies the TTL rules for the assets under this service. Supports wildcards for fine grained control.
+ Caching []Cache `mapstructure:"caching"`
+ // Specifies the restrictions that define who can access assets (content from the CDN cache).
+ Restrictions []Restriction `mapstructure:"restrictions"`
+ // Specifies the CDN provider flavor ID to use. For a list of flavors, see the operation to list the available flavors.
+ FlavorID string `mapstructure:"flavor_id"`
+ // Specifies the current status of the service.
+ Status string `mapstructure:"status"`
+ // Specifies the list of errors that occurred during the previous service action.
+ Errors []Error `mapstructure:"errors"`
+ // Specifies the self-navigating JSON document paths.
+ Links []gophercloud.Link `mapstructure:"links"`
+}
+
+// ServicePage is the page returned by a pager when traversing over a
+// collection of CDN services.
+type ServicePage struct {
+ pagination.MarkerPageBase
+}
+
+// IsEmpty returns true if a ListResult contains no services.
+func (r ServicePage) IsEmpty() (bool, error) {
+ services, err := ExtractServices(r)
+ if err != nil {
+ return true, err
+ }
+ return len(services) == 0, nil
+}
+
+// LastMarker returns the last service in a ListResult.
+func (r ServicePage) LastMarker() (string, error) {
+ services, err := ExtractServices(r)
+ if err != nil {
+ return "", err
+ }
+ if len(services) == 0 {
+ return "", nil
+ }
+ return (services[len(services)-1]).ID, nil
+}
+
+// ExtractServices is a function that takes a ListResult and returns the services' information.
+func ExtractServices(page pagination.Page) ([]Service, error) {
+ untyped := page.(ServicePage).Body.([]interface{})
+ results := make([]Service, len(untyped))
+ for index, each := range untyped {
+ service := each.(map[string]interface{})
+ err := mapstructure.Decode(service, &results[index])
+ if err != nil {
+ return results, err
+ }
+ }
+ return results, nil
+}
+
+// CreateResult represents the result of a Create operation.
+type CreateResult struct {
+ gophercloud.HeaderResult
+}
+
+// Extract is a method that extracts the location of a newly created service.
+func (cr CreateResult) Extract() string {
+ return cr.Header["Location"][0]
+}
+
+// GetResult represents the result of a get operation.
+type GetResult struct {
+ gophercloud.Result
+}
+
+// Extract is a function that extracts a service from a GetResult.
+func (r GetResult) Extract() (*Service, error) {
+ if r.Err != nil {
+ return nil, r.Err
+ }
+
+ var res Service
+
+ err := mapstructure.Decode(r.Body, &res)
+
+ return &res, err
+}
+
+// UpdateResult represents the result of a Update operation.
+type UpdateResult struct {
+ gophercloud.HeaderResult
+}
+
+// Extract is a method that extracts the location of an updated service.
+func (ur UpdateResult) Extract() string {
+ return ur.Header["Location"][0]
+}
+
+// DeleteResult represents the result of a Delete operation.
+type DeleteResult struct {
+ gophercloud.ErrResult
+}
diff --git a/openstack/cdn/v1/services/urls.go b/openstack/cdn/v1/services/urls.go
new file mode 100644
index 0000000..d953d4c
--- /dev/null
+++ b/openstack/cdn/v1/services/urls.go
@@ -0,0 +1,23 @@
+package services
+
+import "github.com/rackspace/gophercloud"
+
+func listURL(c *gophercloud.ServiceClient) string {
+ return c.ServiceURL("services")
+}
+
+func createURL(c *gophercloud.ServiceClient) string {
+ return listURL(c)
+}
+
+func getURL(c *gophercloud.ServiceClient, id string) string {
+ return c.ServiceURL("services", id)
+}
+
+func updateURL(c *gophercloud.ServiceClient, id string) string {
+ return getURL(c, id)
+}
+
+func deleteURL(c *gophercloud.ServiceClient, id string) string {
+ return getURL(c, id)
+}