Skip to content

Commit 8dd722e

Browse files
authored
Merge pull request #29167 from xiaozhu36/xiaozhu
backend/oss: Changes the DescribeEndpoint to DescribeEndpoints to fixes the unsupported sts bug
2 parents 431aa02 + c495caa commit 8dd722e

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

internal/backend/remote-state/oss/backend.go

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -319,20 +319,14 @@ func (b *Backend) configure(ctx context.Context) error {
319319
}
320320

321321
if endpoint == "" {
322-
endpointItem, _ := b.getOSSEndpointByRegion(accessKey, secretKey, securityToken, region)
323-
if endpointItem != nil && len(endpointItem.Endpoint) > 0 {
324-
if len(endpointItem.Protocols.Protocols) > 0 {
325-
// HTTP or HTTPS
326-
schma = strings.ToLower(endpointItem.Protocols.Protocols[0])
327-
for _, p := range endpointItem.Protocols.Protocols {
328-
if strings.ToLower(p) == "https" {
329-
schma = strings.ToLower(p)
330-
break
331-
}
332-
}
322+
endpointsResponse, _ := b.getOSSEndpointByRegion(accessKey, secretKey, securityToken, region)
323+
for _, endpointItem := range endpointsResponse.Endpoints.Endpoint {
324+
if endpointItem.Type == "openAPI" {
325+
endpoint = endpointItem.Endpoint
326+
break
333327
}
334-
endpoint = endpointItem.Endpoint
335-
} else {
328+
}
329+
if endpoint == "" {
336330
endpoint = fmt.Sprintf("oss-%s.aliyuncs.com", region)
337331
}
338332
}
@@ -367,8 +361,8 @@ func (b *Backend) configure(ctx context.Context) error {
367361
return err
368362
}
369363

370-
func (b *Backend) getOSSEndpointByRegion(access_key, secret_key, security_token, region string) (*location.DescribeEndpointResponse, error) {
371-
args := location.CreateDescribeEndpointRequest()
364+
func (b *Backend) getOSSEndpointByRegion(access_key, secret_key, security_token, region string) (*location.DescribeEndpointsResponse, error) {
365+
args := location.CreateDescribeEndpointsRequest()
372366
args.ServiceCode = "oss"
373367
args.Id = region
374368
args.Domain = "location-readonly.aliyuncs.com"
@@ -379,7 +373,7 @@ func (b *Backend) getOSSEndpointByRegion(access_key, secret_key, security_token,
379373

380374
}
381375
locationClient.AppendUserAgent(TerraformUA, TerraformVersion)
382-
endpointsResponse, err := locationClient.DescribeEndpoint(args)
376+
endpointsResponse, err := locationClient.DescribeEndpoints(args)
383377
if err != nil {
384378
return nil, fmt.Errorf("Describe oss endpoint using region: %#v got an error: %#v.", region, err)
385379
}

internal/backend/remote-state/oss/backend_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package oss
22

33
import (
44
"fmt"
5+
"math/rand"
56
"os"
67
"testing"
78
"time"
@@ -69,25 +70,26 @@ func TestBackendConfig(t *testing.T) {
6970

7071
func TestBackendConfigWorkSpace(t *testing.T) {
7172
testACC(t)
73+
bucketName := fmt.Sprintf("terraform-backend-oss-test-%d", rand.Intn(1000))
7274
config := map[string]interface{}{
7375
"region": "cn-beijing",
74-
"bucket": "terraform-backend-oss-test",
76+
"bucket": bucketName,
7577
"prefix": "mystate",
7678
"key": "first.tfstate",
7779
"tablestore_endpoint": "https://terraformstate.cn-beijing.ots.aliyuncs.com",
7880
"tablestore_table": "TableStore",
7981
}
8082

8183
b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(config)).(*Backend)
82-
createOSSBucket(t, b.ossClient, "terraform-backend-oss-test")
83-
defer deleteOSSBucket(t, b.ossClient, "terraform-backend-oss-test")
84+
createOSSBucket(t, b.ossClient, bucketName)
85+
defer deleteOSSBucket(t, b.ossClient, bucketName)
8486
if _, err := b.Workspaces(); err != nil {
8587
t.Fatal(err.Error())
8688
}
8789
if !strings.HasPrefix(b.ossClient.Config.Endpoint, "https://oss-cn-beijing") {
8890
t.Fatalf("Incorrect region was provided")
8991
}
90-
if b.bucketName != "terraform-backend-oss-test" {
92+
if b.bucketName != bucketName {
9193
t.Fatalf("Incorrect bucketName was provided")
9294
}
9395
if b.statePrefix != "mystate" {

0 commit comments

Comments
 (0)