Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource azurerm_dns_zone: Add option to re-use host_name with dns_zone with soa_record creation #22312

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions internal/services/dns/dns_zone_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
Expand All @@ -13,6 +14,7 @@ import (
"github.com/hashicorp/go-azure-sdk/resource-manager/dns/2018-05-01/zones"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/features"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/dns/migration"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/dns/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
Expand Down Expand Up @@ -76,7 +78,7 @@ func resourceDnsZone() *pluginsdk.Resource {
MaxItems: 1,
Optional: true,
Computed: true,
ForceNew: true,
//ForceNew: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"email": {
Expand All @@ -86,9 +88,9 @@ func resourceDnsZone() *pluginsdk.Resource {
},

"host_name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
Type: pluginsdk.TypeString,
Optional: !features.FourPointOhBeta(), // (@jackofallops) - This should not be set or updatable to meet API design, see https://learn.microsoft.com/en-us/azure/dns/dns-zones-records#soa-records
Computed: true,
},

"expire_time": {
Expand Down Expand Up @@ -182,11 +184,27 @@ func resourceDnsZoneCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) er

if v, ok := d.GetOk("soa_record"); ok {
soaRecord := v.([]interface{})[0].(map[string]interface{})

soaRecordID := recordsets.NewRecordTypeID(id.SubscriptionId, id.ResourceGroupName, id.DnsZoneName, recordsets.RecordTypeSOA, "@")
soaRecordResp, err := recordSetsClient.Get(ctx, soaRecordID)
if err != nil {
return fmt.Errorf("retrieving %s to update SOA: %+v", id, err)
}

props := soaRecordResp.Model.Properties
if props == nil || props.SOARecord == nil {
return fmt.Errorf("could not read SOA properties for %s", id)
}

inputSOARecord := expandArmDNSZoneSOARecord(soaRecord)

inputSOARecord.Host = props.SOARecord.Host

rsParameters := recordsets.RecordSet{
Properties: &recordsets.RecordSetProperties{
TTL: utils.Int64(int64(soaRecord["ttl"].(int))),
Metadata: tags.Expand(soaRecord["tags"].(map[string]interface{})),
SOARecord: expandArmDNSZoneSOARecord(soaRecord),
SOARecord: inputSOARecord,
},
}

Expand Down Expand Up @@ -278,15 +296,20 @@ func resourceDnsZoneDelete(d *pluginsdk.ResourceData, meta interface{}) error {
}

func expandArmDNSZoneSOARecord(input map[string]interface{}) *recordsets.SoaRecord {
return &recordsets.SoaRecord{
result := &recordsets.SoaRecord{
Email: utils.String(input["email"].(string)),
Host: utils.String(input["host_name"].(string)),
ExpireTime: utils.Int64(int64(input["expire_time"].(int))),
MinimumTTL: utils.Int64(int64(input["minimum_ttl"].(int))),
RefreshTime: utils.Int64(int64(input["refresh_time"].(int))),
RetryTime: utils.Int64(int64(input["retry_time"].(int))),
SerialNumber: utils.Int64(int64(input["serial_number"].(int))),
}

if !features.FourPointOhBeta() && input["host_name"].(string) != "" {
result.Host = pointer.To(input["host_name"].(string))
}

return result
}

func flattenArmDNSZoneSOARecord(input *recordsets.RecordSet) []interface{} {
Expand Down
43 changes: 39 additions & 4 deletions internal/services/dns/dns_zone_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestAccDnsZone_withTags(t *testing.T) {
})
}

func TestAccDnsZone_withSOARecord(t *testing.T) {
func TestAccDnsZone_basicSOARecord(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_dns_zone", "test")
r := DnsZoneResource{}

Expand All @@ -83,6 +83,43 @@ func TestAccDnsZone_withSOARecord(t *testing.T) {
),
},
data.ImportStep(),
})
}

func TestAccDnsZone_completeSOARecord(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_dns_zone", "test")
r := DnsZoneResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.withCompletedSOARecord(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccDnsZone_updateSOARecord(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_dns_zone", "test")
r := DnsZoneResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.withBasicSOARecord(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.withCompletedSOARecord(data),
Check: acceptance.ComposeTestCheckFunc(
Expand Down Expand Up @@ -205,8 +242,7 @@ resource "azurerm_dns_zone" "test" {
resource_group_name = azurerm_resource_group.test.name

soa_record {
email = "testemail.com"
host_name = "testhost.contoso.com"
email = "testemail.com"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
Expand All @@ -229,7 +265,6 @@ resource "azurerm_dns_zone" "test" {

soa_record {
email = "testemail.com"
host_name = "testhost.contoso.com"
expire_time = 2419200
minimum_ttl = 200
refresh_time = 2600
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/dns_zone.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ The `soa_record` block supports:

* `email` - (Required) The email contact for the SOA record.

* `host_name` - (Required) The domain name of the authoritative name server for the SOA record.
* `host_name` - (Optional) The domain name of the authoritative name server for the SOA record. If not set, computed value from Azure will be used.

* `expire_time` - (Optional) The expire time for the SOA record. Defaults to `2419200`.

Expand Down