2020-12-09 15:31:34 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2021-03-01 15:04:43 +00:00
|
|
|
"github.com/aws/aws-sdk-go/service/lambda"
|
2021-02-24 15:26:12 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
|
2020-12-09 15:31:34 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/deserializer"
|
2021-03-01 15:04:43 +00:00
|
|
|
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
|
2020-12-09 15:31:34 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
|
|
|
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
|
|
|
|
awsdeserializer "github.com/cloudskiff/driftctl/pkg/resource/aws/deserializer"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/terraform"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LambdaFunctionSupplier struct {
|
|
|
|
reader terraform.ResourceReader
|
|
|
|
deserializer deserializer.CTYDeserializer
|
2021-02-24 15:26:12 +00:00
|
|
|
client repository.LambdaRepository
|
2020-12-09 15:31:34 +00:00
|
|
|
runner *terraform.ParallelResourceReader
|
|
|
|
}
|
|
|
|
|
2021-02-09 13:56:11 +00:00
|
|
|
func NewLambdaFunctionSupplier(provider *AWSTerraformProvider) *LambdaFunctionSupplier {
|
2021-01-20 13:01:57 +00:00
|
|
|
return &LambdaFunctionSupplier{
|
2021-01-22 17:06:17 +00:00
|
|
|
provider,
|
2021-01-20 13:01:57 +00:00
|
|
|
awsdeserializer.NewLambdaFunctionDeserializer(),
|
2021-02-24 15:26:12 +00:00
|
|
|
repository.NewLambdaRepository(provider.session),
|
2021-01-22 17:06:17 +00:00
|
|
|
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
|
2021-01-20 13:01:57 +00:00
|
|
|
}
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s LambdaFunctionSupplier) Resources() ([]resource.Resource, error) {
|
2021-02-24 15:26:12 +00:00
|
|
|
functions, err := s.client.ListAllLambdaFunctions()
|
2020-12-09 15:31:34 +00:00
|
|
|
if err != nil {
|
2021-01-20 13:01:57 +00:00
|
|
|
return nil, remoteerror.NewResourceEnumerationError(err, resourceaws.AwsLambdaFunctionResourceType)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
results := make([]cty.Value, 0)
|
|
|
|
if len(functions) > 0 {
|
|
|
|
for _, function := range functions {
|
|
|
|
fun := *function
|
|
|
|
s.runner.Run(func() (cty.Value, error) {
|
|
|
|
return s.readLambda(fun)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
results, err = s.runner.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s.deserializer.Deserialize(results)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s LambdaFunctionSupplier) readLambda(function lambda.FunctionConfiguration) (cty.Value, error) {
|
|
|
|
name := *function.FunctionName
|
|
|
|
resFunction, err := s.reader.ReadResource(
|
|
|
|
terraform.ReadResourceArgs{
|
|
|
|
Ty: resourceaws.AwsLambdaFunctionResourceType,
|
|
|
|
ID: name,
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"function_name": name,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("Error reading function %s[%s]: %+v", name, resourceaws.AwsLambdaFunctionResourceType, err)
|
|
|
|
return cty.NilVal, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return *resFunction, nil
|
|
|
|
}
|