Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/alicloud_quotas_quota_alarm: Support for international languages. #7035

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions alicloud/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -1633,6 +1633,20 @@ func genRoaParam(action, method, version, path string) *openapi.Params {
}
}

func rpcParam(action, method, version string) *openapi.Params {
return &openapi.Params{
Action: tea.String(action),
Version: tea.String(version),
Protocol: tea.String("HTTPS"),
Pathname: tea.String("/"),
Method: tea.String(method),
AuthType: tea.String("AK"),
Style: tea.String("RPC"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不是说不区分 rpc 和 roa 了么?是不是可以统一掉了

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不行,还是区分的

ReqBodyType: tea.String("formData"),
BodyType: tea.String("json"),
}
}

func genXmlParam(action, method, version, path string) *openapi.Params {
return &openapi.Params{
Action: tea.String(action),
Expand Down
37 changes: 37 additions & 0 deletions alicloud/connectivity/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ type AliyunClient struct {
config *Config
teaSdkConfig rpc.Config
teaRoaSdkConfig roa.Config
teaRpcOpenapiConfig openapi.Config
teaRoaOpenapiConfig openapi.Config
accountId string
ecsconn *ecs.Client
essconn *ess.Client
Expand Down Expand Up @@ -229,10 +231,20 @@ func (c *Config) Client() (*AliyunClient, error) {
if err != nil {
return nil, err
}
teaRpcOpenapiConfig, err := c.getTeaRpcOpenapiConfig(true)
if err != nil {
return nil, err
}
teaRoaOpenapiConfig, err := c.getTeaRoaOpenapiConfig(true)
if err != nil {
return nil, err
}
return &AliyunClient{
config: c,
teaSdkConfig: teaSdkConfig,
teaRoaSdkConfig: teaRoaSdkConfig,
teaRpcOpenapiConfig: teaRpcOpenapiConfig,
teaRoaOpenapiConfig: teaRoaOpenapiConfig,
SourceIp: c.SourceIp,
Region: c.Region,
RegionId: c.RegionId,
Expand Down Expand Up @@ -2832,6 +2844,31 @@ func (client *AliyunClient) NewQuotasClient() (*rpc.Client, error) {
return conn, nil
}

func (client *AliyunClient) NewQuotasClientV2() (*openapi.Client, error) {
productCode := "quotas"
endpoint := ""
if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" {
if err := client.loadEndpoint(productCode); err != nil {
endpoint = "quotas.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)
}
openapiConfig := client.teaRpcOpenapiConfig
openapiConfig.Endpoint = tea.String(endpoint)
result, err := openapi.NewClient(&openapiConfig)
if err != nil {
return nil, fmt.Errorf("unable to initialize the %s client: %#v", productCode, err)
}
return result, nil
}

func (client *AliyunClient) NewNasClient() (*rpc.Client, error) {
productCode := "nas"
endpoint := ""
Expand Down
53 changes: 51 additions & 2 deletions alicloud/connectivity/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package connectivity

import (
"fmt"
"github.com/alibabacloud-go/tea/tea"
"log"
"sync"

roa "github.com/alibabacloud-go/tea-roa/client"

"encoding/json"
"net/http"
"strings"

openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
roa "github.com/alibabacloud-go/tea-roa/client"
rpc "github.com/alibabacloud-go/tea-rpc/client"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
Expand Down Expand Up @@ -357,6 +358,54 @@ func (c *Config) getTeaRoaDslSdkConfig(stsSupported bool) (config roa.Config, er
}
return
}
func (c *Config) getTeaRpcOpenapiConfig(stsSupported bool) (config openapi.Config, err error) {
config.SetRegionId(c.RegionId)
config.SetUserAgent(fmt.Sprintf("%s/%s %s/%s %s/%s %s/%s", Terraform, c.TerraformVersion, Provider, providerVersion, Module, c.ConfigurationSource, TerraformTraceId, c.TerraformTraceId))
credential, err := credential.NewCredential(c.getCredentialConfig(stsSupported))
config.SetCredential(credential).
SetRegionId(c.RegionId).
SetProtocol(c.Protocol).
SetReadTimeout(c.ClientReadTimeout).
SetConnectTimeout(c.ClientConnectTimeout).
SetMaxIdleConns(500)

query := map[string]*string{
"AcceptLanguage": tea.String("en-US"),
}
if c.SourceIp != "" {
query["SourceIp"] = tea.String(c.SourceIp)
}
if c.SecureTransport != "" {
query["SecureTransport"] = tea.String(c.SecureTransport)
}

param := &openapi.GlobalParameters{Queries: query}
config.GlobalParameters = param
return
}
func (c *Config) getTeaRoaOpenapiConfig(stsSupported bool) (config openapi.Config, err error) {
config.SetRegionId(c.RegionId)
config.SetUserAgent(fmt.Sprintf("%s/%s %s/%s %s/%s %s/%s", Terraform, c.TerraformVersion, Provider, providerVersion, Module, c.ConfigurationSource, TerraformTraceId, c.TerraformTraceId))
credential, err := credential.NewCredential(c.getCredentialConfig(stsSupported))
config.SetCredential(credential).
SetRegionId(c.RegionId).
SetProtocol(c.Protocol).
SetReadTimeout(c.ClientReadTimeout).
SetConnectTimeout(c.ClientConnectTimeout).
SetMaxIdleConns(500)

header := make(map[string]*string)
if c.SourceIp != "" {
header["x-acs-source-ip"] = tea.String(c.SourceIp)
}
if c.SecureTransport != "" {
header["x-acs-secure-transport"] = tea.String(c.SecureTransport)
}

param := &openapi.GlobalParameters{Headers: header}
config.GlobalParameters = param
return
}
func (c *Config) getCredentialConfig(stsSupported bool) *credential.Config {
credentialType := ""
credentialConfig := &credential.Config{}
Expand Down
17 changes: 9 additions & 8 deletions alicloud/resource_alicloud_quotas_quota_alarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package alicloud

import (
"fmt"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
"log"
"time"

util "github.com/alibabacloud-go/tea-utils/service"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/blues/jsonata-go"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
Expand Down Expand Up @@ -93,7 +94,7 @@ func resourceAliCloudQuotasQuotaAlarmCreate(d *schema.ResourceData, meta interfa
action := "CreateQuotaAlarm"
var request map[string]interface{}
var response map[string]interface{}
conn, err := client.NewQuotasClient()
conn, err := client.NewQuotasClientV2()
if err != nil {
return WrapError(err)
}
Expand Down Expand Up @@ -128,7 +129,7 @@ func resourceAliCloudQuotasQuotaAlarmCreate(d *schema.ResourceData, meta interfa
}
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-05-10"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
response, err = conn.CallApi(rpcParam(action, "POST", "2020-05-10"), &openapi.OpenApiRequest{Query: nil, Body: request, HostMap: nil}, &util.RuntimeOptions{})

if err != nil {
if NeedRetry(err) {
Expand All @@ -145,7 +146,7 @@ func resourceAliCloudQuotasQuotaAlarmCreate(d *schema.ResourceData, meta interfa
return WrapErrorf(err, DefaultErrorMsg, "alicloud_quotas_quota_alarm", action, AlibabaCloudSdkGoERROR)
}

d.SetId(fmt.Sprint(response["AlarmId"]))
d.SetId(fmt.Sprint(response["body"].(map[string]interface{})["AlarmId"]))

return resourceAliCloudQuotasQuotaAlarmUpdate(d, meta)
}
Expand Down Expand Up @@ -186,7 +187,7 @@ func resourceAliCloudQuotasQuotaAlarmUpdate(d *schema.ResourceData, meta interfa
var response map[string]interface{}
update := false
action := "UpdateQuotaAlarm"
conn, err := client.NewQuotasClient()
conn, err := client.NewQuotasClientV2()
if err != nil {
return WrapError(err)
}
Expand Down Expand Up @@ -220,7 +221,7 @@ func resourceAliCloudQuotasQuotaAlarmUpdate(d *schema.ResourceData, meta interfa
if update {
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-05-10"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
response, err = conn.CallApi(rpcParam(action, "POST", "2020-05-10"), &openapi.OpenApiRequest{Query: nil, Body: request, HostMap: nil}, &util.RuntimeOptions{})

if err != nil {
if NeedRetry(err) {
Expand All @@ -247,7 +248,7 @@ func resourceAliCloudQuotasQuotaAlarmDelete(d *schema.ResourceData, meta interfa
action := "DeleteQuotaAlarm"
var request map[string]interface{}
var response map[string]interface{}
conn, err := client.NewQuotasClient()
conn, err := client.NewQuotasClientV2()
if err != nil {
return WrapError(err)
}
Expand All @@ -257,7 +258,7 @@ func resourceAliCloudQuotasQuotaAlarmDelete(d *schema.ResourceData, meta interfa

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-05-10"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
response, err = conn.CallApi(rpcParam(action, "POST", "2020-05-10"), &openapi.OpenApiRequest{Query: nil, Body: request, HostMap: nil}, &util.RuntimeOptions{})

if err != nil {
if NeedRetry(err) {
Expand Down
2 changes: 1 addition & 1 deletion alicloud/resource_alicloud_quotas_quota_alarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestAccAlicloudQuotasQuotaAlarm_basic(t *testing.T) {
resourceId := "alicloud_quotas_quota_alarm.default"
ra := resourceAttrInit(resourceId, AlicloudQuotasQuotaAlarmMap)
rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} {
return &QuotasService{testAccProvider.Meta().(*connectivity.AliyunClient)}
return &QuotasServiceV2{testAccProvider.Meta().(*connectivity.AliyunClient)}
}, "DescribeQuotasQuotaAlarm")
rac := resourceAttrCheckInit(rc, ra)
rand := acctest.RandIntRange(1000000, 9999999)
Expand Down
11 changes: 5 additions & 6 deletions alicloud/service_alicloud_quotas_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package alicloud

import (
"fmt"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
"time"

"github.com/PaesslerAG/jsonpath"
util "github.com/alibabacloud-go/tea-utils/service"
utilv2 "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/blues/jsonata-go"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
Expand Down Expand Up @@ -164,20 +166,17 @@ func (s *QuotasServiceV2) DescribeQuotasQuotaAlarm(id string) (object map[string
client := s.client
var request map[string]interface{}
var response map[string]interface{}
var query map[string]interface{}
action := "GetQuotaAlarm"
conn, err := client.NewQuotasClient()
conn, err := client.NewQuotasClientV2()
if err != nil {
return object, WrapError(err)
}
request = make(map[string]interface{})
query = make(map[string]interface{})

request["AlarmId"] = id

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("2020-05-10"), StringPointer("AK"), query, request, &util.RuntimeOptions{})
response, err = conn.CallApi(rpcParam(action, "POST", "2020-05-10"), &openapi.OpenApiRequest{Query: nil, Body: request, HostMap: nil}, &utilv2.RuntimeOptions{})

if err != nil {
if NeedRetry(err) {
Expand All @@ -196,7 +195,7 @@ func (s *QuotasServiceV2) DescribeQuotasQuotaAlarm(id string) (object map[string
return object, WrapErrorf(err, DefaultErrorMsg, id, action, AlibabaCloudSdkGoERROR)
}

v, err := jsonpath.Get("$.QuotaAlarm", response)
v, err := jsonpath.Get("$.QuotaAlarm", response["body"].(map[string]interface{}))
if err != nil {
return object, WrapErrorf(err, FailedGetAttributeMsg, id, "$.QuotaAlarm", response)
}
Expand Down