github.com/gophercloud/gophercloud@v1.11.0/openstack/cdn/v1/services/results.go (about)

     1  package services
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/pagination"
     6  )
     7  
     8  // Domain represents a domain used by users to access their website.
     9  type Domain struct {
    10  	// Specifies the domain used to access the assets on their website, for which
    11  	// a CNAME is given to the CDN provider.
    12  	Domain string `json:"domain" required:"true"`
    13  	// Specifies the protocol used to access the assets on this domain. Only "http"
    14  	// or "https" are currently allowed. The default is "http".
    15  	Protocol string `json:"protocol,omitempty"`
    16  }
    17  
    18  func (d Domain) toPatchValue() interface{} {
    19  	r := make(map[string]interface{})
    20  	r["domain"] = d.Domain
    21  	if d.Protocol != "" {
    22  		r["protocol"] = d.Protocol
    23  	}
    24  	return r
    25  }
    26  
    27  func (d Domain) appropriatePath() Path {
    28  	return PathDomains
    29  }
    30  
    31  func (d Domain) renderRootOr(render func(p Path) string) string {
    32  	return render(d.appropriatePath())
    33  }
    34  
    35  // DomainList provides a useful way to perform bulk operations in a single Patch.
    36  type DomainList []Domain
    37  
    38  func (list DomainList) toPatchValue() interface{} {
    39  	r := make([]interface{}, len(list))
    40  	for i, domain := range list {
    41  		r[i] = domain.toPatchValue()
    42  	}
    43  	return r
    44  }
    45  
    46  func (list DomainList) appropriatePath() Path {
    47  	return PathDomains
    48  }
    49  
    50  func (list DomainList) renderRootOr(_ func(p Path) string) string {
    51  	return list.appropriatePath().renderRoot()
    52  }
    53  
    54  // OriginRule represents a rule that defines when an origin should be accessed.
    55  type OriginRule struct {
    56  	// Specifies the name of this rule.
    57  	Name string `json:"name" required:"true"`
    58  	// Specifies the request URL this rule should match for this origin to be used. Regex is supported.
    59  	RequestURL string `json:"request_url" required:"true"`
    60  }
    61  
    62  // Origin specifies a list of origin domains or IP addresses where the original assets are stored.
    63  type Origin struct {
    64  	// Specifies the URL or IP address to pull origin content from.
    65  	Origin string `json:"origin" required:"true"`
    66  	// Specifies the port used to access the origin. The default is port 80.
    67  	Port int `json:"port,omitempty"`
    68  	// Specifies whether or not to use HTTPS to access the origin. The default
    69  	// is false.
    70  	SSL bool `json:"ssl"`
    71  	// Specifies a collection of rules that define the conditions when this origin
    72  	// should be accessed. If there is more than one origin, the rules parameter is required.
    73  	Rules []OriginRule `json:"rules,omitempty"`
    74  }
    75  
    76  func (o Origin) toPatchValue() interface{} {
    77  	r := make(map[string]interface{})
    78  	r["origin"] = o.Origin
    79  	r["port"] = o.Port
    80  	r["ssl"] = o.SSL
    81  	if len(o.Rules) > 0 {
    82  		r["rules"] = make([]map[string]interface{}, len(o.Rules))
    83  		for index, rule := range o.Rules {
    84  			submap := r["rules"].([]map[string]interface{})[index]
    85  			submap["name"] = rule.Name
    86  			submap["request_url"] = rule.RequestURL
    87  		}
    88  	}
    89  	return r
    90  }
    91  
    92  func (o Origin) appropriatePath() Path {
    93  	return PathOrigins
    94  }
    95  
    96  func (o Origin) renderRootOr(render func(p Path) string) string {
    97  	return render(o.appropriatePath())
    98  }
    99  
   100  // OriginList provides a useful way to perform bulk operations in a single Patch.
   101  type OriginList []Origin
   102  
   103  func (list OriginList) toPatchValue() interface{} {
   104  	r := make([]interface{}, len(list))
   105  	for i, origin := range list {
   106  		r[i] = origin.toPatchValue()
   107  	}
   108  	return r
   109  }
   110  
   111  func (list OriginList) appropriatePath() Path {
   112  	return PathOrigins
   113  }
   114  
   115  func (list OriginList) renderRootOr(_ func(p Path) string) string {
   116  	return list.appropriatePath().renderRoot()
   117  }
   118  
   119  // TTLRule specifies a rule that determines if a TTL should be applied to an asset.
   120  type TTLRule struct {
   121  	// Specifies the name of this rule.
   122  	Name string `json:"name" required:"true"`
   123  	// Specifies the request URL this rule should match for this TTL to be used. Regex is supported.
   124  	RequestURL string `json:"request_url" required:"true"`
   125  }
   126  
   127  // CacheRule specifies the TTL rules for the assets under this service.
   128  type CacheRule struct {
   129  	// Specifies the name of this caching rule. Note: 'default' is a reserved name used for the default TTL setting.
   130  	Name string `json:"name" required:"true"`
   131  	// Specifies the TTL to apply.
   132  	TTL int `json:"ttl,omitempty"`
   133  	// Specifies a collection of rules that determine if this TTL should be applied to an asset.
   134  	Rules []TTLRule `json:"rules,omitempty"`
   135  }
   136  
   137  func (c CacheRule) toPatchValue() interface{} {
   138  	r := make(map[string]interface{})
   139  	r["name"] = c.Name
   140  	r["ttl"] = c.TTL
   141  	r["rules"] = make([]map[string]interface{}, len(c.Rules))
   142  	for index, rule := range c.Rules {
   143  		submap := r["rules"].([]map[string]interface{})[index]
   144  		submap["name"] = rule.Name
   145  		submap["request_url"] = rule.RequestURL
   146  	}
   147  	return r
   148  }
   149  
   150  func (c CacheRule) appropriatePath() Path {
   151  	return PathCaching
   152  }
   153  
   154  func (c CacheRule) renderRootOr(render func(p Path) string) string {
   155  	return render(c.appropriatePath())
   156  }
   157  
   158  // CacheRuleList provides a useful way to perform bulk operations in a single Patch.
   159  type CacheRuleList []CacheRule
   160  
   161  func (list CacheRuleList) toPatchValue() interface{} {
   162  	r := make([]interface{}, len(list))
   163  	for i, rule := range list {
   164  		r[i] = rule.toPatchValue()
   165  	}
   166  	return r
   167  }
   168  
   169  func (list CacheRuleList) appropriatePath() Path {
   170  	return PathCaching
   171  }
   172  
   173  func (list CacheRuleList) renderRootOr(_ func(p Path) string) string {
   174  	return list.appropriatePath().renderRoot()
   175  }
   176  
   177  // RestrictionRule specifies a rule that determines if this restriction should be applied to an asset.
   178  type RestrictionRule struct {
   179  	// Specifies the name of this rule.
   180  	Name string `json:"name" required:"true"`
   181  	// Specifies the http host that requests must come from.
   182  	Referrer string `json:"referrer,omitempty"`
   183  }
   184  
   185  // Restriction specifies a restriction that defines who can access assets (content from the CDN cache).
   186  type Restriction struct {
   187  	// Specifies the name of this restriction.
   188  	Name string `json:"name" required:"true"`
   189  	// Specifies a collection of rules that determine if this TTL should be applied to an asset.
   190  	Rules []RestrictionRule `json:"rules,omitempty"`
   191  }
   192  
   193  // Error specifies an error that occurred during the previous service action.
   194  type Error struct {
   195  	// Specifies an error message detailing why there is an error.
   196  	Message string `json:"message"`
   197  }
   198  
   199  // Service represents a CDN service resource.
   200  type Service struct {
   201  	// Specifies the service ID that represents distributed content. The value is
   202  	// a UUID, such as 96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0, that is generated by the server.
   203  	ID string `json:"id"`
   204  	// Specifies the name of the service.
   205  	Name string `json:"name"`
   206  	// Specifies a list of domains used by users to access their website.
   207  	Domains []Domain `json:"domains"`
   208  	// Specifies a list of origin domains or IP addresses where the original assets are stored.
   209  	Origins []Origin `json:"origins"`
   210  	// Specifies the TTL rules for the assets under this service. Supports wildcards for fine grained control.
   211  	Caching []CacheRule `json:"caching"`
   212  	// Specifies the restrictions that define who can access assets (content from the CDN cache).
   213  	Restrictions []Restriction `json:"restrictions"`
   214  	// Specifies the CDN provider flavor ID to use. For a list of flavors, see the operation to list the available flavors.
   215  	FlavorID string `json:"flavor_id"`
   216  	// Specifies the current status of the service.
   217  	Status string `json:"status"`
   218  	// Specifies the list of errors that occurred during the previous service action.
   219  	Errors []Error `json:"errors"`
   220  	// Specifies the self-navigating JSON document paths.
   221  	Links []gophercloud.Link `json:"links"`
   222  }
   223  
   224  // ServicePage is the page returned by a pager when traversing over a
   225  // collection of CDN services.
   226  type ServicePage struct {
   227  	pagination.MarkerPageBase
   228  }
   229  
   230  // IsEmpty returns true if a ListResult contains no services.
   231  func (r ServicePage) IsEmpty() (bool, error) {
   232  	if r.StatusCode == 204 {
   233  		return true, nil
   234  	}
   235  
   236  	services, err := ExtractServices(r)
   237  	return len(services) == 0, err
   238  }
   239  
   240  // LastMarker returns the last service in a ListResult.
   241  func (r ServicePage) LastMarker() (string, error) {
   242  	services, err := ExtractServices(r)
   243  	if err != nil {
   244  		return "", err
   245  	}
   246  	if len(services) == 0 {
   247  		return "", nil
   248  	}
   249  	return (services[len(services)-1]).ID, nil
   250  }
   251  
   252  // ExtractServices is a function that takes a ListResult and returns the services' information.
   253  func ExtractServices(r pagination.Page) ([]Service, error) {
   254  	var s struct {
   255  		Services []Service `json:"services"`
   256  	}
   257  	err := (r.(ServicePage)).ExtractInto(&s)
   258  	return s.Services, err
   259  }
   260  
   261  // CreateResult represents the result of a Create operation.
   262  type CreateResult struct {
   263  	gophercloud.Result
   264  }
   265  
   266  // Extract is a method that extracts the location of a newly created service.
   267  func (r CreateResult) Extract() (string, error) {
   268  	if r.Err != nil {
   269  		return "", r.Err
   270  	}
   271  	if l, ok := r.Header["Location"]; ok && len(l) > 0 {
   272  		return l[0], nil
   273  	}
   274  	return "", nil
   275  }
   276  
   277  // GetResult represents the result of a get operation.
   278  type GetResult struct {
   279  	gophercloud.Result
   280  }
   281  
   282  // Extract is a function that extracts a service from a GetResult.
   283  func (r GetResult) Extract() (*Service, error) {
   284  	var s Service
   285  	err := r.ExtractInto(&s)
   286  	return &s, err
   287  }
   288  
   289  // UpdateResult represents the result of a Update operation.
   290  type UpdateResult struct {
   291  	gophercloud.Result
   292  }
   293  
   294  // Extract is a method that extracts the location of an updated service.
   295  func (r UpdateResult) Extract() (string, error) {
   296  	if r.Err != nil {
   297  		return "", r.Err
   298  	}
   299  	if l, ok := r.Header["Location"]; ok && len(l) > 0 {
   300  		return l[0], nil
   301  	}
   302  	return "", nil
   303  }
   304  
   305  // DeleteResult represents the result of a Delete operation.
   306  type DeleteResult struct {
   307  	gophercloud.ErrResult
   308  }