Skip to content

Commit

Permalink
add account_type and support more getting endpoint rule
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaozhu36 committed Nov 11, 2024
1 parent 2fc545b commit 6c18af7
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 42 deletions.
68 changes: 68 additions & 0 deletions alicloud/connectivity/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5564,6 +5564,74 @@ 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)
}

// RpcGet invoking RPC API request with GET 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
func (client *AliyunClient) RpcGet(apiProductCode string, apiVersion string, apiName string, query map[string]interface{}, body map[string]interface{}) (map[string]interface{}, error) {
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(true)
return conn.DoRequest(tea.String(apiName), nil, tea.String("GET"), tea.String(apiVersion), tea.String("AK"), query, body, runtime)
}
func (client *AliyunClient) NewPaiworkspaceClient() (*roa.Client, error) {
productCode := "paiworkspace"
endpoint := ""
Expand Down
1 change: 1 addition & 0 deletions alicloud/connectivity/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Config struct {
SecurityToken string
OtsInstanceName string
AccountId string
AccountType string
Protocol string
ClientReadTimeout int
ClientConnectTimeout int
Expand Down
123 changes: 85 additions & 38 deletions alicloud/connectivity/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ var productCodeToLocationCode = map[string]string{
"ots": "ots", // OTS
"tablestore": "ots", // OTS
"ram": "ram", //RAM
"quotas": "quotas", //Quotas
"market": "market", //Market
}

Expand All @@ -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
Expand Down Expand Up @@ -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",
"bssopenapi": BssOpenAPIEndpointInternational,
"mscopensubscription": "mscopensubscription.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",
"eds_user": "eds-user.ap-southeast-1.aliyuncs.com",
"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.
Expand All @@ -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)
}
Expand All @@ -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
Expand Down
16 changes: 12 additions & 4 deletions alicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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))
}
Expand Down

0 comments on commit 6c18af7

Please sign in to comment.