Skip to content

Commit

Permalink
fix deleting accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaozhu36 committed Dec 2, 2024
1 parent 689cd89 commit 9a77236
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 24 deletions.
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
71 changes: 67 additions & 4 deletions alicloud/provider_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package alicloud

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"testing"
"time"
Expand Down Expand Up @@ -65,10 +67,34 @@ func testAccPreCheck(t *testing.T) {
}
}

func testAccPreCheckForIntl(t *testing.T) {
if strings.TrimSpace(os.Getenv("ALIBABA_CLOUD_ACCOUNT_TYPE")) != "International" {
t.Skipf("Skipping %s Site.", "International")
t.Skipped()
func testAccPreCheckForCleanUpInstances(t *testing.T, instanceRegion, productCode, productType, productCodeIntl, productTypeIntl string) {
rawClient, err := sharedClientForRegion(defaultRegionToTest)
if err != nil {
t.Errorf("error getting AliCloud client: %s", err)
}
client := rawClient.(*connectivity.AliyunClient)
bssOpenApiService := BssOpenApiService{client}
accountId, err := client.AccountId()
if err != nil {
t.Errorf("error getting AliCloud client: %s", err)
}

for {
instance, err := bssOpenApiService.QueryAvailableInstances("", instanceRegion, productCode, productType, productCodeIntl, productTypeIntl)
if err != nil {
t.Errorf("error querying available instances: %s", err)
}
if len(instance) < 1 {
break
}
sendMessage(fmt.Sprintf(`
[Critical] Please cleaning up instance before running integration test.
AccountId: %s
ProductCode: %s
InstanceId: %s
`, accountId, productCode, instance["InstanceID"]))
time.Sleep(3 * time.Minute)
}
}

Expand Down Expand Up @@ -423,6 +449,43 @@ func testAccPreCheckWithResourceManagerHandshakesSetting(t *testing.T) {
}
}

type DingTalkMessage struct {
MsgType string `json:"msgtype"`
Text struct {
Content string `json:"content"`
} `json:"text"`
}

func sendMessage(msg string) {
// 钉钉机器人的 Webhook 地址
webhookURL := "https://oapi.dingtalk.com/robot/send?access_token=" + os.Getenv("DINGTALK_WEBHOOK_ACCESS_TOKEN")

// 构建消息内容
message := DingTalkMessage{
MsgType: "text",
Text: struct {
Content string `json:"content"`
}{
Content: "Hello, 这是一条来自 Go 程序的测试消息!",
},
}

// 将消息内容转换为 JSON 格式
jsonData, err := json.Marshal(message)
if err != nil {
fmt.Println("[ERROR] send dingTalk message failed. Error:", err)
return
}

// 发送 POST 请求
resp, err := http.Post(webhookURL, "application/json", bytes.NewBuffer(jsonData))
if err != nil {
fmt.Println("[ERROR] send dingTalk message failed. Error:", err)
return
}
defer resp.Body.Close()
}

func setStsCredential() {
// 创建OSSClient实例。
client, err := oss.New("https://oss-cn-zhangjiakou.aliyuncs.com", os.Getenv("ALICLOUD_ACCESS_KEY"), os.Getenv("ALICLOUD_SECRET_KEY"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestAccAlicloudCloudFirewallInstanceMember_basic(t *testing.T) {
{
Config: testAccConfig(map[string]interface{}{
"member_desc": "${var.name}",
"member_uid": "${alicloud_resource_manager_account.default.id}",
"member_uid": "${data.alicloud_resource_manager_accounts.default.ids.0}",
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
Expand Down Expand Up @@ -69,6 +69,7 @@ variable "name" {
resource "alicloud_resource_manager_account" "default" {
display_name = var.name
abandon_able_check_id = ["SP_fc_fc"]
}
`, name)
}
12 changes: 6 additions & 6 deletions alicloud/resource_alicloud_cloud_firewall_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccAliCloudCloudFirewallAaInstance_basic0(t *testing.T) {
func TestAccAliCloudCloudFirewallInstance_basic0(t *testing.T) {
var v map[string]interface{}
resourceId := "alicloud_cloud_firewall_instance.default"
ra := resourceAttrInit(resourceId, AliCloudCloudFirewallInstanceMap0)
Expand All @@ -34,7 +34,7 @@ func TestAccAliCloudCloudFirewallAaInstance_basic0(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckForIntl(t)
testAccPreCheckForCleanUpInstances(t, "", "vipcloudfw", "vipcloudfw", "cfw", "cfw_pre_intl")
},
IDRefreshName: resourceId,
Providers: testAccProviders,
Expand Down Expand Up @@ -69,7 +69,7 @@ func TestAccAliCloudCloudFirewallAaInstance_basic0(t *testing.T) {
})
}

func TestAccAliCloudCloudFirewallAaInstance_basic0_twin(t *testing.T) {
func TestAccAliCloudCloudFirewallInstance_basic0_twin(t *testing.T) {
var v map[string]interface{}
resourceId := "alicloud_cloud_firewall_instance.default"
ra := resourceAttrInit(resourceId, AliCloudCloudFirewallInstanceMap0)
Expand All @@ -84,7 +84,7 @@ func TestAccAliCloudCloudFirewallAaInstance_basic0_twin(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckForIntl(t)
testAccPreCheckForCleanUpInstances(t, "", "vipcloudfw", "vipcloudfw", "cfw", "cfw_pre_intl")
},
IDRefreshName: resourceId,
Providers: testAccProviders,
Expand Down Expand Up @@ -113,7 +113,7 @@ func TestAccAliCloudCloudFirewallAaInstance_basic0_twin(t *testing.T) {
})
}

func TestAccAliCloudCloudFirewallA0Instance_basic1(t *testing.T) {
func TestAccAliCloudCloudFirewallInstance_basic1(t *testing.T) {
var v map[string]interface{}
resourceId := "alicloud_cloud_firewall_instance.default"
ra := resourceAttrInit(resourceId, AliCloudCloudFirewallInstanceMap0)
Expand All @@ -128,7 +128,7 @@ func TestAccAliCloudCloudFirewallA0Instance_basic1(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckForIntl(t)
testAccPreCheckForCleanUpInstances(t, "", "vipcloudfw", "vipcloudfw", "cfw", "cfw_pre_intl")
},
IDRefreshName: resourceId,
Providers: testAccProviders,
Expand Down
24 changes: 13 additions & 11 deletions alicloud/resource_alicloud_cloud_firewall_nat_firewall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// Case 云防火墙资源测试-v1 6822
func TestAccAliCloudCloudFirewallA0NatFirewall_basic6822(t *testing.T) {
var v map[string]interface{}
checkoutSupportedRegions(t, true, []connectivity.Region{"cn-shenzhen"})
checkoutSupportedRegions(t, true, []connectivity.Region{"cn-hangzhou"})
resourceId := "alicloud_cloud_firewall_nat_firewall.default"
ra := resourceAttrInit(resourceId, AlicloudCloudFirewallNatFirewallMap6822)
rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} {
Expand All @@ -27,7 +27,6 @@ func TestAccAliCloudCloudFirewallA0NatFirewall_basic6822(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
//testAccPreCheckWithRegions(t, true, []connectivity.Region{"cn-shenzhen"})
},
IDRefreshName: resourceId,
Providers: testAccProviders,
Expand All @@ -36,7 +35,7 @@ func TestAccAliCloudCloudFirewallA0NatFirewall_basic6822(t *testing.T) {
{
Config: testAccConfig(map[string]interface{}{
"status": "normal",
"region_no": "cn-shenzhen",
"region_no": "${data.alicloud_regions.current.ids.0}",
"vswitch_auto": "true",
"strict_mode": "0",
"vpc_id": "${alicloud_vpc.defaultikZ0gD.id}",
Expand All @@ -57,7 +56,7 @@ func TestAccAliCloudCloudFirewallA0NatFirewall_basic6822(t *testing.T) {
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
"region_no": "cn-shenzhen",
"region_no": defaultRegionToTest,
"vpc_id": CHECKSET,
"proxy_name": "YQC-防火墙测试",
"nat_gateway_id": CHECKSET,
Expand Down Expand Up @@ -87,7 +86,7 @@ func TestAccAliCloudCloudFirewallA0NatFirewall_basic6822(t *testing.T) {
},
{
Config: testAccConfig(map[string]interface{}{
"region_no": "cn-shenzhen",
"region_no": "${data.alicloud_regions.current.ids.0}",
"vswitch_auto": "true",
"strict_mode": "0",
"vpc_id": "${alicloud_vpc.defaultikZ0gD.id}",
Expand All @@ -109,7 +108,7 @@ func TestAccAliCloudCloudFirewallA0NatFirewall_basic6822(t *testing.T) {
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
"region_no": "cn-shenzhen",
"region_no": defaultRegionToTest,
"vswitch_auto": "true",
"strict_mode": "0",
"vpc_id": CHECKSET,
Expand Down Expand Up @@ -144,6 +143,9 @@ variable "name" {
default = "%s"
}
data "alicloud_regions" "current" {
current = true
}
data "alicloud_zones" "default" {
available_resource_creation = "VSwitch"
}
Expand Down Expand Up @@ -194,7 +196,7 @@ resource "alicloud_snat_entry" "defaultAKE43g" {
// Case 云防火墙资源测试-v1 6822 twin
func TestAccAliCloudCloudFirewallA0NatFirewall_basic6822_twin(t *testing.T) {
var v map[string]interface{}
checkoutSupportedRegions(t, true, []connectivity.Region{"cn-shenzhen"})
checkoutSupportedRegions(t, true, []connectivity.Region{"cn-hangzhou"})
resourceId := "alicloud_cloud_firewall_nat_firewall.default"
ra := resourceAttrInit(resourceId, AlicloudCloudFirewallNatFirewallMap6822)
rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} {
Expand All @@ -216,7 +218,7 @@ func TestAccAliCloudCloudFirewallA0NatFirewall_basic6822_twin(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccConfig(map[string]interface{}{
"region_no": "cn-shenzhen",
"region_no": "${data.alicloud_regions.current.ids.0}",
"vswitch_auto": "true",
"strict_mode": "0",
"vpc_id": "${alicloud_vpc.defaultikZ0gD.id}",
Expand All @@ -238,7 +240,7 @@ func TestAccAliCloudCloudFirewallA0NatFirewall_basic6822_twin(t *testing.T) {
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
"region_no": "cn-shenzhen",
"region_no": defaultRegionToTest,
"vswitch_auto": "true",
"strict_mode": "0",
"vpc_id": CHECKSET,
Expand Down Expand Up @@ -266,7 +268,7 @@ func TestAccAliCloudCloudFirewallA0NatFirewall_basic6822_twin(t *testing.T) {
// Case 云防火墙资源测试-v1 6822 raw
func TestAccAliCloudCloudFirewallA0NatFirewall_basic6822_raw(t *testing.T) {
var v map[string]interface{}
checkoutSupportedRegions(t, true, []connectivity.Region{"cn-shenzhen"})
checkoutSupportedRegions(t, true, []connectivity.Region{"cn-hangzhou"})
resourceId := "alicloud_cloud_firewall_nat_firewall.default"
ra := resourceAttrInit(resourceId, AlicloudCloudFirewallNatFirewallMap6822)
rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} {
Expand All @@ -288,7 +290,7 @@ func TestAccAliCloudCloudFirewallA0NatFirewall_basic6822_raw(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccConfig(map[string]interface{}{
"region_no": defaultRegionToTest,
"region_no": "${data.alicloud_regions.current.ids.0}",
"vswitch_auto": "true",
"strict_mode": "0",
"vpc_id": "${alicloud_vpc.defaultikZ0gD.id}",
Expand Down
6 changes: 4 additions & 2 deletions alicloud/service_alicloud_bss_open_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ func (s *BssOpenApiService) QueryAvailableInstances(id, instanceRegion, productC
}
action := "QueryAvailableInstances"
request := map[string]interface{}{
"InstanceIDs": id,
"ProductCode": productCode,
"ProductType": productType,
}
if id != "" {
request["InstanceIDs"] = id
}
if instanceRegion != "" {
request["Region"] = instanceRegion
}
Expand Down Expand Up @@ -78,7 +80,7 @@ func (s *BssOpenApiService) QueryAvailableInstances(id, instanceRegion, productC
}
if len(v.([]interface{})) < 1 {
return object, WrapErrorf(Error(GetNotFoundMessage(productCode+"Instance", id)), NotFoundWithResponse, response)
} else {
} else if id != "" {
if fmt.Sprint(v.([]interface{})[0].(map[string]interface{})["InstanceID"]) != id {
return object, WrapErrorf(Error(GetNotFoundMessage(productCode+"Instance", id)), NotFoundWithResponse, response)
}
Expand Down

0 comments on commit 9a77236

Please sign in to comment.