Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CloudFirewall: Improves the invoking cloud firewall api method and supports refreshing credential automatically #7931

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions alicloud/connectivity/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,14 @@ var irregularProductEndpointForIntlAccount = map[string]string{
"esa": "esa.ap-southeast-1.aliyuncs.com",
}

// 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 @@ -483,6 +491,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
19 changes: 6 additions & 13 deletions alicloud/data_source_alicloud_cloud_firewall_address_books.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"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 Down Expand Up @@ -139,30 +138,24 @@ func dataSourceAliCloudCloudFirewallAddressBooksRead(d *schema.ResourceData, met
}

var response map[string]interface{}
conn, err := client.NewCloudfwClient()
if err != nil {
return WrapError(err)
}
var err error
var endpoint string

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-07"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPostWithEndpoint("Cloudfw", "2017-12-07", action, nil, request, true, endpoint)
if err != nil {
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" {
conn.Endpoint = String(connectivity.CloudFirewallOpenAPIEndpointControlPolicy)
return resource.RetryableError(fmt.Errorf("%s", response))
}

return nil
})
addDebug(action, response, request)
Expand Down
19 changes: 6 additions & 13 deletions alicloud/data_source_alicloud_cloud_firewall_control_policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"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 Down Expand Up @@ -237,30 +236,24 @@ func dataSourceAliCloudCloudFirewallControlPoliciesRead(d *schema.ResourceData,

