driftctl/pkg/remote/aws/ec2_nat_gateway_enumerator.go

55 lines
1.4 KiB
Go
Raw Normal View History

2021-07-06 14:24:54 +00:00
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"
"github.com/cloudskiff/driftctl/pkg/resource/aws"
)
type EC2NatGatewayEnumerator struct {
repository repository.EC2Repository
factory resource.ResourceFactory
}
func NewEC2NatGatewayEnumerator(repo repository.EC2Repository, factory resource.ResourceFactory) *EC2NatGatewayEnumerator {
return &EC2NatGatewayEnumerator{
repository: repo,
factory: factory,
}
}
func (e *EC2NatGatewayEnumerator) SupportedType() resource.ResourceType {
return aws.AwsNatGatewayResourceType
}
func (e *EC2NatGatewayEnumerator) Enumerate() ([]resource.Resource, error) {
natGateways, err := e.repository.ListAllNatGateways()
if err != nil {
2021-08-03 10:34:36 +00:00
return nil, remoteerror.NewResourceListingError(err, string(e.SupportedType()))
2021-07-06 14:24:54 +00:00
}
results := make([]resource.Resource, len(natGateways))
for _, natGateway := range natGateways {
attrs := map[string]interface{}{}
if len(natGateway.NatGatewayAddresses) > 0 {
if allocId := natGateway.NatGatewayAddresses[0].AllocationId; allocId != nil {
attrs["allocation_id"] = *allocId
}
}
2021-07-06 14:24:54 +00:00
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
*natGateway.NatGatewayId,
attrs,
2021-07-06 14:24:54 +00:00
),
)
}
return results, err
}