Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow more than 1000 state files #223

Merged
merged 3 commits into from Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 16 additions & 9 deletions state/aws.go
Expand Up @@ -135,25 +135,32 @@ func (a *AWS) GetLocks() (locks map[string]LockInfo, err error) {

// GetStates returns a slice of State files in the S3 bucket
func (a *AWS) GetStates() (states []string, err error) {
truncatedListing := true
var keys []string
log.WithFields(log.Fields{
"bucket": a.bucket,
"prefix": a.keyPrefix,
}).Debug("Listing states from S3")
result, err := a.svc.ListObjects(&s3.ListObjectsInput{

params := s3.ListObjectsV2Input{
Bucket: aws_sdk.String(a.bucket),
Prefix: &a.keyPrefix,
})
if err != nil {
return states, err
}
for truncatedListing {
result, err := a.svc.ListObjectsV2(&params)
if err != nil {
return states, err
}

var keys []string
for _, obj := range result.Contents {
for _, ext := range a.fileExtension {
if strings.HasSuffix(*obj.Key, ext) {
keys = append(keys, *obj.Key)
for _, obj := range result.Contents {
for _, ext := range a.fileExtension {
if strings.HasSuffix(*obj.Key, ext) {
keys = append(keys, *obj.Key)
}
}
}
params.ContinuationToken = result.NextContinuationToken
truncatedListing = *result.IsTruncated
}
states = keys
log.WithFields(log.Fields{
Expand Down
12 changes: 8 additions & 4 deletions state/aws_test.go
Expand Up @@ -276,8 +276,12 @@ type s3Mock struct {
s3iface.S3API
}

func (s *s3Mock) ListObjects(_ *s3.ListObjectsInput) (*s3.ListObjectsOutput, error) {
return &s3.ListObjectsOutput{Contents: []*s3.Object{{Key: aws.String("test.tfstate")}}}, nil
func (s *s3Mock) ListObjectsV2(_ *s3.ListObjectsV2Input) (*s3.ListObjectsV2Output, error) {
return &s3.ListObjectsV2Output{Contents: []*s3.Object{
{Key: aws.String("test.tfstate")}, {Key: aws.String("test2.tfstate")}, {Key: aws.String("test3.tfstate")}},
IsTruncated: func() *bool { b := false; return &b }(),
KeyCount: func() *int64 { b := int64(3); return &b }(),
MaxKeys: func() *int64 { b := int64(1000); return &b }()}, nil
}
func (s *s3Mock) ListObjectVersions(_ *s3.ListObjectVersionsInput) (*s3.ListObjectVersionsOutput, error) {
return &s3.ListObjectVersionsOutput{
Expand Down Expand Up @@ -316,8 +320,8 @@ func TestGetStates(t *testing.T) {
states, err := awsInstance.GetStates()
if err != nil {
t.Error(err)
} else if len(states) != 1 {
t.Error("Expected one state")
} else if len(states) != 3 {
t.Error("Expected three states")
}
}

Expand Down