driftctl/pkg/remote/aws/ec2_key_pair_supplier.go

66 lines
1.9 KiB
Go
Raw Normal View History

package aws
import (
2021-02-23 17:16:10 +00:00
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
"github.com/cloudskiff/driftctl/pkg/remote/deserializer"
"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/aws/aws-sdk-go/aws"
"github.com/sirupsen/logrus"
"github.com/zclconf/go-cty/cty"
)
type EC2KeyPairSupplier struct {
reader terraform.ResourceReader
deserializer deserializer.CTYDeserializer
2021-02-23 17:16:10 +00:00
client repository.EC2Repository
runner *terraform.ParallelResourceReader
}
func NewEC2KeyPairSupplier(provider *AWSTerraformProvider) *EC2KeyPairSupplier {
return &EC2KeyPairSupplier{
provider,
awsdeserializer.NewEC2KeyPairDeserializer(),
2021-02-23 17:16:10 +00:00
repository.NewEC2Repository(provider.session),
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
}
}
func (s *EC2KeyPairSupplier) Resources() ([]resource.Resource, error) {
2021-02-23 17:16:10 +00:00
keyPairs, err := s.client.ListAllKeyPairs()
if err != nil {
return nil, remoteerror.NewResourceEnumerationError(err, resourceaws.AwsKeyPairResourceType)
}
results := make([]cty.Value, 0)
2021-02-23 17:16:10 +00:00
if len(keyPairs) > 0 {
for _, kp := range keyPairs {
name := aws.StringValue(kp.KeyName)
s.runner.Run(func() (cty.Value, error) {
return s.readKeyPair(name)
})
}
results, err = s.runner.Wait()
if err != nil {
return nil, err
}
}
return s.deserializer.Deserialize(results)
}
func (s *EC2KeyPairSupplier) readKeyPair(name string) (cty.Value, error) {
resKp, err := s.reader.ReadResource(terraform.ReadResourceArgs{
Ty: resourceaws.AwsKeyPairResourceType,
ID: name,
})
if err != nil {
logrus.Warnf("Error reading key pair %s: %+v", name, err)
return cty.NilVal, err
}
return *resKp, nil
}