Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions openstack/cdn/v1/domains/requests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package domains

import (
"github.com/huaweicloud/golangsdk"
)

// ExtensionOpts allows extensions to add parameters to some requests
// the possible requests include get,delete,enable or disable.
type ExtensionOpts struct {
// specifies the enterprise_project_id.
EnterpriseProjectId string `q:"enterprise_project_id"`
}

// ToExtensionQuery formats a ExtensionOpts into a query string.
func (opts ExtensionOpts) ToExtensionQuery() (string, error) {
q, err := golangsdk.BuildQueryString(opts)
return q.String(), err
}

// SourcesOpts specifies the domain name or the IP address of the origin server
type SourcesOpts struct {
IporDomain string `json:"ip_or_domain" required:"true"`
OriginType string `json:"origin_type" required:"true"`
ActiveStandby int `json:"active_standby" required:"true"`
}

// CreateOpts specifies the attributes used to create a CDN domain.
type CreateOpts struct {
// the acceleration domain name, the length of a label is within 50 characters.
DomainName string `json:"domain_name" required:"true"`
// the service type, valid values are web, downlaod and video
BusinessType string `json:"business_type" required:"true"`
// the domain name or the IP address of the origin server
Sources []SourcesOpts `json:"sources" required:"true"`
// the enterprise project ID
EnterpriseProjectId string `json:"enterprise_project_id,omitempty"`
}

// CreateOptsBuilder allows extensions to add additional parameters to the
// Create request.
type CreateOptsBuilder interface {
ToCdnDomainCreateMap() (map[string]interface{}, error)
}

// ToCdnDomainCreateMap assembles a request body based on the contents of a
// CreateOpts.
func (opts CreateOpts) ToCdnDomainCreateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "domain")
}

// Create implements a CDN domain create request.
func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
reqBody, err := opts.ToCdnDomainCreateMap()
if err != nil {
r.Err = err
return
}

_, r.Err = client.Post(createURL(client), reqBody, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}})
return
}

// Get retrieves a particular CDN domain based on its unique ID.
func Get(client *golangsdk.ServiceClient, id string, opts *ExtensionOpts) (r GetResult) {
url := getURL(client, id)
if opts != nil {
query, err := opts.ToExtensionQuery()
if err != nil {
r.Err = err
return
}
url += query
}
_, r.Err = client.Get(url, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}})
return
}

// Delete requests a CDN domain to be deleted to the user in the current tenant.
func Delete(client *golangsdk.ServiceClient, id string, opts *ExtensionOpts) (r DeleteResult) {
url := deleteURL(client, id)
if opts != nil {
query, err := opts.ToExtensionQuery()
if err != nil {
r.Err = err
return
}
url += query
}

// Delete requests will response 'domain' body, so we use DeleteWithResponse
_, r.Err = client.DeleteWithResponse(url, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}})
return
}

// Enable implements a CDN domain enable request.
func Enable(client *golangsdk.ServiceClient, id string, opts *ExtensionOpts) (r EnableResult) {
url := enableURL(client, id)
if opts != nil {
query, err := opts.ToExtensionQuery()
if err != nil {
r.Err = err
return
}
url += query
}

_, r.Err = client.Put(url, nil, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}})
return
}

// Disable implements a CDN domain disable request.
func Disable(client *golangsdk.ServiceClient, id string, opts *ExtensionOpts) (r DisableResult) {
url := disableURL(client, id)
if opts != nil {
query, err := opts.ToExtensionQuery()
if err != nil {
r.Err = err
return
}
url += query
}

_, r.Err = client.Put(url, nil, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}})
return
}
139 changes: 139 additions & 0 deletions openstack/cdn/v1/domains/results.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package domains

import (
"fmt"
"time"

"github.com/huaweicloud/golangsdk"
)

// sources
type DomainSources struct {
DomainID string `json:"domain_id"`
IporDomain string `json:"ip_or_domain"`
OriginType string `json:"origin_type"`
ActiveStandby int `json:"active_standby"`
}

// domain_origin_host
type DomainOriginHost struct {
DomainID string `json:"domain_id"`
OriginHostType string `json:"origin_host_type"`
CustomizeDomain string `json:"customize_domain"`
}

