Skip to content

Commit

Permalink
resource/alicloud_alidns_instance: Improves the invoking api method a…
Browse files Browse the repository at this point in the history
…nd supports refreshing credential automatically; resource/alicloud_alidns_gtm_instance: Improves the invoking api method and supports refreshing credential automatically
  • Loading branch information
xiaozhu36 committed Jan 24, 2025
1 parent 481ac9f commit a822a20
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 54 deletions.
30 changes: 12 additions & 18 deletions alicloud/resource_alicloud_alidns_gtm_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,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 @@ -164,18 +163,19 @@ func resourceAlicloudAlidnsGtmInstance() *schema.Resource {
func resourceAlicloudAlidnsGtmInstanceCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
var response map[string]interface{}
var endpoint string
var err error
action := "CreateInstance"
request := make(map[string]interface{})
conn, err := client.NewBssopenapiClient()
if err != nil {
return WrapError(err)
}
request["SubscriptionType"] = d.Get("payment_type")
if v, ok := d.GetOk("period"); ok {
request["Period"] = v
}
request["ProductCode"] = "dns"
request["ProductType"] = "dns_gtm_public_cn"
if client.IsInternationalAccount() {
request["ProductType"] = "dns_gtm_public_intl"
}
if v, ok := d.GetOk("renew_period"); ok {
request["RenewPeriod"] = v
}
Expand All @@ -201,15 +201,15 @@ func resourceAlicloudAlidnsGtmInstanceCreate(d *schema.ResourceData, meta interf
request["ClientToken"] = buildClientToken("CreateInstance")
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2017-12-14"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
response, err = client.RpcPostWithEndpoint("BssOpenApi", "2017-12-14", action, nil, request, true, endpoint)
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
if IsExpectedErrors(err, []string{"NotApplicable"}) {
if !client.IsInternationalAccount() && IsExpectedErrors(err, []string{"NotApplicable"}) {
request["ProductType"] = "dns_gtm_public_intl"
conn.Endpoint = String(connectivity.BssOpenAPIEndpointInternational)
endpoint = connectivity.BssOpenAPIEndpointInternational
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
Expand All @@ -221,9 +221,6 @@ func resourceAlicloudAlidnsGtmInstanceCreate(d *schema.ResourceData, meta interf
return WrapErrorf(err, DefaultErrorMsg, "alicloud_alidns_gtm_instance", action, AlibabaCloudSdkGoERROR)
}

if fmt.Sprint(response["Code"]) != "Success" {
return WrapError(fmt.Errorf("%s failed, response: %v", action, response))
}
responseData := response["Data"].(map[string]interface{})
d.SetId(fmt.Sprint(responseData["InstanceId"]))
return resourceAlicloudAlidnsGtmInstanceUpdate(d, meta)
Expand Down Expand Up @@ -285,10 +282,7 @@ func resourceAlicloudAlidnsGtmInstanceRead(d *schema.ResourceData, meta interfac
}
func resourceAlicloudAlidnsGtmInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
conn, err := client.NewAlidnsClient()
if err != nil {
return WrapError(err)
}
var err error
var response map[string]interface{}
d.Partial(true)

Expand All @@ -309,7 +303,7 @@ func resourceAlicloudAlidnsGtmInstanceUpdate(d *schema.ResourceData, meta interf
action := "MoveGtmResourceGroup"
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2015-01-09"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
response, err = client.RpcPost("Alidns", "2015-01-09", action, nil, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -342,7 +336,7 @@ func resourceAlicloudAlidnsGtmInstanceUpdate(d *schema.ResourceData, meta interf
action := "SwitchDnsGtmInstanceStrategyMode"
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2015-01-09"), StringPointer("AK"), nil, switchDnsGtmInstanceStrategyModeRequest, &util.RuntimeOptions{})
response, err = client.RpcPost("Alidns", "2015-01-09", action, nil, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -437,7 +431,7 @@ func resourceAlicloudAlidnsGtmInstanceUpdate(d *schema.ResourceData, meta interf
action := "UpdateDnsGtmInstanceGlobalConfig"
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2015-01-09"), StringPointer("AK"), nil, updateDnsGtmInstanceGlobalConfigReq, &util.RuntimeOptions{})
response, err = client.RpcPost("Alidns", "2015-01-09", action, nil, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
18 changes: 8 additions & 10 deletions alicloud/resource_alicloud_alidns_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,19 @@ func resourceAlicloudAlidnsInstance() *schema.Resource {
func resourceAlicloudAlidnsInstanceCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
var response map[string]interface{}
var endpoint string
var err error
action := "CreateInstance"
request := make(map[string]interface{})
conn, err := client.NewBssopenapiClient()
if err != nil {
return WrapError(err)
}
request["SubscriptionType"] = d.Get("payment_type")
if v, ok := d.GetOk("period"); ok {
request["Period"] = v
}
request["ProductCode"] = "dns"
request["ProductType"] = "alidns_pre"
if client.IsInternationalAccount() {
request["ProductType"] = "dns_dns_public_intl"
}
if v, ok := d.GetOk("renew_period"); ok {
request["RenewPeriod"] = v
}
Expand All @@ -116,15 +117,15 @@ func resourceAlicloudAlidnsInstanceCreate(d *schema.ResourceData, meta interface
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2017-12-14"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPostWithEndpoint("BssOpenApi", "2017-12-14", action, nil, request, true, endpoint)
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
if IsExpectedErrors(err, []string{"NotApplicable"}) {
conn.Endpoint = String(connectivity.BssOpenAPIEndpointInternational)
if !client.IsInternationalAccount() && IsExpectedErrors(err, []string{"NotApplicable"}) {
request["ProductType"] = "dns_dns_public_intl"
endpoint = connectivity.BssOpenAPIEndpointInternational
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
Expand All @@ -135,9 +136,6 @@ func resourceAlicloudAlidnsInstanceCreate(d *schema.ResourceData, meta interface
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, "alicloud_alidns_instance", action, AlibabaCloudSdkGoERROR)
}
if fmt.Sprint(response["Code"]) != "Success" {
return WrapErrorf(fmt.Errorf("%v", response), DefaultErrorMsg, "alicloud_alidns_instance", action, AlibabaCloudSdkGoERROR)
}
responseData := response["Data"].(map[string]interface{})
d.SetId(fmt.Sprint(responseData["InstanceId"]))

Expand Down
18 changes: 4 additions & 14 deletions alicloud/service_alicloud_alidns.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,21 +166,16 @@ func (s *AlidnsService) DescribeAlidnsDomain(id string) (object alidns.DescribeD
}

func (s *AlidnsService) DescribeAlidnsInstance(id string) (object map[string]interface{}, err error) {
client := s.client
var response map[string]interface{}
conn, err := s.client.NewAlidnsClient()
if err != nil {
return nil, WrapError(err)
}
action := "DescribeDnsProductInstance"
request := map[string]interface{}{
"RegionId": s.client.RegionId,
"InstanceId": id,
}
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(11*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2015-01-09"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Alidns", "2015-01-09", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -284,20 +279,15 @@ func (s *AlidnsService) DescribeCustomLine(id string) (object map[string]interfa
}

func (s *AlidnsService) DescribeAlidnsGtmInstance(id string) (object map[string]interface{}, err error) {
client := s.client
var response map[string]interface{}
conn, err := s.client.NewAlidnsClient()
if err != nil {
return nil, WrapError(err)
}
action := "DescribeDnsGtmInstance"
request := map[string]interface{}{
"InstanceId": id,
}
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("2015-01-09"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Alidns", "2015-01-09", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
24 changes: 12 additions & 12 deletions alicloud/service_alicloud_bss_open_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ type BssOpenApiService struct {
}

func (s *BssOpenApiService) QueryAvailableInstances(id, instanceRegion, productCode, productType, productCodeIntl, productTypeIntl string) (object map[string]interface{}, err error) {
client := s.client
var response map[string]interface{}
conn, err := s.client.NewBssopenapiClient()
if err != nil {
return nil, WrapError(err)
}
var endpoint string
action := "QueryAvailableInstances"
request := map[string]interface{}{
"ProductCode": productCode,
"ProductType": productType,
}
if client.IsInternationalAccount() {
request["ProductCode"] = productCodeIntl
request["ProductType"] = productTypeIntl
}
if id != "" {
request["InstanceIDs"] = id
}
Expand All @@ -35,16 +37,16 @@ func (s *BssOpenApiService) QueryAvailableInstances(id, instanceRegion, productC
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("2017-12-14"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPostWithEndpoint("BssOpenApi", "2017-12-14", action, nil, request, true, endpoint)
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
if IsExpectedErrors(err, []string{"NotApplicable", "SignatureDoesNotMatch"}) {
conn.Endpoint = String(connectivity.BssOpenAPIEndpointInternational)
if !client.IsInternationalAccount() && IsExpectedErrors(err, []string{"NotApplicable"}) {
request["ProductCode"] = productCodeIntl
request["ProductType"] = productTypeIntl
endpoint = connectivity.BssOpenAPIEndpointInternational
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
Expand All @@ -55,8 +57,8 @@ func (s *BssOpenApiService) QueryAvailableInstances(id, instanceRegion, productC
if productTypeIntl != "" {
request["ProductType"] = productTypeIntl
}
conn.Endpoint = String(connectivity.BssOpenAPIEndpointInternational)
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2017-12-14"), StringPointer("AK"), nil, request, &runtime)
endpoint = connectivity.BssOpenAPIEndpointInternational
response, err = client.RpcPostWithEndpoint("BssOpenApi", "2017-12-14", action, nil, request, true, endpoint)
if err != nil {
if NeedRetry(err) {
wait()
Expand All @@ -71,9 +73,7 @@ func (s *BssOpenApiService) QueryAvailableInstances(id, instanceRegion, productC
if err != nil {
return object, WrapErrorf(err, DefaultErrorMsg, id, action, AlibabaCloudSdkGoERROR)
}
if fmt.Sprint(response["Code"]) != "Success" {
return object, WrapError(fmt.Errorf("%s failed, response: %v", action, response))
}

v, err := jsonpath.Get("$.Data.InstanceList", response)
if err != nil {
return object, WrapErrorf(err, FailedGetAttributeMsg, id, "$.Data.InstanceList", response)
Expand Down

0 comments on commit a822a20

Please sign in to comment.