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"
|
2021-06-04 15:24:10 +00:00
|
|
|
"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
|
2021-06-04 15:24:10 +00:00
|
|
|
cache cache.Cache
|
2021-02-11 11:21:49 +00:00
|
|
|
}
|
|
|
|
|
2021-06-04 15:24:10 +00:00
|
|
|
func NewDynamoDBRepository(session *session.Session, c cache.Cache) *dynamoDBRepository {
|
2021-02-11 11:21:49 +00:00
|
|
|
return &dynamoDBRepository{
|
|
|
|
dynamodb.New(session),
|
2021-06-04 15:24:10 +00:00
|
|
|
c,
|
2021-02-11 11:21:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *dynamoDBRepository) ListAllTables() ([]*string, error) {
|
2021-06-04 15:24:10 +00:00
|
|
|
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
|
|
|
|
}
|
2021-06-04 15:24:10 +00:00
|
|
|
|
|
|
|
r.cache.Put("dynamodbListAllTables", tables)
|
2021-02-11 11:21:49 +00:00
|
|
|
return tables, nil
|
|
|
|
}
|