github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/ultradns/resource_ultradns_record_test.go (about)

     1  package ultradns
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/Ensighten/udnssdk"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestAccUltradnsRecord(t *testing.T) {
    13  	var record udnssdk.RRSet
    14  	// domain := os.Getenv("ULTRADNS_DOMAIN")
    15  	domain := "ultradns.phinze.com"
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccRecordCheckDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: fmt.Sprintf(testCfgRecordMinimal, domain),
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckUltradnsRecordExists("ultradns_record.it", &record),
    26  					resource.TestCheckResourceAttr("ultradns_record.it", "zone", domain),
    27  					resource.TestCheckResourceAttr("ultradns_record.it", "name", "test-record"),
    28  					resource.TestCheckResourceAttr("ultradns_record.it", "rdata.3994963683", "10.5.0.1"),
    29  				),
    30  			},
    31  			resource.TestStep{
    32  				Config: fmt.Sprintf(testCfgRecordMinimal, domain),
    33  				Check: resource.ComposeTestCheckFunc(
    34  					testAccCheckUltradnsRecordExists("ultradns_record.it", &record),
    35  					resource.TestCheckResourceAttr("ultradns_record.it", "zone", domain),
    36  					resource.TestCheckResourceAttr("ultradns_record.it", "name", "test-record"),
    37  					resource.TestCheckResourceAttr("ultradns_record.it", "rdata.3994963683", "10.5.0.1"),
    38  				),
    39  			},
    40  			resource.TestStep{
    41  				Config: fmt.Sprintf(testCfgRecordUpdated, domain),
    42  				Check: resource.ComposeTestCheckFunc(
    43  					testAccCheckUltradnsRecordExists("ultradns_record.it", &record),
    44  					resource.TestCheckResourceAttr("ultradns_record.it", "zone", domain),
    45  					resource.TestCheckResourceAttr("ultradns_record.it", "name", "test-record"),
    46  					resource.TestCheckResourceAttr("ultradns_record.it", "rdata.1998004057", "10.5.0.2"),
    47  				),
    48  			},
    49  		},
    50  	})
    51  }
    52  
    53  func TestAccUltradnsRecordTXT(t *testing.T) {
    54  	var record udnssdk.RRSet
    55  	// domain := os.Getenv("ULTRADNS_DOMAIN")
    56  	domain := "ultradns.phinze.com"
    57  
    58  	resource.Test(t, resource.TestCase{
    59  		PreCheck:     func() { testAccPreCheck(t) },
    60  		Providers:    testAccProviders,
    61  		CheckDestroy: testAccRecordCheckDestroy,
    62  		Steps: []resource.TestStep{
    63  			resource.TestStep{
    64  				Config: fmt.Sprintf(testCfgRecordTXTMinimal, domain),
    65  				Check: resource.ComposeTestCheckFunc(
    66  					testAccCheckUltradnsRecordExists("ultradns_record.it", &record),
    67  					resource.TestCheckResourceAttr("ultradns_record.it", "zone", domain),
    68  					resource.TestCheckResourceAttr("ultradns_record.it", "name", "test-record-txt"),
    69  					resource.TestCheckResourceAttr("ultradns_record.it", "rdata.1447448707", "simple answer"),
    70  					resource.TestCheckResourceAttr("ultradns_record.it", "rdata.3337444205", "backslash answer \\"),
    71  					resource.TestCheckResourceAttr("ultradns_record.it", "rdata.3135730072", "quote answer \""),
    72  					resource.TestCheckResourceAttr("ultradns_record.it", "rdata.126343430", "complex answer \\ \""),
    73  				),
    74  			},
    75  		},
    76  	})
    77  }
    78  
    79  func testAccRecordCheckDestroy(s *terraform.State) error {
    80  	client := testAccProvider.Meta().(*udnssdk.Client)
    81  
    82  	for _, rs := range s.RootModule().Resources {
    83  		if rs.Type != "ultradns_record" {
    84  			continue
    85  		}
    86  
    87  		k := udnssdk.RRSetKey{
    88  			Zone: rs.Primary.Attributes["zone"],
    89  			Name: rs.Primary.Attributes["name"],
    90  			Type: rs.Primary.Attributes["type"],
    91  		}
    92  
    93  		_, err := client.RRSets.Select(k)
    94  
    95  		if err == nil {
    96  			return fmt.Errorf("Record still exists")
    97  		}
    98  	}
    99  
   100  	return nil
   101  }
   102  
   103  const testCfgRecordMinimal = `
   104  resource "ultradns_record" "it" {
   105    zone = "%s"
   106    name  = "test-record"
   107  
   108    rdata = ["10.5.0.1"]
   109    type  = "A"
   110    ttl   = 3600
   111  }
   112  `
   113  
   114  const testCfgRecordUpdated = `
   115  resource "ultradns_record" "it" {
   116    zone = "%s"
   117    name  = "test-record"
   118  
   119    rdata = ["10.5.0.2"]
   120    type  = "A"
   121    ttl   = 3600
   122  }
   123  `
   124  
   125  const testCfgRecordTXTMinimal = `
   126  resource "ultradns_record" "it" {
   127    zone = "%s"
   128    name  = "test-record-txt"
   129  
   130    rdata = [
   131      "simple answer",
   132      "backslash answer \\",
   133      "quote answer \"",
   134      "complex answer \\ \"",
   135    ]
   136    type  = "TXT"
   137    ttl   = 3600
   138  }
   139  `