Skip to content

Commit

Permalink
resource/alicloud_cloud_monitor_service_basic_public: Improves the in…
Browse files Browse the repository at this point in the history
…voking api method and supports refreshing credential automatically
  • Loading branch information
xiaozhu36 committed Jan 25, 2025
1 parent 6a53eca commit fbf365a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 59 deletions.
25 changes: 2 additions & 23 deletions alicloud/resource_alicloud_cloud_monitor_service_basic_public.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +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/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -36,9 +35,7 @@ func resourceAliCloudCloudMonitorServiceBasicPublic() *schema.Resource {
}

func resourceAliCloudCloudMonitorServiceBasicPublicCreate(d *schema.ResourceData, meta interface{}) error {

client := meta.(*connectivity.AliyunClient)

action := "CreateInstance"
var request map[string]interface{}
var response map[string]interface{}
Expand All @@ -48,20 +45,16 @@ func resourceAliCloudCloudMonitorServiceBasicPublicCreate(d *schema.ResourceData
request = make(map[string]interface{})

request["ClientToken"] = buildClientToken(action)

request["ProductCode"] = "cms"
request["ProductType"] = "cms_basic_public_cn"
if client.IsInternationalAccount() {
request["ProductType"] = "cms_basic_public_intl"
}
request["SubscriptionType"] = "PayAsYouGo"
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 = client.RpcPostWithEndpoint("BssOpenApi", "2017-12-14", action, query, request, true, endpoint)
request["ClientToken"] = buildClientToken(action)

if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -121,20 +114,14 @@ func resourceAliCloudCloudMonitorServiceBasicPublicDelete(d *schema.ResourceData
action := "StopPostPayQuota"
var request map[string]interface{}
var response map[string]interface{}
var err error
query := make(map[string]interface{})
conn, err := client.NewCloudmonitorserviceClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
query["InstanceId"] = d.Id()
request["PostType"] = "postPayV2"
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("2019-01-01"), StringPointer("AK"), query, request, &runtime)

response, err = client.RpcPost("Cms", "2019-01-01", action, query, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand All @@ -157,11 +144,3 @@ func resourceAliCloudCloudMonitorServiceBasicPublicDelete(d *schema.ResourceData
}
return nil
}

func convertCloudMonitorServicePostTypeRequest(source interface{}) interface{} {
switch source {
case "cms_basic_public_cn":
return "postPayV2"
}
return source
}
49 changes: 13 additions & 36 deletions alicloud/service_alicloud_cloud_monitor_service_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,9 @@ func (s *CloudMonitorServiceServiceV2) DescribeCloudMonitorServiceBasicPublic(id
client := s.client
var request map[string]interface{}
var response map[string]interface{}
var endpoint string
var query map[string]interface{}
action := "QueryAvailableInstances"
conn, err := client.NewBssopenapiClient()
if err != nil {
return object, WrapError(err)
}
request = make(map[string]interface{})
query = make(map[string]interface{})
query["InstanceIDs"] = id
Expand All @@ -288,13 +285,17 @@ func (s *CloudMonitorServiceServiceV2) DescribeCloudMonitorServiceBasicPublic(id
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(1*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2017-12-14"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPostWithEndpoint("BssOpenApi", "2017-12-14", action, query, request, true, endpoint)

if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
if !client.IsInternationalAccount() && IsExpectedErrors(err, []string{"NotApplicable"}) {
endpoint = connectivity.BssOpenAPIEndpointInternational
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
addDebug(action, response, request)
Expand Down Expand Up @@ -458,33 +459,31 @@ func (s *CloudMonitorServiceServiceV2) DescribeCloudMonitorServiceNaamPublic(id
client := s.client
var request map[string]interface{}
var response map[string]interface{}
var endpoint string
var query map[string]interface{}
action := "QueryAvailableInstances"
conn, err := client.NewBssopenapiClient()
if err != nil {
return object, WrapError(err)
}
request = make(map[string]interface{})
query = make(map[string]interface{})
query["InstanceIDs"] = id

request["SubscriptionType"] = "PayAsYouGo"
request["ProductCode"] = "cms"
request["ProductType"] = "cms_naam_public_cn"
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
if client.IsInternationalAccount() {
request["ProductType"] = "cms_naam_public_intl"
}
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(1*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2017-12-14"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPostWithEndpoint("BssOpenApi", "2017-12-14", action, query, 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"] = "cms_naam_public_intl"
conn.Endpoint = String(connectivity.BssOpenAPIEndpointInternational)
endpoint = connectivity.BssOpenAPIEndpointInternational
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
Expand Down Expand Up @@ -524,26 +523,4 @@ func (s *CloudMonitorServiceServiceV2) DescribeCloudMonitorServiceNaamPublic(id
return object, WrapErrorf(Error(GetNotFoundMessage("NaamPublic", id)), NotFoundMsg, response)
}

func (s *CloudMonitorServiceServiceV2) CloudMonitorServiceNaamPublicStateRefreshFunc(id string, field string, failStates []string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
object, err := s.DescribeCloudMonitorServiceNaamPublic(id)
if err != nil {
if NotFoundError(err) {
return nil, "", nil
}
return nil, "", WrapError(err)
}

v, err := jsonpath.Get(field, object)
currentStatus := fmt.Sprint(v)

for _, failState := range failStates {
if currentStatus == failState {
return object, currentStatus, WrapError(Error(FailedToReachTargetStatus, currentStatus))
}
}
return object, currentStatus, nil
}
}

// DescribeCloudMonitorServiceNaamPublic >>> Encapsulated.

0 comments on commit fbf365a

Please sign in to comment.