Merge pull request #25585 from chushenmeshile/master

fix ossbackend only returned 100 workspaces
This commit is contained in:
James Bardin 2020-08-19 16:55:31 -04:00 committed by GitHub
commit 0c5430f88d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 12 deletions

View File

@ -57,28 +57,39 @@ func (b *Backend) Workspaces() ([]string, error) {
} }
var options []oss.Option var options []oss.Option
options = append(options, oss.Prefix(b.statePrefix+"/")) options = append(options, oss.Prefix(b.statePrefix+"/"), oss.MaxKeys(1000))
resp, err := bucket.ListObjects(options...) resp, err := bucket.ListObjects(options...)
if err != nil { if err != nil {
return nil, err return nil, err
} }
result := []string{backend.DefaultStateName} result := []string{backend.DefaultStateName}
prefix := b.statePrefix prefix := b.statePrefix
for _, obj := range resp.Objects { lastObj := ""
// we have 3 parts, the state prefix, the workspace name, and the state file: <prefix>/<worksapce-name>/<key> for {
if path.Join(b.statePrefix, b.stateKey) == obj.Key { for _, obj := range resp.Objects {
// filter the default workspace // we have 3 parts, the state prefix, the workspace name, and the state file: <prefix>/<worksapce-name>/<key>
continue if path.Join(b.statePrefix, b.stateKey) == obj.Key {
// filter the default workspace
continue
}
lastObj = obj.Key
parts := strings.Split(strings.TrimPrefix(obj.Key, prefix+"/"), "/")
if len(parts) > 0 && parts[0] != "" {
result = append(result, parts[0])
}
} }
if resp.IsTruncated {
parts := strings.Split(strings.TrimPrefix(obj.Key, prefix+"/"), "/") if len(options) == 3 {
if len(parts) > 0 && parts[0] != "" { options[2] = oss.Marker(lastObj)
result = append(result, parts[0]) } else {
options = append(options, oss.Marker(lastObj))
}
resp, err = bucket.ListObjects(options...)
} else {
break
} }
} }
sort.Strings(result[1:]) sort.Strings(result[1:])
return result, nil return result, nil
} }

View File

@ -67,6 +67,44 @@ func TestBackendConfig(t *testing.T) {
} }
} }
func TestBackendConfigWorkSpace(t *testing.T) {
testACC(t)
config := map[string]interface{}{
"region": "cn-beijing",
"bucket": "terraform-backend-oss-test",
"prefix": "mystate",
"key": "first.tfstate",
"tablestore_endpoint": "https://terraformstate.cn-beijing.ots.aliyuncs.com",
"tablestore_table": "TableStore",
}
b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(config)).(*Backend)
createOSSBucket(t, b.ossClient, "terraform-backend-oss-test")
defer deleteOSSBucket(t, b.ossClient, "terraform-backend-oss-test")
if _, err := b.Workspaces(); err != nil {
t.Fatal(err.Error())
}
if !strings.HasPrefix(b.ossClient.Config.Endpoint, "https://oss-cn-beijing") {
t.Fatalf("Incorrect region was provided")
}
if b.bucketName != "terraform-backend-oss-test" {
t.Fatalf("Incorrect bucketName was provided")
}
if b.statePrefix != "mystate" {
t.Fatalf("Incorrect state file path was provided")
}
if b.stateKey != "first.tfstate" {
t.Fatalf("Incorrect keyName was provided")
}
if b.ossClient.Config.AccessKeyID == "" {
t.Fatalf("No Access Key Id was provided")
}
if b.ossClient.Config.AccessKeySecret == "" {
t.Fatalf("No Secret Access Key was provided")
}
}
func TestBackendConfigProfile(t *testing.T) { func TestBackendConfigProfile(t *testing.T) {
testACC(t) testACC(t)
config := map[string]interface{}{ config := map[string]interface{}{