diff --git a/go.mod b/go.mod index 7c07882da0..637057e946 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/hashicorp/errwrap v1.0.0 github.com/hashicorp/go-cleanhttp v0.5.1 github.com/hashicorp/terraform-plugin-sdk v1.0.0 - github.com/huaweicloud/golangsdk v0.0.0-20191015074635-101cf928fd59 + github.com/huaweicloud/golangsdk v0.0.0-20191101095004-04427fad1e00 github.com/jen20/awspolicyequivalence v0.0.0-20170831201602-3d48364a137a github.com/mitchellh/go-homedir v1.1.0 github.com/smartystreets/assertions v0.0.0-20190116191733-b6c0e53d7304 // indirect diff --git a/go.sum b/go.sum index 7cac623d19..4ff756f16a 100644 --- a/go.sum +++ b/go.sum @@ -123,8 +123,8 @@ github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKe github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d h1:kJCB4vdITiW1eC1vq2e6IsrXKrZit1bv/TDYFGMp4BQ= github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= -github.com/huaweicloud/golangsdk v0.0.0-20191015074635-101cf928fd59 h1:niLc18KsKDSiCIEEJRtCwmjtGcgcjelHVysjyVSA7Fo= -github.com/huaweicloud/golangsdk v0.0.0-20191015074635-101cf928fd59/go.mod h1:WQBcHRNX9shz3928lWEvstQJtAtYI7ks6XlgtRT9Tcw= +github.com/huaweicloud/golangsdk v0.0.0-20191101095004-04427fad1e00 h1:Gw2+Mx0dVIgiCwZw1iSSIS9Za46VQBhD875dDtD62xg= +github.com/huaweicloud/golangsdk v0.0.0-20191101095004-04427fad1e00/go.mod h1:WQBcHRNX9shz3928lWEvstQJtAtYI7ks6XlgtRT9Tcw= github.com/jen20/awspolicyequivalence v0.0.0-20170831201602-3d48364a137a h1:FyS/ubzBR5xJlnJGRTwe7GUHpJOR4ukYK3y+LFNffuA= github.com/jen20/awspolicyequivalence v0.0.0-20170831201602-3d48364a137a/go.mod h1:uoIMjNxUfXi48Ci40IXkPRbghZ1vbti6v9LCbNqRgHY= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= diff --git a/huaweicloud/config.go b/huaweicloud/config.go index 8a671fcb07..7124c3f15f 100644 --- a/huaweicloud/config.go +++ b/huaweicloud/config.go @@ -660,6 +660,13 @@ func (c *Config) RdsV1Client(region string) (*golangsdk.ServiceClient, error) { }) } +func (c *Config) CdnV1Client(region string) (*golangsdk.ServiceClient, error) { + return huaweisdk.NewCDNV1(c.HwClient, golangsdk.EndpointOpts{ + Region: c.determineRegion(region), + Availability: c.getHwEndpointType(), + }) +} + func (c *Config) sdkClient(region, serviceType string, level string) (*golangsdk.ServiceClient, error) { client := c.HwClient if level == serviceDomainLevel { diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index 4342626317..e21112b77f 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -330,6 +330,7 @@ func Provider() terraform.ResourceProvider { "huaweicloud_cs_route_v1": resourceCsRouteV1(), "huaweicloud_networking_vip_v2": resourceNetworkingVIPV2(), "huaweicloud_networking_vip_associate_v2": resourceNetworkingVIPAssociateV2(), + "huaweicloud_cdn_domain_v1": resourceCdnDomainV1(), }, } diff --git a/huaweicloud/provider_test.go b/huaweicloud/provider_test.go index c9d3c3bbb9..13f592121f 100644 --- a/huaweicloud/provider_test.go +++ b/huaweicloud/provider_test.go @@ -36,6 +36,7 @@ var ( OS_DMS_ENVIRONMENT = os.Getenv("OS_DMS_ENVIRONMENT") OS_NAT_ENVIRONMENT = os.Getenv("OS_NAT_ENVIRONMENT") OS_KMS_ENVIRONMENT = os.Getenv("OS_KMS_ENVIRONMENT") + OS_CDN_DOMAIN_NAME = os.Getenv("OS_CDN_DOMAIN_NAME") ) var testAccProviders map[string]terraform.ResourceProvider @@ -198,6 +199,14 @@ func testAccPreCheckKms(t *testing.T) { } } +func testAccPreCheckCDN(t *testing.T) { + testAccPreCheckRequiredEnvVars(t) + + if OS_CDN_DOMAIN_NAME == "" { + t.Skip("This environment does not support CDN tests") + } +} + func TestProvider(t *testing.T) { if err := Provider().(*schema.Provider).InternalValidate(); err != nil { t.Fatalf("err: %s", err) diff --git a/huaweicloud/resource_huaweicloud_cdn_domain.go b/huaweicloud/resource_huaweicloud_cdn_domain.go new file mode 100644 index 0000000000..34dd695ea0 --- /dev/null +++ b/huaweicloud/resource_huaweicloud_cdn_domain.go @@ -0,0 +1,274 @@ +package huaweicloud + +import ( + "fmt" + "log" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/helper/validation" + "github.com/huaweicloud/golangsdk" + "github.com/huaweicloud/golangsdk/openstack/cdn/v1/domains" +) + +func resourceCdnDomainV1() *schema.Resource { + return &schema.Resource{ + Create: resourceCdnDomainV1Create, + Read: resourceCdnDomainV1Read, + Delete: resourceCdnDomainV1Delete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + + Timeouts: &schema.ResourceTimeout{ + Create: schema.DefaultTimeout(20 * time.Minute), + Delete: schema.DefaultTimeout(20 * time.Minute), + }, + + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "type": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice([]string{ + "web", "download", "video", + }, true), + }, + "sources": { + Type: schema.TypeList, + Required: true, + ForceNew: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "domain": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "domain_type": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice([]string{ + "ipaddr", "domain", "obs_bucket", + }, true), + }, + "active": { + Type: schema.TypeInt, + Computed: true, + ForceNew: false, + }, + }, + }, + }, + "enterprise_project_id": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "cname": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + Computed: true, + }, + "domain_status": { + Type: schema.TypeString, + Optional: true, + ForceNew: false, + Computed: true, + }, + "service_area": { + Type: schema.TypeString, + Optional: true, + ForceNew: false, + Computed: true, + }, + }, + } +} + +type WaitDomainStatus struct { + ID string + Penging []string + Target []string + Opts *domains.ExtensionOpts +} + +func getDomainSources(d *schema.ResourceData) []domains.SourcesOpts { + var sourceRequests []domains.SourcesOpts + + sources := d.Get("sources").([]interface{}) + for i := range sources { + source := sources[i].(map[string]interface{}) + sourceRequest := domains.SourcesOpts{ + IporDomain: source["domain"].(string), + OriginType: source["domain_type"].(string), + ActiveStandby: 1, + } + sourceRequests = append(sourceRequests, sourceRequest) + } + return sourceRequests +} + +func resourceCdnDomainV1Create(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + cdnClient, err := config.CdnV1Client(GetRegion(d, config)) + if err != nil { + return fmt.Errorf("Error creating HuaweiCloud CDN v1 client: %s", err) + } + + createOpts := &domains.CreateOpts{ + DomainName: d.Get("name").(string), + BusinessType: d.Get("type").(string), + Sources: getDomainSources(d), + EnterpriseProjectId: d.Get("enterprise_project_id").(string), + } + + log.Printf("[DEBUG] Create Options: %#v", createOpts) + v, err := domains.Create(cdnClient, createOpts).Extract() + if err != nil { + return fmt.Errorf("Error creating HuaweiCloud CDN Domain: %s", err) + } + + // Wait for CDN domain to become active again before continuing + log.Printf("[INFO] Waiting for CDN domain %s to become online.", v.ID) + wait := &WaitDomainStatus{ + ID: v.ID, + Penging: []string{"configuring"}, + Target: []string{"online"}, + Opts: getResourceExtensionOpts(d), + } + timeout := d.Timeout(schema.TimeoutCreate) + err = waitforCDNV1DomainStatus(cdnClient, wait, timeout) + if err != nil { + return fmt.Errorf("Error waiting for CDN domain %s to become online: %s", v.ID, err) + } + + // Store the ID now + d.SetId(v.ID) + + return resourceCdnDomainV1Read(d, meta) +} + +func waitforCDNV1DomainStatus(c *golangsdk.ServiceClient, waitstatus *WaitDomainStatus, timeout time.Duration) error { + stateConf := &resource.StateChangeConf{ + Pending: waitstatus.Penging, + Target: waitstatus.Target, + Refresh: resourceCDNV1DomainRefreshFunc(c, waitstatus.ID, waitstatus.Opts), + Timeout: timeout, + Delay: 5 * time.Second, + MinTimeout: 3 * time.Second, + } + + _, err := stateConf.WaitForState() + if err != nil { + return fmt.Errorf("Error waiting for CDN domain %s to become %s: %s", + waitstatus.ID, waitstatus.Target, err) + } + return nil +} + +func resourceCDNV1DomainRefreshFunc(c *golangsdk.ServiceClient, id string, opts *domains.ExtensionOpts) resource.StateRefreshFunc { + return func() (interface{}, string, error) { + domain, err := domains.Get(c, id, opts).Extract() + if err != nil { + return nil, "", err + } + + // return DomainStatus attribute of CDN domain resource + return domain, domain.DomainStatus, nil + } +} + +func resourceCdnDomainV1Read(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + cdnClient, err := config.CdnV1Client(GetRegion(d, config)) + if err != nil { + return fmt.Errorf("Error creating HuaweiCloud CDN v1 client: %s", err) + } + + opts := getResourceExtensionOpts(d) + v, err := domains.Get(cdnClient, d.Id(), opts).Extract() + if err != nil { + return fmt.Errorf("Error reading CDN Domain: %s", err) + } + + log.Printf("[DEBUG] Retrieved CDN domain %s: %+v", d.Id(), v) + + d.Set("name", v.DomainName) + d.Set("type", v.BusinessType) + d.Set("cname", v.CName) + d.Set("domain_status", v.DomainStatus) + d.Set("service_area", v.ServiceArea) + + // set sources + sources := make([]map[string]interface{}, len(v.Sources)) + for i, source := range v.Sources { + sources[i] = make(map[string]interface{}) + sources[i]["domain"] = source.IporDomain + sources[i]["domain_type"] = source.OriginType + sources[i]["active"] = source.ActiveStandby + } + d.Set("sources", sources) + + return nil +} + +func resourceCdnDomainV1Delete(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + cdnClient, err := config.CdnV1Client(GetRegion(d, config)) + if err != nil { + return fmt.Errorf("Error creating HuaweiCloud CDN v1 client: %s", err) + } + + id := d.Id() + opts := getResourceExtensionOpts(d) + timeout := d.Timeout(schema.TimeoutCreate) + + if d.Get("domain_status").(string) == "online" { + // make sure the status has changed to offline + log.Printf("[INFO] Disable CDN domain %s.", id) + if err = domains.Disable(cdnClient, id, opts).Err; err != nil { + return fmt.Errorf("Error disable HuaweiCloud CDN Domain %s: %s", id, err) + } + + log.Printf("[INFO] Waiting for disabling CDN domain %s.", id) + wait := &WaitDomainStatus{ + ID: id, + Penging: []string{"configuring", "online"}, + Target: []string{"offline"}, + Opts: opts, + } + + err = waitforCDNV1DomainStatus(cdnClient, wait, timeout) + if err != nil { + return fmt.Errorf("Error waiting for CDN domain %s to become offline: %s", id, err) + } + } + + log.Printf("[INFO] Waiting for deleting CDN domain %s.", id) + _, err = domains.Delete(cdnClient, id, opts).Extract() + if err != nil { + return fmt.Errorf("Error deleting CDN Domain %s: %s", id, err) + } + + d.SetId("") + return nil +} + +func getResourceExtensionOpts(d *schema.ResourceData) *domains.ExtensionOpts { + if hasFilledOpt(d, "enterprise_project_id") { + return &domains.ExtensionOpts{ + EnterpriseProjectId: d.Get("enterprise_project_id").(string), + } + } + + return nil +} diff --git a/huaweicloud/resource_huaweicloud_cdn_domain_test.go b/huaweicloud/resource_huaweicloud_cdn_domain_test.go new file mode 100644 index 0000000000..13cd56301b --- /dev/null +++ b/huaweicloud/resource_huaweicloud_cdn_domain_test.go @@ -0,0 +1,94 @@ +package huaweicloud + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/terraform" + "github.com/huaweicloud/golangsdk/openstack/cdn/v1/domains" +) + +func TestAccCdnDomain_basic(t *testing.T) { + var domain domains.CdnDomain + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheckCDN(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckCdnDomainV1Destroy, + Steps: []resource.TestStep{ + { + Config: testAccCdnDomainV1_basic, + Check: resource.ComposeTestCheckFunc( + testAccCheckCdnDomainV1Exists("huaweicloud_cdn_domain_v1.domain_1", &domain), + resource.TestCheckResourceAttr( + "huaweicloud_cdn_domain_v1.domain_1", "name", OS_CDN_DOMAIN_NAME), + ), + }, + }, + }) +} + +func testAccCheckCdnDomainV1Destroy(s *terraform.State) error { + config := testAccProvider.Meta().(*Config) + cdnClient, err := config.CdnV1Client(OS_REGION_NAME) + if err != nil { + return fmt.Errorf("Error creating HuaweiCloud CDN Domain client: %s", err) + } + + for _, rs := range s.RootModule().Resources { + if rs.Type != "huaweicloud_cdn_domain_v1" { + continue + } + + found, err := domains.Get(cdnClient, rs.Primary.ID, nil).Extract() + if err == nil && found.DomainStatus != "deleting" { + return fmt.Errorf("Destroying CDN domain failed or domain still exists") + } + } + + return nil +} + +func testAccCheckCdnDomainV1Exists(n string, domain *domains.CdnDomain) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("CDN Domain Resource not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No ID is set") + } + + config := testAccProvider.Meta().(*Config) + cdnClient, err := config.CdnV1Client(OS_REGION_NAME) + if err != nil { + return fmt.Errorf("Error creating HuaweiCloud CDN Domain client: %s", err) + } + + found, err := domains.Get(cdnClient, rs.Primary.ID, nil).Extract() + if err != nil { + return err + } + + if found.ID != rs.Primary.ID { + return fmt.Errorf("CDN Domain not found") + } + + *domain = *found + return nil + } +} + +var testAccCdnDomainV1_basic = fmt.Sprintf(` +resource "huaweicloud_cdn_domain_v1" "domain_1" { + name = "%s" + type = "web" + enterprise_project_id = 0 + sources { + domain = "100.254.53.75" + domain_type = "ipaddr" + } +} +`, OS_CDN_DOMAIN_NAME) diff --git a/vendor/github.com/huaweicloud/golangsdk/openstack/cdn/v1/domains/requests.go b/vendor/github.com/huaweicloud/golangsdk/openstack/cdn/v1/domains/requests.go new file mode 100644 index 0000000000..fe315a09c1 --- /dev/null +++ b/vendor/github.com/huaweicloud/golangsdk/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/vendor/github.com/huaweicloud/golangsdk/openstack/cdn/v1/domains/results.go b/vendor/github.com/huaweicloud/golangsdk/openstack/cdn/v1/domains/results.go new file mode 100644 index 0000000000..423ebd2a23 --- /dev/null +++ b/vendor/github.com/huaweicloud/golangsdk/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/vendor/github.com/huaweicloud/golangsdk/openstack/cdn/v1/domains/urls.go b/vendor/github.com/huaweicloud/golangsdk/openstack/cdn/v1/domains/urls.go new file mode 100644 index 0000000000..b4e3a20bb2 --- /dev/null +++ b/vendor/github.com/huaweicloud/golangsdk/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/vendor/github.com/huaweicloud/golangsdk/openstack/client.go b/vendor/github.com/huaweicloud/golangsdk/openstack/client.go index 93b4e8aa89..00cfb23468 100644 --- a/vendor/github.com/huaweicloud/golangsdk/openstack/client.go +++ b/vendor/github.com/huaweicloud/golangsdk/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 diff --git a/website/docs/r/cdn_domain_v1.html.markdown b/website/docs/r/cdn_domain_v1.html.markdown new file mode 100644 index 0000000000..1cd4209b16 --- /dev/null +++ b/website/docs/r/cdn_domain_v1.html.markdown @@ -0,0 +1,86 @@ +--- +layout: "huaweicloud" +page_title: "HuaweiCloud: huaweicloud_cdn_domain_v1" +sidebar_current: "docs-huaweicloud-resource-cdn-domain-v1" +description: |- + cdn domain management +--- + +# huaweicloud\_cdn\_domain\_v1 + +cdn domain management + +## Example Usage + +### create a cdn domain + +```hcl +resource "huaweicloud_cdn_domain_v1" "domain_1" { + name = "${var.domain_name}" + type = "web" + + sources { + domain = "${var.origin_server}" + domain_type = "ipaddr" + } +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) The acceleration domain name. + Changing this parameter will create a new resource. + +* `type` - (Required) The service type. The valid values are 'web', 'download' and 'video'. + Changing this parameter will create a new resource. + +* `sources` - (Required) An array of one or more objects specifies the domain name of the origin server. + The sources object structure is documented below. Changing this parameter will create a new resource. + +* `enterprise_project_id` - (Optional) The enterprise project id. + Changing this parameter will create a new resource. + + +The `sources` block supports: + +* `domain` - (Required) The domain name or IP address of the origin server. + Changing this parameter will create a new resource. + +* `domain_type` - (Required) The origin server type. The valid values are 'ipaddr', 'domain', and 'obs_bucket'. + Changing this parameter will create a new resource. + +## Attributes Reference + +The following attributes are exported: + +* `name` - See Argument Reference above. + +* `type` - See Argument Reference above. + +* `enterprise_project_id` - See Argument Reference above. + +* `sources/domain` - See Argument Reference above. + +* `sources/domain_type` - See Argument Reference above. + +* `sources/active` - Whether an origin server is active or standby (1: active; 0: standby). + +* `id` - The acceleration domain name ID. + +* `cname` - The CNAME of the acceleration domain name. + +* `domain_status` - The status of the acceleration domain name. The available values are + 'online', 'offline', 'configuring', 'configure_failed', 'checking', 'check_failed' and 'deleting.' + +* `service_area` - The area covered by the acceleration service. + + +## Import + +Domains can be imported using the `id`, e.g. + +``` +$ terraform import huaweicloud_cdn_domain_v1.domain_1 fe2462fac09a4a42a76ecc4a1ef542f1 +``` \ No newline at end of file diff --git a/website/huaweicloud.erb b/website/huaweicloud.erb index 7df10e92fe..9ff5054f7c 100644 --- a/website/huaweicloud.erb +++ b/website/huaweicloud.erb @@ -170,6 +170,15 @@ +