github.com/peterbale/terraform@v0.9.0-beta2.0.20170315142748-5723acd55547/builtin/providers/dnsimple/resource_dnsimple_record_test.go (about) 1 package dnsimple 2 3 import ( 4 "fmt" 5 "os" 6 "strconv" 7 "testing" 8 9 "github.com/dnsimple/dnsimple-go/dnsimple" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccDNSimpleRecord_Basic(t *testing.T) { 15 var record dnsimple.ZoneRecord 16 domain := os.Getenv("DNSIMPLE_DOMAIN") 17 18 resource.Test(t, resource.TestCase{ 19 PreCheck: func() { testAccPreCheck(t) }, 20 Providers: testAccProviders, 21 CheckDestroy: testAccCheckDNSimpleRecordDestroy, 22 Steps: []resource.TestStep{ 23 resource.TestStep{ 24 Config: fmt.Sprintf(testAccCheckDNSimpleRecordConfig_basic, domain), 25 Check: resource.ComposeTestCheckFunc( 26 testAccCheckDNSimpleRecordExists("dnsimple_record.foobar", &record), 27 testAccCheckDNSimpleRecordAttributes(&record), 28 resource.TestCheckResourceAttr( 29 "dnsimple_record.foobar", "name", "terraform"), 30 resource.TestCheckResourceAttr( 31 "dnsimple_record.foobar", "domain", domain), 32 resource.TestCheckResourceAttr( 33 "dnsimple_record.foobar", "value", "192.168.0.10"), 34 ), 35 }, 36 }, 37 }) 38 } 39 40 func TestAccDNSimpleRecord_Updated(t *testing.T) { 41 var record dnsimple.ZoneRecord 42 domain := os.Getenv("DNSIMPLE_DOMAIN") 43 44 resource.Test(t, resource.TestCase{ 45 PreCheck: func() { testAccPreCheck(t) }, 46 Providers: testAccProviders, 47 CheckDestroy: testAccCheckDNSimpleRecordDestroy, 48 Steps: []resource.TestStep{ 49 resource.TestStep{ 50 Config: fmt.Sprintf(testAccCheckDNSimpleRecordConfig_basic, domain), 51 Check: resource.ComposeTestCheckFunc( 52 testAccCheckDNSimpleRecordExists("dnsimple_record.foobar", &record), 53 testAccCheckDNSimpleRecordAttributes(&record), 54 resource.TestCheckResourceAttr( 55 "dnsimple_record.foobar", "name", "terraform"), 56 resource.TestCheckResourceAttr( 57 "dnsimple_record.foobar", "domain", domain), 58 resource.TestCheckResourceAttr( 59 "dnsimple_record.foobar", "value", "192.168.0.10"), 60 ), 61 }, 62 resource.TestStep{ 63 Config: fmt.Sprintf(testAccCheckDNSimpleRecordConfig_new_value, domain), 64 Check: resource.ComposeTestCheckFunc( 65 testAccCheckDNSimpleRecordExists("dnsimple_record.foobar", &record), 66 testAccCheckDNSimpleRecordAttributesUpdated(&record), 67 resource.TestCheckResourceAttr( 68 "dnsimple_record.foobar", "name", "terraform"), 69 resource.TestCheckResourceAttr( 70 "dnsimple_record.foobar", "domain", domain), 71 resource.TestCheckResourceAttr( 72 "dnsimple_record.foobar", "value", "192.168.0.11"), 73 ), 74 }, 75 }, 76 }) 77 } 78 79 func testAccCheckDNSimpleRecordDestroy(s *terraform.State) error { 80 provider := testAccProvider.Meta().(*Client) 81 82 for _, rs := range s.RootModule().Resources { 83 if rs.Type != "dnsimple_record" { 84 continue 85 } 86 87 recordID, _ := strconv.Atoi(rs.Primary.ID) 88 _, err := provider.client.Zones.GetRecord(provider.config.Account, rs.Primary.Attributes["domain"], recordID) 89 if err == nil { 90 return fmt.Errorf("Record still exists") 91 } 92 } 93 94 return nil 95 } 96 97 func testAccCheckDNSimpleRecordAttributes(record *dnsimple.ZoneRecord) resource.TestCheckFunc { 98 return func(s *terraform.State) error { 99 100 if record.Content != "192.168.0.10" { 101 return fmt.Errorf("Bad content: %s", record.Content) 102 } 103 104 return nil 105 } 106 } 107 108 func testAccCheckDNSimpleRecordAttributesUpdated(record *dnsimple.ZoneRecord) resource.TestCheckFunc { 109 return func(s *terraform.State) error { 110 111 if record.Content != "192.168.0.11" { 112 return fmt.Errorf("Bad content: %s", record.Content) 113 } 114 115 return nil 116 } 117 } 118 119 func testAccCheckDNSimpleRecordExists(n string, record *dnsimple.ZoneRecord) resource.TestCheckFunc { 120 return func(s *terraform.State) error { 121 rs, ok := s.RootModule().Resources[n] 122 123 if !ok { 124 return fmt.Errorf("Not found: %s", n) 125 } 126 127 if rs.Primary.ID == "" { 128 return fmt.Errorf("No Record ID is set") 129 } 130 131 provider := testAccProvider.Meta().(*Client) 132 133 recordID, _ := strconv.Atoi(rs.Primary.ID) 134 resp, err := provider.client.Zones.GetRecord(provider.config.Account, rs.Primary.Attributes["domain"], recordID) 135 if err != nil { 136 return err 137 } 138 139 foundRecord := resp.Data 140 if foundRecord.ID != recordID { 141 return fmt.Errorf("Record not found") 142 } 143 144 *record = *foundRecord 145 146 return nil 147 } 148 } 149 150 const testAccCheckDNSimpleRecordConfig_basic = ` 151 resource "dnsimple_record" "foobar" { 152 domain = "%s" 153 154 name = "terraform" 155 value = "192.168.0.10" 156 type = "A" 157 ttl = 3600 158 }` 159 160 const testAccCheckDNSimpleRecordConfig_new_value = ` 161 resource "dnsimple_record" "foobar" { 162 domain = "%s" 163 164 name = "terraform" 165 value = "192.168.0.11" 166 type = "A" 167 ttl = 3600 168 }`