Skip to content

Commit

Permalink
chore(cdn): unify automatically generated code style (#5791)
Browse files Browse the repository at this point in the history
  • Loading branch information
deer-hang authored Nov 1, 2024
1 parent fbca9a0 commit 03be615
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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)
Expand All @@ -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{}{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
78 changes: 44 additions & 34 deletions huaweicloud/services/cdn/data_source_huaweicloud_cdn_domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 03be615

Please sign in to comment.