github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/dme/resource_dme_record_test.go (about)

     1  package dme
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"github.com/soniah/dnsmadeeasy"
    11  )
    12  
    13  var _ = fmt.Sprintf("dummy") // dummy
    14  var _ = os.DevNull           // dummy
    15  
    16  func TestAccDMERecord_basic(t *testing.T) {
    17  	var record dnsmadeeasy.Record
    18  	domainid := os.Getenv("DME_DOMAINID")
    19  
    20  	resource.Test(t, resource.TestCase{
    21  		PreCheck:     func() { testAccPreCheck(t) },
    22  		Providers:    testAccProviders,
    23  		CheckDestroy: testAccCheckDMERecordDestroy,
    24  		Steps: []resource.TestStep{
    25  			resource.TestStep{
    26  				Config: fmt.Sprintf(testDMERecordConfigA, domainid),
    27  				Check: resource.ComposeTestCheckFunc(
    28  					testAccCheckDMERecordExists("dme_record.test", &record),
    29  					resource.TestCheckResourceAttr(
    30  						"dme_record.test", "domainid", domainid),
    31  					resource.TestCheckResourceAttr(
    32  						"dme_record.test", "name", "testa"),
    33  					resource.TestCheckResourceAttr(
    34  						"dme_record.test", "type", "A"),
    35  					resource.TestCheckResourceAttr(
    36  						"dme_record.test", "value", "1.1.1.1"),
    37  					resource.TestCheckResourceAttr(
    38  						"dme_record.test", "ttl", "2000"),
    39  					resource.TestCheckResourceAttr(
    40  						"dme_record.test", "gtdLocation", "DEFAULT"),
    41  				),
    42  			},
    43  		},
    44  	})
    45  }
    46  
    47  func TestAccDMERecordCName(t *testing.T) {
    48  	var record dnsmadeeasy.Record
    49  	domainid := os.Getenv("DME_DOMAINID")
    50  
    51  	resource.Test(t, resource.TestCase{
    52  		PreCheck:     func() { testAccPreCheck(t) },
    53  		Providers:    testAccProviders,
    54  		CheckDestroy: testAccCheckDMERecordDestroy,
    55  		Steps: []resource.TestStep{
    56  			resource.TestStep{
    57  				Config: fmt.Sprintf(testDMERecordConfigCName, domainid),
    58  				Check: resource.ComposeTestCheckFunc(
    59  					testAccCheckDMERecordExists("dme_record.test", &record),
    60  					resource.TestCheckResourceAttr(
    61  						"dme_record.test", "domainid", domainid),
    62  					resource.TestCheckResourceAttr(
    63  						"dme_record.test", "name", "testcname"),
    64  					resource.TestCheckResourceAttr(
    65  						"dme_record.test", "type", "CNAME"),
    66  					resource.TestCheckResourceAttr(
    67  						"dme_record.test", "value", "foo"),
    68  					resource.TestCheckResourceAttr(
    69  						"dme_record.test", "ttl", "2000"),
    70  					resource.TestCheckResourceAttr(
    71  						"dme_record.test", "gtdLocation", "DEFAULT"),
    72  				),
    73  			},
    74  		},
    75  	})
    76  }
    77  
    78  func TestAccDMERecordMX(t *testing.T) {
    79  	var record dnsmadeeasy.Record
    80  	domainid := os.Getenv("DME_DOMAINID")
    81  
    82  	resource.Test(t, resource.TestCase{
    83  		PreCheck:     func() { testAccPreCheck(t) },
    84  		Providers:    testAccProviders,
    85  		CheckDestroy: testAccCheckDMERecordDestroy,
    86  		Steps: []resource.TestStep{
    87  			resource.TestStep{
    88  				Config: fmt.Sprintf(testDMERecordConfigMX, domainid),
    89  				Check: resource.ComposeTestCheckFunc(
    90  					testAccCheckDMERecordExists("dme_record.test", &record),
    91  					resource.TestCheckResourceAttr(
    92  						"dme_record.test", "domainid", domainid),
    93  					resource.TestCheckResourceAttr(
    94  						"dme_record.test", "name", "testmx"),
    95  					resource.TestCheckResourceAttr(
    96  						"dme_record.test", "type", "MX"),
    97  					resource.TestCheckResourceAttr(
    98  						"dme_record.test", "value", "foo"),
    99  					resource.TestCheckResourceAttr(
   100  						"dme_record.test", "mxLevel", "10"),
   101  					resource.TestCheckResourceAttr(
   102  						"dme_record.test", "ttl", "2000"),
   103  					resource.TestCheckResourceAttr(
   104  						"dme_record.test", "gtdLocation", "DEFAULT"),
   105  				),
   106  			},
   107  		},
   108  	})
   109  }
   110  
   111  func TestAccDMERecordHTTPRED(t *testing.T) {
   112  	var record dnsmadeeasy.Record
   113  	domainid := os.Getenv("DME_DOMAINID")
   114  
   115  	resource.Test(t, resource.TestCase{
   116  		PreCheck:     func() { testAccPreCheck(t) },
   117  		Providers:    testAccProviders,
   118  		CheckDestroy: testAccCheckDMERecordDestroy,
   119  		Steps: []resource.TestStep{
   120  			resource.TestStep{
   121  				Config: fmt.Sprintf(testDMERecordConfigHTTPRED, domainid),
   122  				Check: resource.ComposeTestCheckFunc(
   123  					testAccCheckDMERecordExists("dme_record.test", &record),
   124  					resource.TestCheckResourceAttr(
   125  						"dme_record.test", "domainid", domainid),
   126  					resource.TestCheckResourceAttr(
   127  						"dme_record.test", "name", "testhttpred"),
   128  					resource.TestCheckResourceAttr(
   129  						"dme_record.test", "type", "HTTPRED"),
   130  
   131  					resource.TestCheckResourceAttr(
   132  						"dme_record.test", "value", "https://github.com/soniah/terraform-provider-dme"),
   133  					resource.TestCheckResourceAttr(
   134  						"dme_record.test", "hardLink", "true"),
   135  					resource.TestCheckResourceAttr(
   136  						"dme_record.test", "redirectType", "Hidden Frame Masked"),
   137  					resource.TestCheckResourceAttr(
   138  						"dme_record.test", "title", "An Example"),
   139  					resource.TestCheckResourceAttr(
   140  						"dme_record.test", "keywords", "terraform example"),
   141  					resource.TestCheckResourceAttr(
   142  						"dme_record.test", "description", "This is a description"),
   143  
   144  					resource.TestCheckResourceAttr(
   145  						"dme_record.test", "ttl", "2000"),
   146  					resource.TestCheckResourceAttr(
   147  						"dme_record.test", "gtdLocation", "DEFAULT"),
   148  				),
   149  			},
   150  		},
   151  	})
   152  }
   153  
   154  func TestAccDMERecordTXT(t *testing.T) {
   155  	var record dnsmadeeasy.Record
   156  	domainid := os.Getenv("DME_DOMAINID")
   157  
   158  	resource.Test(t, resource.TestCase{
   159  		PreCheck:     func() { testAccPreCheck(t) },
   160  		Providers:    testAccProviders,
   161  		CheckDestroy: testAccCheckDMERecordDestroy,
   162  		Steps: []resource.TestStep{
   163  			resource.TestStep{
   164  				Config: fmt.Sprintf(testDMERecordConfigTXT, domainid),
   165  				Check: resource.ComposeTestCheckFunc(
   166  					testAccCheckDMERecordExists("dme_record.test", &record),
   167  					resource.TestCheckResourceAttr(
   168  						"dme_record.test", "domainid", domainid),
   169  					resource.TestCheckResourceAttr(
   170  						"dme_record.test", "name", "testtxt"),
   171  					resource.TestCheckResourceAttr(
   172  						"dme_record.test", "type", "TXT"),
   173  					resource.TestCheckResourceAttr(
   174  						"dme_record.test", "value", "\"foo\""),
   175  					resource.TestCheckResourceAttr(
   176  						"dme_record.test", "ttl", "2000"),
   177  					resource.TestCheckResourceAttr(
   178  						"dme_record.test", "gtdLocation", "DEFAULT"),
   179  				),
   180  			},
   181  		},
   182  	})
   183  }
   184  
   185  func TestAccDMERecordSPF(t *testing.T) {
   186  	var record dnsmadeeasy.Record
   187  	domainid := os.Getenv("DME_DOMAINID")
   188  
   189  	resource.Test(t, resource.TestCase{
   190  		PreCheck:     func() { testAccPreCheck(t) },
   191  		Providers:    testAccProviders,
   192  		CheckDestroy: testAccCheckDMERecordDestroy,
   193  		Steps: []resource.TestStep{
   194  			resource.TestStep{
   195  				Config: fmt.Sprintf(testDMERecordConfigSPF, domainid),
   196  				Check: resource.ComposeTestCheckFunc(
   197  					testAccCheckDMERecordExists("dme_record.test", &record),
   198  					resource.TestCheckResourceAttr(
   199  						"dme_record.test", "domainid", domainid),
   200  					resource.TestCheckResourceAttr(
   201  						"dme_record.test", "name", "testspf"),
   202  					resource.TestCheckResourceAttr(
   203  						"dme_record.test", "type", "SPF"),
   204  					resource.TestCheckResourceAttr(
   205  						"dme_record.test", "value", "\"foo\""),
   206  					resource.TestCheckResourceAttr(
   207  						"dme_record.test", "ttl", "2000"),
   208  					resource.TestCheckResourceAttr(
   209  						"dme_record.test", "gtdLocation", "DEFAULT"),
   210  				),
   211  			},
   212  		},
   213  	})
   214  }
   215  
   216  func TestAccDMERecordPTR(t *testing.T) {
   217  	var record dnsmadeeasy.Record
   218  	domainid := os.Getenv("DME_DOMAINID")
   219  
   220  	resource.Test(t, resource.TestCase{
   221  		PreCheck:     func() { testAccPreCheck(t) },
   222  		Providers:    testAccProviders,
   223  		CheckDestroy: testAccCheckDMERecordDestroy,
   224  		Steps: []resource.TestStep{
   225  			resource.TestStep{
   226  				Config: fmt.Sprintf(testDMERecordConfigPTR, domainid),
   227  				Check: resource.ComposeTestCheckFunc(
   228  					testAccCheckDMERecordExists("dme_record.test", &record),
   229  					resource.TestCheckResourceAttr(
   230  						"dme_record.test", "domainid", domainid),
   231  					resource.TestCheckResourceAttr(
   232  						"dme_record.test", "name", "testptr"),
   233  					resource.TestCheckResourceAttr(
   234  						"dme_record.test", "type", "PTR"),
   235  					resource.TestCheckResourceAttr(
   236  						"dme_record.test", "value", "foo"),
   237  					resource.TestCheckResourceAttr(
   238  						"dme_record.test", "ttl", "2000"),
   239  					resource.TestCheckResourceAttr(
   240  						"dme_record.test", "gtdLocation", "DEFAULT"),
   241  				),
   242  			},
   243  		},
   244  	})
   245  }
   246  
   247  func TestAccDMERecordNS(t *testing.T) {
   248  	var record dnsmadeeasy.Record
   249  	domainid := os.Getenv("DME_DOMAINID")
   250  
   251  	resource.Test(t, resource.TestCase{
   252  		PreCheck:     func() { testAccPreCheck(t) },
   253  		Providers:    testAccProviders,
   254  		CheckDestroy: testAccCheckDMERecordDestroy,
   255  		Steps: []resource.TestStep{
   256  			resource.TestStep{
   257  				Config: fmt.Sprintf(testDMERecordConfigNS, domainid),
   258  				Check: resource.ComposeTestCheckFunc(
   259  					testAccCheckDMERecordExists("dme_record.test", &record),
   260  					resource.TestCheckResourceAttr(
   261  						"dme_record.test", "domainid", domainid),
   262  					resource.TestCheckResourceAttr(
   263  						"dme_record.test", "name", "testns"),
   264  					resource.TestCheckResourceAttr(
   265  						"dme_record.test", "type", "NS"),
   266  					resource.TestCheckResourceAttr(
   267  						"dme_record.test", "value", "foo"),
   268  					resource.TestCheckResourceAttr(
   269  						"dme_record.test", "ttl", "2000"),
   270  					resource.TestCheckResourceAttr(
   271  						"dme_record.test", "gtdLocation", "DEFAULT"),
   272  				),
   273  			},
   274  		},
   275  	})
   276  }
   277  
   278  func TestAccDMERecordAAAA(t *testing.T) {
   279  	var record dnsmadeeasy.Record
   280  	domainid := os.Getenv("DME_DOMAINID")
   281  
   282  	resource.Test(t, resource.TestCase{
   283  		PreCheck:     func() { testAccPreCheck(t) },
   284  		Providers:    testAccProviders,
   285  		CheckDestroy: testAccCheckDMERecordDestroy,
   286  		Steps: []resource.TestStep{
   287  			resource.TestStep{
   288  				Config: fmt.Sprintf(testDMERecordConfigAAAA, domainid),
   289  				Check: resource.ComposeTestCheckFunc(
   290  					testAccCheckDMERecordExists("dme_record.test", &record),
   291  					resource.TestCheckResourceAttr(
   292  						"dme_record.test", "domainid", domainid),
   293  					resource.TestCheckResourceAttr(
   294  						"dme_record.test", "name", "testaaaa"),
   295  					resource.TestCheckResourceAttr(
   296  						"dme_record.test", "type", "AAAA"),
   297  					resource.TestCheckResourceAttr(
   298  						"dme_record.test", "value", "fe80::0202:b3ff:fe1e:8329"),
   299  					resource.TestCheckResourceAttr(
   300  						"dme_record.test", "ttl", "2000"),
   301  					resource.TestCheckResourceAttr(
   302  						"dme_record.test", "gtdLocation", "DEFAULT"),
   303  				),
   304  			},
   305  		},
   306  	})
   307  }
   308  
   309  func TestAccDMERecordSRV(t *testing.T) {
   310  	var record dnsmadeeasy.Record
   311  	domainid := os.Getenv("DME_DOMAINID")
   312  
   313  	resource.Test(t, resource.TestCase{
   314  		PreCheck:     func() { testAccPreCheck(t) },
   315  		Providers:    testAccProviders,
   316  		CheckDestroy: testAccCheckDMERecordDestroy,
   317  		Steps: []resource.TestStep{
   318  			resource.TestStep{
   319  				Config: fmt.Sprintf(testDMERecordConfigSRV, domainid),
   320  				Check: resource.ComposeTestCheckFunc(
   321  					testAccCheckDMERecordExists("dme_record.test", &record),
   322  					resource.TestCheckResourceAttr(
   323  						"dme_record.test", "domainid", domainid),
   324  					resource.TestCheckResourceAttr(
   325  						"dme_record.test", "name", "testsrv"),
   326  					resource.TestCheckResourceAttr(
   327  						"dme_record.test", "type", "SRV"),
   328  					resource.TestCheckResourceAttr(
   329  						"dme_record.test", "value", "foo"),
   330  					resource.TestCheckResourceAttr(
   331  						"dme_record.test", "priority", "10"),
   332  					resource.TestCheckResourceAttr(
   333  						"dme_record.test", "weight", "20"),
   334  					resource.TestCheckResourceAttr(
   335  						"dme_record.test", "port", "30"),
   336  					resource.TestCheckResourceAttr(
   337  						"dme_record.test", "ttl", "2000"),
   338  					resource.TestCheckResourceAttr(
   339  						"dme_record.test", "gtdLocation", "DEFAULT"),
   340  				),
   341  			},
   342  		},
   343  	})
   344  }
   345  
   346  func testAccCheckDMERecordDestroy(s *terraform.State) error {
   347  	client := testAccProvider.Meta().(*dnsmadeeasy.Client)
   348  
   349  	for _, rs := range s.RootModule().Resources {
   350  		if rs.Type != "dnsmadeeasy_record" {
   351  			continue
   352  		}
   353  
   354  		_, err := client.ReadRecord(rs.Primary.Attributes["domainid"], rs.Primary.ID)
   355  
   356  		if err == nil {
   357  			return fmt.Errorf("Record still exists")
   358  		}
   359  	}
   360  
   361  	return nil
   362  }
   363  
   364  func testAccCheckDMERecordExists(n string, record *dnsmadeeasy.Record) resource.TestCheckFunc {
   365  	return func(s *terraform.State) error {
   366  		rs, ok := s.RootModule().Resources[n]
   367  
   368  		if !ok {
   369  			return fmt.Errorf("Not found: %s", n)
   370  		}
   371  
   372  		if rs.Primary.ID == "" {
   373  			return fmt.Errorf("No Record ID is set")
   374  		}
   375  
   376  		client := testAccProvider.Meta().(*dnsmadeeasy.Client)
   377  
   378  		foundRecord, err := client.ReadRecord(rs.Primary.Attributes["domainid"], rs.Primary.ID)
   379  
   380  		if err != nil {
   381  			return err
   382  		}
   383  
   384  		if foundRecord.StringRecordID() != rs.Primary.ID {
   385  			return fmt.Errorf("Record not found")
   386  		}
   387  
   388  		*record = *foundRecord
   389  
   390  		return nil
   391  	}
   392  }
   393  
   394  const testDMERecordConfigA = `
   395  resource "dme_record" "test" {
   396    domainid = "%s"
   397    name = "testa"
   398    type = "A"
   399    value = "1.1.1.1"
   400    ttl = 2000
   401    gtdLocation = "DEFAULT"
   402  }`
   403  
   404  const testDMERecordConfigCName = `
   405  resource "dme_record" "test" {
   406    domainid = "%s"
   407    name = "testcname"
   408    type = "CNAME"
   409    value = "foo"
   410    ttl = 2000
   411    gtdLocation = "DEFAULT"
   412  }`
   413  
   414  const testDMERecordConfigAName = `
   415  resource "dme_record" "test" {
   416    domainid = "%s"
   417    name = "testaname"
   418    type = "ANAME"
   419    value = "foo"
   420    ttl = 2000
   421    gtdLocation = "DEFAULT"
   422  }`
   423  
   424  const testDMERecordConfigMX = `
   425  resource "dme_record" "test" {
   426    domainid = "%s"
   427    name = "testmx"
   428    type = "MX"
   429    value = "foo"
   430    mxLevel = 10
   431    ttl = 2000
   432    gtdLocation = "DEFAULT"
   433  }`
   434  
   435  const testDMERecordConfigHTTPRED = `
   436  resource "dme_record" "test" {
   437    domainid = "%s"
   438    name = "testhttpred"
   439    type = "HTTPRED"
   440    value = "https://github.com/soniah/terraform-provider-dme"
   441    hardLink = true
   442    redirectType = "Hidden Frame Masked"
   443    title = "An Example"
   444    keywords = "terraform example"
   445    description = "This is a description"
   446    ttl = 2000
   447    gtdLocation = "DEFAULT"
   448  }`
   449  
   450  const testDMERecordConfigTXT = `
   451  resource "dme_record" "test" {
   452    domainid = "%s"
   453    name = "testtxt"
   454    type = "TXT"
   455    value = "foo"
   456    ttl = 2000
   457    gtdLocation = "DEFAULT"
   458  }`
   459  
   460  const testDMERecordConfigSPF = `
   461  resource "dme_record" "test" {
   462    domainid = "%s"
   463    name = "testspf"
   464    type = "SPF"
   465    value = "foo"
   466    ttl = 2000
   467    gtdLocation = "DEFAULT"
   468  }`
   469  
   470  const testDMERecordConfigPTR = `
   471  resource "dme_record" "test" {
   472    domainid = "%s"
   473    name = "testptr"
   474    type = "PTR"
   475    value = "foo"
   476    ttl = 2000
   477    gtdLocation = "DEFAULT"
   478  }`
   479  
   480  const testDMERecordConfigNS = `
   481  resource "dme_record" "test" {
   482    domainid = "%s"
   483    name = "testns"
   484    type = "NS"
   485    value = "foo"
   486    ttl = 2000
   487    gtdLocation = "DEFAULT"
   488  }`
   489  
   490  const testDMERecordConfigAAAA = `
   491  resource "dme_record" "test" {
   492    domainid = "%s"
   493    name = "testaaaa"
   494    type = "AAAA"
   495    value = "FE80::0202:B3FF:FE1E:8329"
   496    ttl = 2000
   497    gtdLocation = "DEFAULT"
   498  }`
   499  
   500  const testDMERecordConfigSRV = `
   501  resource "dme_record" "test" {
   502    domainid = "%s"
   503    name = "testsrv"
   504    type = "SRV"
   505    value = "foo"
   506    priority = 10
   507    weight = 20
   508    port = 30
   509    ttl = 2000
   510    gtdLocation = "DEFAULT"
   511  }`