Skip to content

Commit

Permalink
ESA: Improves the invoking esa api method and supports refreshing cre…
Browse files Browse the repository at this point in the history
…dential automatically
  • Loading branch information
xiaozhu36 committed Dec 20, 2024
1 parent 77043fc commit c3fbf81
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 88 deletions.
4 changes: 4 additions & 0 deletions alicloud/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ func testAccPreCheckWithAccountSiteType(t *testing.T, account AccountSite) {
defaultAccount := string(DomesticSite)
if v := strings.TrimSpace(os.Getenv("ALICLOUD_ACCOUNT_SITE")); v != "" {
defaultAccount = v
} else if v = strings.TrimSpace(os.Getenv("ALIBABA_CLOUD_ACCOUNT_TYPE")); v != "" {
defaultAccount = v
} else if v = strings.TrimSpace(os.Getenv("ALICLOUD_ACCOUNT_TYPE")); v != "" {
defaultAccount = v
}
if defaultAccount != string(account) {
t.Skipf("Skipping unsupported account type %s-Site. It only supports %s-Site.", defaultAccount, account)
Expand Down
19 changes: 4 additions & 15 deletions alicloud/resource_alicloud_esa_rate_plan_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"log"
"time"

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 @@ -82,10 +81,7 @@ func resourceAliCloudEsaRatePlanInstanceCreate(d *schema.ResourceData, meta inte
var request map[string]interface{}
var response map[string]interface{}
query := make(map[string]interface{})
conn, err := client.NewEsaClient()
if err != nil {
return WrapError(err)
}
var err error
request = make(map[string]interface{})

if v, ok := d.GetOk("plan_name"); ok {
Expand All @@ -109,11 +105,9 @@ func resourceAliCloudEsaRatePlanInstanceCreate(d *schema.ResourceData, meta inte
if v, ok := d.GetOkExists("auto_pay"); ok {
request["AutoPay"] = v
}
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2024-09-10"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("ESA", "2024-09-10", action, query, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -186,10 +180,7 @@ func resourceAliCloudEsaRatePlanInstanceUpdate(d *schema.ResourceData, meta inte
var query map[string]interface{}
update := false
action := "UpdateRatePlanSpec"
conn, err := client.NewEsaClient()
if err != nil {
return WrapError(err)
}
var err error
request = make(map[string]interface{})
query = make(map[string]interface{})
request["InstanceId"] = d.Id()
Expand All @@ -207,11 +198,9 @@ func resourceAliCloudEsaRatePlanInstanceUpdate(d *schema.ResourceData, meta inte

request["AutoPay"] = true
if update {
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2024-09-10"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("ESA", "2024-09-10", action, query, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
64 changes: 64 additions & 0 deletions alicloud/resource_alicloud_esa_rate_plan_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestAccAliCloudEsaRatePlanInstance_basic8489(t *testing.T) {
testAccConfig := resourceTestAccConfigFunc(resourceId, name, AlicloudEsaRatePlanInstanceBasicDependence8489)
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheckWithAccountSiteType(t, DomesticSite)
testAccPreCheckWithRegions(t, true, []connectivity.Region{"cn-hangzhou"})
testAccPreCheck(t)
},
Expand Down Expand Up @@ -90,3 +91,66 @@ variable "name" {
}

// Test Esa RatePlanInstance. <<< Resource test cases, automatically generated.

func TestAccAliCloudEsaRatePlanInstance_intl(t *testing.T) {
var v map[string]interface{}
resourceId := "alicloud_esa_rate_plan_instance.default"
ra := resourceAttrInit(resourceId, AlicloudEsaRatePlanInstanceMap8489)
rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} {
return &EsaServiceV2{testAccProvider.Meta().(*connectivity.AliyunClient)}
}, "DescribeEsaRatePlanInstance")
rac := resourceAttrCheckInit(rc, ra)
testAccCheck := rac.resourceAttrMapUpdateSet()
rand := acctest.RandIntRange(10000, 99999)
name := fmt.Sprintf("tf-testacc%sesarateplaninstance%d", defaultRegionToTest, rand)
testAccConfig := resourceTestAccConfigFunc(resourceId, name, AlicloudEsaRatePlanInstanceBasicDependence8489)
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheckWithAccountSiteType(t, IntlSite)
testAccPreCheckWithRegions(t, true, []connectivity.Region{"cn-hangzhou"})
testAccPreCheck(t)
},
IDRefreshName: resourceId,
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccConfig(map[string]interface{}{
"type": "NS",
"auto_renew": "true",
"period": "1",
"payment_type": "Subscription",
"coverage": "overseas",
"plan_name": "entranceplan_intl",
"auto_pay": "true",
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
"type": "NS",
"auto_renew": "true",
"period": "1",
"payment_type": "Subscription",
"coverage": "overseas",
"plan_name": "entranceplan_intl",
"auto_pay": "true",
}),
),
},
{
Config: testAccConfig(map[string]interface{}{
"plan_name": "basicplan_intl",
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
"plan_name": "basicplan_intl",
}),
),
},
{
ResourceName: resourceId,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"auto_pay", "auto_renew", "coverage", "period", "type"},
},
},
})
}
27 changes: 7 additions & 20 deletions alicloud/resource_alicloud_esa_site.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,7 @@ func resourceAliCloudEsaSiteCreate(d *schema.ResourceData, meta interface{}) err
var request map[string]interface{}
var response map[string]interface{}
query := make(map[string]interface{})
conn, err := client.NewEsaClient()
if err != nil {
return WrapError(err)
}
var err error
request = make(map[string]interface{})
request["RegionId"] = client.RegionId