// CdnDomain represents a CDN domain
type CdnDomain struct {
// the acceleration domain name ID
ID string `json:"id"`
// the acceleration domain name
DomainName string `json:"domain_name"`
// the service type, valid values are web, download, video
BusinessType string `json:"business_type"`
// the domain ID of the domain name's owner
UserDomainId string `json:"user_domain_id"`
// the status of the acceleration domain name.
DomainStatus string `json:"domain_status"`
// the CNAME of the acceleration domain name
CName string `json:"cname"`
// the domain name or the IP address of the origin server
Sources []DomainSources `json:"sources"`
// the configuration information of the retrieval host
OriginHost DomainOriginHost `json:"domain_origin_host"`
// whether the HTTPS certificate is enabled
HttpsStatus *int `json:"https_status"`
// whether the status is disabled
Disabled *int `json:"disabled"`
// whether the status is locked
Locked *int `json:"locked"`
// the area covered by the accelecration service
ServiceArea string `json:"service_area"`
// whether range-based retrieval is enabled
RangeStatus string `json:"range_status"`
// a thrid-party CDN
ThridPartCDN string `json:"third_part_cdn"`
// the id of enterprise project
EnterpriseProjectId string `json:"enterprise_project_id"`

CreateTime time.Time `json:"-"`
ModifyTime time.Time `json:"-"`
}

type commonResult struct {
golangsdk.Result
}

// GetResult is the response from a Get operation. Call its Extract
// method to interpret it as a CDN domain.
type GetResult struct {
commonResult
}

func (r GetResult) Extract() (*CdnDomain, error) {
var domain CdnDomain
err := r.ExtractInto(&domain)

// the get request API will response OK, even if errors occurrred.
// so we judge domain whether is existing
if err == nil && domain.DomainName == "" {
err = fmt.Errorf("The CDN domain does not exist.")
}
return &domain, err
}

func (r GetResult) ExtractInto(v interface{}) error {
return r.Result.ExtractIntoStructPtr(v, "domain")
}

// CreateResult is the result of a Create request.
type CreateResult struct {
commonResult
}

func (r CreateResult) Extract() (*CdnDomain, error) {
var domain CdnDomain
err := r.ExtractInto(&domain)

// the create request API will response OK, even if errors occurrred.
// so we judge domain whether is existing
if err == nil && domain.DomainStatus != "configuring" {
err = fmt.Errorf("%v", r.Body)
}
return &domain, err
}

func (r CreateResult) ExtractInto(v interface{}) error {
return r.Result.ExtractIntoStructPtr(v, "domain")
}

// DeleteResult is the result of a Delete request. Call its ExtractErr method
// to determine if the request succeeded or failed.
type DeleteResult struct {
commonResult
}

func (r DeleteResult) Extract() (*CdnDomain, error) {
var domain CdnDomain
err := r.ExtractInto(&domain)

// the delete request API will response OK, even if errors occurrred.
// so we judge domain whether is existing
if err == nil && domain.DomainStatus != "deleting" {
err = fmt.Errorf("%v", r.Body)
}
return &domain, err
}

func (r DeleteResult) ExtractInto(v interface{}) error {
return r.Result.ExtractIntoStructPtr(v, "domain")
}

// EnableResult is the result of a Enable request.
type EnableResult struct {
commonResult
}

// DisableResult is the result of a Disable request.
type DisableResult struct {
commonResult
}
27 changes: 27 additions & 0 deletions openstack/cdn/v1/domains/urls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package domains

import "github.com/huaweicloud/golangsdk"

const (
rootPath = "cdn/domains"
)

func createURL(sc *golangsdk.ServiceClient) string {
return sc.ServiceURL(rootPath)
}

func deleteURL(sc *golangsdk.ServiceClient, domainId string) string {
return sc.ServiceURL(rootPath, domainId)
}

func getURL(sc *golangsdk.ServiceClient, domainId string) string {
return sc.ServiceURL(rootPath, domainId, "detail")
}

func enableURL(sc *golangsdk.ServiceClient, domainId string) string {
return sc.ServiceURL(rootPath, domainId, "enable")
}

func disableURL(sc *golangsdk.ServiceClient, domainId string) string {
return sc.ServiceURL(rootPath, domainId, "disable")
}
7 changes: 5 additions & 2 deletions openstack/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,10 +592,13 @@ func NewSharedFileSystemV2(client *golangsdk.ProviderClient, eo golangsdk.Endpoi
return initClientOpts(client, eo, "sharev2")
}

// NewCDNV1 creates a ServiceClient that may be used to access the OpenStack v1
// NewCDNV1 creates a ServiceClient that may be used to access the v1
// CDN service.
func NewCDNV1(client *golangsdk.ProviderClient, eo golangsdk.EndpointOpts) (*golangsdk.ServiceClient, error) {
return initClientOpts(client, eo, "cdn")
sc, err := initClientOpts(client, eo, "network")
sc.Endpoint = "https://cdn.myhuaweicloud.com/"
sc.ResourceBase = sc.Endpoint + "v1.0/"
return sc, err
}

// NewOrchestrationV1 creates a ServiceClient that may be used to access the v1
Expand Down