2021-02-24 15:26:12 +00:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
|
|
|
"github.com/aws/aws-sdk-go/service/lambda"
|
|
|
|
"github.com/aws/aws-sdk-go/service/lambda/lambdaiface"
|
2021-06-04 14:16:35 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
2021-02-24 15:26:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type LambdaRepository interface {
|
|
|
|
ListAllLambdaFunctions() ([]*lambda.FunctionConfiguration, error)
|
2021-03-01 15:04:43 +00:00
|
|
|
ListAllLambdaEventSourceMappings() ([]*lambda.EventSourceMappingConfiguration, error)
|
2021-02-24 15:26:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type lambdaRepository struct {
|
|
|
|
client lambdaiface.LambdaAPI
|
2021-06-04 14:16:35 +00:00
|
|
|
cache cache.Cache
|
2021-02-24 15:26:12 +00:00
|
|
|
}
|
|
|
|
|
2021-06-04 14:16:35 +00:00
|
|
|
func NewLambdaRepository(session *session.Session, c cache.Cache) *lambdaRepository {
|
2021-02-24 15:26:12 +00:00
|
|
|
return &lambdaRepository{
|
|
|
|
lambda.New(session),
|
2021-06-04 14:16:35 +00:00
|
|
|
c,
|
2021-02-24 15:26:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *lambdaRepository) ListAllLambdaFunctions() ([]*lambda.FunctionConfiguration, error) {
|
2021-06-04 14:16:35 +00:00
|
|
|
if v := r.cache.Get("lambdaListAllLambdaFunctions"); v != nil {
|
|
|
|
return v.([]*lambda.FunctionConfiguration), nil
|
|
|
|
}
|
|
|
|
|
2021-02-24 15:26:12 +00:00
|
|
|
var functions []*lambda.FunctionConfiguration
|
|
|
|
input := &lambda.ListFunctionsInput{}
|
|
|
|
err := r.client.ListFunctionsPages(input, func(res *lambda.ListFunctionsOutput, lastPage bool) bool {
|
|
|
|
functions = append(functions, res.Functions...)
|
|
|
|
return !lastPage
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-04 14:16:35 +00:00
|
|
|
|
|
|
|
r.cache.Put("lambdaListAllLambdaFunctions", functions)
|
2021-02-24 15:26:12 +00:00
|
|
|
return functions, nil
|
|
|
|
}
|
2021-03-01 15:04:43 +00:00
|
|
|
|
|
|
|
func (r *lambdaRepository) ListAllLambdaEventSourceMappings() ([]*lambda.EventSourceMappingConfiguration, error) {
|
2021-06-04 14:16:35 +00:00
|
|
|
if v := r.cache.Get("lambdaListAllLambdaEventSourceMappings"); v != nil {
|
|
|
|
return v.([]*lambda.EventSourceMappingConfiguration), nil
|
|
|
|
}
|
|
|
|
|
2021-03-01 15:04:43 +00:00
|
|
|
var eventSourceMappingConfigurations []*lambda.EventSourceMappingConfiguration
|
|
|
|
input := &lambda.ListEventSourceMappingsInput{}
|
|
|
|
err := r.client.ListEventSourceMappingsPages(input, func(res *lambda.ListEventSourceMappingsOutput, lastPage bool) bool {
|
|
|
|
eventSourceMappingConfigurations = append(eventSourceMappingConfigurations, res.EventSourceMappings...)
|
|
|
|
return !lastPage
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-04 14:16:35 +00:00
|
|
|
|
|
|
|
r.cache.Put("lambdaListAllLambdaEventSourceMappings", eventSourceMappingConfigurations)
|
2021-03-01 15:04:43 +00:00
|
|
|
return eventSourceMappingConfigurations, nil
|
|
|
|
}
|