diff --git a/huaweicloud/services/cdn/data_source_huaweicloud_cdn_domain_certificates.go b/huaweicloud/services/cdn/data_source_huaweicloud_cdn_domain_certificates.go index 79986c8c44..ac8b1cf80a 100644 --- a/huaweicloud/services/cdn/data_source_huaweicloud_cdn_domain_certificates.go +++ b/huaweicloud/services/cdn/data_source_huaweicloud_cdn_domain_certificates.go @@ -30,14 +30,14 @@ func DataSourceDomainCertificates() *schema.Resource { }, "domain_certificates": { Type: schema.TypeList, - Elem: domainCertificateschema(), + Elem: domainCertificateSchema(), Computed: true, }, }, } } -func domainCertificateschema() *schema.Resource { +func domainCertificateSchema() *schema.Resource { sc := schema.Resource{ Schema: map[string]*schema.Schema{ "domain_id": { @@ -81,52 +81,59 @@ func domainCertificateschema() *schema.Resource { return &sc } -func resourceDomainCertificatesRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - conf := meta.(*config.Config) - region := conf.GetRegion(d) +func buildCertificateQueryParams(conf *config.Config, d *schema.ResourceData) string { + rst := "?page_size=10" + if epsId := conf.GetEnterpriseProjectID(d); epsId != "" { + rst += fmt.Sprintf("&enterprise_project_id=%v", epsId) + } - var mErr *multierror.Error + if v, ok := d.GetOk("name"); ok { + rst += fmt.Sprintf("&domain_name=%v", v) + } + return rst +} +func resourceDomainCertificatesRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var ( - domainCertificatesHttpUrl = "v1.0/cdn/domains/https-certificate-info" - domainCertificatesProduct = "cdn" + conf = meta.(*config.Config) + region = conf.GetRegion(d) + mErr *multierror.Error + httpUrl = "v1.0/cdn/domains/https-certificate-info" + product = "cdn" + currentTotal = 1 + rst = make([]interface{}, 0) ) - domainCertificatesClient, err := conf.NewServiceClient(domainCertificatesProduct, region) + client, err := conf.NewServiceClient(product, region) if err != nil { return diag.Errorf("error creating CDN client: %s", err) } - domainCertificatesPath := domainCertificatesClient.Endpoint + domainCertificatesHttpUrl + "?page_size=10" - epsId := conf.GetEnterpriseProjectID(d) - if epsId != "" { - domainCertificatesPath += fmt.Sprintf("&enterprise_project_id=%v", epsId) - } - if v, ok := d.GetOk("name"); ok { - domainCertificatesPath += fmt.Sprintf("&domain_name=%v", v) - } - getCDNCertificateDomainsOpt := golangsdk.RequestOpts{ + requestPath := client.Endpoint + httpUrl + requestPath += buildCertificateQueryParams(conf, d) + requestOpt := golangsdk.RequestOpts{ KeepResponseBody: true, } - currentTotal := 1 - rst := make([]interface{}, 0) for { - currentPath := fmt.Sprintf("%s&page_number=%v", domainCertificatesPath, currentTotal) - getCDNCertificateDomainsResp, err := domainCertificatesClient.Request("GET", currentPath, &getCDNCertificateDomainsOpt) + currentPath := fmt.Sprintf("%s&page_number=%v", requestPath, currentTotal) + resp, err := client.Request("GET", currentPath, &requestOpt) if err != nil { return diag.FromErr(err) } - getCDNCertificateDomainsRespBody, err := utils.FlattenResponse(getCDNCertificateDomainsResp) + + respBody, err := utils.FlattenResponse(resp) if err != nil { return diag.FromErr(err) } - domainCertificates := utils.PathSearch("https", getCDNCertificateDomainsRespBody, make([]interface{}, 0)).([]interface{}) + + domainCertificates := utils.PathSearch("https", respBody, make([]interface{}, 0)).([]interface{}) if len(domainCertificates) == 0 { break } rst = append(rst, domainCertificates...) currentTotal++ } + dataSourceId, err := uuid.GenerateUUID() if err != nil { return diag.Errorf("unable to generate ID: %s", err) @@ -142,7 +149,7 @@ func resourceDomainCertificatesRead(_ context.Context, d *schema.ResourceData, m } func flattenListCertificateDomainsBody(domainCertificates []interface{}) []interface{} { - rst := make([]interface{}, 0) + rst := make([]interface{}, 0, len(domainCertificates)) for _, v := range domainCertificates { expirationTime := utils.PathSearch("expiration_time", v, 0) rst = append(rst, map[string]interface{}{ diff --git a/huaweicloud/services/cdn/data_source_huaweicloud_cdn_domain_statistics.go b/huaweicloud/services/cdn/data_source_huaweicloud_cdn_domain_statistics.go index a03e5ec936..fcabf04b7b 100644 --- a/huaweicloud/services/cdn/data_source_huaweicloud_cdn_domain_statistics.go +++ b/huaweicloud/services/cdn/data_source_huaweicloud_cdn_domain_statistics.go @@ -95,52 +95,43 @@ func DataSourceStatistics() *schema.Resource { } func resourceStatisticsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - conf := meta.(*config.Config) - region := conf.GetRegion(d) - - var mErr *multierror.Error - - // domainStatistics: Query the statistics of CDN domain. var ( - domainStatisticsHttpUrl = "v1.0/cdn/statistics/domain-location-stats" - domainStatisticsProduct = "cdn" + conf = meta.(*config.Config) + region = conf.GetRegion(d) + mErr *multierror.Error + httpUrl = "v1.0/cdn/statistics/domain-location-stats" + product = "cdn" ) - domainStatisticsClient, err := conf.NewServiceClient(domainStatisticsProduct, region) + client, err := conf.NewServiceClient(product, region) if err != nil { - return diag.Errorf("error creating Statistics Client: %s", err) + return diag.Errorf("error creating CDN client: %s", err) } - domainStatisticsPath := domainStatisticsClient.Endpoint + domainStatisticsHttpUrl - - domainStatisticsqueryParams := buildDomainStatisticsQueryParams(d) - domainStatisticsPath += domainStatisticsqueryParams - - domainStatisticsOpt := golangsdk.RequestOpts{ + requestPath := client.Endpoint + httpUrl + requestPath += buildDomainStatisticsQueryParams(d) + requestOpt := golangsdk.RequestOpts{ KeepResponseBody: true, - OkCodes: []int{ - 200, - }, } - domainStatisticsResp, err := domainStatisticsClient.Request("GET", domainStatisticsPath, &domainStatisticsOpt) + resp, err := client.Request("GET", requestPath, &requestOpt) if err != nil { return diag.Errorf("error retrieving CDN statistics: %s", err) } - domainStatisticsRespBody, err := utils.FlattenResponse(domainStatisticsResp) + respBody, err := utils.FlattenResponse(resp) if err != nil { return diag.FromErr(err) } - uuid, err := uuid.GenerateUUID() + generateUUID, err := uuid.GenerateUUID() if err != nil { return diag.Errorf("unable to generate ID: %s", err) } - d.SetId(uuid) + d.SetId(generateUUID) - results, err := utils.JsonMarshal(utils.PathSearch("result", domainStatisticsRespBody, nil)) + results, err := utils.JsonMarshal(utils.PathSearch("result", respBody, nil)) if err != nil { - return diag.Errorf("error marshaling Statistics result: %s", err) + return diag.Errorf("error marshaling statistics result: %s", err) } mErr = multierror.Append( diff --git a/huaweicloud/services/cdn/data_source_huaweicloud_cdn_domains.go b/huaweicloud/services/cdn/data_source_huaweicloud_cdn_domains.go index 18a3b0bee6..9138da84c3 100644 --- a/huaweicloud/services/cdn/data_source_huaweicloud_cdn_domains.go +++ b/huaweicloud/services/cdn/data_source_huaweicloud_cdn_domains.go @@ -171,61 +171,68 @@ func sourceSchema() *schema.Resource { return sc } -func datasourceDomainsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - cfg := meta.(*config.Config) - region := cfg.GetRegion(d) - - var mErr *multierror.Error - - var ( - getCDNDomainsHttpUrl = "v1.0/cdn/domains" - getCDNDomainsProduct = "cdn" - ) - getCDNDomainsClient, err := cfg.NewServiceClient(getCDNDomainsProduct, region) - if err != nil { - return diag.Errorf("error creating CDN client: %s", err) - } - - getCDNDomainsPath := getCDNDomainsClient.Endpoint + getCDNDomainsHttpUrl + "?page_size=10" - epsId := cfg.GetEnterpriseProjectID(d) - if epsId != "" { - getCDNDomainsPath += fmt.Sprintf("&enterprise_project_id=%v", epsId) +func buildDomainsQueryParams(cfg *config.Config, d *schema.ResourceData) string { + rst := "?page_size=10" + if epsId := cfg.GetEnterpriseProjectID(d); epsId != "" { + rst += fmt.Sprintf("&enterprise_project_id=%v", epsId) } if v, ok := d.GetOk("name"); ok { - getCDNDomainsPath += fmt.Sprintf("&domain_name=%v", v) + rst += fmt.Sprintf("&domain_name=%v", v) } if v, ok := d.GetOk("type"); ok { - getCDNDomainsPath += fmt.Sprintf("&business_type=%v", v) + rst += fmt.Sprintf("&business_type=%v", v) } if v, ok := d.GetOk("domain_status"); ok { - getCDNDomainsPath += fmt.Sprintf("&domain_status=%v", v) + rst += fmt.Sprintf("&domain_status=%v", v) } if v, ok := d.GetOk("service_area"); ok { - getCDNDomainsPath += fmt.Sprintf("&service_area=%v", v) + rst += fmt.Sprintf("&service_area=%v", v) + } + return rst +} + +func datasourceDomainsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + var ( + cfg = meta.(*config.Config) + region = cfg.GetRegion(d) + mErr *multierror.Error + httpUrl = "v1.0/cdn/domains" + product = "cdn" + currentTotal = 1 + rst = make([]interface{}, 0) + ) + client, err := cfg.NewServiceClient(product, region) + if err != nil { + return diag.Errorf("error creating CDN client: %s", err) } - getCDNDomainsOpt := golangsdk.RequestOpts{ + + requestPath := client.Endpoint + httpUrl + requestPath += buildDomainsQueryParams(cfg, d) + requestOpt := golangsdk.RequestOpts{ KeepResponseBody: true, } - currentTotal := 1 - rst := make([]interface{}, 0) for { - currentPath := fmt.Sprintf("%s&page_number=%v", getCDNDomainsPath, currentTotal) - getCDNDomainsResp, err := getCDNDomainsClient.Request("GET", currentPath, &getCDNDomainsOpt) + currentPath := fmt.Sprintf("%s&page_number=%v", requestPath, currentTotal) + resp, err := client.Request("GET", currentPath, &requestOpt) if err != nil { return diag.FromErr(err) } - getCDNDomainsRespBody, err := utils.FlattenResponse(getCDNDomainsResp) + + respBody, err := utils.FlattenResponse(resp) if err != nil { return diag.FromErr(err) } - domains := utils.PathSearch("domains", getCDNDomainsRespBody, make([]interface{}, 0)).([]interface{}) + + domains := utils.PathSearch("domains", respBody, make([]interface{}, 0)).([]interface{}) if len(domains) == 0 { break } + rst = append(rst, domains...) currentTotal++ } + dataSourceId, err := uuid.GenerateUUID() if err != nil { return diag.Errorf("unable to generate ID: %s", err) @@ -241,7 +248,7 @@ func datasourceDomainsRead(_ context.Context, d *schema.ResourceData, meta inter } func flattenListDomainsBody(domains []interface{}) []interface{} { - rst := make([]interface{}, 0) + rst := make([]interface{}, 0, len(domains)) for _, v := range domains { createTime := utils.PathSearch("create_time", v, 0) updateTime := utils.PathSearch("modify_time", v, 0) @@ -281,10 +288,13 @@ func converseOBSWebHostStatusToBool(status interface{}) bool { } func filterListDomainsBody(all []interface{}, d *schema.ResourceData) []interface{} { - rst := make([]interface{}, 0, len(all)) + var ( + domainID = d.Get("domain_id").(string) + rst = make([]interface{}, 0, len(all)) + ) + for _, v := range all { - if param, ok := d.GetOk("domain_id"); ok && - fmt.Sprint(param) != fmt.Sprint(utils.PathSearch("id", v, nil)) { + if domainID != "" && domainID != fmt.Sprint(utils.PathSearch("id", v, nil)) { continue } rst = append(rst, v)