driftctl/pkg/remote/aws/repository/dynamodb_repository.go

44 lines
1.0 KiB
Go
Raw Normal View History

2021-02-11 11:21:49 +00:00
package repository
import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface"
"github.com/cloudskiff/driftctl/pkg/remote/cache"
2021-02-11 11:21:49 +00:00
)
type DynamoDBRepository interface {
ListAllTables() ([]*string, error)
}
type dynamoDBRepository struct {
client dynamodbiface.DynamoDBAPI
cache cache.Cache
2021-02-11 11:21:49 +00:00
}
func NewDynamoDBRepository(session *session.Session, c cache.Cache) *dynamoDBRepository {
2021-02-11 11:21:49 +00:00
return &dynamoDBRepository{
dynamodb.New(session),
c,
2021-02-11 11:21:49 +00:00
}
}
func (r *dynamoDBRepository) ListAllTables() ([]*string, error) {
if v := r.cache.Get("dynamodbListAllTables"); v != nil {
return v.([]*string), nil
}
2021-02-11 11:21:49 +00:00
var tables []*string
input := &dynamodb.ListTablesInput{}
err := r.client.ListTablesPages(input, func(res *dynamodb.ListTablesOutput, lastPage bool) bool {
tables = append(tables, res.TableNames...)
return !lastPage
})
if err != nil {
return nil, err
}
r.cache.Put("dynamodbListAllTables", tables)
2021-02-11 11:21:49 +00:00
return tables, nil
}