From 537203a203c7774db3cfb4b5c085a692f063d384 Mon Sep 17 00:00:00 2001 From: "guimin.hgm" Date: Wed, 16 Oct 2024 14:43:19 +0800 Subject: [PATCH 1/3] client: Improves the method of setting endpoint --- alicloud/connectivity/client.go | 252 ++++++++++++------------- alicloud/connectivity/endpoint.go | 293 +++++++++++++++++++++++++++--- alicloud/connectivity/helper.go | 7 + alicloud/provider.go | 172 +++++++++++++++++- 4 files changed, 566 insertions(+), 158 deletions(-) create mode 100644 alicloud/connectivity/helper.go diff --git a/alicloud/connectivity/client.go b/alicloud/connectivity/client.go index eb31c941218d..b7d7e680871b 100644 --- a/alicloud/connectivity/client.go +++ b/alicloud/connectivity/client.go @@ -604,66 +604,6 @@ func (client *AliyunClient) WithEssClient(do func(*ess.Client) (interface{}, err return do(client.essconn) } -type ossCredentials struct { - client *AliyunClient -} - -func (defCre *ossCredentials) GetAccessKeyID() string { - value, err := defCre.client.teaSdkConfig.Credential.GetAccessKeyId() - if err == nil && value != nil { - return *value - } - return defCre.client.config.AccessKey -} - -func (defCre *ossCredentials) GetAccessKeySecret() string { - value, err := defCre.client.teaSdkConfig.Credential.GetAccessKeySecret() - if err == nil && value != nil { - return *value - } - return defCre.client.config.SecretKey -} - -func (defCre *ossCredentials) GetSecurityToken() string { - value, err := defCre.client.teaSdkConfig.Credential.GetSecurityToken() - if err == nil && value != nil { - return *value - } - return defCre.client.config.SecurityToken -} - -type ossCredentialsProvider struct { - client *AliyunClient -} - -func (defBuild *ossCredentialsProvider) GetCredentials() oss.Credentials { - return &ossCredentials{client: defBuild.client} -} - -func (client *AliyunClient) GetRetryTimeout(defaultTimeout time.Duration) time.Duration { - - maxRetryTimeout := client.config.MaxRetryTimeout - if maxRetryTimeout != 0 { - return time.Duration(maxRetryTimeout) * time.Second - } - - return defaultTimeout -} - -func (client *AliyunClient) GenRoaParam(action, method, version, path string) *openapi.Params { - return &openapi.Params{ - Action: tea.String(action), - Version: tea.String(version), - Protocol: tea.String(client.config.Protocol), - Pathname: tea.String(path), - Method: tea.String(method), - AuthType: tea.String("AK"), - Style: tea.String("ROA"), - ReqBodyType: tea.String("formData"), - BodyType: tea.String("json"), - } -} - func (client *AliyunClient) WithOssClient(do func(*oss.Client) (interface{}, error)) (interface{}, error) { goSdkMutex.Lock() defer goSdkMutex.Unlock() @@ -2258,7 +2198,7 @@ func (client *AliyunClient) WithDcdnClient(do func(*dcdn.Client) (interface{}, e } func (client *AliyunClient) WithRKvstoreClient(do func(*r_kvstore.Client) (interface{}, error)) (interface{}, error) { - productCode := "redisa" + productCode := "r_kvstore" endpoint := "" if client.r_kvstoreConn == nil { if endpoint == "" { @@ -2372,7 +2312,7 @@ func (client *AliyunClient) NewConfigClient() (*rpc.Client, error) { } func (client *AliyunClient) NewWafClient() (*rpc.Client, error) { - productCode := "waf" + productCode := "waf_openapi" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -2541,7 +2481,7 @@ func (client *AliyunClient) NewDcdnClient() (*rpc.Client, error) { } func (client *AliyunClient) NewOdpsClient() (*roa.Client, error) { - productCode := "odps" + productCode := "maxcompute" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -2564,7 +2504,7 @@ func (client *AliyunClient) NewOdpsClient() (*roa.Client, error) { } func (client *AliyunClient) NewRessharingClient() (*rpc.Client, error) { - productCode := "ressharing" + productCode := "resourcesharing" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -2587,7 +2527,7 @@ func (client *AliyunClient) NewRessharingClient() (*rpc.Client, error) { } func (client *AliyunClient) NewGaplusClient() (*rpc.Client, error) { - productCode := "gaplus" + productCode := "ga" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -2704,7 +2644,7 @@ func (client *AliyunClient) NewHitsdbClient() (*rpc.Client, error) { } func (client *AliyunClient) NewAistudioClient() (*rpc.Client, error) { - productCode := "aistudio" + productCode := "brain_industrial" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -2928,7 +2868,7 @@ func (client *AliyunClient) NewNasClient() (*rpc.Client, error) { } func (client *AliyunClient) NewDmsenterpriseClient() (*rpc.Client, error) { - productCode := "dmsenterprise" + productCode := "dms_enterprise" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -2953,7 +2893,7 @@ func (client *AliyunClient) NewDmsenterpriseClient() (*rpc.Client, error) { } func (client *AliyunClient) NewHcsSgwClient() (*rpc.Client, error) { - productCode := "hcs_sgw" + productCode := "sgw" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -3164,7 +3104,7 @@ func (client *AliyunClient) NewEventbridgeClient() (*rpc.Client, error) { } func (client *AliyunClient) NewOnsproxyClient() (*rpc.Client, error) { - productCode := "onsproxy" + productCode := "amqp" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -3187,7 +3127,7 @@ func (client *AliyunClient) NewOnsproxyClient() (*rpc.Client, error) { } func (client *AliyunClient) NewCdsClient() (*rpc.Client, error) { - productCode := "cds" + productCode := "cassandra" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -3315,7 +3255,7 @@ func (client *AliyunClient) NewCloudfwClient() (*rpc.Client, error) { } func (client *AliyunClient) NewServerlessClient() (*roa.Client, error) { - productCode := "serverless" + productCode := "sae" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -3363,7 +3303,7 @@ func (client *AliyunClient) NewAlbClient() (*rpc.Client, error) { } func (client *AliyunClient) NewRedisaClient() (*rpc.Client, error) { - productCode := "redisa" + productCode := "r_kvstore" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -3388,7 +3328,7 @@ func (client *AliyunClient) NewRedisaClient() (*rpc.Client, error) { } func (client *AliyunClient) NewGwsecdClient() (*rpc.Client, error) { - productCode := "gwsecd" + productCode := "ecd" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -3463,7 +3403,7 @@ func (client *AliyunClient) NewScdnClient() (*rpc.Client, error) { } func (client *AliyunClient) NewDataworkspublicClient() (*rpc.Client, error) { - productCode := "dataworkspublic" + productCode := "dataworks_public" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -3539,7 +3479,7 @@ func (client *AliyunClient) NewCddcClient() (*rpc.Client, error) { } func (client *AliyunClient) NewMscopensubscriptionClient() (*rpc.Client, error) { - productCode := "mscsub" + productCode := "mscopensubscription" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -3638,7 +3578,7 @@ func (client *AliyunClient) NewSasClient() (*rpc.Client, error) { } func (client *AliyunClient) NewAlidfsClient() (*rpc.Client, error) { - productCode := "alidfs" + productCode := "dfs" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -3888,7 +3828,7 @@ func (client *AliyunClient) NewCloudssoClient() (*rpc.Client, error) { } func (client *AliyunClient) NewSwasClient() (*rpc.Client, error) { - productCode := "swas" + productCode := "swas_open" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -3938,7 +3878,7 @@ func (client *AliyunClient) NewVsClient() (*rpc.Client, error) { } func (client *AliyunClient) NewQuickbiClient() (*rpc.Client, error) { - productCode := "quickbi" + productCode := "quickbi_public" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -3963,7 +3903,7 @@ func (client *AliyunClient) NewQuickbiClient() (*rpc.Client, error) { } func (client *AliyunClient) NewDevopsrdcClient() (*rpc.Client, error) { - productCode := "rdcdevops" + productCode := "devops_rdc" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -4038,7 +3978,7 @@ func (client *AliyunClient) NewOpensearchClient() (*roa.Client, error) { } func (client *AliyunClient) NewGdsClient() (*rpc.Client, error) { - productCode := "gds" + productCode := "gdb" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -4163,7 +4103,7 @@ func (client *AliyunClient) NewImpClient() (*rpc.Client, error) { } func (client *AliyunClient) NewMhubClient() (*rpc.Client, error) { - productCode := "emas" + productCode := "mhub" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -4210,7 +4150,7 @@ func (client *AliyunClient) NewServicemeshClient() (*rpc.Client, error) { } func (client *AliyunClient) NewAcrClient() (*rpc.Client, error) { - productCode := "acr" + productCode := "cr" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -4235,7 +4175,7 @@ func (client *AliyunClient) NewAcrClient() (*rpc.Client, error) { } func (client *AliyunClient) NewEdsuserClient() (*rpc.Client, error) { - productCode := "eds-user" + productCode := "eds_user" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -4367,7 +4307,7 @@ func (client *AliyunClient) NewEssClient() (*rpc.Client, error) { } func (client *AliyunClient) NewDdosbasicClient() (*rpc.Client, error) { - productCode := "ddosbasic" + productCode := "antiddos_public" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -4468,7 +4408,7 @@ func (client *AliyunClient) NewEdasClient() (*roa.Client, error) { } func (client *AliyunClient) NewEdasschedulerxClient() (*rpc.Client, error) { - productCode := "edasschedulerx" + productCode := "schedulerx2" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -4494,7 +4434,7 @@ func (client *AliyunClient) NewEdasschedulerxClient() (*rpc.Client, error) { } func (client *AliyunClient) NewEhsClient() (*rpc.Client, error) { - productCode := "ehs" + productCode := "ehpc" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -4519,7 +4459,7 @@ func (client *AliyunClient) NewEhsClient() (*rpc.Client, error) { } func (client *AliyunClient) NewDysmsClient() (*rpc.Client, error) { - productCode := "dysms" + productCode := "sms" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -4544,7 +4484,7 @@ func (client *AliyunClient) NewDysmsClient() (*rpc.Client, error) { } func (client *AliyunClient) NewFcClient() (*roa.Client, error) { - productCode := "fc" + productCode := "fc_open" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -4651,7 +4591,7 @@ func (client *AliyunClient) NewVpcpeerClient() (*rpc.Client, error) { } func (client *AliyunClient) NewCbsClient() (*rpc.Client, error) { - productCode := "cbs" + productCode := "dbs" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -4726,7 +4666,7 @@ func (client *AliyunClient) NewEbsClient() (*rpc.Client, error) { } func (client *AliyunClient) NewMnsClient() (*rpc.Client, error) { - productCode := "mns" + productCode := "mns_open" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -4803,7 +4743,7 @@ func (client *AliyunClient) NewDasClient() (*rpc.Client, error) { } func (client *AliyunClient) NewCloudfirewallClient() (*rpc.Client, error) { - productCode := "cloudfirewall" + productCode := "cloudfw" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -4855,7 +4795,7 @@ func (client *AliyunClient) NewThreatdetectionClient() (*rpc.Client, error) { } func (client *AliyunClient) NewSrvcatalogClient() (*rpc.Client, error) { - productCode := "srvcatalog" + productCode := "servicecatalog" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -4881,7 +4821,7 @@ func (client *AliyunClient) NewSrvcatalogClient() (*rpc.Client, error) { } func (client *AliyunClient) NewVpcPeerClient() (*rpc.Client, error) { - productCode := "VpcPeer" + productCode := "vpcpeer" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -4933,7 +4873,7 @@ func (client *AliyunClient) NewEfloClient() (*rpc.Client, error) { } func (client *AliyunClient) NewOceanbaseClient() (*rpc.Client, error) { - productCode := "oceanbase" + productCode := "oceanbasepro" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -4958,7 +4898,7 @@ func (client *AliyunClient) NewOceanbaseClient() (*rpc.Client, error) { } func (client *AliyunClient) NewBeebotClient() (*rpc.Client, error) { - productCode := "beebot" + productCode := "chatbot" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -5012,7 +4952,7 @@ func (client *AliyunClient) NewComputenestClient() (*rpc.Client, error) { return conn, nil } func (client *AliyunClient) NewRedisClient() (*rpc.Client, error) { - productCode := "redisa" + productCode := "r_kvstore" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -5193,7 +5133,7 @@ func (client *AliyunClient) NewSlsClient() (*openapi.Client, error) { return openapiClient, nil } func (client *AliyunClient) NewRocketmqClient() (*roa.Client, error) { - productCode := "rmq" + productCode := "rocketmq" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -5433,7 +5373,7 @@ func (client *AliyunClient) NewCloudmonitorserviceClient() (*rpc.Client, error) return conn, nil } func (client *AliyunClient) NewWafv3Client() (*rpc.Client, error) { - productCode := "waf" + productCode := "waf_openapi" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -5457,7 +5397,7 @@ func (client *AliyunClient) NewWafv3Client() (*rpc.Client, error) { return conn, nil } func (client *AliyunClient) NewDfsClient() (*rpc.Client, error) { - productCode := "alidfs" + productCode := "dfs" endpoint := "" if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { if err := client.loadEndpoint(productCode); err != nil { @@ -5480,30 +5420,7 @@ func (client *AliyunClient) NewDfsClient() (*rpc.Client, error) { } return conn, nil } -func (client *AliyunClient) NewAmqpClient() (*rpc.Client, error) { - productCode := "onsproxy" - endpoint := "" - if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { - if err := client.loadEndpoint(productCode); err != nil { - endpoint = fmt.Sprintf("amqp-open.%s.aliyuncs.com", client.config.RegionId) - client.config.Endpoints.Store(productCode, endpoint) - log.Printf("[ERROR] loading %s endpoint got an error: %#v. Using the 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) NewCenClient() (*rpc.Client, error) { productCode := "cbn" endpoint := "" @@ -5601,21 +5518,32 @@ func (client *AliyunClient) NewGovernanceClient() (*rpc.Client, error) { return conn, nil } -func (client *AliyunClient) loadApiEndpoint(locationCode string) (string, error) { - if v, ok := client.config.Endpoints.Load(locationCode); !ok || v.(string) == "" { - if err := client.loadEndpoint(locationCode); err != nil { - return "", fmt.Errorf("[ERROR] loading %s endpoint got an error: %#v.", locationCode, err) +func (client *AliyunClient) loadApiEndpoint(productCode string) (string, error) { + if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { + if err := client.loadEndpoint(productCode); err != nil { + return "", fmt.Errorf("[ERROR] loading %s endpoint got an error: %#v.", productCode, err) } } else { return v.(string), nil } - if v, ok := client.config.Endpoints.Load(locationCode); ok && v.(string) != "" { + if v, ok := client.config.Endpoints.Load(productCode); ok && v.(string) != "" { return v.(string), nil } - return "", fmt.Errorf("[ERROR] missing the product %s endpoint.", locationCode) + return "", fmt.Errorf("[ERROR] missing the product %s endpoint.", productCode) } -func (client *AliyunClient) RpcPost(locationCode string, apiVersion string, apiName string, query map[string]interface{}, body map[string]interface{}, autoRetry bool) (map[string]interface{}, error) { - endpoint, err := client.loadApiEndpoint(locationCode) + +// RpcPost invoking RPC API request with POST method +// parameters: +// +// apiProductCode: API Product code, its value equals to the gateway code of the API +// apiVersion - API version +// apiName - API Name +// query - API parameters in query +// body - API parameters in body +// autoRetry - whether to auto retry while the runtime has a 5xx error +func (client *AliyunClient) RpcPost(apiProductCode string, apiVersion string, apiName string, query map[string]interface{}, body map[string]interface{}, autoRetry bool) (map[string]interface{}, error) { + apiProductCode = strings.ToLower(ConvertKebabToSnake(apiProductCode)) + endpoint, err := client.loadApiEndpoint(apiProductCode) if err != nil { return nil, err } @@ -5630,7 +5558,7 @@ func (client *AliyunClient) RpcPost(locationCode string, apiVersion string, apiN sdkConfig.SetSecurityToken(*credential.SecurityToken) conn, err := rpc.NewClient(&sdkConfig) if err != nil { - return nil, fmt.Errorf("unable to initialize the %s client: %#v", locationCode, err) + return nil, fmt.Errorf("unable to initialize the %s api client: %#v", apiProductCode, err) } runtime := &util.RuntimeOptions{} runtime.SetAutoretry(autoRetry) @@ -5662,3 +5590,63 @@ func (client *AliyunClient) NewPaiworkspaceClient() (*roa.Client, error) { } return conn, nil } + +type ossCredentials struct { + client *AliyunClient +} + +func (defCre *ossCredentials) GetAccessKeyID() string { + value, err := defCre.client.teaSdkConfig.Credential.GetAccessKeyId() + if err == nil && value != nil { + return *value + } + return defCre.client.config.AccessKey +} + +func (defCre *ossCredentials) GetAccessKeySecret() string { + value, err := defCre.client.teaSdkConfig.Credential.GetAccessKeySecret() + if err == nil && value != nil { + return *value + } + return defCre.client.config.SecretKey +} + +func (defCre *ossCredentials) GetSecurityToken() string { + value, err := defCre.client.teaSdkConfig.Credential.GetSecurityToken() + if err == nil && value != nil { + return *value + } + return defCre.client.config.SecurityToken +} + +type ossCredentialsProvider struct { + client *AliyunClient +} + +func (defBuild *ossCredentialsProvider) GetCredentials() oss.Credentials { + return &ossCredentials{client: defBuild.client} +} + +func (client *AliyunClient) GetRetryTimeout(defaultTimeout time.Duration) time.Duration { + + maxRetryTimeout := client.config.MaxRetryTimeout + if maxRetryTimeout != 0 { + return time.Duration(maxRetryTimeout) * time.Second + } + + return defaultTimeout +} + +func (client *AliyunClient) GenRoaParam(action, method, version, path string) *openapi.Params { + return &openapi.Params{ + Action: tea.String(action), + Version: tea.String(version), + Protocol: tea.String(client.config.Protocol), + Pathname: tea.String(path), + Method: tea.String(method), + AuthType: tea.String("AK"), + Style: tea.String("ROA"), + ReqBodyType: tea.String("formData"), + BodyType: tea.String("json"), + } +} diff --git a/alicloud/connectivity/endpoint.go b/alicloud/connectivity/endpoint.go index cf815a3866f4..3199b895862a 100644 --- a/alicloud/connectivity/endpoint.go +++ b/alicloud/connectivity/endpoint.go @@ -4,6 +4,7 @@ import ( "encoding/xml" "fmt" "io/ioutil" + "log" "os" "regexp" "strings" @@ -165,26 +166,271 @@ func loadEndpoint(region string, serviceCode ServiceCode) string { return "" } -// regularProductCode specially records those product codes that -// cannot be parsed out by the location service or have been confirmed to be regional endpoints. +// productCodeToLocationCode records all products' code mapping to location +// Key: product code, its value equals to the gateway code of the API after converting it to lowercase and using underscores +// Value: location code +var productCodeToLocationCode = map[string]string{ + "ecs": "ecs", // ECS + "adb": "ads", //ADB + "ess": "ess", //AutoScaling + "cs": "cs", // ACK + "polardb": "polardb", // PolarDB + "cr": "acr", // CR + "dds": "dds", //MongoDB + "gpdb": "gpdb", //GPDB + "fc_open": "fc", // FC, FCV2 + "fc": "fc", // FCV3 + "apigateway": "apigateway", + "datahub": "datahub", // DataHub + "mns_open": "mns", // MessageService + "elasticsearch": "elasticsearch", // Elasticsearch + "ddoscoo": "ddoscoo", // DdosCoo + "ddosbgp": "ddosbgp", // DdosBgp + "antiddos_public": "ddosbasic", // DdosBasic + "bssopenapi": "bssopenapi", //BssOpenApi + "alikafka": "alikafka", //AliKafka + "emr": "emr", //EMR + "smartag": "smartag", // Smartag + "yundun_dbaudit": "dbaudit", //DBAudit + "yundun_bastionhost": "bastionhost", //Bastionhost + "hbase": "hbase", //HBase + "edas": "edas", // EDAS + "alidns": "alidns", //Alidns + "cassandra": "cds", //Cassandra + "eci": "eci", // ECI + "dcdn": "dcdn", // DCDN + "r_kvstore": "redisa", // Redis + "ons": "ons", //Ons + "config": "config", //Config + "fnf": "fnf", // FnF + "ros": "ros", // ROS + "mse": "mse", // MSE + "pvtz": "pvtz", //PrivateZone + "privatelink": "privatelink", // PrivateLink + "maxcompute": "odps", //MaxCompute + "resourcesharing": "ressharing", // ResourceManager + "ga": "gaplus", // Ga + "actiontrail": "actiontrail", //ActionTrail + "hitsdb": "hitsdb", //Lindorm + "brain_industrial": "aistudio", //BrainIndustrial + "eipanycast": "eipanycast", // Eipanycast + "oos": "oos", // OOS + "ims": "ims", //IMS + "resourcemanager": "resourcemanager", // ResourceManager + "nas": "nas", //NAS + "dms_enterprise": "dmsenterprise", //DMSEnterprise + "sgw": "hcs_sgw", // CloudStorageGateway + "slb": "slb", // SLB + "kms": "kms", //KMS + "dm": "dm", //DirectMail + "eventbridge": "eventbridge", // EventBridge + "hbr": "hbr", //HBR + "cas": "cas", //SSLCertificatesService + "arms": "arms", // ARMS + "cloudfw": "cloudfirewall", //CloudFirewall + "sae": "serverless", //SAE + "alb": "alb", // ALB + "ecd": "gwsecd", // ECD + "cloudphone": "cloudphone", // ECP + "scdn": "scdn", //SCDN + "dataworks_public": "dide", //DataWorks + "cdn": "cdn", // CDN + "cddc": "cddc", // CDDC + "mscopensubscription": "mscsub", //MscSub + "sddp": "sddp", // SDDP + "sas": "sas", // ThreatDetection + "ehpc": "ehs", // Ehpc + "ens": "ens", // ENS + "iot": "iot", // Iot + "imm": "imm", // IMM + "clickhouse": "clickhouse", // ClickHouse + "selectdb": "selectdb", //SelectDB + "dts": "dts", // DTS + "dg": "dg", // DatabaseGateway + "cloudsso": "cloudsso", // CloudSSO + "swas_open": "swas", // SimpleApplicationServer + "vs": "vs", // VideoSurveillanceSystem + "quickbi_public": "quickbi", // QuickBI + "devops_rdc": "rdcdevops", // RDC + "vod": "vod", // VOD + "opensearch": "opensearch", // OpenSearch + "gdb": "gds", // GraphDatabase + "dbfs": "dbfs", // DBFS + "eais": "eais", // EAIS + "cloudauth": "cloudauth", // Cloudauth + "imp": "imp", // IMP + "mhub": "emas", // MHUB + "servicemesh": "servicemesh", // ServiceMesh + "eds_user": "edsuser", // ECD + "tag": "tag", // Tag + "schedulerx2": "edasschedulerx", // Schedulerx + "sms": "dysms", // SMS + "vpcpeer": "vpcpeer", // VpcPeer + "dbs": "cbs", // DBS + "nlb": "nlb", // NLB + "ebs": "ebs", // EBS + "bpstudio": "bpstudio", // BPStudio + "das": "hdm", // DAS + "servicecatalog": "srvcatalog", // ServiceCatalog + "eflo": "eflo", //Eflo + "oceanbasepro": "oceanbase", // OceanBase + "chatbot": "beebot", // Chatbot + "computenest": "computenest", // ComputeNest + "drds": "drds", // DRDS + "polardbx": "polardbx", // DRDS + "adcp": "adcp", // AckOne + "sls": "sls", // SLS + "rocketmq": "rmq", // RocketMQ + "resourcecenter": "", // ResourceManager + "hologram": "hologram", // Hologram + "foasconsole": "foasconsole", // RealtimeCompute + "vpc": "vpc", // VPC, VPNGateway,ExpressConnect, CBWP, EIP + "sss": "oss", // OSS + "cms": "cms", // CloudMonitorService + "waf_openapi": "waf", //WAFV3,WAF + "dfs": "alidfs", //DFS + "amqp": "onsproxy", // Amqp + "cbn": "cbn", // CEN + "expressconnectrouter": "ecr", // ExpressConnectRouter + "green": "green", // Aligreen + "governance": "governance", // Governance + "ots": "ots", // OTS + "tablestore": "ots", // OTS + "ram": "ram", //RAM + "market": "market", //Market +} + +// irregularProductEndpoint specially records those product codes that +// cannot be parsed out by the location service. +// Key: product code, its value equals to the gateway code of the API after converting it to lowercase and using underscores +// Value: product endpoint // The priority of this configuration is higher than location service, lower than user environment variable configuration -var regularProductCode = map[string]string{ - "mse": "mse.%s.aliyuncs.com", - "tablestore": "tablestore.%s.aliyuncs.com", - "bssopenapi": BssOpenAPIEndpointDomestic, +var irregularProductEndpoint = map[string]string{ + "tablestore": "tablestore.%s.aliyuncs.com", + "bssopenapi": BssOpenAPIEndpointDomestic, + "ram": "ram.aliyuncs.com", + "ddoscoo": "ddoscoo.cn-hangzhou.aliyuncs.com", + "dcdn": "dcdn.aliyuncs.com", + "config": "config.cn-shanghai.aliyuncs.com", + "ga": "ga.cn-hangzhou.aliyuncs.com", + "brain_industrial": "brain-industrial.cn-hangzhou.aliyuncs.com", + "eipanycast": "eipanycast.cn-hangzhou.aliyuncs.com", + "ims": "ims.aliyuncs.com", + "resourcemanager": "resourcemanager.aliyuncs.com", + "quotas": "quotas.aliyuncs.com", + "cassandra": "cassandra.aliyuncs.com", + "cas": "cas.aliyuncs.com", + "cloudfw": "cloudfw.aliyuncs.com", + "scdn": "scdn.aliyuncs.com", + "cdn": "cdn.aliyuncs.com", + "mscopensubscription": "mscopensubscription.aliyuncs.com", + "cloudauth": "cloudauth.aliyuncs.com", + "imp": "imp.aliyuncs.com", + "mhub": "mhub.cn-shanghai.aliyuncs.com", + "eds_user": "eds-user.cn-shanghai.aliyuncs.com", //eds-user.ap-southeast-1.aliyuncs.com + "vpcpeer": "vpcpeer.aliyuncs.com", + "das": "das.cn-shanghai.aliyuncs.com", + "servicecatalog": "servicecatalog.cn-hangzhou.aliyuncs.com", + "chatbot": "chatbot.cn-shanghai.aliyuncs.com", + "computenest": "computenest.cn-hangzhou.aliyuncs.com", + "resourcecenter": "resourcecenter.aliyuncs.com", //resourcecenter-intl.aliyuncs.com + "market": "market.aliyuncs.com", +} + +// regularProductEndpoint specially records those product codes that have been confirmed to be +// regional or central endpoints. +// Key: product code, its value equals to the gateway code of the API after converting it to lowercase and using underscores +// Value: product endpoint +// The priority of this configuration is lower than location service, and as a backup endpoint +var regularProductEndpoint = map[string]string{ + "mse": "mse.%s.aliyuncs.com", + "vpc": "vpc.%s.aliyuncs.com", + "oss": "oss-%s.aliyuncs.com", + "cr": "cr.%s.aliyuncs.com", + "cms": "metrics.%s.aliyuncs.com", + "sls": "%s.log.aliyuncs.com", + "drds": "drds.%s.aliyuncs.com", + "polardbx": "polardbx.%s.aliyuncs.com", + "fc_open": "%s.fc.aliyuncs.com", + "fc": "%s.fc.aliyuncs.com", + "apigateway": "apigateway.%s.aliyuncs.com", + "mns_open": "mns-open.%s.aliyuncs.com", + "elasticsearch": "elasticsearch.%s.aliyuncs.com", + "alikafka": "alikafka.%s.aliyuncs.com", + "emr": "emr.%s.aliyuncs.com", + "smartag": "smartag.%s.aliyuncs.com", + "alidns": "alidns.%s.aliyuncs.com", + "eci": "eci.%s.aliyuncs.com", + "ons": "ons.%s.aliyuncs.com", + "ros": "ros.aliyuncs.com", + "pvtz": "pvtz.aliyuncs.com", + "privatelink": "privatelink.%s.aliyuncs.com", + "maxcompute": "maxcompute.%s.aliyuncs.com", + "resourcesharing": "resourcesharing.%s.aliyuncs.com", + "actiontrail": "actiontrail.%s.aliyuncs.com", + "hitsdb": "hitsdb.%s.aliyuncs.com", + "oos": "oos.%s.aliyuncs.com", + "nas": "nas.%s.aliyuncs.com", + "dms_enterprise": "dms-enterprise.%s.aliyuncs.com", + "kms": "kms.%s.aliyuncs.com", + "eventbridge": "eventbridge-console.%s.aliyuncs.com", + "hbr": "hbr.%s.aliyuncs.com", + "arms": "arms.%s.aliyuncs.com", + "sae": "sae.%s.aliyuncs.com", + "alb": "alb.%s.aliyuncs.com", + "ecd": "ecd.%s.aliyuncs.com", + "cloudphone": "cloudphone.%s.aliyuncs.com", + "dataworks_public": "dataworks.%s.aliyuncs.com", + "sas": "tds.aliyuncs.com", + "ehpc": "ehpc.%s.aliyuncs.com", + "ens": "ens.aliyuncs.com", + "iot": "iot.%s.aliyuncs.com", + "imm": "imm.%s.aliyuncs.com", + "swas_open": "swas.%s.aliyuncs.com", + "vs": "vs.%s.aliyuncs.com", + "vod": "vod.%s.aliyuncs.com", + "opensearch": "opensearch.%s.aliyuncs.com", + "dbfs": "dbfs.%s.aliyuncs.com", + "eais": "eais.%s.aliyuncs.com", + "servicemesh": "servicemesh.aliyuncs.com", + "tag": "tag.%s.aliyuncs.com", + "schedulerx2": "schedulerx.%s.aliyuncs.com", + "dbs": "dbs-api.%s.aliyuncs.com", + "nlb": "nlb.%s.aliyuncs.com", + "ebs": "ebs.%s.aliyuncs.com", + "eflo": "eflo.%s.aliyuncs.com", + "oceanbasepro": "oceanbasepro.%s.aliyuncs.com", + "adcp": "adcp.%s.aliyuncs.com", + "rocketmq": "rocketmq.%s.aliyuncs.com", + "hologram": "hologram.%s.aliyuncs.com", + "foasconsole": "foasconsole.aliyuncs.com", + "cs": "cs.%s.aliyuncs.com", + "waf_openapi": "wafopenapi.cn-hangzhou.aliyuncs.com", // wafopenapi.ap-southeast-1.aliyuncs.com + "dfs": "dfs.%s.aliyuncs.com", + "amqp": "amqp-open.%s.aliyuncs.com", + "cbn": "cbn.aliyuncs.com", + "expressconnectrouter": "expressconnectrouter.cn-shanghai.aliyuncs.com", + "green": "green.%s.aliyuncs.com", + "governance": "governance.cn-hangzhou.aliyuncs.com", // governance.ap-southeast-1.aliyuncs.com + "sms": "dysmsapi.aliyuncs.com", // dysmsapi.ap-southeast-1.aliyuncs.com + "sddp": "sddp.cn-zhangjiakou.aliyuncs.com", // sddp.ap-southeast-1.aliyuncs.com } // NOTE: The productCode must be lower. func (client *AliyunClient) loadEndpoint(productCode string) error { // Firstly, load endpoint from environment variables - endpoint := strings.TrimSpace(os.Getenv(fmt.Sprintf("%s_ENDPOINT", strings.ToUpper(productCode)))) + endpoint := strings.TrimSpace(os.Getenv(fmt.Sprintf("ALIBABA_CLOUD_ENDPOINT_%s", strings.ToUpper(productCode)))) + if endpoint == "" { + // Compatible with the previous implementation method + endpoint = strings.TrimSpace(os.Getenv(fmt.Sprintf("%s_ENDPOINT", strings.ToUpper(productCode)))) + } if endpoint != "" { client.config.Endpoints.Store(productCode, endpoint) return nil } // Secondly, load endpoint from known rules - if endpointFmt, ok := regularProductCode[productCode]; ok { + if endpointFmt, ok := irregularProductEndpoint[productCode]; ok { if strings.Contains(endpointFmt, "%s") { endpointFmt = fmt.Sprintf(endpointFmt, client.RegionId) } @@ -193,13 +439,16 @@ func (client *AliyunClient) loadEndpoint(productCode string) error { } // Thirdly, load endpoint from location - serviceCode := serviceCodeMapping[productCode] - if serviceCode == "" { - serviceCode = productCode - } - endpoint, err := client.describeEndpointForService(serviceCode) + endpoint, err := client.describeEndpointForService(productCode) if err == nil { - client.config.Endpoints.Store(strings.ToLower(serviceCode), endpoint) + client.config.Endpoints.Store(strings.ToLower(productCode), endpoint) + } else if v, ok := regularProductEndpoint[productCode]; ok { + if strings.Contains(v, "%s") { + v = fmt.Sprintf(v, client.RegionId) + } + client.config.Endpoints.Store(productCode, v) + log.Printf("[WARN] loading %s endpoint got an error: %#v. Using the endpoint %s instead.", productCode, err, v) + return nil } return err } @@ -242,9 +491,13 @@ func incrementalWait(firstDuration time.Duration, increaseDuration time.Duration retryCount++ } } -func (client *AliyunClient) describeEndpointForService(serviceCode string) (string, error) { +func (client *AliyunClient) describeEndpointForService(productCode string) (string, error) { + locationCode := productCodeToLocationCode[productCode] + if locationCode == "" { + locationCode = productCode + } args := location.CreateDescribeEndpointsRequest() - args.ServiceCode = serviceCode + args.ServiceCode = locationCode args.Id = client.config.RegionId args.Domain = client.config.LocationEndpoint if args.Domain == "" { @@ -288,18 +541,14 @@ func (client *AliyunClient) describeEndpointForService(serviceCode string) (stri return nil }) if err != nil { - return "", fmt.Errorf("Describe %s endpoint using region: %#v got an error: %#v.", serviceCode, client.RegionId, err) + return "", fmt.Errorf("Describe %s endpoint using region: %#v got an error: %#v.", productCode, client.RegionId, err) } if endpointResult == "" { - return "", fmt.Errorf("There is no any available endpoint for %s in region %s.", serviceCode, client.RegionId) + return "", fmt.Errorf("There is no any available endpoint for %s in region %s.", productCode, client.RegionId) } return endpointResult, nil } -var serviceCodeMapping = map[string]string{ - "cloudapi": "apigateway", -} - const ( OpenApiGatewayService = "apigateway.cn-hangzhou.aliyuncs.com" OpenOtsService = "ots.cn-hangzhou.aliyuncs.com" diff --git a/alicloud/connectivity/helper.go b/alicloud/connectivity/helper.go new file mode 100644 index 000000000000..ae627d33cb78 --- /dev/null +++ b/alicloud/connectivity/helper.go @@ -0,0 +1,7 @@ +package connectivity + +import "strings" + +func ConvertKebabToSnake(s string) string { + return strings.ReplaceAll(s, "-", "_") +} diff --git a/alicloud/provider.go b/alicloud/provider.go index 96a15cbeda93..0db78afa5251 100644 --- a/alicloud/provider.go +++ b/alicloud/provider.go @@ -1912,6 +1912,12 @@ func providerConfigure(d *schema.ResourceData, p *schema.Provider) (interface{}, for _, endpointsSetI := range endpointsSet.List() { endpoints := endpointsSetI.(map[string]interface{}) for key, val := range endpoints { + // Compatible with the deprecated endpoint setting + if val == nil || val.(string) == "" { + if v, ok := deprecatedEndpointMap[key]; ok { + val = endpoints[v] + } + } endpointInit.Store(key, val) } config.EcsEndpoint = strings.TrimSpace(endpoints["ecs"].(string)) @@ -2563,6 +2569,12 @@ func endpointsSchema() *schema.Schema { Default: "", Description: descriptions["beebot_endpoint"], }, + "chatbot": { + Type: schema.TypeString, + Optional: true, + Default: "", + Description: descriptions["beebot_endpoint"], + }, "eflo": { Type: schema.TypeString, @@ -2577,7 +2589,12 @@ func endpointsSchema() *schema.Schema { Default: "", Description: descriptions["srvcatalog_endpoint"], }, - + "servicecatalog": { + Type: schema.TypeString, + Optional: true, + Default: "", + Description: descriptions["srvcatalog_endpoint"], + }, "cloudfirewall": { Type: schema.TypeString, Optional: true, @@ -2626,6 +2643,12 @@ func endpointsSchema() *schema.Schema { Default: "", Description: descriptions["cbs_endpoint"], }, + "dbs": { + Type: schema.TypeString, + Optional: true, + Default: "", + Description: descriptions["cbs_endpoint"], + }, "vpcpeer": { Type: schema.TypeString, @@ -2640,6 +2663,12 @@ func endpointsSchema() *schema.Schema { Default: "", Description: descriptions["dysms_endpoint"], }, + "sms": { + Type: schema.TypeString, + Optional: true, + Default: "", + Description: descriptions["dysms_endpoint"], + }, "edas": { Type: schema.TypeString, @@ -2654,7 +2683,12 @@ func endpointsSchema() *schema.Schema { Default: "", Description: descriptions["edasschedulerx_endpoint"], }, - + "schedulerx2": { + Type: schema.TypeString, + Optional: true, + Default: "", + Description: descriptions["edasschedulerx_endpoint"], + }, "ehs": { Type: schema.TypeString, Optional: true, @@ -2676,6 +2710,12 @@ func endpointsSchema() *schema.Schema { Description: descriptions["ddosbasic_endpoint"], }, + "antiddos_public": { + Type: schema.TypeString, + Optional: true, + Default: "", + Description: descriptions["ddosbasic_endpoint"], + }, "smartag": { Type: schema.TypeString, Optional: true, @@ -2689,6 +2729,12 @@ func endpointsSchema() *schema.Schema { Default: "", Description: descriptions["oceanbase_endpoint"], }, + "oceanbasepro": { + Type: schema.TypeString, + Optional: true, + Default: "", + Description: descriptions["oceanbase_endpoint"], + }, "gaplus": { Type: schema.TypeString, @@ -2710,6 +2756,12 @@ func endpointsSchema() *schema.Schema { Default: "", Description: descriptions["edsuser_endpoint"], }, + "eds_user": { + Type: schema.TypeString, + Optional: true, + Default: "", + Description: descriptions["edsuser_endpoint"], + }, "acr": { Type: schema.TypeString, @@ -2755,6 +2807,12 @@ func endpointsSchema() *schema.Schema { Default: "", Description: descriptions["quickbi_endpoint"], }, + "quickbi_public": { + Type: schema.TypeString, + Optional: true, + Default: "", + Description: descriptions["quickbi_endpoint"], + }, "vod": { Type: schema.TypeString, Optional: true, @@ -2773,6 +2831,12 @@ func endpointsSchema() *schema.Schema { Default: "", Description: descriptions["gds_endpoint"], }, + "gdb": { + Type: schema.TypeString, + Optional: true, + Default: "", + Description: descriptions["gds_endpoint"], + }, "dbfs": { Type: schema.TypeString, Optional: true, @@ -2828,6 +2892,12 @@ func endpointsSchema() *schema.Schema { Default: "", Description: descriptions["swas_endpoint"], }, + "swas_open": { + Type: schema.TypeString, + Optional: true, + Default: "", + Description: descriptions["swas_endpoint"], + }, "imm": { Type: schema.TypeString, @@ -2854,6 +2924,12 @@ func endpointsSchema() *schema.Schema { Default: "", Description: descriptions["alidfs_endpoint"], }, + "dfs": { + Type: schema.TypeString, + Optional: true, + Default: "", + Description: descriptions["alidfs_endpoint"], + }, "ens": { Type: schema.TypeString, @@ -2908,7 +2984,12 @@ func endpointsSchema() *schema.Schema { Default: "", Description: descriptions["dataworkspublic_endpoint"], }, - + "dataworks_public": { + Type: schema.TypeString, + Optional: true, + Default: "", + Description: descriptions["dataworkspublic_endpoint"], + }, "hcs_sgw": { Type: schema.TypeString, Optional: true, @@ -2941,6 +3022,12 @@ func endpointsSchema() *schema.Schema { Default: "", Description: descriptions["gwsecd_endpoint"], }, + "ecd": { + Type: schema.TypeString, + Optional: true, + Default: "", + Description: descriptions["gwsecd_endpoint"], + }, "scdn": { Type: schema.TypeString, Optional: true, @@ -2960,7 +3047,12 @@ func endpointsSchema() *schema.Schema { Default: "", Description: descriptions["serverless_endpoint"], }, - + "sae": { + Type: schema.TypeString, + Optional: true, + Default: "", + Description: descriptions["serverless_endpoint"], + }, "hbr": { Type: schema.TypeString, Optional: true, @@ -2968,6 +3060,13 @@ func endpointsSchema() *schema.Schema { Description: descriptions["hbr_endpoint"], }, + "amqp": { + Type: schema.TypeString, + Optional: true, + Default: "", + Description: descriptions["onsproxy_endpoint"], + }, + "onsproxy": { Type: schema.TypeString, Optional: true, @@ -3029,6 +3128,11 @@ func endpointsSchema() *schema.Schema { Default: "", Description: descriptions["resourcesharing_endpoint"], }, + "resourcesharing": { + Type: schema.TypeString, + Optional: true, + Default: "", + }, "ga": { Type: schema.TypeString, Optional: true, @@ -3281,6 +3385,12 @@ func endpointsSchema() *schema.Schema { Default: "", Description: descriptions["drds_endpoint"], }, + "polardbx": { + Type: schema.TypeString, + Optional: true, + Default: "", + Description: descriptions["drds_endpoint"], + }, "dds": { Type: schema.TypeString, Optional: true, @@ -3311,6 +3421,12 @@ func endpointsSchema() *schema.Schema { Default: "", Description: descriptions["fc_endpoint"], }, + "fc_open": { + Type: schema.TypeString, + Optional: true, + Default: "", + Description: descriptions["fc_endpoint"], + }, "apigateway": { Type: schema.TypeString, Optional: true, @@ -3323,12 +3439,28 @@ func endpointsSchema() *schema.Schema { Default: "", Description: descriptions["datahub_endpoint"], }, + "devops_rdc": { + Type: schema.TypeString, + Optional: true, + Default: "", + }, "mns": { Type: schema.TypeString, Optional: true, Default: "", Description: descriptions["mns_endpoint"], }, + "mns_open": { + Type: schema.TypeString, + Optional: true, + Default: "", + Description: descriptions["mns_endpoint"], + }, + "rocketmq": { + Type: schema.TypeString, + Optional: true, + Default: "", + }, "location": { Type: schema.TypeString, Optional: true, @@ -3543,6 +3675,38 @@ func endpointsToHash(v interface{}) int { return hashcode.String(buf.String()) } +// deprecatedEndpointMap is used to map old service name to new service name +// key: new endpoint key +// value: deprecated endpoint key +var deprecatedEndpointMap = map[string]string{ + "resourcesharing": "ressharing", + "ga": "gaplus", + "dms_enterprise": "dmsenterprise", + "sgw": "hcs_sgw", + "amqp": "onsproxy", + "cassandra": "cds", + "cloudfw": "cloudfirewall", + "sae": "serverless", + "r_kvstore": "redisa", + "ecd": "gwsecd", + "dataworks_public": "dataworkspublic", + "dfs": "alidfs", + "swas_open": "swas", + "quickbi_public": "quickbi", + "gdb": "gds", + "cr": "acr", + "eds_user": "edsuser", + "antiddos_public": "ddosbasic", + "schedulerx2": "edasschedulerx", + "ehpc": "ehs", + "sms": "dysms", + "dbs": "cbs", + "mns_open": "mns", + "servicecatalog": "srvcatalog", + "oceanbasepro": "oceanbase", + "chatbot": "beebot", +} + func getConfigFromProfile(d *schema.ResourceData, ProfileKey string) (interface{}, error) { if providerConfig == nil { From cf67c06c416fb5c0aa040f438fe4b5cc306dbc8e Mon Sep 17 00:00:00 2001 From: "guimin.hgm" Date: Fri, 8 Nov 2024 14:49:19 +0800 Subject: [PATCH 2/3] add account_type and support more getting endpoint rule --- alicloud/connectivity/client.go | 36 +++++++++ alicloud/connectivity/config.go | 1 + alicloud/connectivity/endpoint.go | 123 +++++++++++++++++++++--------- alicloud/provider.go | 16 +++- 4 files changed, 134 insertions(+), 42 deletions(-) diff --git a/alicloud/connectivity/client.go b/alicloud/connectivity/client.go index b7d7e680871b..1c6f8ffc2317 100644 --- a/alicloud/connectivity/client.go +++ b/alicloud/connectivity/client.go @@ -5564,6 +5564,42 @@ func (client *AliyunClient) RpcPost(apiProductCode string, apiVersion string, ap runtime.SetAutoretry(autoRetry) return conn.DoRequest(tea.String(apiName), nil, tea.String("POST"), tea.String(apiVersion), tea.String("AK"), query, body, runtime) } + +// RpcPost invoking RPC API request with POST method +// parameters: +// +// apiProductCode: API Product code, its value equals to the gateway code of the API +// apiVersion - API version +// apiName - API Name +// query - API parameters in query +// body - API parameters in body +// autoRetry - whether to auto retry while the runtime has a 5xx error +func (client *AliyunClient) RpcPostWithEndpoint(apiProductCode string, apiVersion string, apiName string, query map[string]interface{}, body map[string]interface{}, autoRetry bool, endpoint string) (map[string]interface{}, error) { + var err error + if endpoint == "" { + apiProductCode = strings.ToLower(ConvertKebabToSnake(apiProductCode)) + endpoint, err = client.loadApiEndpoint(apiProductCode) + if err != nil { + return nil, err + } + } + sdkConfig := client.teaSdkConfig + sdkConfig.SetEndpoint(endpoint) + credential, err := client.config.Credential.GetCredential() + if err != nil || credential == nil { + return nil, fmt.Errorf("get credential failed. Error: %#v", err) + } + sdkConfig.SetAccessKeyId(*credential.AccessKeyId) + sdkConfig.SetAccessKeySecret(*credential.AccessKeySecret) + sdkConfig.SetSecurityToken(*credential.SecurityToken) + conn, err := rpc.NewClient(&sdkConfig) + if err != nil { + return nil, fmt.Errorf("unable to initialize the %s api client: %#v", apiProductCode, err) + } + runtime := &util.RuntimeOptions{} + runtime.SetAutoretry(autoRetry) + return conn.DoRequest(tea.String(apiName), nil, tea.String("POST"), tea.String(apiVersion), tea.String("AK"), query, body, runtime) +} func (client *AliyunClient) NewPaiworkspaceClient() (*roa.Client, error) { productCode := "paiworkspace" endpoint := "" diff --git a/alicloud/connectivity/config.go b/alicloud/connectivity/config.go index 60e8e3a7b68c..9528c111ca0e 100644 --- a/alicloud/connectivity/config.go +++ b/alicloud/connectivity/config.go @@ -31,6 +31,7 @@ type Config struct { SecurityToken string OtsInstanceName string AccountId string + AccountType string Protocol string ClientReadTimeout int ClientConnectTimeout int diff --git a/alicloud/connectivity/endpoint.go b/alicloud/connectivity/endpoint.go index 3199b895862a..3e9ee927ca74 100644 --- a/alicloud/connectivity/endpoint.go +++ b/alicloud/connectivity/endpoint.go @@ -297,6 +297,7 @@ var productCodeToLocationCode = map[string]string{ "ots": "ots", // OTS "tablestore": "ots", // OTS "ram": "ram", //RAM + "quotas": "quotas", //Quotas "market": "market", //Market } @@ -306,35 +307,26 @@ var productCodeToLocationCode = map[string]string{ // Value: product endpoint // The priority of this configuration is higher than location service, lower than user environment variable configuration var irregularProductEndpoint = map[string]string{ - "tablestore": "tablestore.%s.aliyuncs.com", - "bssopenapi": BssOpenAPIEndpointDomestic, - "ram": "ram.aliyuncs.com", - "ddoscoo": "ddoscoo.cn-hangzhou.aliyuncs.com", - "dcdn": "dcdn.aliyuncs.com", - "config": "config.cn-shanghai.aliyuncs.com", - "ga": "ga.cn-hangzhou.aliyuncs.com", - "brain_industrial": "brain-industrial.cn-hangzhou.aliyuncs.com", - "eipanycast": "eipanycast.cn-hangzhou.aliyuncs.com", - "ims": "ims.aliyuncs.com", - "resourcemanager": "resourcemanager.aliyuncs.com", - "quotas": "quotas.aliyuncs.com", - "cassandra": "cassandra.aliyuncs.com", - "cas": "cas.aliyuncs.com", - "cloudfw": "cloudfw.aliyuncs.com", - "scdn": "scdn.aliyuncs.com", - "cdn": "cdn.aliyuncs.com", - "mscopensubscription": "mscopensubscription.aliyuncs.com", - "cloudauth": "cloudauth.aliyuncs.com", - "imp": "imp.aliyuncs.com", - "mhub": "mhub.cn-shanghai.aliyuncs.com", - "eds_user": "eds-user.cn-shanghai.aliyuncs.com", //eds-user.ap-southeast-1.aliyuncs.com - "vpcpeer": "vpcpeer.aliyuncs.com", - "das": "das.cn-shanghai.aliyuncs.com", - "servicecatalog": "servicecatalog.cn-hangzhou.aliyuncs.com", - "chatbot": "chatbot.cn-shanghai.aliyuncs.com", - "computenest": "computenest.cn-hangzhou.aliyuncs.com", - "resourcecenter": "resourcecenter.aliyuncs.com", //resourcecenter-intl.aliyuncs.com - "market": "market.aliyuncs.com", + "tablestore": "tablestore.%s.aliyuncs.com", + "ram": "ram.aliyuncs.com", + "brain_industrial": "brain-industrial.cn-hangzhou.aliyuncs.com", + "cassandra": "cassandra.aliyuncs.com", + "cas": "cas.aliyuncs.com", + "cloudfw": "cloudfw.aliyuncs.com", + "scdn": "scdn.aliyuncs.com", + "vpcpeer": "vpcpeer.aliyuncs.com", + "resourcecenter": "resourcecenter.aliyuncs.com", //resourcecenter-intl.aliyuncs.com + "market": "market.aliyuncs.com", +} + +// irregularProductEndpointForIntlAccount specially records those product codes that +// cannot be parsed out by the location service and sensitive to account type. +// These products adapt to international account. +// Key: product code, its value equals to the gateway code of the API after converting it to lowercase and using underscores +// Value: product endpoint +// The priority of this configuration is higher than location service, lower than user environment variable configuration +var irregularProductEndpointForIntlAccount = map[string]string{ + "cas": "cas.ap-southeast-1.aliyuncs.com", } // regularProductEndpoint specially records those product codes that have been confirmed to be @@ -405,15 +397,61 @@ var regularProductEndpoint = map[string]string{ "hologram": "hologram.%s.aliyuncs.com", "foasconsole": "foasconsole.aliyuncs.com", "cs": "cs.%s.aliyuncs.com", - "waf_openapi": "wafopenapi.cn-hangzhou.aliyuncs.com", // wafopenapi.ap-southeast-1.aliyuncs.com + "waf_openapi": "wafopenapi.cn-hangzhou.aliyuncs.com", "dfs": "dfs.%s.aliyuncs.com", "amqp": "amqp-open.%s.aliyuncs.com", "cbn": "cbn.aliyuncs.com", "expressconnectrouter": "expressconnectrouter.cn-shanghai.aliyuncs.com", "green": "green.%s.aliyuncs.com", - "governance": "governance.cn-hangzhou.aliyuncs.com", // governance.ap-southeast-1.aliyuncs.com - "sms": "dysmsapi.aliyuncs.com", // dysmsapi.ap-southeast-1.aliyuncs.com - "sddp": "sddp.cn-zhangjiakou.aliyuncs.com", // sddp.ap-southeast-1.aliyuncs.com + "governance": "governance.cn-hangzhou.aliyuncs.com", + "sms": "dysmsapi.aliyuncs.com", + "sddp": "sddp.cn-zhangjiakou.aliyuncs.com", + "ddoscoo": "ddoscoo.cn-hangzhou.aliyuncs.com", + "config": "config.cn-shanghai.aliyuncs.com", + "ga": "ga.cn-hangzhou.aliyuncs.com", + "dcdn": "dcdn.aliyuncs.com", + "cdn": "cdn.aliyuncs.com", + "cloudauth": "cloudauth.aliyuncs.com", + "ims": "ims.aliyuncs.com", + "mhub": "mhub.cn-shanghai.aliyuncs.com", + "eds_user": "eds-user.cn-shanghai.aliyuncs.com", + "eipanycast": "eipanycast.cn-hangzhou.aliyuncs.com", + "mscopensubscription": "mscopensubscription.aliyuncs.com", + "resourcemanager": "resourcemanager.aliyuncs.com", + "quotas": "quotas.aliyuncs.com", + "imp": "imp.aliyuncs.com", + "das": "das.cn-shanghai.aliyuncs.com", + "servicecatalog": "servicecatalog.cn-hangzhou.aliyuncs.com", + "chatbot": "chatbot.cn-shanghai.aliyuncs.com", + "computenest": "computenest.cn-hangzhou.aliyuncs.com", + "bssopenapi": BssOpenAPIEndpointDomestic, +} + +// regularProductEndpointForIntlAccount specially records those product codes that have been confirmed to be +// regional or central endpoints. But the endpoints are sensitive to account type. +// These products adapt to international account. +// Key: product code, its value equals to the gateway code of the API after converting it to lowercase and using underscores +// Value: product endpoint +// The priority of this configuration is lower than location service, and as a backup endpoint +var regularProductEndpointForIntlAccount = map[string]string{ + "config": "config.ap-southeast-1.aliyuncs.com", +} + +// regularProductEndpointForIntlRegion specially records those product codes that have been confirmed to be +// regional or central endpoints. But the endpoints are sensitive to region. +// These products adapt to international region, and conflict with regularProductEndpointForIntlAccount +// Key: product code, its value equals to the gateway code of the API after converting it to lowercase and using underscores +// Value: product endpoint +// The priority of this configuration is lower than location service, and as a backup endpoint +var regularProductEndpointForIntlRegion = map[string]string{ + "ddoscoo": "ddoscoo.ap-southeast-1.aliyuncs.com", + "mscopensubscription": "mscopensubscription.aliyuncs.com", + "eds_user": "eds-user.ap-southeast-1.aliyuncs.com", + "bssopenapi": BssOpenAPIEndpointInternational, + "sms": "dysmsapi.ap-southeast-1.aliyuncs.com", + "sddp": "sddp.ap-southeast-1.aliyuncs.com", + "governance": "governance.ap-southeast-1.aliyuncs.com", + "waf_openapi": "wafopenapi.ap-southeast-1.aliyuncs.com", } // NOTE: The productCode must be lower. @@ -431,6 +469,9 @@ func (client *AliyunClient) loadEndpoint(productCode string) error { // Secondly, load endpoint from known rules if endpointFmt, ok := irregularProductEndpoint[productCode]; ok { + if v, ok := irregularProductEndpointForIntlAccount[productCode]; ok && strings.ToLower(client.config.AccountType) == "international" { + endpointFmt = v + } if strings.Contains(endpointFmt, "%s") { endpointFmt = fmt.Sprintf(endpointFmt, client.RegionId) } @@ -442,12 +483,18 @@ func (client *AliyunClient) loadEndpoint(productCode string) error { endpoint, err := client.describeEndpointForService(productCode) if err == nil { client.config.Endpoints.Store(strings.ToLower(productCode), endpoint) - } else if v, ok := regularProductEndpoint[productCode]; ok { - if strings.Contains(v, "%s") { - v = fmt.Sprintf(v, client.RegionId) + } else if endpointFmt, ok := regularProductEndpoint[productCode]; ok { + if v, ok := regularProductEndpointForIntlAccount[productCode]; ok && strings.ToLower(client.config.AccountType) == "international" { + endpointFmt = v } - client.config.Endpoints.Store(productCode, v) - log.Printf("[WARN] loading %s endpoint got an error: %#v. Using the endpoint %s instead.", productCode, err, v) + if v, ok := regularProductEndpointForIntlRegion[productCode]; ok && !strings.HasPrefix(client.RegionId, "cn-") { + endpointFmt = v + } + if strings.Contains(endpointFmt, "%s") { + endpointFmt = fmt.Sprintf(endpointFmt, client.RegionId) + } + client.config.Endpoints.Store(productCode, endpointFmt) + log.Printf("[WARN] loading %s endpoint got an error: %#v. Using the endpoint %s instead.", productCode, err, endpointFmt) return nil } return err diff --git a/alicloud/provider.go b/alicloud/provider.go index 0db78afa5251..7ae269e861b7 100644 --- a/alicloud/provider.go +++ b/alicloud/provider.go @@ -81,6 +81,12 @@ func Provider() terraform.ResourceProvider { DefaultFunc: schema.MultiEnvDefaultFunc([]string{"ALICLOUD_ACCOUNT_ID", "ALIBABA_CLOUD_ACCOUNT_ID"}, nil), Description: descriptions["account_id"], }, + "account_type": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: StringInSlice([]string{"Domestic", "International"}, true), + DefaultFunc: schema.MultiEnvDefaultFunc([]string{"ALICLOUD_ACCOUNT_TYPE", "ALIBABA_CLOUD_ACCOUNT_TYPE"}, nil), + }, "assume_role": assumeRoleSchema(), "sign_version": signVersionSchema(), "assume_role_with_oidc": assumeRoleWithOidcSchema(), @@ -1851,6 +1857,12 @@ func providerConfigure(d *schema.ResourceData, p *schema.Provider) (interface{}, } config.Credential = credential } + if account, ok := d.GetOk("account_id"); ok && account.(string) != "" { + config.AccountId = strings.TrimSpace(account.(string)) + } + if v, ok := d.GetOk("account_type"); ok && v.(string) != "" { + config.AccountType = v.(string) + } if v, ok := d.GetOk("security_transport"); config.SecureTransport == "" && ok && v.(string) != "" { config.SecureTransport = v.(string) } @@ -2068,10 +2080,6 @@ func providerConfigure(d *schema.ResourceData, p *schema.Provider) (interface{}, config.MnsEndpoint = strings.TrimSpace(mnsEndpoint.(string)) } - if account, ok := d.GetOk("account_id"); ok && account.(string) != "" { - config.AccountId = strings.TrimSpace(account.(string)) - } - if fcEndpoint, ok := d.GetOk("fc"); ok && fcEndpoint.(string) != "" { config.FcEndpoint = strings.TrimSpace(fcEndpoint.(string)) } From 5605ab005d1c126f05384bfa08837f581f135e59 Mon Sep 17 00:00:00 2001 From: "guimin.hgm" Date: Fri, 8 Nov 2024 18:47:56 +0800 Subject: [PATCH 3/3] Market: Improves the invoking market api method and supports refreshing credential automatically --- alicloud/connectivity/client.go | 5 +++++ website/docs/r/market_order.html.markdown | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/alicloud/connectivity/client.go b/alicloud/connectivity/client.go index 1c6f8ffc2317..bf4f2590203f 100644 --- a/alicloud/connectivity/client.go +++ b/alicloud/connectivity/client.go @@ -1901,6 +1901,11 @@ func (client *AliyunClient) WithMarketClient(do func(*market.Client) (interface{ marketconn.AppendUserAgent(Module, client.config.ConfigurationSource) marketconn.AppendUserAgent(TerraformTraceId, client.config.TerraformTraceId) client.marketconn = marketconn + } else { + err := client.marketconn.InitWithOptions(client.RegionId, client.getSdkConfig(), client.config.getAuthCredential(true)) + if err != nil { + return nil, fmt.Errorf("unable to initialize the Market client: %#v", err) + } } return do(client.marketconn) diff --git a/website/docs/r/market_order.html.markdown b/website/docs/r/market_order.html.markdown index f2a797087399..9c62a990a1fe 100644 --- a/website/docs/r/market_order.html.markdown +++ b/website/docs/r/market_order.html.markdown @@ -7,13 +7,13 @@ description: |- Provides a market order resource. --- -# alicloud\_market\_order +# alicloud_market_order Provides a market order resource. -> **NOTE:** Terraform will auto build a market order while it uses `alicloud_market_order` to build a market order resource. --> **NOTE:** Available in 1.69.0+ +-> **NOTE:** Available since v1.69.0. ## Example Usage @@ -28,7 +28,7 @@ Basic Usage ```terraform resource "alicloud_market_order" "order" { product_code = "cmapi033136" - pay_type = "prepay" + pay_type = "PrePaid" quantity = 1 duration = 1 pricing_cycle = "Month"