driftctl/pkg/remote/aws/kms_key_supplier.go

64 lines
1.6 KiB
Go
Raw Normal View History

2021-02-24 17:49:02 +00:00
package aws
import (
"github.com/aws/aws-sdk-go/service/kms"
"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"
2021-05-21 14:09:45 +00:00
2021-02-24 17:49:02 +00:00
"github.com/cloudskiff/driftctl/pkg/terraform"
)
type KMSKeySupplier struct {
reader terraform.ResourceReader
2021-05-21 14:09:45 +00:00
deserializer *resource.Deserializer
2021-02-24 17:49:02 +00:00
client repository.KMSRepository
runner *terraform.ParallelResourceReader
}
2021-05-31 15:39:16 +00:00
func NewKMSKeySupplier(provider *AWSTerraformProvider, deserializer *resource.Deserializer, repo repository.KMSRepository) *KMSKeySupplier {
2021-02-24 17:49:02 +00:00
return &KMSKeySupplier{
provider,
2021-05-21 14:09:45 +00:00
deserializer,
2021-05-31 15:39:16 +00:00
repo,
2021-02-24 17:49:02 +00:00
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
}
}
func (s *KMSKeySupplier) Resources() ([]resource.Resource, error) {
2021-02-24 17:49:02 +00:00
keys, err := s.client.ListAllKeys()
if err != nil {
return nil, remoteerror.NewResourceEnumerationError(err, aws.AwsKmsKeyResourceType)
}
for _, key := range keys {
key := key
s.runner.Run(func() (cty.Value, error) {
return s.readKey(key)
})
}
retrieve, err := s.runner.Wait()
if err != nil {
return nil, err
}
2021-05-21 14:09:45 +00:00
return s.deserializer.Deserialize(aws.AwsKmsKeyResourceType, retrieve)
2021-02-24 17:49:02 +00:00
}
func (s *KMSKeySupplier) readKey(key *kms.KeyListEntry) (cty.Value, error) {
2021-02-24 17:49:02 +00:00
val, err := s.reader.ReadResource(terraform.ReadResourceArgs{
ID: *key.KeyId,
Ty: aws.AwsKmsKeyResourceType,
})
if err != nil {
logrus.Error(err)
return cty.NilVal, err
}
return *val, nil
}