Skip to content

Commit

Permalink
EIP: Improves the invoking api method and supports refreshing credent…
Browse files Browse the repository at this point in the history
…ial automatically
  • Loading branch information
xiaozhu36 committed Jan 30, 2025
1 parent 4e3f0fd commit f39b172
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 194 deletions.
24 changes: 0 additions & 24 deletions alicloud/connectivity/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4882,30 +4882,6 @@ func (client *AliyunClient) NewComputenestClient() (*rpc.Client, error) {

return conn, nil
}
func (client *AliyunClient) NewEipClient() (*rpc.Client, error) {
productCode := "vpc"
endpoint := ""
if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" {
if err := client.loadEndpoint(productCode); err != nil {
endpoint = "vpc.aliyuncs.com"
client.config.Endpoints.Store(productCode, endpoint)
log.Printf("[ERROR] loading %s endpoint got an error: %#v. Using the endpoint %s instead.", productCode, err, endpoint)
}
}
if v, ok := client.config.Endpoints.Load(productCode); ok && v.(string) != "" {
endpoint = v.(string)
}
if endpoint == "" {
return nil, fmt.Errorf("[ERROR] missing the product %s endpoint.", productCode)
}
sdkConfig := client.teaSdkConfig
sdkConfig.SetEndpoint(endpoint)
conn, err := rpc.NewClient(&sdkConfig)
if err != nil {
return nil, fmt.Errorf("unable to initialize the %s client: %#v", productCode, err)
}
return conn, nil
}

