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

     1  package ns1
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"sort"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  
    12  	ns1 "gopkg.in/ns1/ns1-go.v2/rest"
    13  	"gopkg.in/ns1/ns1-go.v2/rest/model/dns"
    14  )
    15  
    16  func TestAccRecord_basic(t *testing.T) {
    17  	var record dns.Record
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccCheckRecordDestroy,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: testAccRecordBasic,
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckRecordExists("ns1_record.it", &record),
    27  					testAccCheckRecordDomain(&record, "test.terraform-record-test.io"),
    28  					testAccCheckRecordTTL(&record, 60),
    29  					testAccCheckRecordUseClientSubnet(&record, true),
    30  					testAccCheckRecordRegionName(&record, []string{"cal"}),
    31  					// testAccCheckRecordAnswerMetaWeight(&record, 10),
    32  					testAccCheckRecordAnswerRdata(&record, 0, "test1.terraform-record-test.io"),
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func TestAccRecord_updated(t *testing.T) {
    40  	var record dns.Record
    41  	resource.Test(t, resource.TestCase{
    42  		PreCheck:     func() { testAccPreCheck(t) },
    43  		Providers:    testAccProviders,
    44  		CheckDestroy: testAccCheckRecordDestroy,
    45  		Steps: []resource.TestStep{
    46  			resource.TestStep{
    47  				Config: testAccRecordBasic,
    48  				Check: resource.ComposeTestCheckFunc(
    49  					testAccCheckRecordExists("ns1_record.it", &record),
    50  					testAccCheckRecordDomain(&record, "test.terraform-record-test.io"),
    51  					testAccCheckRecordTTL(&record, 60),
    52  					testAccCheckRecordUseClientSubnet(&record, true),
    53  					testAccCheckRecordRegionName(&record, []string{"cal"}),
    54  					// testAccCheckRecordAnswerMetaWeight(&record, 10),
    55  					testAccCheckRecordAnswerRdata(&record, 0, "test1.terraform-record-test.io"),
    56  				),
    57  			},
    58  			resource.TestStep{
    59  				Config: testAccRecordUpdated,
    60  				Check: resource.ComposeTestCheckFunc(
    61  					testAccCheckRecordExists("ns1_record.it", &record),
    62  					testAccCheckRecordDomain(&record, "test.terraform-record-test.io"),
    63  					testAccCheckRecordTTL(&record, 120),
    64  					testAccCheckRecordUseClientSubnet(&record, false),
    65  					testAccCheckRecordRegionName(&record, []string{"ny", "wa"}),
    66  					// testAccCheckRecordAnswerMetaWeight(&record, 5),
    67  					testAccCheckRecordAnswerRdata(&record, 0, "test2.terraform-record-test.io"),
    68  				),
    69  			},
    70  		},
    71  	})
    72  }
    73  
    74  func TestAccRecord_SPF(t *testing.T) {
    75  	var record dns.Record
    76  	resource.Test(t, resource.TestCase{
    77  		PreCheck:     func() { testAccPreCheck(t) },
    78  		Providers:    testAccProviders,
    79  		CheckDestroy: testAccCheckRecordDestroy,
    80  		Steps: []resource.TestStep{
    81  			resource.TestStep{
    82  				Config: testAccRecordSPF,
    83  				Check: resource.ComposeTestCheckFunc(
    84  					testAccCheckRecordExists("ns1_record.spf", &record),
    85  					testAccCheckRecordDomain(&record, "terraform-record-test.io"),
    86  					testAccCheckRecordTTL(&record, 86400),
    87  					testAccCheckRecordUseClientSubnet(&record, true),
    88  					testAccCheckRecordAnswerRdata(&record, 0, "v=DKIM1; k=rsa; p=XXXXXXXX"),
    89  				),
    90  			},
    91  		},
    92  	})
    93  }
    94  
    95  func TestAccRecord_SRV(t *testing.T) {
    96  	var record dns.Record
    97  	resource.Test(t, resource.TestCase{
    98  		PreCheck:     func() { testAccPreCheck(t) },
    99  		Providers:    testAccProviders,
   100  		CheckDestroy: testAccCheckRecordDestroy,
   101  		Steps: []resource.TestStep{
   102  			resource.TestStep{
   103  				Config: testAccRecordSRV,
   104  				Check: resource.ComposeTestCheckFunc(
   105  					testAccCheckRecordExists("ns1_record.srv", &record),
   106  					testAccCheckRecordDomain(&record, "_some-server._tcp.terraform-record-test.io"),
   107  					testAccCheckRecordTTL(&record, 86400),
   108  					testAccCheckRecordUseClientSubnet(&record, true),
   109  					testAccCheckRecordAnswerRdata(&record, 0, "10"),
   110  					testAccCheckRecordAnswerRdata(&record, 1, "0"),
   111  					testAccCheckRecordAnswerRdata(&record, 2, "2380"),
   112  					testAccCheckRecordAnswerRdata(&record, 3, "node-1.terraform-record-test.io"),
   113  				),
   114  			},
   115  		},
   116  	})
   117  }
   118  
   119  func testAccCheckRecordExists(n string, record *dns.Record) resource.TestCheckFunc {
   120  	return func(s *terraform.State) error {
   121  		rs, ok := s.RootModule().Resources[n]
   122  		if !ok {
   123  			return fmt.Errorf("Not found: %v", n)
   124  		}
   125  
   126  		if rs.Primary.ID == "" {
   127  			return fmt.Errorf("NoID is set")
   128  		}
   129  
   130  		client := testAccProvider.Meta().(*ns1.Client)
   131  
   132  		p := rs.Primary
   133  
   134  		foundRecord, _, err := client.Records.Get(p.Attributes["zone"], p.Attributes["domain"], p.Attributes["type"])
   135  		if err != nil {
   136  			return fmt.Errorf("Record not found")
   137  		}
   138  
   139  		if foundRecord.Domain != p.Attributes["domain"] {
   140  			return fmt.Errorf("Record not found")
   141  		}
   142  
   143  		*record = *foundRecord
   144  
   145  		return nil
   146  	}
   147  }
   148  
   149  func testAccCheckRecordDestroy(s *terraform.State) error {
   150  	client := testAccProvider.Meta().(*ns1.Client)
   151  
   152  	var recordDomain string
   153  	var recordZone string
   154  	var recordType string
   155  
   156  	for _, rs := range s.RootModule().Resources {
   157  		if rs.Type != "ns1_record" {
   158  			continue
   159  		}
   160  
   161  		if rs.Type == "ns1_record" {
   162  			recordType = rs.Primary.Attributes["type"]
   163  			recordDomain = rs.Primary.Attributes["domain"]
   164  			recordZone = rs.Primary.Attributes["zone"]
   165  		}
   166  	}
   167  
   168  	foundRecord, _, err := client.Records.Get(recordZone, recordDomain, recordType)
   169  	if err != ns1.ErrRecordMissing {
   170  		return fmt.Errorf("Record still exists: %#v %#v", foundRecord, err)
   171  	}
   172  
   173  	return nil
   174  }
   175  
   176  func testAccCheckRecordDomain(r *dns.Record, expected string) resource.TestCheckFunc {
   177  	return func(s *terraform.State) error {
   178  		if r.Domain != expected {
   179  			return fmt.Errorf("Domain: got: %#v want: %#v", r.Domain, expected)
   180  		}
   181  		return nil
   182  	}
   183  }
   184  
   185  func testAccCheckRecordTTL(r *dns.Record, expected int) resource.TestCheckFunc {
   186  	return func(s *terraform.State) error {
   187  		if r.TTL != expected {
   188  			return fmt.Errorf("TTL: got: %#v want: %#v", r.TTL, expected)
   189  		}
   190  		return nil
   191  	}
   192  }
   193  
   194  func testAccCheckRecordUseClientSubnet(r *dns.Record, expected bool) resource.TestCheckFunc {
   195  	return func(s *terraform.State) error {
   196  		if *r.UseClientSubnet != expected {
   197  			return fmt.Errorf("UseClientSubnet: got: %#v want: %#v", *r.UseClientSubnet, expected)
   198  		}
   199  		return nil
   200  	}
   201  }
   202  
   203  func testAccCheckRecordRegionName(r *dns.Record, expected []string) resource.TestCheckFunc {
   204  	return func(s *terraform.State) error {
   205  		regions := make([]string, len(r.Regions))
   206  
   207  		i := 0
   208  		for k := range r.Regions {
   209  			regions[i] = k
   210  			i++
   211  		}
   212  		sort.Strings(regions)
   213  		sort.Strings(expected)
   214  		if !reflect.DeepEqual(regions, expected) {
   215  			return fmt.Errorf("Regions: got: %#v want: %#v", regions, expected)
   216  		}
   217  		return nil
   218  	}
   219  }
   220  
   221  func testAccCheckRecordAnswerMetaWeight(r *dns.Record, expected float64) resource.TestCheckFunc {
   222  	return func(s *terraform.State) error {
   223  		recordAnswer := r.Answers[0]
   224  		recordMetas := recordAnswer.Meta
   225  		weight := recordMetas.Weight.(float64)
   226  		if weight != expected {
   227  			return fmt.Errorf("Answers[0].Meta.Weight: got: %#v want: %#v", weight, expected)
   228  		}
   229  		return nil
   230  	}
   231  }
   232  
   233  func testAccCheckRecordAnswerRdata(r *dns.Record, idx int, expected string) resource.TestCheckFunc {
   234  	return func(s *terraform.State) error {
   235  		recordAnswer := r.Answers[0]
   236  		recordAnswerString := recordAnswer.Rdata[idx]
   237  		if recordAnswerString != expected {
   238  			return fmt.Errorf("Answers[0].Rdata[%d]: got: %#v want: %#v", idx, recordAnswerString, expected)
   239  		}
   240  		return nil
   241  	}
   242  }
   243  
   244  const testAccRecordBasic = `
   245  resource "ns1_record" "it" {
   246    zone              = "${ns1_zone.test.zone}"
   247    domain            = "test.${ns1_zone.test.zone}"
   248    type              = "CNAME"
   249    ttl               = 60
   250  
   251    // meta {
   252    //   weight = 5
   253    //   connections = 3
   254    //   // up = false // Ignored by d.GetOk("meta.0.up") due to known issue
   255    // }
   256  
   257    answers {
   258      answer = "test1.terraform-record-test.io"
   259      region = "cal"
   260  
   261      // meta {
   262      //   weight = 10
   263      //   up = true
   264      // }
   265    }
   266  
   267    regions {
   268      name = "cal"
   269      // meta {
   270      //   up = true
   271      //   us_state = ["CA"]
   272      // }
   273    }
   274  
   275    filters {
   276      filter = "up"
   277    }
   278  
   279    filters {
   280      filter = "geotarget_country"
   281    }
   282  
   283    filters {
   284      filter = "select_first_n"
   285      config = {N=1}
   286    }
   287  }
   288  
   289  resource "ns1_zone" "test" {
   290    zone = "terraform-record-test.io"
   291  }
   292  `
   293  
   294  const testAccRecordUpdated = `
   295  resource "ns1_record" "it" {
   296    zone              = "${ns1_zone.test.zone}"
   297    domain            = "test.${ns1_zone.test.zone}"
   298    type              = "CNAME"
   299    ttl               = 120
   300    use_client_subnet = false
   301  
   302    // meta {
   303    //   weight = 5
   304    //   connections = 3
   305    //   // up = false // Ignored by d.GetOk("meta.0.up") due to known issue
   306    // }
   307  
   308    answers {
   309      answer = "test2.terraform-record-test.io"
   310      region = "ny"
   311  
   312      // meta {
   313      //   weight = 5
   314      //   up = true
   315      // }
   316    }
   317  
   318    regions {
   319      name = "wa"
   320      // meta {
   321      //   us_state = ["WA"]
   322      // }
   323    }
   324  
   325    regions {
   326      name = "ny"
   327      // meta {
   328      //   us_state = ["NY"]
   329      // }
   330    }
   331  
   332    filters {
   333      filter = "up"
   334    }
   335  
   336    filters {
   337      filter = "geotarget_country"
   338    }
   339  }
   340  
   341  resource "ns1_zone" "test" {
   342    zone = "terraform-record-test.io"
   343  }
   344  `
   345  
   346  const testAccRecordSPF = `
   347  resource "ns1_record" "spf" {
   348    zone              = "${ns1_zone.test.zone}"
   349    domain            = "${ns1_zone.test.zone}"
   350    type              = "SPF"
   351    ttl               = 86400
   352    use_client_subnet = "true"
   353    answers = {
   354      answer = "v=DKIM1; k=rsa; p=XXXXXXXX"
   355    }
   356  }
   357  
   358  resource "ns1_zone" "test" {
   359    zone = "terraform-record-test.io"
   360  }
   361  `
   362  
   363  const testAccRecordSRV = `
   364  resource "ns1_record" "srv" {
   365    zone              = "${ns1_zone.test.zone}"
   366    domain            = "_some-server._tcp.${ns1_zone.test.zone}"
   367    type              = "SRV"
   368    ttl               = 86400
   369    use_client_subnet = "true"
   370    answers {
   371      answer = "10 0 2380 node-1.${ns1_zone.test.zone}"
   372    }
   373  }
   374  
   375  resource "ns1_zone" "test" {
   376    zone = "terraform-record-test.io"
   377  }
   378  `