var objects []map[string]interface{}
var response map[string]interface{}
conn, err := client.NewCloudfwClient()
if err != nil {
return WrapError(err)
}
var err error
var endpoint string

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-07"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPostWithEndpoint("Cloudfw", "2017-12-07", action, nil, request, true, endpoint)
if err != nil {
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" {
conn.Endpoint = String(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 @@ -8,7 +8,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
)

func TestAccCheckAliCloudCloudFirewallControlPoliciesDataSource(t *testing.T) {
func TestAccAliCloudCloudFirewallControlPoliciesDataSource(t *testing.T) {
rand := acctest.RandInt()
aclActionConf := dataSourceTestAccConfig{
existConfig: testAccCheckAliCloudCloudFirewallControlPoliciesDataSourceName(rand, map[string]string{
Expand All @@ -18,14 +18,6 @@ func TestAccCheckAliCloudCloudFirewallControlPoliciesDataSource(t *testing.T) {
"acl_action": `"drop"`,
}),
}
aclUuidConf := dataSourceTestAccConfig{
existConfig: testAccCheckAliCloudCloudFirewallControlPoliciesDataSourceName(rand, map[string]string{
"acl_uuid": `"${alicloud_cloud_firewall_control_policy.default.acl_uuid}"`,
}),
fakeConfig: testAccCheckAliCloudCloudFirewallControlPoliciesDataSourceName(rand, map[string]string{
"acl_uuid": `"${alicloud_cloud_firewall_control_policy.default.acl_uuid}_fake"`,
}),
}
descriptionConf := dataSourceTestAccConfig{
existConfig: testAccCheckAliCloudCloudFirewallControlPoliciesDataSourceName(rand, map[string]string{
"description": `"${alicloud_cloud_firewall_control_policy.default.description}"`,
Expand Down Expand Up @@ -70,7 +62,6 @@ func TestAccCheckAliCloudCloudFirewallControlPoliciesDataSource(t *testing.T) {
allConf := dataSourceTestAccConfig{
existConfig: testAccCheckAliCloudCloudFirewallControlPoliciesDataSourceName(rand, map[string]string{
"acl_action": `"${alicloud_cloud_firewall_control_policy.default.acl_action}"`,
"acl_uuid": `"${alicloud_cloud_firewall_control_policy.default.acl_uuid}"`,
"description": `"${alicloud_cloud_firewall_control_policy.default.description}"`,
"destination": `"${alicloud_cloud_firewall_control_policy.default.destination}"`,
"ip_version": `"${alicloud_cloud_firewall_control_policy.default.ip_version}"`,
Expand All @@ -79,7 +70,6 @@ func TestAccCheckAliCloudCloudFirewallControlPoliciesDataSource(t *testing.T) {
}),
fakeConfig: testAccCheckAliCloudCloudFirewallControlPoliciesDataSourceName(rand, map[string]string{
"acl_action": `"drop"`,
"acl_uuid": `"${alicloud_cloud_firewall_control_policy.default.acl_uuid}_fake"`,
"description": `"${alicloud_cloud_firewall_control_policy.default.description}_fake"`,
"destination": `"${alicloud_cloud_firewall_control_policy.default.destination}_fake"`,
"ip_version": `"6"`,
Expand Down Expand Up @@ -132,7 +122,7 @@ func TestAccCheckAliCloudCloudFirewallControlPoliciesDataSource(t *testing.T) {
preCheck := func() {
testAccPreCheck(t)
}
alicloudCloudFirewallControlPoliciesCheckInfo.dataSourceTestCheckWithPreCheck(t, rand, preCheck, aclActionConf, aclUuidConf, descriptionConf, destinationConf, ipVersionConf, protoConf, sourceConf, allConf)
alicloudCloudFirewallControlPoliciesCheckInfo.dataSourceTestCheckWithPreCheck(t, rand, preCheck, aclActionConf, descriptionConf, destinationConf, ipVersionConf, protoConf, sourceConf, allConf)
}

func testAccCheckAliCloudCloudFirewallControlPoliciesDataSourceName(rand int, attrMap map[string]string) string {
Expand Down Expand Up @@ -161,7 +151,8 @@ func testAccCheckAliCloudCloudFirewallControlPoliciesDataSourceName(rand int, at
}

data "alicloud_cloud_firewall_control_policies" "default" {
direction = alicloud_cloud_firewall_control_policy.default.direction
direction = alicloud_cloud_firewall_control_policy.default.direction
acl_uuid = alicloud_cloud_firewall_control_policy.default.acl_uuid
%s
}
`, rand, strings.Join(pairs, " \n "))
Expand Down
15 changes: 6 additions & 9 deletions alicloud/data_source_alicloud_cloud_firewall_instance_members.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"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 Down Expand Up @@ -110,28 +109,26 @@ func dataSourceAlicloudCloudFirewallInstanceMembersRead(d *schema.ResourceData,
}
}

conn, err := client.NewCloudfirewallClient()
if err != nil {
return WrapError(err)
}
var err error
var endpoint string
var objects []interface{}
var response map[string]interface{}

for {
action := "DescribeInstanceMembers"
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
resp, err := conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2017-12-07"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPostWithEndpoint("Cloudfw", "2017-12-07", action, nil, request, true, endpoint)
if err != nil {
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)
}
response = resp
addDebug(action, response, request)
return nil
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
)

func TestAccAlicloudCloudFirewallInstanceMemberDataSource(t *testing.T) {
func TestAccAliCloudCloudFirewallInstanceMemberDataSource(t *testing.T) {
rand := acctest.RandIntRange(1000000, 9999999)

idsConf := dataSourceTestAccConfig{
Expand All @@ -29,7 +29,13 @@ func TestAccAlicloudCloudFirewallInstanceMemberDataSource(t *testing.T) {
}),
}

CloudFirewallInstanceMemberCheckInfo.dataSourceTestCheck(t, rand, idsConf, allConf)
preCheck := func() {
testAccPreCheck(t)
// currently, international test account has not enabled RD
testAccPreCheckWithAccountSiteType(t, DomesticSite)
}

CloudFirewallInstanceMemberCheckInfo.dataSourceTestCheckWithPreCheck(t, rand, preCheck, idsConf, allConf)
}

var existCloudFirewallInstanceMemberMapFunc = func(rand int) map[string]string {
Expand Down Expand Up @@ -63,6 +69,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
Loading
Loading