func (client *AliyunClient) NewFcv2Client() (*roa.Client, error) {
productCode := "fc"
Expand Down
10 changes: 2 additions & 8 deletions alicloud/data_source_alicloud_common_bandwidth_packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"regexp"

"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/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
Expand Down Expand Up @@ -220,14 +219,9 @@ func dataSourceAlicloudCommonBandwidthPackagesRead(d *schema.ResourceData, meta
}
status, statusOk := d.GetOk("status")
var response map[string]interface{}
conn, err := client.NewVpcClient()
if err != nil {
return WrapError(err)
}
var err error
for {
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2016-04-28"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Vpc", "2016-04-28", action, nil, request, true)
if err != nil {
return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_common_bandwidth_packages", action, AlibabaCloudSdkGoERROR)
}
Expand Down
10 changes: 2 additions & 8 deletions alicloud/data_source_alicloud_eip_addresses.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 @@ -381,16 +380,11 @@ func dataSourceAlicloudEipAddressesRead(d *schema.ResourceData, meta interface{}
tagsMap = v.(map[string]interface{})
}
var response map[string]interface{}
conn, err := client.NewVpcClient()
if err != nil {
return WrapError(err)
}
var err error
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("2016-04-28"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Vpc", "2016-04-28", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
12 changes: 2 additions & 10 deletions alicloud/resource_alicloud_common_bandwidth_package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,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/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
Expand Down Expand Up @@ -44,15 +42,9 @@ func testSweepCommonBandwidthPackage(region string) error {
request["PageSize"] = PageSizeLarge
request["PageNumber"] = 1
var response map[string]interface{}
conn, err := client.NewVpcClient()
if err != nil {
return WrapError(err)
}
packageIds := make([]string, 0)
for {
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2016-04-28"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Vpc", "2016-04-28", action, nil, request, true)
if err != nil {
return fmt.Errorf("Error retrieving CommonBandwidthPackages: %s", err)
}
Expand Down Expand Up @@ -94,7 +86,7 @@ func testSweepCommonBandwidthPackage(region string) error {
request["RegionId"] = client.RegionId
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(10*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2016-04-28"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
response, err = client.RpcPost("Vpc", "2016-04-28", action, nil, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
60 changes: 10 additions & 50 deletions alicloud/resource_alicloud_eip_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,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 @@ -185,10 +184,7 @@ func resourceAliCloudEipAddressCreate(d *schema.ResourceData, meta interface{})
var request map[string]interface{}
var response map[string]interface{}
query := make(map[string]interface{})
conn, err := client.NewEipClient()
if err != nil {
return WrapError(err)
}
var err error
request = make(map[string]interface{})
query["InstanceId"] = d.Get("allocation_id")
request["RegionId"] = client.RegionId
Expand Down Expand Up @@ -274,11 +270,9 @@ func resourceAliCloudEipAddressCreate(d *schema.ResourceData, meta interface{})
request["AutoPay"] = autoPay
}

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("2016-04-28"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("Vpc", "2016-04-28", action, query, request, true)
if err != nil {
if IsExpectedErrors(err, []string{"OperationConflict", "LastTokenProcessing", "IncorrectStatus", "SystemBusy", "ServiceUnavailable", "FrequentPurchase.EIP"}) || NeedRetry(err) {
wait()
Expand Down Expand Up @@ -415,10 +409,7 @@ func resourceAliCloudEipAddressUpdate(d *schema.ResourceData, meta interface{})
update := false
d.Partial(true)
action := "ModifyEipAddressAttribute"
conn, err := client.NewEipClient()
if err != nil {
return WrapError(err)
}
var err error
request = make(map[string]interface{})
query = make(map[string]interface{})
query["AllocationId"] = d.Id()
Expand All @@ -444,11 +435,9 @@ func resourceAliCloudEipAddressUpdate(d *schema.ResourceData, meta interface{})
}

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("2016-04-28"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("Vpc", "2016-04-28", action, query, request, false)
if err != nil {
if IsExpectedErrors(err, []string{"OperationConflict", "LastTokenProcessing", "IncorrectStatus", "SystemBusy", "ServiceUnavailable", "IncorrectEipStatus", "IncorrectStatus.ResourceStatus", "VPC_TASK_CONFLICT"}) || NeedRetry(err) {
wait()
Expand All @@ -470,10 +459,6 @@ func resourceAliCloudEipAddressUpdate(d *schema.ResourceData, meta interface{})
}
update = false
action = "SetHighDefinitionMonitorLogStatus"
conn, err = client.NewEipClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
query = make(map[string]interface{})
query["InstanceId"] = d.Id()
Expand All @@ -496,11 +481,9 @@ func resourceAliCloudEipAddressUpdate(d *schema.ResourceData, meta interface{})
request["InstanceType"] = "EIP"

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("2016-04-28"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("Vpc", "2016-04-28", action, query, request, false)
if err != nil {
if IsExpectedErrors(err, []string{"OperationConflict", "LastTokenProcessing", "IncorrectStatus", "SystemBusy", "ServiceUnavailable", "IncorrectEipStatus", "IncorrectInstanceStatus", "InvalidBindingStatus", "IncorrectStatus.NatGateway", "InvalidStatus.EcsStatusNotSupport", "InvalidStatus.InstanceHasBandWidth", "InvalidStatus.EniStatusNotSupport", "TaskConflict"}) || NeedRetry(err) {
wait()
Expand All @@ -517,10 +500,6 @@ func resourceAliCloudEipAddressUpdate(d *schema.ResourceData, meta interface{})
}
update = false
action = "MoveResourceGroup"
conn, err = client.NewEipClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
query = make(map[string]interface{})
query["ResourceId"] = d.Id()
Expand All @@ -533,11 +512,9 @@ func resourceAliCloudEipAddressUpdate(d *schema.ResourceData, meta interface{})
request["ResourceType"] = "EIP"

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("2016-04-28"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("Vpc", "2016-04-28", action, query, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand All @@ -554,10 +531,6 @@ func resourceAliCloudEipAddressUpdate(d *schema.ResourceData, meta interface{})
}
update = false
action = "DeletionProtection"
conn, err = client.NewEipClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
query = make(map[string]interface{})
query["InstanceId"] = d.Id()
Expand All @@ -571,11 +544,9 @@ func resourceAliCloudEipAddressUpdate(d *schema.ResourceData, meta interface{})
request["Type"] = "EIP"

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("2016-04-28"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("Vpc", "2016-04-28", action, query, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand All @@ -592,10 +563,6 @@ func resourceAliCloudEipAddressUpdate(d *schema.ResourceData, meta interface{})
}
update = false
action = "ModifyEipForwardMode"
conn, err = client.NewEipClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
query = make(map[string]interface{})
query["InstanceId"] = d.Id()
Expand All @@ -607,11 +574,9 @@ func resourceAliCloudEipAddressUpdate(d *schema.ResourceData, meta interface{})
}

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("2016-04-28"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("Vpc", "2016-04-28", action, query, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -650,19 +615,14 @@ func resourceAliCloudEipAddressDelete(d *schema.ResourceData, meta interface{})
var request map[string]interface{}
var response map[string]interface{}
query := make(map[string]interface{})
conn, err := client.NewEipClient()
if err != nil {
return WrapError(err)
}
var err error
request = make(map[string]interface{})
query["AllocationId"] = d.Id()
request["RegionId"] = client.RegionId

runtime := util.RuntimeOptions{}
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("2016-04-28"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("Vpc", "2016-04-28", action, query, request, false)

if err != nil {
if IsExpectedErrors(err, []string{"OperationConflict", "LastTokenProcessing", "IncorrectStatus", "SystemBusy", "ServiceUnavailable", "IncorrectEipStatus", "TaskConflict.AssociateGlobalAccelerationInstance"}) || NeedRetry(err) {
Expand Down
14 changes: 2 additions & 12 deletions alicloud/resource_alicloud_eip_address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ func testSweepEipAddress(region string) error {
"tf-testAcc",
"tf_testAcc",
}
conn, err := client.NewVpcClient()
if err != nil {
return WrapError(err)
}

action := "DescribeEipAddresses"
request := map[string]interface{}{
Expand All @@ -55,11 +51,9 @@ func testSweepEipAddress(region string) error {
addressIds := make([]string, 0)
var response map[string]interface{}
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("2016-04-28"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Vpc", "2016-04-28", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -101,17 +95,13 @@ func testSweepEipAddress(region string) error {
for _, addressId := range addressIds {
log.Printf("[INFO] Deleting Eip Address: (%s)", addressId)
action := "ReleaseEipAddress"
conn, err := client.NewVpcClient()
if err != nil {
return WrapError(err)
}
request := map[string]interface{}{
"AllocationId": addressId,
}
request["RegionId"] = client.RegionId
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(time.Minute*9, func() *resource.RetryError {
_, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2016-04-28"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
_, err = client.RpcPost("Vpc", "2016-04-28", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
Loading

0 comments on commit f39b172

Please sign in to comment.