Skip to content

Commit

Permalink
improves the testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaozhu36 committed Dec 9, 2024
1 parent 9c2add2 commit ed4b6ab
Show file tree
Hide file tree
Showing 31 changed files with 590 additions and 424 deletions.
11 changes: 11 additions & 0 deletions alicloud/connectivity/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,14 @@ var irregularProductEndpointForIntlAccount = map[string]string{
"bssopenapi": BssOpenAPIEndpointInternational,
}

// irregularProductEndpointForIntlRegion specially records those product codes that
// cannot be parsed out by the location service and sensitive to region.
// These products adapt to international region, and conflict with irregularProductEndpointForIntlAccount
// Key: product code, its value equals to the gateway code of the API after converting it to lowercase and using underscores
// Value: product endpoint
// The priority of this configuration is higher than location service, lower than user environment variable configuration
var irregularProductEndpointForIntlRegion = map[string]string{}

// regularProductEndpoint specially records those product codes that have been confirmed to be
// regional or central endpoints.
// Key: product code, its value equals to the gateway code of the API after converting it to lowercase and using underscores
Expand Down Expand Up @@ -474,6 +482,9 @@ func (client *AliyunClient) loadEndpoint(productCode string) error {
if v, ok := irregularProductEndpointForIntlAccount[productCode]; ok && strings.ToLower(client.config.AccountType) == "international" {
endpointFmt = v
}
if v, ok := irregularProductEndpointForIntlRegion[productCode]; ok && !strings.HasPrefix(client.RegionId, "cn-") {
endpointFmt = v
}
if strings.Contains(endpointFmt, "%s") {
endpointFmt = fmt.Sprintf(endpointFmt, client.RegionId)
}
Expand Down
8 changes: 3 additions & 5 deletions alicloud/data_source_alicloud_cloud_firewall_address_books.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,13 @@ func dataSourceAliCloudCloudFirewallAddressBooksRead(d *schema.ResourceData, met
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
} else if IsExpectedErrors(err, []string{"not buy user"}) {
endpoint = connectivity.CloudFirewallOpenAPIEndpointControlPolicy
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}

if fmt.Sprint(response["Message"]) == "not buy user" {
endpoint = connectivity.CloudFirewallOpenAPIEndpointControlPolicy
return resource.RetryableError(fmt.Errorf("%s", response))
}

return nil
})
addDebug(action, response, request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,13 @@ func dataSourceAliCloudCloudFirewallControlPoliciesRead(d *schema.ResourceData,
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
} else if IsExpectedErrors(err, []string{"not buy user"}) {
endpoint = connectivity.CloudFirewallOpenAPIEndpointControlPolicy
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}

if fmt.Sprint(response["Message"]) == "not buy user" {
endpoint = connectivity.CloudFirewallOpenAPIEndpointControlPolicy
return resource.RetryableError(fmt.Errorf("%s", response))
}

return nil
})
addDebug(action, response, request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,12 @@ func dataSourceAlicloudCloudFirewallInstanceMembersRead(d *schema.ResourceData,
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
} else if IsExpectedErrors(err, []string{"not buy user"}) {
endpoint = connectivity.CloudFirewallOpenAPIEndpointControlPolicy
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
if fmt.Sprint(response["Message"]) == "not buy user" {
endpoint = connectivity.CloudFirewallOpenAPIEndpointControlPolicy
return resource.RetryableError(fmt.Errorf("%s", response))
}
addDebug(action, response, request)
return nil
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ variable "name" {
resource "alicloud_resource_manager_account" "default" {
display_name = var.name
abandon_able_check_id = ["SP_fc_fc"]
}
resource "alicloud_cloud_firewall_instance_member" "default" {
Expand Down
126 changes: 79 additions & 47 deletions alicloud/data_source_alicloud_cloud_firewall_instances.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package alicloud

import (
"fmt"
"strconv"
"time"

"github.com/PaesslerAG/jsonpath"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand All @@ -16,6 +14,12 @@ func dataSourceAlicloudCloudFirewallInstances() *schema.Resource {
return &schema.Resource{
Read: dataSourceAlicloudCloudFirewallInstancesRead,
Schema: map[string]*schema.Schema{
"payment_type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: StringInSlice([]string{"Subscription", "PayAsYouGo"}, false),
},
"output_file": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -69,61 +73,89 @@ func dataSourceAlicloudCloudFirewallInstancesRead(d *schema.ResourceData, meta i

action := "QueryAvailableInstances"
request := make(map[string]interface{})
request["ProductCode"] = "vipcloudfw"
request["ProductType"] = "vipcloudfw"
request["PageSize"] = PageSizeLarge
request["PageNum"] = 1
var objects []map[string]interface{}

var response map[string]interface{}
conn, err := client.NewBssopenapiClient()
if err != nil {
return WrapError(err)
var err error
var isIntl bool
if client.GetAccountType() == "International" {
isIntl = true
}
for {
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2017-12-14"), StringPointer("AK"), nil, request, &runtime)
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
if IsExpectedErrors(err, []string{"NotApplicable"}) {
request["ProductCode"] = "cfw"
request["ProductType"] = "cfw_pre_intl"
conn.Endpoint = String(connectivity.BssOpenAPIEndpointInternational)
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
addDebug(action, response, request)
if err != nil {
return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_cloud_firewall_instances", action, AlibabaCloudSdkGoERROR)
}
if fmt.Sprint(response["Code"]) != "Success" {
return WrapError(fmt.Errorf("%s failed, response: %v", action, response))
var productTypes []string
paymentType := d.Get("payment_type").(string)
productMapping := map[string]string{
"vipcloudfw": "vipcloudfw",
"cfw_elasticity_public_cn": "cfw",
"cfw_pre_intl": "cfw",
"cfw_elasticity_public_intl": "cfw",
}
paymentTypeMapping := map[string]string{
"vipcloudfw": "Subscription",
"cfw_elasticity_public_cn": "PayAsYouGo",
"cfw_pre_intl": "Subscription",
"cfw_elasticity_public_intl": "PayAsYouGo",
}
if paymentType == "Subscription" {
if isIntl {
productTypes = []string{"cfw_pre_intl"}
} else {
productTypes = []string{"vipcloudfw"}
}
resp, err := jsonpath.Get("$.Data.InstanceList", response)
if err != nil {
return WrapErrorf(err, FailedGetAttributeMsg, action, "$.Data.InstanceList", response)
} else if paymentType == "PayAsYouGo" {
if isIntl {
productTypes = []string{"cfw_elasticity_public_intl"}
} else {
productTypes = []string{"cfw_elasticity_public_cn"}
}
result, _ := resp.([]interface{})
for _, v := range result {
item := v.(map[string]interface{})
objects = append(objects, item)
} else {
if isIntl {
productTypes = []string{"cfw_pre_intl", "cfw_elasticity_public_intl"}
} else {
productTypes = []string{"vipcloudfw", "cfw_elasticity_public_cn"}
}
if len(result) < PageSizeLarge {
break
}
for _, productType := range productTypes {
request["PageSize"] = PageSizeLarge
request["PageNum"] = 1
request["ProductCode"] = productMapping[productType]
request["ProductType"] = productType
request["SubscriptionType"] = paymentTypeMapping[productType]
for {
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
response, err = client.RpcPost("BssOpenApi", "2017-12-14", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
addDebug(action, response, request)
if err != nil {
return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_cloud_firewall_instances", action, AlibabaCloudSdkGoERROR)
}
resp, err := jsonpath.Get("$.Data.InstanceList", response)
if err != nil {
return WrapErrorf(err, FailedGetAttributeMsg, action, "$.Data.InstanceList", response)
}
result, _ := resp.([]interface{})
for _, v := range result {
item := v.(map[string]interface{})
objects = append(objects, item)
}
if len(result) < PageSizeLarge {
break
}
request["PageNum"] = request["PageNum"].(int) + 1
}
request["PageNum"] = request["PageNum"].(int) + 1
}
s := make([]map[string]interface{}, 0)
for _, object := range objects {
if v, ok := object["Status"]; ok && v.(string) != "Normal" {
continue
}
mapping := map[string]interface{}{
"status": object["Status"],
"end_time": object["EndTime"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,12 @@ func dataSourceAlicloudCloudFirewallVpcFirewallCensRead(d *schema.ResourceData,
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
} else if IsExpectedErrors(err, []string{"not buy user"}) {
endpoint = connectivity.CloudFirewallOpenAPIEndpointControlPolicy
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
if fmt.Sprint(response["Message"]) == "not buy user" {
endpoint = connectivity.CloudFirewallOpenAPIEndpointControlPolicy
return resource.RetryableError(fmt.Errorf("%s", response))
}
addDebug(action, response, request)
return nil
})
Expand Down
Loading

0 comments on commit ed4b6ab

Please sign in to comment.