2020-12-09 15:31:34 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2021-02-22 10:22:29 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
|
|
|
|
|
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/resource"
|
|
|
|
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/terraform"
|
|
|
|
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Route53RecordSupplier struct {
|
|
|
|
reader terraform.ResourceReader
|
2021-05-21 14:09:45 +00:00
|
|
|
deserializer *resource.Deserializer
|
2021-02-22 10:22:29 +00:00
|
|
|
client repository.Route53Repository
|
2020-12-09 15:31:34 +00:00
|
|
|
runner *terraform.ParallelResourceReader
|
|
|
|
}
|
|
|
|
|
2021-05-31 15:39:16 +00:00
|
|
|
func NewRoute53RecordSupplier(provider *AWSTerraformProvider, deserializer *resource.Deserializer, repo repository.Route53Repository) *Route53RecordSupplier {
|
2021-01-20 13:01:57 +00:00
|
|
|
return &Route53RecordSupplier{
|
2021-01-22 17:06:17 +00:00
|
|
|
provider,
|
2021-05-21 14:09:45 +00:00
|
|
|
deserializer,
|
2021-05-31 15:39:16 +00:00
|
|
|
repo,
|
2021-01-22 17:06:17 +00:00
|
|
|
terraform.NewParallelResourceReader(provider.Runner().SubRunner())}
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-03-17 15:54:53 +00:00
|
|
|
func (s *Route53RecordSupplier) Resources() ([]resource.Resource, error) {
|
2020-12-09 15:31:34 +00:00
|
|
|
|
|
|
|
zones, err := s.listZones()
|
|
|
|
if err != nil {
|
2021-01-20 13:01:57 +00:00
|
|
|
return nil, remoteerror.NewResourceEnumerationErrorWithType(err, resourceaws.AwsRoute53RecordResourceType, resourceaws.AwsRoute53ZoneResourceType)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, zone := range zones {
|
|
|
|
if err := s.listRecordsForZone(zone[0], zone[1]); err != nil {
|
2021-01-20 13:01:57 +00:00
|
|
|
return nil, remoteerror.NewResourceEnumerationError(err, resourceaws.AwsRoute53RecordResourceType)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
results, err := s.runner.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-21 14:09:45 +00:00
|
|
|
return s.deserializer.Deserialize(resourceaws.AwsRoute53RecordResourceType, results)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-03-17 15:54:53 +00:00
|
|
|
func (s *Route53RecordSupplier) listZones() ([][2]string, error) {
|
2020-12-09 15:31:34 +00:00
|
|
|
results := make([][2]string, 0)
|
2021-02-22 10:22:29 +00:00
|
|
|
zones, err := s.client.ListAllZones()
|
2020-12-09 15:31:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, hostedZone := range zones {
|
|
|
|
results = append(results, [2]string{strings.TrimPrefix(*hostedZone.Id, "/hostedzone/"), *hostedZone.Name})
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
2021-03-17 15:54:53 +00:00
|
|
|
func (s *Route53RecordSupplier) listRecordsForZone(zoneId string, zoneName string) error {
|
2020-12-09 15:31:34 +00:00
|
|
|
|
2021-02-22 10:22:29 +00:00
|
|
|
records, err := s.client.ListRecordsForZone(zoneId)
|
2020-12-09 15:31:34 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, raw := range records {
|
|
|
|
rawType := *raw.Type
|
|
|
|
rawName := *raw.Name
|
|
|
|
rawSetIdentifier := raw.SetIdentifier
|
|
|
|
s.runner.Run(func() (cty.Value, error) {
|
|
|
|
vars := []string{
|
|
|
|
zoneId,
|
2021-01-04 12:47:02 +00:00
|
|
|
strings.ToLower(strings.TrimSuffix(rawName, ".")),
|
2020-12-09 15:31:34 +00:00
|
|
|
rawType,
|
|
|
|
}
|
|
|
|
if rawSetIdentifier != nil {
|
|
|
|
vars = append(vars, *rawSetIdentifier)
|
|
|
|
}
|
|
|
|
|
|
|
|
record, err := s.reader.ReadResource(
|
|
|
|
terraform.ReadResourceArgs{
|
|
|
|
Ty: resourceaws.AwsRoute53RecordResourceType,
|
|
|
|
ID: strings.Join(vars, "_"),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return cty.NilVal, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return *record, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|