Skip to content

Commit

Permalink
Adding TXT Record examples and aligning unit tests with more common f…
Browse files Browse the repository at this point in the history
…ormat
  • Loading branch information
shadtimm committed Nov 19, 2021
1 parent 396ea4c commit 4e41168
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 98 deletions.
19 changes: 19 additions & 0 deletions examples/v0.14/Resources/TXTRecord/infoblox.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# TXT Record with minimumally required input parameters
resource "infoblox_txt_record" "txt_record_minimal" {
name = "txt-minimal.test.com"
text = "minimal"
}

# TXT Record with all possible input parameters
resource "infoblox_txt_record" "txt_record_all" {
name = "txt-all.test.com"
text = "all"
dns_view = "default"
ttl = 3600
comment = "txt record comment"
ext_attrs = jsonencode({
"Tenant ID" = "tf-plugin"
"Location" = "Test location"
"Site" = "Test site"
})
}
8 changes: 8 additions & 0 deletions examples/v0.14/Resources/TXTRecord/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
terraform {
required_providers {
infoblox = {
source = "infobloxopen/infoblox"
version = ">= 2.1"
}
}
}
211 changes: 113 additions & 98 deletions infoblox/resource_infoblox_txt_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,125 +9,140 @@ import (
ibclient "github.com/infobloxopen/infoblox-go-client/v2"
)

func TestAccResourceTXTRecord(t *testing.T) {

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckTXTRecordDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccresourceTXTRecordCreate,
Check: resource.ComposeTestCheckFunc(
testAccTXTRecordExists(t, "infoblox_txt_record.foo", "test-name", "a.com", "default"),
),
},
resource.TestStep{
Config: testAccresourceTXTRecordAllocate,
Check: resource.ComposeTestCheckFunc(
testAccTXTRecordExists(t, "infoblox_txt_record.foo1", "test-name", "a.com", "default"),
testAccTXTRecordExists(t, "infoblox_txt_record.foo2", "test-name", "a.com", "default"),
),
},
resource.TestStep{
Config: testAccresourceTXTRecordUpdate,
Check: resource.ComposeTestCheckFunc(
testAccTXTRecordExists(t, "infoblox_txt_record.foo", "test-name", "a.com", "default"),
),
},
},
})
}

