diff --git a/commands/getZones.go b/commands/getZones.go index b1102672b..00b2b8b4a 100644 --- a/commands/getZones.go +++ b/commands/getZones.go @@ -235,7 +235,7 @@ func GetZone(args GetZoneArgs) error { } default: - return fmt.Errorf("format %q unknown", args.OutputFile) + return fmt.Errorf("format %q unknown", args.OutputFormat) } } return nil @@ -245,9 +245,11 @@ func formatDsl(zonename string, rec *models.RecordConfig, defaultTTL uint32) str target := rec.GetTargetCombined() + ttl := uint32(0) ttlop := "" if rec.TTL != defaultTTL && rec.TTL != 0 { - ttlop = fmt.Sprintf(", TTL(%d)", rec.TTL) + ttl = rec.TTL + ttlop = fmt.Sprintf(", TTL(%d)", ttl) } switch rec.Type { // #rtype_variations @@ -277,6 +279,8 @@ func formatDsl(zonename string, rec *models.RecordConfig, defaultTTL uint32) str return fmt.Sprintf("NAMESERVER('%s')", target) } target = "'" + target + "'" + case "R53_ALIAS": + return makeR53alias(rec, ttl) default: target = "'" + target + "'" } @@ -295,3 +299,18 @@ func makeCaa(rec *models.RecordConfig, ttlop string) string { // TODO(tlim): Generate a CAA_BUILDER() instead? } + +func makeR53alias(rec *models.RecordConfig, ttl uint32) string { + items := []string{ + "'" + rec.Name + "'", + "'" + rec.R53Alias["type"] + "'", + "'" + rec.GetTargetField() + "'", + } + if z, ok := rec.R53Alias["zone_id"]; ok { + items = append(items, "R53_ZONE('"+z+"')") + } + if ttl != 0 { + items = append(items, fmt.Sprintf("TTL(%d)", ttl)) + } + return rec.Type + "(" + strings.Join(items, ", ") + ")" +} diff --git a/commands/r53_test.go b/commands/r53_test.go new file mode 100644 index 000000000..136f3a84a --- /dev/null +++ b/commands/r53_test.go @@ -0,0 +1,70 @@ +package commands + +import ( + "testing" + + "github.com/StackExchange/dnscontrol/v3/models" + _ "github.com/StackExchange/dnscontrol/v3/providers/_all" +) + +func TestR53Test_1(t *testing.T) { + rec := models.RecordConfig{ + Type: "R53_ALIAS", + Name: "foo", + NameFQDN: "foo.domain.tld", + Target: "bar", + } + rec.R53Alias = make(map[string]string) + rec.R53Alias["type"] = "A" + w := `R53_ALIAS('foo', 'A', 'bar')` + if g := makeR53alias(&rec, 0); g != w { + t.Errorf("makeR53alias failure: got `%s` want `%s`", g, w) + } +} + +func TestR53Test_1ttl(t *testing.T) { + rec := models.RecordConfig{ + Type: "R53_ALIAS", + Name: "foo", + NameFQDN: "foo.domain.tld", + Target: "bar", + } + rec.R53Alias = make(map[string]string) + rec.R53Alias["type"] = "A" + w := `R53_ALIAS('foo', 'A', 'bar', TTL(321))` + if g := makeR53alias(&rec, 321); g != w { + t.Errorf("makeR53alias failure: got `%s` want `%s`", g, w) + } +} + +func TestR53Test_2(t *testing.T) { + rec := models.RecordConfig{ + Type: "R53_ALIAS", + Name: "foo", + NameFQDN: "foo.domain.tld", + Target: "bar", + } + rec.R53Alias = make(map[string]string) + rec.R53Alias["type"] = "A" + rec.R53Alias["zone_id"] = "blarg" + w := `R53_ALIAS('foo', 'A', 'bar', R53_ZONE('blarg'))` + if g := makeR53alias(&rec, 0); g != w { + t.Errorf("makeR53alias failure: got `%s` want `%s`", g, w) + } +} + +func TestR53Test_2ttl(t *testing.T) { + rec := models.RecordConfig{ + Type: "R53_ALIAS", + Name: "foo", + NameFQDN: "foo.domain.tld", + Target: "bar", + } + rec.R53Alias = make(map[string]string) + rec.R53Alias["type"] = "A" + rec.R53Alias["zone_id"] = "blarg" + w := `R53_ALIAS('foo', 'A', 'bar', R53_ZONE('blarg'), TTL(123))` + if g := makeR53alias(&rec, 123); g != w { + t.Errorf("makeR53alias failure: got `%s` want `%s`", g, w) + } +} diff --git a/docs/_functions/domain/R53_ALIAS.md b/docs/_functions/domain/R53_ALIAS.md index 938f8336a..5c12b417d 100644 --- a/docs/_functions/domain/R53_ALIAS.md +++ b/docs/_functions/domain/R53_ALIAS.md @@ -35,13 +35,13 @@ The zone id can be found depending on the target type: {% include startExample.html %} {% highlight js %} -D("example.com", REGISTRAR, DnsProvider("ROUTE53"), - R53_ALIAS("foo", "A", "bar"), // record in same zone - R53_ALIAS("foo", "A", "bar", R53_ZONE('Z35SXDOTRQ7X7K')), // record in same zone, zone specified - R53_ALIAS("foo", "A", "blahblah.elasticloadbalancing.us-west-1.amazonaws.com", R53_ZONE('Z368ELLRRE2KJ0')), // a classic ELB in us-west-1 - R53_ALIAS("foo", "A", "blahblah.elasticbeanstalk.us-west-2.amazonaws.com", R53_ZONE('Z38NKT9BP95V3O')), // an Elastic Beanstalk environment in us-west-2 - R53_ALIAS("foo", "A", "blahblah-bucket.s3-website-us-west-1.amazonaws.com", R53_ZONE('Z2F56UZL2M1ACD')), // a website S3 Bucket in us-west-1 +D('example.com', REGISTRAR, DnsProvider('ROUTE53'), + R53_ALIAS('foo', 'A', 'bar'), // record in same zone + R53_ALIAS('foo', 'A', 'bar', R53_ZONE('Z35SXDOTRQ7X7K')), // record in same zone, zone specified + R53_ALIAS('foo', 'A', 'blahblah.elasticloadbalancing.us-west-1.amazonaws.com', R53_ZONE('Z368ELLRRE2KJ0')), // a classic ELB in us-west-1 + R53_ALIAS('foo', 'A', 'blahblah.elasticbeanstalk.us-west-2.amazonaws.com', R53_ZONE('Z38NKT9BP95V3O')), // an Elastic Beanstalk environment in us-west-2 + R53_ALIAS('foo', 'A', 'blahblah-bucket.s3-website-us-west-1.amazonaws.com', R53_ZONE('Z2F56UZL2M1ACD')), // a website S3 Bucket in us-west-1 ); {%endhighlight%} -{% include endExample.html %} \ No newline at end of file +{% include endExample.html %}