driftctl/pkg/remote/aws/vpc_security_group_supplier.go

101 lines
3.2 KiB
Go
Raw Normal View History

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"
2021-05-21 14:09:45 +00:00
"github.com/cloudskiff/driftctl/pkg/terraform"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/sirupsen/logrus"
"github.com/zclconf/go-cty/cty"
)
type VPCSecurityGroupSupplier struct {
2021-05-21 14:09:45 +00:00
reader terraform.ResourceReader
deserializer *resource.Deserializer
repo repository.EC2Repository
2021-05-21 14:09:45 +00:00
defaultSecurityGroupRunner *terraform.ParallelResourceReader
securityGroupRunner *terraform.ParallelResourceReader
}
func NewVPCSecurityGroupSupplier(provider *AWSTerraformProvider, deserializer *resource.Deserializer, repo repository.EC2Repository) *VPCSecurityGroupSupplier {
2021-01-15 13:15:31 +00:00
return &VPCSecurityGroupSupplier{
provider,
2021-05-21 14:09:45 +00:00
deserializer,
repo,
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
2021-01-15 13:15:31 +00:00
}
}
func (s *VPCSecurityGroupSupplier) Resources() ([]resource.Resource, error) {
securityGroups, defaultSecurityGroups, err := s.repo.ListAllSecurityGroups()
if err != nil {
return nil, remoteerror.NewResourceEnumerationError(err, resourceaws.AwsSecurityGroupResourceType)
}
2021-01-15 13:15:31 +00:00
for _, item := range securityGroups {
res := *item
s.securityGroupRunner.Run(func() (cty.Value, error) {
return s.readSecurityGroup(res)
})
}
securityGroupResources, err := s.securityGroupRunner.Wait()
if err != nil {
return nil, err
}
for _, item := range defaultSecurityGroups {
res := *item
s.defaultSecurityGroupRunner.Run(func() (cty.Value, error) {
return s.readSecurityGroup(res)
})
}
defaultSecurityGroupResources, err := s.defaultSecurityGroupRunner.Wait()
if err != nil {
return nil, err
}
2021-01-15 13:15:31 +00:00
// Deserialize
2021-05-21 14:09:45 +00:00
deserializedDefaultSecurityGroups, err := s.deserializer.Deserialize(resourceaws.AwsDefaultSecurityGroupResourceType, defaultSecurityGroupResources)
2021-01-15 13:15:31 +00:00
if err != nil {
return nil, err
}
2021-05-21 14:09:45 +00:00
deserializedSecurityGroups, err := s.deserializer.Deserialize(resourceaws.AwsSecurityGroupResourceType, securityGroupResources)
2021-01-15 13:15:31 +00:00
if err != nil {
return nil, err
}
resources := make([]resource.Resource, 0, len(securityGroupResources)+len(defaultSecurityGroupResources))
resources = append(resources, deserializedDefaultSecurityGroups...)
resources = append(resources, deserializedSecurityGroups...)
return resources, nil
}
func (s *VPCSecurityGroupSupplier) readSecurityGroup(securityGroup ec2.SecurityGroup) (cty.Value, error) {
2021-01-15 13:15:31 +00:00
var Ty resource.ResourceType = resourceaws.AwsSecurityGroupResourceType
if isDefaultSecurityGroup(securityGroup) {
2021-01-15 13:15:31 +00:00
Ty = resourceaws.AwsDefaultSecurityGroupResourceType
}
val, err := s.reader.ReadResource(terraform.ReadResourceArgs{
ID: aws.StringValue(securityGroup.GroupId),
Ty: Ty,
})
if err != nil {
2021-01-15 13:15:31 +00:00
logrus.Error(err)
return cty.NilVal, err
}
2021-01-15 13:15:31 +00:00
return *val, nil
}
// Return true if the security group is considered as a default one
func isDefaultSecurityGroup(securityGroup ec2.SecurityGroup) bool {
return securityGroup.GroupName != nil && *securityGroup.GroupName == "default"
}