From 951655b0d661fac6d9292118239cb4d603748ba0 Mon Sep 17 00:00:00 2001 From: ShiChangkuo Date: Fri, 1 Nov 2019 16:56:33 +0800 Subject: [PATCH] Add CDN client and domain APIs Add CDN client and domain APIs, including create, delete, get, enable and disable. Signed-off-by: ShiChangkuo --- openstack/cdn/v1/domains/requests.go | 125 ++++++++++++++++++++++++ openstack/cdn/v1/domains/results.go | 139 +++++++++++++++++++++++++++ openstack/cdn/v1/domains/urls.go | 27 ++++++ openstack/client.go | 7 +- 4 files changed, 296 insertions(+), 2 deletions(-) create mode 100644 openstack/cdn/v1/domains/requests.go create mode 100644 openstack/cdn/v1/domains/results.go create mode 100644 openstack/cdn/v1/domains/urls.go diff --git a/openstack/cdn/v1/domains/requests.go b/openstack/cdn/v1/domains/requests.go new file mode 100644 index 000000000..fe315a09c --- /dev/null +++ b/openstack/cdn/v1/domains/requests.go @@ -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 +} diff --git a/openstack/cdn/v1/domains/results.go b/openstack/cdn/v1/domains/results.go new file mode 100644 index 000000000..423ebd2a2 --- /dev/null +++ b/openstack/cdn/v1/domains/results.go @@ -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 +} diff --git a/openstack/cdn/v1/domains/urls.go b/openstack/cdn/v1/domains/urls.go new file mode 100644 index 000000000..b4e3a20bb --- /dev/null +++ b/openstack/cdn/v1/domains/urls.go @@ -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") +} diff --git a/openstack/client.go b/openstack/client.go index c6202dadc..35543995e 100644 --- a/openstack/client.go +++ b/openstack/client.go @@ -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