func testAccCheckTXTRecordDestroy(s *terraform.State) error {
meta := testAccProvider.Meta()

for _, rs := range s.RootModule().Resources {
if rs.Type != "resource_txt_record" {
continue
}
Connector := meta.(*ibclient.Connector)
objMgr := ibclient.NewObjectManager(Connector, "terraform_test", "terraform_test_tenant")
recordName, _ := objMgr.GetTXTRecordByRef(rs.Primary.ID)
if recordName != nil {
connector := meta.(ibclient.IBConnector)
objMgr := ibclient.NewObjectManager(connector, "terraform_test", "test")
rec, _ := objMgr.GetTXTRecordByRef(rs.Primary.ID)
if rec != nil {
return fmt.Errorf("record not found")
}

}
return nil
}
func testAccTXTRecordExists(t *testing.T, n string, recordName string, text string, ttl int, dnsView string) resource.TestCheckFunc {

func testAccTXTRecordCompare(t *testing.T, resPath string, expectedRec *ibclient.RecordTXT) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found:%s", n)
res, found := s.RootModule().Resources[resPath]
if !found {
return fmt.Errorf("Not found: %s", resPath)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No ID i set")
if res.Primary.ID == "" {
return fmt.Errorf("ID is not set")
}
meta := testAccProvider.Meta()
Connector := meta.(*ibclient.Connector)
objMgr := ibclient.NewObjectManager(Connector, "terraform_test", "terraform_test_tenant")
connector := meta.(ibclient.IBConnector)
objMgr := ibclient.NewObjectManager(connector, "terraform_test", "test")

recordName, _ := objMgr.GetTXTRecordByRef(rs.Primary.ID)
if recordName == nil {
rec, _ := objMgr.GetTXTRecordByRef(res.Primary.ID)
if rec == nil {
return fmt.Errorf("record not found")
}

return nil
if rec.Name != expectedRec.Name {
return fmt.Errorf(
"'name' does not match: got '%s', expected '%s'",
rec.Name,
expectedRec.Name)
}
if rec.Text != expectedRec.Text {
return fmt.Errorf(
"'text' does not match: got '%s', expected '%s'",
rec.Text, expectedRec.Text)
}
if rec.View != expectedRec.View {
return fmt.Errorf(
"'dns_view' does not match: got '%s', expected '%s'",
rec.View, expectedRec.View)
}
if rec.Ttl != expectedRec.Ttl {
return fmt.Errorf(
"Ttl value does not match: got '%d', expected '%d'",
rec.Ttl, expectedRec.Ttl)
}
if rec.Comment != expectedRec.Comment {
return fmt.Errorf(
"'comment' does not match: got '%s', expected '%s'",
rec.Comment, expectedRec.Comment)
}
return validateEAs(rec.Ea, expectedRec.Ea)
}
}

var testAccresourceTXTRecordCreate = fmt.Sprintf(`
resource "infoblox_txt_record" "foo"{
name="test-name"
text="a.com"
dns_view="default"
comment="TXT record created"
ext_attrs = jsonencode({
"Tenant ID" = "terraform_test_tenant"
"Location" = "Test loc"
"Site" = "Test site"
"TestEA1"=["text1","text2"]
})
}`)

var testAccresourceTXTRecordAllocate = fmt.Sprintf(`
resource "infoblox_txt_record" "foo1"{
name="test-name"
text="a.com"
dns_view="default"
comment="TXT record created"
ext_attrs = jsonencode({
"Tenant ID" = "terraform_test_tenant"
"Location" = "Test loc"
"Site" = "Test site"
"TestEA1"=["text1","text2"]
})
}
resource "infoblox_txt_record" "foo2"{
name="test-name"
text="a.com"
dns_view="default"
comment="TXT record created"
ext_attrs = jsonencode({
"Tenant ID" = "terraform_test_tenant"
"Location" = "Test loc"
"Site" = "Test site"
"TestEA1"=["text1","text2"]
})
}`)
func TestAccResourceTXTRecord(t *testing.T) {

var testAccresourceTXTRecordUpdate = fmt.Sprintf(`
resource "infoblox_txt_record" "foo"{
name="test-name"
text="a.com"
dns_view="default"
comment="TXT record created"
ext_attrs = jsonencode({
"Tenant ID" = "terraform_test_tenant"
"Location" = "Test loc"
"Site" = "Test site"
"TestEA1"=["text1","text2"]
})
}`)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckTXTRecordDestroy,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "infoblox_txt_record" "foo"{
name="name1.test.com"
text="test"
dns_view="default"
ttl = 0
comment="test comment 1"
ext_attrs = jsonencode({
"Tenant ID" = "terraform_test_tenant"
"Location" = "Test loc"
"Site" = "Test site"
"TestEA1"=["text1","text2"]
})
}`),
Check: resource.ComposeTestCheckFunc(
testAccTXTRecordCompare(t, "infoblox_txt_record.foo", &ibclient.RecordTXT{
Name: "name1.test.com",
Text: "test",
View: "default",
Ttl: 0,
Comment: "test comment 1",
Ea: ibclient.EA{
"Tenant ID": "terraform_test_tenant",
"Location": "Test loc",
"Site": "Test site",
"TestEA1": []string{"text1", "text2"},
},
}),
),
},
{
Config: fmt.Sprintf(`
resource "infoblox_txt_record" "foo2"{
name="name2.test.com"
text="test"
dns_view="default"
ttl = 3600
comment="test comment 1"
ext_attrs = jsonencode({
"Tenant ID" = "terraform_test_tenant"
"Location" = "Test loc"
"Site" = "Test site"
"TestEA1"=["text1","text2"]
})
}`),
Check: resource.ComposeTestCheckFunc(
testAccTXTRecordCompare(t, "infoblox_txt_record.foo2", &ibclient.RecordTXT{
Name: "name2.test.com",
Text: "test",
View: "default",
Ttl: 3600,
Comment: "test comment 1",
Ea: ibclient.EA{
"Tenant ID": "terraform_test_tenant",
"Location": "Test loc",
"Site": "Test site",
"TestEA1": []string{"text1", "text2"},
},
}),
),
},
},
})
}

0 comments on commit 4e41168

Please sign in to comment.