Skip to content

Commit

Permalink
Merge pull request #57 from quite4work/add-priority-field
Browse files Browse the repository at this point in the history
Add Priority field for MX record
  • Loading branch information
Tristan971 authored Dec 15, 2022
2 parents 702e833 + 56f5d35 commit 0d8d0b4
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 9 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/hashicorp/terraform-plugin-docs v0.13.0
github.com/hashicorp/terraform-plugin-log v0.7.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.1
github.com/matschundbrei/cloudns-go v0.0.2-alpha
github.com/matschundbrei/cloudns-go v0.0.2-alpha.0.20221214225745-cfd16954b6c8
go.uber.org/ratelimit v0.2.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/matryer/is v1.2.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA=
github.com/matschundbrei/cloudns-go v0.0.2-alpha h1:K1Izv9Iu1DB22PO+FnA5IANCKu8rH+FHCxO3VGMtx1k=
github.com/matschundbrei/cloudns-go v0.0.2-alpha/go.mod h1:PuG0q4AYafYOah/NwxT3p0ZMJi0X8RBGMKrN4ypUHl8=
github.com/matschundbrei/cloudns-go v0.0.2-alpha.0.20221214225745-cfd16954b6c8 h1:Boe9R16+C7ydAryrVIIq+jYhl5Col8NLRIF2fR+Fq7o=
github.com/matschundbrei/cloudns-go v0.0.2-alpha.0.20221214225745-cfd16954b6c8/go.mod h1:PuG0q4AYafYOah/NwxT3p0ZMJi0X8RBGMKrN4ypUHl8=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
Expand Down
42 changes: 36 additions & 6 deletions internal/cloudns/resource_dns_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/matschundbrei/cloudns-go"
)

Expand All @@ -19,6 +20,7 @@ func resourceDnsRecord() *schema.Resource {
ReadContext: resourceDnsRecordRead,
UpdateContext: resourceDnsRecordUpdate,
DeleteContext: resourceDnsRecordDelete,
CustomizeDiff: resourceDnsRecordValidate,

// Naming **does not** follow the scheme used by ClouDNS, due to how comically misleading and unclear it is
// see: https://www.cloudns.net/wiki/article/58/ for the relevant "vanilla" schema on ClouDNS side
Expand Down Expand Up @@ -53,6 +55,13 @@ func resourceDnsRecord() *schema.Resource {
Required: true,
ForceNew: false,
},
"priority": {
Description: "Priority for MX record (eg: `something.cloudns.net 600 in MX [10] mail.example.com`)",
Type: schema.TypeInt,
Optional: true,
ForceNew: false,
ValidateDiagFunc: validation.ToDiagFunc(validation.IntBetween(0, 65535)),
},
},
}
}
Expand Down Expand Up @@ -139,6 +148,15 @@ func resourceDnsRecordDelete(ctx context.Context, d *schema.ResourceData, meta i
return resourceDnsRecordRead(ctx, d, meta)
}

func resourceDnsRecordValidate(ctx context.Context, d *schema.ResourceDiff, meta interface{}) error {
rtype := d.Get("type").(string)
_, isPriorityProvided := d.GetOkExists("priority")
if rtype == "MX" && !isPriorityProvided {
return fmt.Errorf("Priority is required for MX record")
}
return nil
}

func updateState(d *schema.ResourceData, zoneRecord *cloudns.Record) error {
err := d.Set("name", zoneRecord.Host)
if err != nil {
Expand All @@ -165,6 +183,13 @@ func updateState(d *schema.ResourceData, zoneRecord *cloudns.Record) error {
return err
}

if zoneRecord.Rtype == "MX" {
err = d.Set("priority", zoneRecord.Priority)
if err != nil {
return err
}
}

return nil
}

Expand All @@ -175,13 +200,18 @@ func toApiRecord(d *schema.ResourceData) cloudns.Record {
rtype := d.Get("type").(string)
value := d.Get("value").(string)
ttl := d.Get("ttl").(int)
priority := 0
if rtype == "MX" {
priority = d.Get("priority").(int)
}

return cloudns.Record{
ID: id,
Host: name,
Domain: zone,
Rtype: rtype,
Record: value,
TTL: ttl,
ID: id,
Host: name,
Domain: zone,
Rtype: rtype,
Record: value,
TTL: ttl,
Priority: priority,
}
}
64 changes: 64 additions & 0 deletions internal/cloudns/resource_dns_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,70 @@ resource "cloudns_dns_record" "some-record" {
})
}

func TestAccDnsMXRecord(t *testing.T) {
testZone := os.Getenv(EnvVarAcceptanceTestsZone)

initialRecordValue := fmt.Sprintf("target-init.%s", testZone)
updatedRecordValue := fmt.Sprintf("target-updated.%s", testZone)

genUuid, err := uuid.NewRandom()
if err != nil {
t.Fatal(err)
}
testUuid := genUuid.String()

initialRecord := fmt.Sprintf(`
resource "cloudns_dns_record" "some-record" {
name = "%s"
zone = "%s"
type = "MX"
value = "%s"
ttl = "600"
priority = "10"
}
`, testUuid, testZone, initialRecordValue)

updatedRecord := fmt.Sprintf(`
resource "cloudns_dns_record" "some-record" {
name = "%s"
zone = "%s"
type = "MX"
value = "%s"
ttl = "600"
priority = "0"
}
`, testUuid, testZone, updatedRecordValue)
resource.UnitTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: initialRecord,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("cloudns_dns_record.some-record", "name", testUuid),
resource.TestCheckResourceAttr("cloudns_dns_record.some-record", "zone", testZone),
resource.TestCheckResourceAttr("cloudns_dns_record.some-record", "type", "MX"),
resource.TestCheckResourceAttr("cloudns_dns_record.some-record", "value", initialRecordValue),
resource.TestCheckResourceAttr("cloudns_dns_record.some-record", "ttl", "600"),
resource.TestCheckResourceAttr("cloudns_dns_record.some-record", "priority", "10"),
),
},
{
Config: updatedRecord,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("cloudns_dns_record.some-record", "name", testUuid),
resource.TestCheckResourceAttr("cloudns_dns_record.some-record", "zone", testZone),
resource.TestCheckResourceAttr("cloudns_dns_record.some-record", "type", "MX"),
resource.TestCheckResourceAttr("cloudns_dns_record.some-record", "value", updatedRecordValue),
resource.TestCheckResourceAttr("cloudns_dns_record.some-record", "ttl", "600"),
resource.TestCheckResourceAttr("cloudns_dns_record.some-record", "priority", "0"),
),
},
},
CheckDestroy: CheckDestroyedRecords(testZone),
})
}

func CheckDestroyedRecords(zone string) func(state *terraform.State) error {
return func(state *terraform.State) error {
provider := testAccProvider
Expand Down

0 comments on commit 0d8d0b4

Please sign in to comment.