Expand All @@ -90,7 +87,7 @@ func resourceAliCloudEsaSiteCreate(d *schema.ResourceData, meta interface{}) err
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2024-09-10"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("ESA", "2024-09-10", action, query, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -175,10 +172,7 @@ func resourceAliCloudEsaSiteUpdate(d *schema.ResourceData, meta interface{}) err
update := false
d.Partial(true)
action := "ChangeResourceGroup"
conn, err := client.NewEsaClient()
if err != nil {
return WrapError(err)
}
var err error
request = make(map[string]interface{})
query = make(map[string]interface{})
request["SiteId"] = d.Id()
Expand All @@ -192,7 +186,7 @@ func resourceAliCloudEsaSiteUpdate(d *schema.ResourceData, meta interface{}) err
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2024-09-10"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("ESA", "2024-09-10", action, query, request, false)
if err != nil {
if IsExpectedErrors(err, []string{"Site.ServiceBusy"}) || NeedRetry(err) {
wait()
Expand All @@ -214,10 +208,6 @@ func resourceAliCloudEsaSiteUpdate(d *schema.ResourceData, meta interface{}) err
}
update = false
action = "UpdateSiteCoverage"
conn, err = client.NewEsaClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
query = make(map[string]interface{})
request["SiteId"] = d.Id()
Expand All @@ -231,7 +221,7 @@ func resourceAliCloudEsaSiteUpdate(d *schema.ResourceData, meta interface{}) err
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2024-09-10"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("ESA", "2024-09-10", action, query, request, false)
if err != nil {
if IsExpectedErrors(err, []string{"Site.ServiceBusy"}) || NeedRetry(err) {
wait()
Expand Down Expand Up @@ -269,10 +259,7 @@ func resourceAliCloudEsaSiteDelete(d *schema.ResourceData, meta interface{}) err
var request map[string]interface{}
var response map[string]interface{}
query := make(map[string]interface{})
conn, err := client.NewEsaClient()
if err != nil {
return WrapError(err)
}
var err error
request = make(map[string]interface{})
request["SiteId"] = d.Id()
request["RegionId"] = client.RegionId
Expand All @@ -281,7 +268,7 @@ func resourceAliCloudEsaSiteDelete(d *schema.ResourceData, meta interface{}) err
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2024-09-10"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("ESA", "2024-09-10", action, query, request, false)

if err != nil {
if NeedRetry(err) {
Expand Down
127 changes: 127 additions & 0 deletions alicloud/resource_alicloud_esa_site_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestAccAliCloudEsaSite_basic8490(t *testing.T) {
testAccConfig := resourceTestAccConfigFunc(resourceId, name, AlicloudEsaSiteBasicDependence8490)
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheckWithAccountSiteType(t, DomesticSite)
testAccPreCheckWithRegions(t, true, []connectivity.Region{"cn-hangzhou"})
testAccPreCheck(t)
},
Expand Down Expand Up @@ -167,3 +168,129 @@ resource "alicloud_esa_rate_plan_instance" "defaultIEoDfU" {
}

// Test Esa Site. <<< Resource test cases, automatically generated.

func TestAccAliCloudEsaSite_intl(t *testing.T) {
var v map[string]interface{}
resourceId := "alicloud_esa_site.default"
ra := resourceAttrInit(resourceId, AlicloudEsaSiteMap8490)
rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} {
return &EsaServiceV2{testAccProvider.Meta().(*connectivity.AliyunClient)}
}, "DescribeEsaSite")
rac := resourceAttrCheckInit(rc, ra)
testAccCheck := rac.resourceAttrMapUpdateSet()
rand := acctest.RandIntRange(10000, 99999)
name := fmt.Sprintf("bcd%d.com", rand)
testAccConfig := resourceTestAccConfigFunc(resourceId, name, AlicloudEsaSiteBasicDependenceIntl)
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheckWithAccountSiteType(t, IntlSite)
testAccPreCheckWithRegions(t, true, []connectivity.Region{"cn-hangzhou"})
testAccPreCheck(t)
},
IDRefreshName: resourceId,
Providers: testAccProviders,
CheckDestroy: rac.checkResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccConfig(map[string]interface{}{
"site_name": name,
"coverage": "overseas",
"access_type": "NS",
"instance_id": "${alicloud_esa_rate_plan_instance.defaultIEoDfU.id}",
"resource_group_id": "${data.alicloud_resource_manager_resource_groups.default.ids.0}",
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
"site_name": name,
"coverage": "overseas",
"access_type": "NS",
"instance_id": CHECKSET,
"resource_group_id": CHECKSET,
}),
),
},
// entranceplan_intl and basicplan_intl only support overseas
//{
// Config: testAccConfig(map[string]interface{}{
// "coverage": "global",
// "resource_group_id": "${data.alicloud_resource_manager_resource_groups.default.ids.1}",
// }),
// Check: resource.ComposeTestCheckFunc(
// testAccCheck(map[string]string{
// "coverage": "global",
// "resource_group_id": CHECKSET,
// }),
// ),
//},
{
Config: testAccConfig(map[string]interface{}{
"tags": map[string]string{
"Created": "TF",
"For": "Test",
},
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
"tags.%": "2",
"tags.Created": "TF",
"tags.For": "Test",
}),
),
},
{
Config: testAccConfig(map[string]interface{}{
"tags": map[string]string{
"Created": "TF-update",
"For": "Test-update",
},
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
"tags.%": "2",
"tags.Created": "TF-update",
"tags.For": "Test-update",
}),
),
},
{
Config: testAccConfig(map[string]interface{}{
"tags": REMOVEKEY,
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
"tags.%": "0",
"tags.Created": REMOVEKEY,
"tags.For": REMOVEKEY,
}),
),
},
{
ResourceName: resourceId,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{},
},
},
})
}

func AlicloudEsaSiteBasicDependenceIntl(name string) string {
return fmt.Sprintf(`
variable "name" {
default = "%s"
}
data "alicloud_resource_manager_resource_groups" "default" {
}
resource "alicloud_esa_rate_plan_instance" "defaultIEoDfU" {
type = "NS"
auto_renew = true
period = "1"
payment_type = "Subscription"
coverage = "overseas"
auto_pay = true
plan_name = "entranceplan_intl"
}
`, name)
}
Loading

0 comments on commit c3fbf81

Please sign in to comment.