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

r/dns_(a|aaaa|cname)_record: support for configuring target_resource_id #5218

Merged
merged 5 commits into from
Dec 19, 2019

Conversation

tombuildsstuff
Copy link
Contributor

@tombuildsstuff tombuildsstuff commented Dec 19, 2019

This PR supersedes (and includes the work from) #5133 - which adds support for target_resource_id to the azurerm_dns_a_record, azurerm_dns_aaaa_record and azurerm_dns_cname_record resources. Since #5133 is from @matt-FFFFFF's master branch, unfortunately we're unable to push to that - hence opening this PR including those changes instead

Fixes #3624
Fixes #4623

Original PR description:


  • Update DNS SDK to 2018-05-01
  • A, AAAA & CNAME record support for aliases
  • New acceptance tests to cover aliases
  • Update docs

matt-FFFFFF and others added 2 commits December 19, 2019 10:41
* Update DNS SDK to 2018-05-01
* A and AAAA record support for aliases

* Add missing }

* Update modules.txt and fix imports for dns tests

* Fic incorrect function name

* AAAA testacc pass! + improve resource read code:

* A
* CNAME
* AAAA

* Add CNAME tests

* Change CNAME tests to Zone Record Set

* Update docs
@matt-FFFFFF
Copy link
Contributor

No problem @tombuildsstuff :)

I was having trouble ensuring that the target_resource_id attribute was correctly set to null in the state when converting a record from an alias to being one for []records.

These were the tests that I wrote to ensure this was happening:

func TestAccAzureRMDnsARecord_RecordsToAlias(t *testing.T) {
	resourceName := "azurerm_dns_a_record.test"
	targetResourceName := "azurerm_public_ip.test"
	ri := tf.AccRandTimeInt()
	location := testLocation()
	preConfig := testAccAzureRMDnsARecord_RecordsToAlias(ri, location)
	postConfig := testAccAzureRMDnsARecord_RecordsToAliasUpdate(ri, location)

	resource.ParallelTest(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testCheckAzureRMDnsARecordDestroy,
		Steps: []resource.TestStep{
			{
				Config: preConfig,
				Check: resource.ComposeTestCheckFunc(
					testCheckAzureRMDnsARecordExists(resourceName),
					resource.TestCheckResourceAttr(resourceName, "records.#", "2"),
				),
			},
			{
				Config: postConfig,
				Check: resource.ComposeTestCheckFunc(
					testCheckAzureRMDnsARecordExists(resourceName),
					resource.TestCheckResourceAttrPair(resourceName, "target_resource_id", targetResourceName, "id"),
					resource.TestCheckNoResourceAttr(resourceName, "records"),
				),
			},
			{
				ResourceName:      resourceName,
				ImportState:       true,
				ImportStateVerify: true,
			},
		},
	})
}

func TestAccAzureRMDnsARecord_AliasToRecords(t *testing.T) {
	resourceName := "azurerm_dns_a_record.test"
	targetResourceName := "azurerm_public_ip.test"
	ri := tf.AccRandTimeInt()
	location := testLocation()
	preConfig := testAccAzureRMDnsARecord_AliasToRecords(ri, location)
	postConfig := testAccAzureRMDnsARecord_AliasToRecordsUpdate(ri, location)

	resource.ParallelTest(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testCheckAzureRMDnsARecordDestroy,
		Steps: []resource.TestStep{
			{
				Config: preConfig,
				Check: resource.ComposeTestCheckFunc(
					testCheckAzureRMDnsARecordExists(resourceName),
					resource.TestCheckResourceAttrPair(resourceName, "target_resource_id", targetResourceName, "id"),
				),
			},
			{
				Config: postConfig,
				Check: resource.ComposeTestCheckFunc(
					testCheckAzureRMDnsARecordExists(resourceName),
					resource.TestCheckResourceAttr(resourceName, "records.#", "2"),
					resource.TestCheckNoResourceAttr(resourceName, "target_resource_id"),
				),
			},
			{
				ResourceName:      resourceName,
				ImportState:       true,
				ImportStateVerify: true,
			},
		},
	})
}

func testAccAzureRMDnsARecord_AliasToRecords(rInt int, location string) string {
	return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
  name     = "acctestRG-%d"
  location = "%s"
}

resource "azurerm_dns_zone" "test" {
  name                = "acctestzone%d.com"
  resource_group_name = "${azurerm_resource_group.test.name}"
}

resource "azurerm_public_ip" "test" {
	name                = "mypublicip%d"
	location            = "${azurerm_resource_group.test.location}"
	resource_group_name = "${azurerm_resource_group.test.name}"
	allocation_method   = "Dynamic"
	ip_version          = "IPv4"
}

resource "azurerm_dns_a_record" "test" {
	name                = "myarecord%d"
	resource_group_name = "${azurerm_resource_group.test.name}"
	zone_name           = "${azurerm_dns_zone.test.name}"
	ttl                 = 300
	target_resource_id  = "${azurerm_public_ip.test.id}"
}
`, rInt, location, rInt, rInt, rInt)
}

func testAccAzureRMDnsARecord_AliasToRecordsUpdate(rInt int, location string) string {
	return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
  name     = "acctestRG-%d"
  location = "%s"
}

resource "azurerm_dns_zone" "test" {
  name                = "acctestzone%d.com"
  resource_group_name = "${azurerm_resource_group.test.name}"
}

resource "azurerm_dns_a_record" "test" {
  name                = "myarecord%d"
  resource_group_name = "${azurerm_resource_group.test.name}"
  zone_name           = "${azurerm_dns_zone.test.name}"
  ttl                 = 300
  records             = ["1.2.3.4", "1.2.4.5"]
}
`, rInt, location, rInt, rInt)
}

@tombuildsstuff
Copy link
Contributor Author

Tests pass:

Screenshot 2019-12-19 at 12 27 46

@tombuildsstuff
Copy link
Contributor Author

@matt-FFFFFF will look to add that prior to review/merge - but that should be fine now given we're nil-ing them out 👍

Copy link
Member

@mbfrahry mbfrahry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@katbyte katbyte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@tombuildsstuff tombuildsstuff merged commit 1db2683 into master Dec 19, 2019
@tombuildsstuff tombuildsstuff deleted the f/dns-alias branch December 19, 2019 21:01
tombuildsstuff added a commit that referenced this pull request Dec 19, 2019
@ghost
Copy link

ghost commented Jan 8, 2020

This has been released in version 1.40.0 of the provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading. As an example:

provider "azurerm" {
    version = "~> 1.40.0"
}
# ... other configuration ...

@ghost
Copy link

ghost commented Mar 28, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 [email protected]. Thanks!

@ghost ghost locked and limited conversation to collaborators Mar 28, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
4 participants