driftctl/pkg/remote/aws/db_instance_supplier.go

60 lines
1.7 KiB
Go

package aws
import (
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
"github.com/cloudskiff/driftctl/pkg/resource"
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
"github.com/cloudskiff/driftctl/pkg/terraform"
"github.com/sirupsen/logrus"
"github.com/zclconf/go-cty/cty"
)
type DBInstanceSupplier struct {
reader terraform.ResourceReader
deserializer *resource.Deserializer
repository repository.RDSRepository
runner *terraform.ParallelResourceReader
}
func NewDBInstanceSupplier(provider *AWSTerraformProvider, deserializer *resource.Deserializer, repository repository.RDSRepository) *DBInstanceSupplier {
return &DBInstanceSupplier{
provider,
deserializer,
repository,
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
}
}
func (s *DBInstanceSupplier) Resources() ([]resource.Resource, error) {
resourceList, err := s.repository.ListAllDBInstances()
if err != nil {
return nil, remoteerror.NewResourceEnumerationError(err, resourceaws.AwsDbInstanceResourceType)
}
for _, res := range resourceList {
id := *res.DBInstanceIdentifier
s.runner.Run(func() (cty.Value, error) {
completeResource, err := s.reader.ReadResource(terraform.ReadResourceArgs{
Ty: resourceaws.AwsDbInstanceResourceType,
ID: id,
})
if err != nil {
logrus.Warnf("Error reading %s[%s]: %+v", id, resourceaws.AwsDbInstanceResourceType, err)
return cty.NilVal, err
}
return *completeResource, nil
})
}
results, err := s.runner.Wait()
if err != nil {
return nil, err
}
return s.deserializer.Deserialize(resourceaws.AwsDbInstanceResourceType, results)
}