Skip to content

Commit

Permalink
add irregularProductEndpointForIntl
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaozhu36 committed Oct 19, 2024
1 parent da66017 commit e35f621
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 10 deletions.
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
57 changes: 51 additions & 6 deletions alicloud/connectivity/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,6 @@ var irregularProductEndpoint = map[string]string{
"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",
Expand All @@ -337,6 +336,32 @@ var irregularProductEndpoint = map[string]string{
"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{
"bssopenapi": BssOpenAPIEndpointInternational,
}

// irregularProductEndpointForIntlRegion specially records those product codes that
// cannot be parsed out by the location service and sensitive to region.
// These products adapt to international region, and conflict with irregularProductEndpointForIntlAccount
// 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 irregularProductEndpointForIntlRegion = map[string]string{
"ddoscoo": "ddoscoo.ap-southeast-1.aliyuncs.com",
"dcdn": "dcdn.aliyuncs.com",
"cas": "cas.ap-southeast-1.aliyuncs.com",
"cdn": "cdn.ap-southeast-1.aliyuncs.com",
"eds_user": "eds-user.ap-southeast-1.aliyuncs.com",
"computenest": "computenest.ap-southeast-1.aliyuncs.com",
"resourcecenter": "resourcecenter-intl.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
Expand Down Expand Up @@ -414,6 +439,17 @@ var regularProductEndpoint = map[string]string{
"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
"config": "config.cn-shanghai.aliyuncs.com",
}

// 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",
}

// NOTE: The productCode must be lower.
Expand All @@ -431,6 +467,12 @@ 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 v, ok := irregularProductEndpointForIntlRegion[productCode]; ok && !strings.HasPrefix(client.RegionId, "cn-") {
endpointFmt = v
}
if strings.Contains(endpointFmt, "%s") {
endpointFmt = fmt.Sprintf(endpointFmt, client.RegionId)
}
Expand All @@ -442,12 +484,15 @@ 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 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 @@ -1848,6 +1854,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 @@ -2065,10 +2077,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 e35f621

Please sign in to comment.