2020-12-09 15:31:34 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2021-01-20 13:01:57 +00:00
|
|
|
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
|
|
|
|
|
2020-12-09 15:31:34 +00:00
|
|
|
"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/aws/aws-sdk-go/service/ec2"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
)
|
|
|
|
|
|
|
|
type EC2AmiSupplier struct {
|
|
|
|
reader terraform.ResourceReader
|
|
|
|
deserializer deserializer.CTYDeserializer
|
|
|
|
client ec2iface.EC2API
|
|
|
|
runner *terraform.ParallelResourceReader
|
|
|
|
}
|
|
|
|
|
2021-01-22 17:06:17 +00:00
|
|
|
func NewEC2AmiSupplier(provider *TerraformProvider) *EC2AmiSupplier {
|
2021-01-20 13:01:57 +00:00
|
|
|
return &EC2AmiSupplier{
|
2021-01-22 17:06:17 +00:00
|
|
|
provider,
|
2021-01-20 13:01:57 +00:00
|
|
|
awsdeserializer.NewEC2AmiDeserializer(),
|
2021-01-22 17:06:17 +00:00
|
|
|
ec2.New(provider.session),
|
|
|
|
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
|
2021-01-20 13:01:57 +00:00
|
|
|
}
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s EC2AmiSupplier) Resources() ([]resource.Resource, error) {
|
|
|
|
input := &ec2.DescribeImagesInput{
|
|
|
|
Owners: []*string{
|
|
|
|
aws.String("self"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
response, err := s.client.DescribeImages(input)
|
|
|
|
if err != nil {
|
2021-01-20 13:01:57 +00:00
|
|
|
return nil, remoteerror.NewResourceEnumerationError(err, resourceaws.AwsAmiResourceType)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
results := make([]cty.Value, 0)
|
|
|
|
if len(response.Images) > 0 {
|
|
|
|
for _, image := range response.Images {
|
|
|
|
id := aws.StringValue(image.ImageId)
|
|
|
|
s.runner.Run(func() (cty.Value, error) {
|
|
|
|
return s.readAMI(id)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
results, err = s.runner.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s.deserializer.Deserialize(results)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s EC2AmiSupplier) readAMI(id string) (cty.Value, error) {
|
|
|
|
resImage, err := s.reader.ReadResource(terraform.ReadResourceArgs{
|
|
|
|
Ty: resourceaws.AwsAmiResourceType,
|
|
|
|
ID: id,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("Error reading image %s[%s]: %+v", id, resourceaws.AwsAmiResourceType, err)
|
|
|
|
return cty.NilVal, err
|
|
|
|
}
|
|
|
|
return *resImage, nil
|
|
|
|
}
|