From 0f5d3110a19f8c5647709743f17e5954a3c05d03 Mon Sep 17 00:00:00 2001 From: Chaithra001 Date: Tue, 15 Oct 2024 13:06:58 +0530 Subject: [PATCH 1/2] test cases for a record and aaaa record --- infoblox/resource_infoblox_a_record.go | 2 +- infoblox/resource_infoblox_a_record_test.go | 80 +++++++++++++++++---- 2 files changed, 67 insertions(+), 15 deletions(-) diff --git a/infoblox/resource_infoblox_a_record.go b/infoblox/resource_infoblox_a_record.go index abf736149..6c377bd1d 100644 --- a/infoblox/resource_infoblox_a_record.go +++ b/infoblox/resource_infoblox_a_record.go @@ -158,7 +158,7 @@ func resourceARecordCreate(d *schema.ResourceData, m interface{}) error { if err != nil { return fmt.Errorf("error unmarshalling extra attributes of network container: %s", err) } - rec, err := objMgr.AllocateNextAvailableIp(fqdn, "record:a", eaMap, nil, false, false, extAttrs, comment, false, nil) + rec, err := objMgr.AllocateNextAvailableIp(fqdn, "record:a", eaMap, nil, false, extAttrs, comment, false, nil, "IPV4") if err != nil { return fmt.Errorf("error allocating next available IP: %w", err) } diff --git a/infoblox/resource_infoblox_a_record_test.go b/infoblox/resource_infoblox_a_record_test.go index e87b19f70..f0ff5b4eb 100644 --- a/infoblox/resource_infoblox_a_record_test.go +++ b/infoblox/resource_infoblox_a_record_test.go @@ -36,7 +36,8 @@ func testAccARecordCompare( resPath string, expectedRec *ibclient.RecordA, notExpectedIpAddr string, - expectedCidr string) resource.TestCheckFunc { + expectedCidr string, + expectedFilterParams string) resource.TestCheckFunc { return func(s *terraform.State) error { res, found := s.RootModule().Resources[resPath] @@ -148,13 +149,26 @@ func testAccARecordCompare( return fmt.Errorf("'comment' is expected to be defined but it is not") } + actualFilterParams, exists := res.Primary.Attributes["filter_params"] + if expectedFilterParams != "" { + if !exists { + return fmt.Errorf("'filter_params' is expected to be defined but it is not") + } + if actualFilterParams != expectedFilterParams { + return fmt.Errorf( + "'filter_params' does not match: got '%s', expected '%s'", + actualFilterParams, expectedFilterParams) + } + } else if exists { + return fmt.Errorf("'filter_params' is expected to be undefined but it is not") + } return validateEAs(rec.Ea, expectedRec.Ea) } } var ( - regexpRequiredMissingIPv4 = regexp.MustCompile("either of 'ip_addr' and 'cidr' values is required") - regexpCidrIpAddrConflictIPv4 = regexp.MustCompile("only one of 'ip_addr' and 'cidr' values is allowed to be defined") + regexpRequiredMissingIPv4 = regexp.MustCompile("either of 'ip_addr' or 'cidr' or 'filter_values' values is required") + regexpCidrIpAddrConflictIPv4 = regexp.MustCompile("only one of 'ip_addr' or 'cidr' or 'filter_values' values is allowed to be defined") regexpUpdateConflictIPv4 = regexp.MustCompile("only one of 'ip_addr' and 'cidr' values is allowed to update") regexpNetviewUpdateNotAllowed = regexp.MustCompile("changing the value of 'network_view' field is not allowed") @@ -225,7 +239,7 @@ func TestAccResourceARecord(t *testing.T) { UseTtl: utils.BoolPtr(false), Comment: nil, Ea: nil, - }, "", ""), + }, "", "", ""), ), }, { @@ -262,7 +276,7 @@ func TestAccResourceARecord(t *testing.T) { "Location": "New York", "Site": "HQ", }, - }, "", ""), + }, "", "", ""), ), }, { @@ -289,7 +303,7 @@ func TestAccResourceARecord(t *testing.T) { Ttl: utils.Uint32Ptr(155), UseTtl: utils.BoolPtr(true), Comment: utils.StringPtr("test comment 2"), - }, "", ""), + }, "", "", ""), ), }, { @@ -312,7 +326,7 @@ func TestAccResourceARecord(t *testing.T) { Name: utils.StringPtr("name3.test.com"), View: "nondefault_view", UseTtl: utils.BoolPtr(false), - }, "", ""), + }, "", "", ""), ), }, { @@ -339,7 +353,7 @@ func TestAccResourceARecord(t *testing.T) { Name: utils.StringPtr("name3.test.com"), View: "nondefault_view", UseTtl: utils.BoolPtr(false), - }, "10.10.0.1", "10.20.30.0/24"), + }, "10.10.0.1", "10.20.30.0/24", ""), ), }, { @@ -388,7 +402,7 @@ func TestAccResourceARecord(t *testing.T) { Name: utils.StringPtr("name3.test.com"), View: "nondefault_view", UseTtl: utils.BoolPtr(false), - }, "", "10.20.33.0/24"), + }, "", "10.20.33.0/24", ""), ), }, { @@ -433,7 +447,7 @@ func TestAccResourceARecord(t *testing.T) { Name: utils.StringPtr("name3.test.com"), View: "nondefault_view", UseTtl: utils.BoolPtr(false), - }, "", ""), + }, "", "", ""), ), }, { @@ -449,6 +463,44 @@ func TestAccResourceARecord(t *testing.T) { }`), ExpectError: regexpDnsviewUpdateNotAllowed, }, + { + Config: fmt.Sprintf(` + resource "infoblox_zone_auth" "zone1" { + fqdn = "test1.com" + } + resource "infoblox_ipv4_network" "net2" { + cidr = "10.1.0.0/24" + ext_attrs = jsonencode({ + "Site" = "Blr" + }) + } + resource "infoblox_a_record" "rec4" { + fqdn = "dynamic.test1.com" + filter_params = jsonencode({ + "*Site" = "Blr"}) + dns_view = "default" + }`), + Check: resource.ComposeTestCheckFunc( + testAccARecordCompare(t, "infoblox_a_record.rec4", &ibclient.RecordA{ + Name: utils.StringPtr("dynamic.test1.com"), + View: "default", + UseTtl: utils.BoolPtr(false), + }, "", "", `{"*Site":"Blr"}`), + ), + }, + { + Config: fmt.Sprintf(` + resource "infoblox_zone_auth" "zone1" { + fqdn = "test1.com" + } + resource "infoblox_a_record" "rec1" { + fqdn = "missing_fields.test1.com" + comment = "missing required fields" + dns_view = "default" + depends_on = [infoblox_zone_auth.zone1] + }`), + ExpectError: regexpRequiredMissingIPv4, + }, }, }) } @@ -484,7 +536,7 @@ func TestAcc_resourceARecord_ea_inheritance(t *testing.T) { Ea: ibclient.EA{ "Location": "test location A", }, - }, "", ""), + }, "", "", ""), }, // When extensible attributes are added by another tool, // terraform shouldn't remove those EAs @@ -547,7 +599,7 @@ func TestAcc_resourceARecord_ea_inheritance(t *testing.T) { "Location": "test location A", "Site": "Test site", }, - }, "", ""), + }, "", "", ""), ), }, // Validate that inherited EA won't be removed if some field is updated in the resource @@ -576,7 +628,7 @@ func TestAcc_resourceARecord_ea_inheritance(t *testing.T) { "Location": "test location A", "Site": "Test site", }, - }, "", ""), + }, "", "", ""), }, // Validate that inherited EA can be updated { @@ -605,7 +657,7 @@ func TestAcc_resourceARecord_ea_inheritance(t *testing.T) { "Location": "test location A", "Site": "new extensible site", }, - }, "", ""), + }, "", "", ""), }, // Validate that inherited EA can be removed, if updated { From 17c34e255ca64901d7dd9b7880941e3f9ec67c69 Mon Sep 17 00:00:00 2001 From: Chaithra001 Date: Tue, 15 Oct 2024 14:23:58 +0530 Subject: [PATCH 2/2] test cases for aaaa record - next_available_ip --- .../resource_infoblox_aaaa_record_test.go | 81 +++++++++++++++---- 1 file changed, 67 insertions(+), 14 deletions(-) diff --git a/infoblox/resource_infoblox_aaaa_record_test.go b/infoblox/resource_infoblox_aaaa_record_test.go index d8d084047..5b41daecf 100644 --- a/infoblox/resource_infoblox_aaaa_record_test.go +++ b/infoblox/resource_infoblox_aaaa_record_test.go @@ -36,7 +36,8 @@ func testAccAAAARecordCompare( resPath string, expectedRec *ibclient.RecordAAAA, notExpectedIpAddr string, - expectedCidr string) resource.TestCheckFunc { + expectedCidr string, + expectedFilterParams string) resource.TestCheckFunc { return func(s *terraform.State) error { res, found := s.RootModule().Resources[resPath] @@ -147,13 +148,27 @@ func testAccAAAARecordCompare( return fmt.Errorf("'comment' is expected to be defined but it is not") } + actualFilterParams, exists := res.Primary.Attributes["filter_params"] + if expectedFilterParams != "" { + if !exists { + return fmt.Errorf("'filter_params' is expected to be defined but it is not") + } + if actualFilterParams != expectedFilterParams { + return fmt.Errorf( + "'filter_params' does not match: got '%s', expected '%s'", + actualFilterParams, expectedFilterParams) + } + } else if exists { + return fmt.Errorf("'filter_params' is expected to be undefined but it is not") + } + return validateEAs(rec.Ea, expectedRec.Ea) } } var ( - regexpRequiredMissingIPv6 = regexp.MustCompile("either of 'ipv6_addr' and 'cidr' values is required") - regexpCidrIpAddrConflictIPv6 = regexp.MustCompile("only one of 'ipv6_addr' and 'cidr' values is allowed to be defined") + regexpRequiredMissingIPv6 = regexp.MustCompile("any one of 'ipv6_addr', 'cidr' and 'filter_params' values is required") + regexpCidrIpAddrConflictIPv6 = regexp.MustCompile("only one of 'ipv6_addr', 'cidr' and 'filter_params' values is allowed to be defined") regexpUpdateConflictIPv6 = regexp.MustCompile("only one of 'ipv6_addr' and 'cidr' values is allowed to update") ) @@ -208,7 +223,7 @@ func TestAccResourceAAAARecord(t *testing.T) { UseTtl: utils.BoolPtr(false), Comment: nil, Ea: nil, - }, "", ""), + }, "", "", ""), ), }, { @@ -245,7 +260,7 @@ func TestAccResourceAAAARecord(t *testing.T) { "Location": "New York", "Site": "HQ", }, - }, "", ""), + }, "", "", ""), ), }, { @@ -274,7 +289,7 @@ func TestAccResourceAAAARecord(t *testing.T) { Ttl: utils.Uint32Ptr(155), UseTtl: utils.BoolPtr(true), Comment: utils.StringPtr("test comment 2"), - }, "", ""), + }, "", "", ""), ), }, { @@ -299,7 +314,7 @@ func TestAccResourceAAAARecord(t *testing.T) { Name: utils.StringPtr("name3.test.com"), View: "nondefault_view", UseTtl: utils.BoolPtr(false), - }, "", ""), + }, "", "", ""), ), }, { @@ -328,7 +343,7 @@ func TestAccResourceAAAARecord(t *testing.T) { Name: utils.StringPtr("name3.test.com"), View: "nondefault_view", UseTtl: utils.BoolPtr(false), - }, "2000::1", "2000:1fde::/96"), + }, "2000::1", "2000:1fde::/96", ""), ), }, { @@ -381,7 +396,7 @@ func TestAccResourceAAAARecord(t *testing.T) { Name: utils.StringPtr("name3.test.com"), View: "nondefault_view", UseTtl: utils.BoolPtr(false), - }, "", "2000:1fcc::/96"), + }, "", "2000:1fcc::/96", ""), ), }, { @@ -435,7 +450,7 @@ func TestAccResourceAAAARecord(t *testing.T) { Name: utils.StringPtr("name3.test.com"), View: "nondefault_view", UseTtl: utils.BoolPtr(false), - }, "", ""), + }, "", "", ""), ), }, { @@ -452,6 +467,44 @@ func TestAccResourceAAAARecord(t *testing.T) { }`), ExpectError: regexpDnsviewUpdateNotAllowed, }, + { + Config: fmt.Sprintf(` + resource "infoblox_zone_auth" "zone1" { + fqdn = "test1.com" + } + resource "infoblox_ipv6_network" "net2" { + cidr = "2001:db8:abcd::/64" + ext_attrs = jsonencode({ + "Site" = "Blr" + }) + } + resource "infoblox_aaaa_record" "rec4" { + fqdn = "dynamic.test1.com" + filter_params = jsonencode({ + "*Site" = "Blr"}) + dns_view = "default" + }`), + Check: resource.ComposeTestCheckFunc( + testAccAAAARecordCompare(t, "infoblox_aaaa_record.rec4", &ibclient.RecordAAAA{ + Name: utils.StringPtr("dynamic.test1.com"), + View: "default", + UseTtl: utils.BoolPtr(false), + }, "", "", `{"*Site":"Blr"}`), + ), + }, + { + Config: fmt.Sprintf(` + resource "infoblox_zone_auth" "zone1" { + fqdn = "test1.com" + } + resource "infoblox_aaaa_record" "rec1" { + fqdn = "missing_fields.test1.com" + comment = "missing required fields" + dns_view = "default" + depends_on = [infoblox_zone_auth.zone1] + }`), + ExpectError: regexpRequiredMissingIPv6, + }, }, }) } @@ -486,7 +539,7 @@ func TestAcc_resourceAAAARecord_ea_inheritance(t *testing.T) { Ea: ibclient.EA{ "Location": "test AAAA location", }, - }, "", ""), + }, "", "", ""), }, // When extensible attributes are added by another tool, // terraform shouldn't remove those EAs @@ -549,7 +602,7 @@ func TestAcc_resourceAAAARecord_ea_inheritance(t *testing.T) { "Location": "test AAAA location", "Site": "Testing Site", }, - }, "", ""), + }, "", "", ""), ), }, // Validate that inherited EA won't be removed if some field is updated in the resource @@ -578,7 +631,7 @@ func TestAcc_resourceAAAARecord_ea_inheritance(t *testing.T) { "Location": "test AAAA location", "Site": "Testing Site", }, - }, "", ""), + }, "", "", ""), }, // Validate that inherited EA can be updated { @@ -607,7 +660,7 @@ func TestAcc_resourceAAAARecord_ea_inheritance(t *testing.T) { "Location": "test AAAA location", "Site": "New Testing Site", }, - }, "", ""), + }, "", "", ""), }, // Validate that inherited EA can be removed, if updated {