backend/oss: Changes the DescribeEndpoint to DescribeEndpoints to fixes the unsupported sts bug

This commit is contained in:
xiaozhu36 2021-07-15 16:07:18 +08:00
parent 3e5bfa7364
commit c495caafeb
2 changed files with 16 additions and 20 deletions

View File

@ -319,20 +319,14 @@ func (b *Backend) configure(ctx context.Context) error {
} }
if endpoint == "" { if endpoint == "" {
endpointItem, _ := b.getOSSEndpointByRegion(accessKey, secretKey, securityToken, region) endpointsResponse, _ := b.getOSSEndpointByRegion(accessKey, secretKey, securityToken, region)
if endpointItem != nil && len(endpointItem.Endpoint) > 0 { for _, endpointItem := range endpointsResponse.Endpoints.Endpoint {
if len(endpointItem.Protocols.Protocols) > 0 { if endpointItem.Type == "openAPI" {
// HTTP or HTTPS endpoint = endpointItem.Endpoint
schma = strings.ToLower(endpointItem.Protocols.Protocols[0]) break
for _, p := range endpointItem.Protocols.Protocols {
if strings.ToLower(p) == "https" {
schma = strings.ToLower(p)
break
}
}
} }
endpoint = endpointItem.Endpoint }
} else { if endpoint == "" {
endpoint = fmt.Sprintf("oss-%s.aliyuncs.com", region) endpoint = fmt.Sprintf("oss-%s.aliyuncs.com", region)
} }
} }
@ -367,8 +361,8 @@ func (b *Backend) configure(ctx context.Context) error {
return err return err
} }
func (b *Backend) getOSSEndpointByRegion(access_key, secret_key, security_token, region string) (*location.DescribeEndpointResponse, error) { func (b *Backend) getOSSEndpointByRegion(access_key, secret_key, security_token, region string) (*location.DescribeEndpointsResponse, error) {
args := location.CreateDescribeEndpointRequest() args := location.CreateDescribeEndpointsRequest()
args.ServiceCode = "oss" args.ServiceCode = "oss"
args.Id = region args.Id = region
args.Domain = "location-readonly.aliyuncs.com" args.Domain = "location-readonly.aliyuncs.com"
@ -379,7 +373,7 @@ func (b *Backend) getOSSEndpointByRegion(access_key, secret_key, security_token,
} }
locationClient.AppendUserAgent(TerraformUA, TerraformVersion) locationClient.AppendUserAgent(TerraformUA, TerraformVersion)
endpointsResponse, err := locationClient.DescribeEndpoint(args) endpointsResponse, err := locationClient.DescribeEndpoints(args)
if err != nil { if err != nil {
return nil, fmt.Errorf("Describe oss endpoint using region: %#v got an error: %#v.", region, err) return nil, fmt.Errorf("Describe oss endpoint using region: %#v got an error: %#v.", region, err)
} }

View File

@ -2,6 +2,7 @@ package oss
import ( import (
"fmt" "fmt"
"math/rand"
"os" "os"
"testing" "testing"
"time" "time"
@ -69,9 +70,10 @@ func TestBackendConfig(t *testing.T) {
func TestBackendConfigWorkSpace(t *testing.T) { func TestBackendConfigWorkSpace(t *testing.T) {
testACC(t) testACC(t)
bucketName := fmt.Sprintf("terraform-backend-oss-test-%d", rand.Intn(1000))
config := map[string]interface{}{ config := map[string]interface{}{
"region": "cn-beijing", "region": "cn-beijing",
"bucket": "terraform-backend-oss-test", "bucket": bucketName,
"prefix": "mystate", "prefix": "mystate",
"key": "first.tfstate", "key": "first.tfstate",
"tablestore_endpoint": "https://terraformstate.cn-beijing.ots.aliyuncs.com", "tablestore_endpoint": "https://terraformstate.cn-beijing.ots.aliyuncs.com",
@ -79,15 +81,15 @@ func TestBackendConfigWorkSpace(t *testing.T) {
} }
b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(config)).(*Backend) b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(config)).(*Backend)
createOSSBucket(t, b.ossClient, "terraform-backend-oss-test") createOSSBucket(t, b.ossClient, bucketName)
defer deleteOSSBucket(t, b.ossClient, "terraform-backend-oss-test") defer deleteOSSBucket(t, b.ossClient, bucketName)
if _, err := b.Workspaces(); err != nil { if _, err := b.Workspaces(); err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if !strings.HasPrefix(b.ossClient.Config.Endpoint, "https://oss-cn-beijing") { if !strings.HasPrefix(b.ossClient.Config.Endpoint, "https://oss-cn-beijing") {
t.Fatalf("Incorrect region was provided") t.Fatalf("Incorrect region was provided")
} }
if b.bucketName != "terraform-backend-oss-test" { if b.bucketName != bucketName {
t.Fatalf("Incorrect bucketName was provided") t.Fatalf("Incorrect bucketName was provided")
} }
if b.statePrefix != "mystate" { if b.statePrefix != "mystate" {