Skip to content

Commit

Permalink
Eipanycast: Improves the invoking api method and supports refreshing …
Browse files Browse the repository at this point in the history
…credential automatically
  • Loading branch information
xiaozhu36 committed Feb 3, 2025
1 parent af5bc87 commit e8d53f2
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 228 deletions.
96 changes: 0 additions & 96 deletions alicloud/connectivity/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2222,102 +2222,6 @@ func (client *AliyunClient) WithRKvstoreClient(do func(*r_kvstore.Client) (inter
return do(client.r_kvstoreConn)
}

func (client *AliyunClient) NewMseClient() (*rpc.Client, error) {
productCode := "mse"
endpoint := ""
if v, exist := client.config.Endpoints.Load(productCode); !exist || v.(string) == "" {
if err := client.loadEndpoint(productCode); err != nil {
return nil, err
}
}
if v, exist := client.config.Endpoints.Load(productCode); exist && 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) NewHitsdbClient() (*rpc.Client, error) {
productCode := "hitsdb"
endpoint := ""
if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" {
if err := client.loadEndpoint(productCode); err != nil {
return nil, err
}
}
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) NewAistudioClient() (*rpc.Client, error) {
productCode := "brain_industrial"
endpoint := ""
if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" {
if err := client.loadEndpoint(productCode); err != nil {
endpoint = "brain-industrial.cn-hangzhou.aliyuncs.com"
client.config.Endpoints.Store(productCode, endpoint)
log.Printf("[ERROR] loading %s endpoint got an error: %#v. Using the central 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) NewEipanycastClient() (*rpc.Client, error) {
productCode := "eipanycast"
endpoint := ""
if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" {
if err := client.loadEndpoint(productCode); err != nil {
endpoint = "eipanycast.cn-hangzhou.aliyuncs.com"
client.config.Endpoints.Store(productCode, endpoint)
log.Printf("[ERROR] loading %s endpoint got an error: %#v. Using the central 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) NewOosClient() (*rpc.Client, error) {
productCode := "oos"
endpoint := ""
Expand Down
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 dataSourceAlicloudEipanycastAnycastEipAddressesRead(d *schema.ResourceData,
}
}
var response map[string]interface{}
conn, err := client.NewEipanycastClient()
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("2020-03-09"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Eipanycast", "2020-03-09", action, nil, request, true)
if err != nil {
return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_eipanycast_anycast_eip_addresses", action, AlibabaCloudSdkGoERROR)
}
Expand Down
42 changes: 8 additions & 34 deletions alicloud/resource_alicloud_eipanycast_anycast_eip_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,7 @@ func resourceAliCloudEipanycastAnycastEipAddressCreate(d *schema.ResourceData, m
var request map[string]interface{}
var response map[string]interface{}
query := make(map[string]interface{})
conn, err := client.NewEipanycastClient()
if err != nil {
return WrapError(err)
}
var err error
request = make(map[string]interface{})
request["RegionId"] = client.RegionId
request["ClientToken"] = buildClientToken(action)
Expand Down Expand Up @@ -115,7 +112,7 @@ func resourceAliCloudEipanycastAnycastEipAddressCreate(d *schema.ResourceData, m
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("2020-03-09"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("Eipanycast", "2020-03-09", action, query, request, true)
request["ClientToken"] = buildClientToken(action)

if err != nil {
Expand Down Expand Up @@ -182,10 +179,7 @@ func resourceAliCloudEipanycastAnycastEipAddressUpdate(d *schema.ResourceData, m
update := false
d.Partial(true)
action := "ModifyAnycastEipAddressAttribute"
conn, err := client.NewEipanycastClient()
if err != nil {
return WrapError(err)
}
var err error
request = make(map[string]interface{})
query = make(map[string]interface{})
query["AnycastId"] = d.Id()
Expand All @@ -201,11 +195,9 @@ func resourceAliCloudEipanycastAnycastEipAddressUpdate(d *schema.ResourceData, m
}

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("2020-03-09"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("Eipanycast", "2020-03-09", action, query, request, true)

if err != nil {
if NeedRetry(err) {
Expand All @@ -225,10 +217,6 @@ func resourceAliCloudEipanycastAnycastEipAddressUpdate(d *schema.ResourceData, m
}
update = false
action = "ModifyAnycastEipAddressSpec"
conn, err = client.NewEipanycastClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
query = make(map[string]interface{})
query["AnycastId"] = d.Id()
Expand All @@ -239,11 +227,9 @@ func resourceAliCloudEipanycastAnycastEipAddressUpdate(d *schema.ResourceData, m
}

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("2020-03-09"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("Eipanycast", "2020-03-09", action, query, request, true)

if err != nil {
if NeedRetry(err) {
Expand All @@ -267,10 +253,6 @@ func resourceAliCloudEipanycastAnycastEipAddressUpdate(d *schema.ResourceData, m
}
update = false
action = "ChangeResourceGroup"
conn, err = client.NewEipanycastClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
query = make(map[string]interface{})
query["ResourceId"] = d.Id()
Expand All @@ -282,11 +264,9 @@ func resourceAliCloudEipanycastAnycastEipAddressUpdate(d *schema.ResourceData, m
}

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("2020-03-09"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("Eipanycast", "2020-03-09", action, query, request, true)

if err != nil {
if NeedRetry(err) {
Expand Down Expand Up @@ -322,21 +302,15 @@ func resourceAliCloudEipanycastAnycastEipAddressDelete(d *schema.ResourceData, m
var request map[string]interface{}
var response map[string]interface{}
query := make(map[string]interface{})
conn, err := client.NewEipanycastClient()
if err != nil {
return WrapError(err)
}
var err error
request = make(map[string]interface{})
query["AnycastId"] = d.Id()
request["RegionId"] = client.RegionId

request["ClientToken"] = buildClientToken(action)

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("2020-03-09"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("Eipanycast", "2020-03-09", action, query, request, true)
request["ClientToken"] = buildClientToken(action)

if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strings"
"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 @@ -96,10 +95,7 @@ func resourceAliCloudEipanycastAnycastEipAddressAttachmentCreate(d *schema.Resou
action := "AssociateAnycastEipAddress"
var request map[string]interface{}
var response map[string]interface{}
conn, err := client.NewEipanycastClient()
if err != nil {
return WrapError(err)
}
var err error
request = make(map[string]interface{})
request["BindInstanceType"] = d.Get("bind_instance_type")
request["AnycastId"] = d.Get("anycast_id")
Expand All @@ -126,10 +122,8 @@ func resourceAliCloudEipanycastAnycastEipAddressAttachmentCreate(d *schema.Resou
}

wait := incrementalWait(3*time.Second, 5*time.Second)
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2020-03-09"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Eipanycast", "2020-03-09", action, nil, request, true)
request["ClientToken"] = buildClientToken(action)

if err != nil {
Expand Down Expand Up @@ -203,10 +197,7 @@ func resourceAliCloudEipanycastAnycastEipAddressAttachmentUpdate(d *schema.Resou
update := false
parts := strings.Split(d.Id(), ":")
action := "UpdateAnycastEipAddressAssociations"
conn, err := client.NewEipanycastClient()
if err != nil {
return WrapError(err)
}
var err error
request = make(map[string]interface{})
request["BindInstanceId"] = parts[1]
request["AnycastId"] = parts[0]
Expand All @@ -220,9 +211,7 @@ func resourceAliCloudEipanycastAnycastEipAddressAttachmentUpdate(d *schema.Resou
if update {
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2020-03-09"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Eipanycast", "2020-03-09", action, nil, request, true)
request["ClientToken"] = buildClientToken(action)

if err != nil {
Expand Down Expand Up @@ -255,10 +244,6 @@ func resourceAliCloudEipanycastAnycastEipAddressAttachmentUpdate(d *schema.Resou
if removed.Len() > 0 {
parts := strings.Split(d.Id(), ":")
action := "UpdateAnycastEipAddressAssociations"
conn, err := client.NewEipanycastClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
request["BindInstanceId"] = parts[1]
request["AnycastId"] = parts[0]
Expand All @@ -276,9 +261,7 @@ func resourceAliCloudEipanycastAnycastEipAddressAttachmentUpdate(d *schema.Resou

wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2020-03-09"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Eipanycast", "2020-03-09", action, nil, request, true)
request["ClientToken"] = buildClientToken(action)

if err != nil {
Expand All @@ -305,10 +288,6 @@ func resourceAliCloudEipanycastAnycastEipAddressAttachmentUpdate(d *schema.Resou
if added.Len() > 0 {
parts := strings.Split(d.Id(), ":")
action := "UpdateAnycastEipAddressAssociations"
conn, err := client.NewEipanycastClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
request["BindInstanceId"] = parts[1]
request["AnycastId"] = parts[0]
Expand All @@ -326,9 +305,7 @@ func resourceAliCloudEipanycastAnycastEipAddressAttachmentUpdate(d *schema.Resou

wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2020-03-09"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Eipanycast", "2020-03-09", action, nil, request, true)
request["ClientToken"] = buildClientToken(action)

if err != nil {
Expand Down Expand Up @@ -363,10 +340,7 @@ func resourceAliCloudEipanycastAnycastEipAddressAttachmentDelete(d *schema.Resou
action := "UnassociateAnycastEipAddress"
var request map[string]interface{}
var response map[string]interface{}
conn, err := client.NewEipanycastClient()
if err != nil {
return WrapError(err)
}
var err error
request = make(map[string]interface{})
request["BindInstanceType"] = parts[3]
request["AnycastId"] = parts[0]
Expand All @@ -381,9 +355,7 @@ func resourceAliCloudEipanycastAnycastEipAddressAttachmentDelete(d *schema.Resou
}
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2020-03-09"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Eipanycast", "2020-03-09", action, nil, request, true)
request["ClientToken"] = buildClientToken(action)

if err != nil {
Expand Down
Loading

0 comments on commit e8d53f2

Please sign in to comment.