From cf67c06c416fb5c0aa040f438fe4b5cc306dbc8e Mon Sep 17 00:00:00 2001 From: "guimin.hgm" Date: Fri, 8 Nov 2024 14:49:19 +0800 Subject: [PATCH] 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)) }