Skip to content

Commit

Permalink
supports dingtalk message
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaozhu36 committed Dec 3, 2024
1 parent 689cd89 commit ccf25b6
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 33 deletions.
18 changes: 17 additions & 1 deletion alicloud/alicloud_sweeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package alicloud

import (
"fmt"
"github.com/aliyun/credentials-go/credentials"
"log"
"os"
"strings"
"sync"
"testing"

Expand Down Expand Up @@ -52,7 +54,7 @@ var endpoints sync.Map
// sharedClientForRegion returns a common AlicloudClient setup needed for the sweeper
// functions for a give n region
func sharedClientForRegion(region string) (interface{}, error) {
var accessKey, secretKey string
var accessKey, secretKey, securityToken string
if accessKey = os.Getenv("ALICLOUD_ACCESS_KEY"); accessKey == "" {
return nil, fmt.Errorf("empty ALICLOUD_ACCESS_KEY")
}
Expand All @@ -61,6 +63,8 @@ func sharedClientForRegion(region string) (interface{}, error) {
return nil, fmt.Errorf("empty ALICLOUD_SECRET_KEY")
}

securityToken = os.Getenv("ALICLOUD_SECURITY_TOKEN")

conf := connectivity.Config{
Region: connectivity.Region(region),
RegionId: region,
Expand All @@ -69,9 +73,21 @@ func sharedClientForRegion(region string) (interface{}, error) {
Protocol: "HTTPS",
Endpoints: &endpoints,
}
if securityToken != "" {
conf.SecurityToken = securityToken
}
if accountId := os.Getenv("ALICLOUD_ACCOUNT_ID"); accountId != "" {
conf.AccountId = accountId
}
credentialConfig := new(credentials.Config).SetType("access_key").SetAccessKeyId(accessKey).SetAccessKeySecret(secretKey)
if v := strings.TrimSpace(securityToken); v != "" {
credentialConfig.SetType("sts").SetSecurityToken(v)
}
credential, err := credentials.NewCredential(credentialConfig)
if err != nil {
return nil, err
}
conf.Credential = credential

// configures a default client for the region, using the above env vars
client, err := conf.Client()
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
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: msg,
},
}

// 将消息内容转换为 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
30 changes: 16 additions & 14 deletions alicloud/resource_alicloud_cloud_firewall_nat_firewall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (

// Test CloudFirewall NatFirewall. >>> Resource test cases, automatically generated.
// Case 云防火墙资源测试-v1 6822
func TestAccAliCloudCloudFirewallA0NatFirewall_basic6822(t *testing.T) {
func TestAccAliCloudCloudFirewallNatFirewall_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 @@ -192,9 +194,9 @@ resource "alicloud_snat_entry" "defaultAKE43g" {
}

// Case 云防火墙资源测试-v1 6822 twin
func TestAccAliCloudCloudFirewallA0NatFirewall_basic6822_twin(t *testing.T) {
func TestAccAliCloudCloudFirewallNatFirewall_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 All @@ -264,9 +266,9 @@ func TestAccAliCloudCloudFirewallA0NatFirewall_basic6822_twin(t *testing.T) {
}

// Case 云防火墙资源测试-v1 6822 raw
func TestAccAliCloudCloudFirewallA0NatFirewall_basic6822_raw(t *testing.T) {
func TestAccAliCloudCloudFirewallNatFirewall_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
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ resource "alicloud_cen_transit_router_vpc_attachment" "tr-vpc1" {
}

// Case VpcCenTrFirewall全生命周期测试_副本1689148389922 3609 raw
func TestAccAliCloudCloudFirewallA0VpcCenTrFirewall_basic3609_raw(t *testing.T) {
func TestAccAliCloudCloudFirewallVpcCenTrFirewall_basic3609_raw(t *testing.T) {
var v map[string]interface{}
checkoutSupportedRegions(t, true, []connectivity.Region{"cn-shanghai"})
resourceId := "alicloud_cloud_firewall_vpc_cen_tr_firewall.default"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccAliCloudCloudFirewallA0VpcFirewallCen_basic(t *testing.T) {
func TestAccAliCloudCloudFirewallVpcFirewallCen_basic(t *testing.T) {
var v map[string]interface{}
//checkoutSupportedRegions(t, true, connectivity.CloudFirewallVpcFirewallCenSupportRegions)
resourceId := "alicloud_cloud_firewall_vpc_firewall_cen.default"
Expand Down
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 TestAccAliCloudCloudFirewallA0VpcFirewallControlPolicy_basic0(t *testing.T) {
func TestAccAliCloudCloudFirewallVpcFirewallControlPolicy_basic0(t *testing.T) {
var v map[string]interface{}
resourceId := "alicloud_cloud_firewall_vpc_firewall_control_policy.default"
ra := resourceAttrInit(resourceId, AliCloudCloudFirewallVpcFirewallControlPolicyMap0)
Expand Down Expand Up @@ -203,7 +203,7 @@ func TestAccAliCloudCloudFirewallA0VpcFirewallControlPolicy_basic0(t *testing.T)
})
}

func TestAccAliCloudCloudFirewallA0VpcFirewallControlPolicy_basic1(t *testing.T) {
func TestAccAliCloudCloudFirewallVpcFirewallControlPolicy_basic1(t *testing.T) {
var v map[string]interface{}
resourceId := "alicloud_cloud_firewall_vpc_firewall_control_policy.default"
ra := resourceAttrInit(resourceId, AliCloudCloudFirewallVpcFirewallControlPolicyMap0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccAliCloudCloudfirewallA0VpcFirewall_basic(t *testing.T) {
func TestAccAliCloudCloudfirewallVpcFirewall_basic(t *testing.T) {
var v map[string]interface{}
checkoutSupportedRegions(t, true, connectivity.CloudFirewallVpcFirewallSupportRegions)
resourceId := "alicloud_cloud_firewall_vpc_firewall.default"
Expand Down
Loading

0 comments on commit ccf25b6

Please sign in to comment.