driftctl/pkg/remote/aws/ecr_repository_supplier.go

63 lines
1.7 KiB
Go
Raw Normal View History

2021-02-26 19:59:46 +00:00
package aws
import (
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
"github.com/sirupsen/logrus"
"github.com/zclconf/go-cty/cty"
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/resource/aws"
"github.com/cloudskiff/driftctl/pkg/terraform"
)
type ECRRepositorySupplier struct {
reader terraform.ResourceReader
2021-05-21 14:09:45 +00:00
deserializer *resource.Deserializer
2021-02-26 19:59:46 +00:00
client repository.ECRRepository
runner *terraform.ParallelResourceReader
}
2021-05-31 15:39:16 +00:00
func NewECRRepositorySupplier(provider *AWSTerraformProvider, deserializer *resource.Deserializer, repo repository.ECRRepository) *ECRRepositorySupplier {
2021-02-26 19:59:46 +00:00
return &ECRRepositorySupplier{
provider,
2021-05-21 14:09:45 +00:00
deserializer,
2021-05-31 15:39:16 +00:00
repo,
2021-02-26 19:59:46 +00:00
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
}
}
func (r *ECRRepositorySupplier) Resources() ([]resource.Resource, error) {
2021-03-02 12:47:30 +00:00
repositories, err := r.client.ListAllRepositories()
2021-02-26 19:59:46 +00:00
if err != nil {
return nil, remoteerror.NewResourceEnumerationError(err, aws.AwsEcrRepositoryResourceType)
}
for _, repository := range repositories {
repository := repository
r.runner.Run(func() (cty.Value, error) {
return r.readRepository(repository)
})
}
retrieve, err := r.runner.Wait()
if err != nil {
return nil, err
}
2021-05-21 14:09:45 +00:00
return r.deserializer.Deserialize(aws.AwsEcrRepositoryResourceType, retrieve)
2021-02-26 19:59:46 +00:00
}
func (r *ECRRepositorySupplier) readRepository(repository *ecr.Repository) (cty.Value, error) {
2021-02-26 19:59:46 +00:00
val, err := r.reader.ReadResource(terraform.ReadResourceArgs{
ID: *repository.RepositoryName,
Ty: aws.AwsEcrRepositoryResourceType,
})
if err != nil {
logrus.Error(err)
return cty.NilVal, err
}
return *val, nil
}