Skip to content

Commit

Permalink
[TF] Improve resource "cloudconnexa_dns_record"
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfmnk committed Aug 7, 2024
1 parent 30ce046 commit e57f6ca
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 7 deletions.
13 changes: 9 additions & 4 deletions cloudconnexa/resource_dns_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,22 @@ func resourceDnsRecord() *schema.Resource {
Description: "The description for the UI. Defaults to `Managed by Terraform`.",
},
"ip_v4_addresses": {
Type: schema.TypeList,
Optional: true,
Type: schema.TypeList,
Optional: true,
AtLeastOneOf: []string{"ip_v4_addresses", "ip_v6_addresses"},
MinItems: 1,
Elem: &schema.Schema{

Type: schema.TypeString,
ValidateFunc: validation.IsIPv4Address,
},
Description: "The list of IPV4 addresses to which this record will resolve.",
},
"ip_v6_addresses": {
Type: schema.TypeList,
Optional: true,
Type: schema.TypeList,
Optional: true,
AtLeastOneOf: []string{"ip_v4_addresses", "ip_v6_addresses"},
MinItems: 1,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.IsIPv6Address,
Expand Down
65 changes: 62 additions & 3 deletions cloudconnexa/resource_dns_record_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package cloudconnexa

import (
"errors"
"fmt"
"github.com/openvpn/cloudconnexa-go-client/v2/cloudconnexa"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand All @@ -18,7 +20,7 @@ func TestAccCloudConnexaDnsRecord_basic(t *testing.T) {
CheckDestroy: testAccCheckCloudConnexaDnsRecordDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudConnexaDnsRecordConfig(domainName),
Config: testAccCloudConnexaDnsRecordConfigWithoutIPs(domainName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "domain", domainName),
resource.TestCheckResourceAttr(resourceName, "description", "test description"),
Expand All @@ -32,6 +34,36 @@ func TestAccCloudConnexaDnsRecord_basic(t *testing.T) {
})
}

func TestAccCloudConnexaDnsRecord_noIPs(t *testing.T) {
expectedErr, _ := regexp.Compile("one of `ip_v4_addresses,ip_v6_addresses` must be specified")
domainName := "test.cloudconnexa.com"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccCloudConnexaDnsRecordConfigWithoutIPs(domainName),
ExpectError: expectedErr,
},
},
})
}

func TestAccCloudConnexaDnsRecord_IPsArrayIsEmpty(t *testing.T) {
expectedErr, _ := regexp.Compile("Attribute ip_v4_addresses requires 1 item minimum, but config has only 0")
domainName := "test.cloudconnexa.com"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccCloudConnexaDnsRecordConfigIPv4Empty(domainName),
ExpectError: expectedErr,
},
},
})
}

func testAccCheckCloudConnexaDnsRecordDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*cloudconnexa.Client)

Expand All @@ -43,8 +75,8 @@ func testAccCheckCloudConnexaDnsRecordDestroy(s *terraform.State) error {
recordId := rs.Primary.ID
r, err := client.DnsRecords.GetDnsRecord(recordId)

if err != nil {
return err
if !errors.Is(err, cloudconnexa.ErrDnsRecordNotFound) {

Check failure on line 78 in cloudconnexa/resource_dns_record_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: cloudconnexa.ErrDnsRecordNotFound (typecheck)

Check failure on line 78 in cloudconnexa/resource_dns_record_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: cloudconnexa.ErrDnsRecordNotFound (typecheck)

Check failure on line 78 in cloudconnexa/resource_dns_record_test.go

View workflow job for this annotation

GitHub Actions / Matrix Test (1.2.*)

undefined: cloudconnexa.ErrDnsRecordNotFound

Check failure on line 78 in cloudconnexa/resource_dns_record_test.go

View workflow job for this annotation

GitHub Actions / Matrix Test (1.3.*)

undefined: cloudconnexa.ErrDnsRecordNotFound
return nil
}

if r != nil {
Expand All @@ -69,3 +101,30 @@ resource "cloudconnexa_dns_record" "test" {
}
`, testCloudID, domainName)
}

func testAccCloudConnexaDnsRecordConfigWithoutIPs(domainName string) string {
return fmt.Sprintf(`
provider "cloudconnexa" {
base_url = "https://%[1]s.api.openvpn.com"
}
resource "cloudconnexa_dns_record" "test" {
domain = "%[2]s"
description = "test description"
}
`, testCloudID, domainName)
}

func testAccCloudConnexaDnsRecordConfigIPv4Empty(domainName string) string {
return fmt.Sprintf(`
provider "cloudconnexa" {
base_url = "https://%[1]s.api.openvpn.com"
}
resource "cloudconnexa_dns_record" "test" {
domain = "%[2]s"
description = "test description"
ip_v4_addresses = []
}
`, testCloudID, domainName)
}

0 comments on commit e57f6ca

Please sign in to comment.