Skip to content

Commit

Permalink
resource/alicloud_kms_instance: Improves the invoking api method and …
Browse files Browse the repository at this point in the history
…supports refreshing credential automatically; data-source/alicloud_kms_instances: improves the invoking api method
  • Loading branch information
xiaozhu36 committed Jan 23, 2025
1 parent 0c942e8 commit 005d4e3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 101 deletions.
10 changes: 2 additions & 8 deletions alicloud/data_source_alicloud_kms_instances.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 @@ -71,22 +70,17 @@ func dataSourceAliCloudKmsInstanceRead(d *schema.ResourceData, meta interface{})

var request map[string]interface{}
var response map[string]interface{}
var err error
var query map[string]interface{}
action := "ListKmsInstances"
conn, err := client.NewKmsClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
query = make(map[string]interface{})
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
request["PageSize"] = 5
request["PageNumber"] = 1
for {
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-01-20"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("Kms", "2016-01-20", action, query, request, true)

if err != nil {
if NeedRetry(err) {
Expand Down
124 changes: 38 additions & 86 deletions alicloud/resource_alicloud_kms_instance.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 @@ -219,11 +218,9 @@ func resourceAliCloudKmsInstanceCreate(d *schema.ResourceData, meta interface{})
action := "CreateInstance"
var request map[string]interface{}
var response map[string]interface{}
var endpoint string
var err error
query := make(map[string]interface{})
conn, err := client.NewBssopenapiClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})

request["ClientToken"] = buildClientToken(action)
Expand Down Expand Up @@ -287,30 +284,25 @@ func resourceAliCloudKmsInstanceCreate(d *schema.ResourceData, meta interface{})

request["ProductCode"] = "kms"
request["ProductType"] = "kms_ddi_public_cn"
if client.IsInternationalAccount() {
request["ProductType"] = "kms_ddi_public_intl"
}
request["SubscriptionType"] = "Subscription"
if v, ok := d.GetOk("payment_type"); ok && v == "PayAsYouGo" {
request["ProductCode"] = "kms"
request["ProductType"] = "kms_ppi_public_cn"
request["SubscriptionType"] = 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("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 IsExpectedErrors(err, []string{"InternalError"}) || NeedRetry(err) {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
if IsExpectedErrors(err, []string{"NotApplicable"}) {
request["ProductCode"] = "kms"
if !client.IsInternationalAccount() && IsExpectedErrors(err, []string{"NotApplicable"}) {
request["ProductType"] = "kms_ddi_public_intl"
if v, ok := d.GetOk("payment_type"); ok && v == "PayAsYouGo" {
request["ProductCode"] = "kms"
request["ProductType"] = "kms_ppi_public_intl"
}
conn.Endpoint = String(connectivity.BssOpenAPIEndpointInternational)
endpoint = connectivity.BssOpenAPIEndpointInternational
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
Expand All @@ -322,12 +314,6 @@ func resourceAliCloudKmsInstanceCreate(d *schema.ResourceData, meta interface{})
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, "alicloud_kms_instance", action, AlibabaCloudSdkGoERROR)
}
if fmt.Sprint(response["Success"]) == "nil" {
return WrapError(fmt.Errorf("%s failed, response: %v", action, response))
}
if fmt.Sprint(response["Success"]) != "true" {
return WrapError(fmt.Errorf("%s failed, response: %v", action, response))
}

id, _ := jsonpath.Get("$.Data.InstanceId", response)
d.SetId(fmt.Sprint(id))
Expand All @@ -339,10 +325,6 @@ func resourceAliCloudKmsInstanceCreate(d *schema.ResourceData, meta interface{})
}

action = "ConnectKmsInstance"
conn, err = client.NewKmsClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
request["KmsInstanceId"] = d.Id()

Expand All @@ -358,11 +340,9 @@ func resourceAliCloudKmsInstanceCreate(d *schema.ResourceData, meta interface{})
request["VSwitchIds"] = convertListToCommaSeparate(jsonPathResult2.([]interface{}))
}

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-01-20"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("Kms", "2016-01-20", action, query, request, false)
if err != nil {
if IsExpectedErrors(err, []string{"Forbidden.RamRoleNotFound"}) || NeedRetry(err) {
wait()
Expand Down Expand Up @@ -471,14 +451,11 @@ func resourceAliCloudKmsInstanceUpdate(d *schema.ResourceData, meta interface{})
client := meta.(*connectivity.AliyunClient)
var request map[string]interface{}
var response map[string]interface{}
var err error
var query map[string]interface{}
update := false
d.Partial(true)
action := "ModifyInstance"
conn, err := client.NewBssopenapiClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
query = make(map[string]interface{})
request["InstanceId"] = d.Id()
Expand Down Expand Up @@ -526,31 +503,29 @@ func resourceAliCloudKmsInstanceUpdate(d *schema.ResourceData, meta interface{})
}
request["Parameter"] = parameterMapList

request["ProductCode"] = "kms"
request["ProductType"] = "kms_ddi_public_cn"
if client.IsInternationalAccount() {
request["ProductType"] = "kms_ddi_public_intl"
}
request["SubscriptionType"] = "Subscription"
if v, ok := d.GetOk("payment_type"); ok && v == "PayAsYouGo" {
request["ProductType"] = "kms_ppi_public_cn"
request["SubscriptionType"] = "PayAsYouGo"
request["SubscriptionType"] = v
}
request["ProductCode"] = "kms"
request["ModifyType"] = "Upgrade"
if update && request["SubscriptionType"] == "Subscription" {
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
var endpoint string
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("2017-12-14"), StringPointer("AK"), query, request, &runtime)
err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
response, err = client.RpcPostWithEndpoint("BssOpenApi", "2017-12-14", action, query, request, true, endpoint)
if err != nil {
if IsExpectedErrors(err, []string{"InternalError"}) || NeedRetry(err) {
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"] = "kms_ddi_public_intl"
if v, ok := d.GetOk("payment_type"); ok && v == "PayAsYouGo" {
request["ProductType"] = "kms_ppi_public_intl"
}
endpoint = connectivity.BssOpenAPIEndpointInternational
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
Expand All @@ -561,9 +536,7 @@ func resourceAliCloudKmsInstanceUpdate(d *schema.ResourceData, meta interface{})
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR)
}
if fmt.Sprint(response["Code"]) != "Success" {
return WrapError(fmt.Errorf("%s failed, response: %v", action, response))
}

kmsServiceV2 := KmsServiceV2{client}
stateConf := BuildStateConf([]string{}, []string{fmt.Sprint(d.Get("key_num"))}, d.Timeout(schema.TimeoutUpdate), 10*time.Second, kmsServiceV2.KmsInstanceStateRefreshFunc(d.Id(), "KeyNum", []string{}))
if _, err := stateConf.WaitForState(); err != nil {
Expand All @@ -572,10 +545,6 @@ func resourceAliCloudKmsInstanceUpdate(d *schema.ResourceData, meta interface{})
}
update = false
action = "UpdateKmsInstanceBindVpc"
conn, err = client.NewKmsClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
query = make(map[string]interface{})
query["KmsInstanceId"] = d.Id()
Expand Down Expand Up @@ -611,11 +580,9 @@ func resourceAliCloudKmsInstanceUpdate(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("GET"), StringPointer("2016-01-20"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("Kms", "2016-01-20", action, query, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand All @@ -642,34 +609,30 @@ func resourceAliCloudKmsInstanceDelete(d *schema.ResourceData, meta interface{})
action := "RefundInstance"
var request map[string]interface{}
var response map[string]interface{}
var err error
query := make(map[string]interface{})
conn, err := client.NewBssopenapiClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
request["InstanceId"] = d.Id()

request["ClientToken"] = buildClientToken(action)

request["ProductType"] = "kms_ddi_public_cn"
request["ProductCode"] = "kms"
request["ProductType"] = "kms_ddi_public_cn"
if client.IsInternationalAccount() {
request["ProductType"] = "kms_ddi_public_intl"
}
request["ImmediatelyRelease"] = "1"
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
var endpoint string
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("2017-12-14"), StringPointer("AK"), query, request, &runtime)
request["ClientToken"] = buildClientToken(action)

err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
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"}) {
conn.Endpoint = String(connectivity.BssOpenAPIEndpointInternational)
if !client.IsInternationalAccount() && IsExpectedErrors(err, []string{"NotApplicable"}) {
request["ProductType"] = "kms_ddi_public_intl"
endpoint = connectivity.BssOpenAPIEndpointInternational
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
Expand Down Expand Up @@ -699,22 +662,18 @@ func resourceAliCloudKmsInstanceDelete(d *schema.ResourceData, meta interface{})
action := "ReleaseKmsInstance"
var request map[string]interface{}
var response map[string]interface{}
var err error
query := make(map[string]interface{})
conn, err := client.NewKmsClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
request["KmsInstanceId"] = d.Id()

if v, ok := d.GetOk("force_delete_without_backup"); ok {
request["ForceDeleteWithoutBackup"] = v
}
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-01-20"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("Kms", "2016-01-20", action, query, request, false)

if err != nil {
if NeedRetry(err) {
Expand All @@ -738,10 +697,3 @@ func resourceAliCloudKmsInstanceDelete(d *schema.ResourceData, meta interface{})
}
return nil
}

func convertKmsInstanceSubscriptionTypeRequest(source interface{}) interface{} {
source = fmt.Sprint(source)
switch source {
}
return source
}
8 changes: 1 addition & 7 deletions alicloud/service_alicloud_kms_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,15 @@ func (s *KmsServiceV2) DescribeKmsInstance(id string) (object map[string]interfa
var request map[string]interface{}
var response map[string]interface{}
var query map[string]interface{}
conn, err := client.NewKmsClient()
if err != nil {
return object, WrapError(err)
}
request = make(map[string]interface{})
query = make(map[string]interface{})
request["KmsInstanceId"] = id

action := "GetKmsInstance"

runtime := util.RuntimeOptions{}
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("2016-01-20"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("Kms", "2016-01-20", action, query, request, true)

if err != nil {
if NeedRetry(err) {
Expand Down

0 comments on commit 005d4e3

Please sign in to comment.