github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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, "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, "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, "test2.terraform-record-test.io"),
    68  				),
    69  			},
    70  		},
    71  	})
    72  }
    73  
    74  func testAccCheckRecordExists(n string, record *dns.Record) resource.TestCheckFunc {
    75  	return func(s *terraform.State) error {
    76  		rs, ok := s.RootModule().Resources[n]
    77  		if !ok {
    78  			return fmt.Errorf("Not found: %v", n)
    79  		}
    80  
    81  		if rs.Primary.ID == "" {
    82  			return fmt.Errorf("NoID is set")
    83  		}
    84  
    85  		client := testAccProvider.Meta().(*ns1.Client)
    86  
    87  		p := rs.Primary
    88  
    89  		foundRecord, _, err := client.Records.Get(p.Attributes["zone"], p.Attributes["domain"], p.Attributes["type"])
    90  		if err != nil {
    91  			return fmt.Errorf("Record not found")
    92  		}
    93  
    94  		if foundRecord.Domain != p.Attributes["domain"] {
    95  			return fmt.Errorf("Record not found")
    96  		}
    97  
    98  		*record = *foundRecord
    99  
   100  		return nil
   101  	}
   102  }
   103  
   104  func testAccCheckRecordDestroy(s *terraform.State) error {
   105  	client := testAccProvider.Meta().(*ns1.Client)
   106  
   107  	var recordDomain string
   108  	var recordZone string
   109  	var recordType string
   110  
   111  	for _, rs := range s.RootModule().Resources {
   112  		if rs.Type != "ns1_record" {
   113  			continue
   114  		}
   115  
   116  		if rs.Type == "ns1_record" {
   117  			recordType = rs.Primary.Attributes["type"]
   118  			recordDomain = rs.Primary.Attributes["domain"]
   119  			recordZone = rs.Primary.Attributes["zone"]
   120  		}
   121  	}
   122  
   123  	foundRecord, _, err := client.Records.Get(recordDomain, recordZone, recordType)
   124  	if err != nil {
   125  		return fmt.Errorf("Record still exists: %#v", foundRecord)
   126  	}
   127  
   128  	return nil
   129  }
   130  
   131  func testAccCheckRecordDomain(r *dns.Record, expected string) resource.TestCheckFunc {
   132  	return func(s *terraform.State) error {
   133  		if r.Domain != expected {
   134  			return fmt.Errorf("Domain: got: %#v want: %#v", r.Domain, expected)
   135  		}
   136  		return nil
   137  	}
   138  }
   139  
   140  func testAccCheckRecordTTL(r *dns.Record, expected int) resource.TestCheckFunc {
   141  	return func(s *terraform.State) error {
   142  		if r.TTL != expected {
   143  			return fmt.Errorf("TTL: got: %#v want: %#v", r.TTL, expected)
   144  		}
   145  		return nil
   146  	}
   147  }
   148  
   149  func testAccCheckRecordUseClientSubnet(r *dns.Record, expected bool) resource.TestCheckFunc {
   150  	return func(s *terraform.State) error {
   151  		if *r.UseClientSubnet != expected {
   152  			return fmt.Errorf("UseClientSubnet: got: %#v want: %#v", *r.UseClientSubnet, expected)
   153  		}
   154  		return nil
   155  	}
   156  }
   157  
   158  func testAccCheckRecordRegionName(r *dns.Record, expected []string) resource.TestCheckFunc {
   159  	return func(s *terraform.State) error {
   160  		regions := make([]string, len(r.Regions))
   161  
   162  		i := 0
   163  		for k := range r.Regions {
   164  			regions[i] = k
   165  			i++
   166  		}
   167  		sort.Strings(regions)
   168  		sort.Strings(expected)
   169  		if !reflect.DeepEqual(regions, expected) {
   170  			return fmt.Errorf("Regions: got: %#v want: %#v", regions, expected)
   171  		}
   172  		return nil
   173  	}
   174  }
   175  
   176  func testAccCheckRecordAnswerMetaWeight(r *dns.Record, expected float64) resource.TestCheckFunc {
   177  	return func(s *terraform.State) error {
   178  		recordAnswer := r.Answers[0]
   179  		recordMetas := recordAnswer.Meta
   180  		weight := recordMetas.Weight.(float64)
   181  		if weight != expected {
   182  			return fmt.Errorf("Answers[0].Meta.Weight: got: %#v want: %#v", weight, expected)
   183  		}
   184  		return nil
   185  	}
   186  }
   187  
   188  func testAccCheckRecordAnswerRdata(r *dns.Record, expected string) resource.TestCheckFunc {
   189  	return func(s *terraform.State) error {
   190  		recordAnswer := r.Answers[0]
   191  		recordAnswerString := recordAnswer.Rdata[0]
   192  		if recordAnswerString != expected {
   193  			return fmt.Errorf("Answers[0].Rdata[0]: got: %#v want: %#v", recordAnswerString, expected)
   194  		}
   195  		return nil
   196  	}
   197  }
   198  
   199  const testAccRecordBasic = `
   200  resource "ns1_record" "it" {
   201    zone              = "${ns1_zone.test.zone}"
   202    domain            = "test.${ns1_zone.test.zone}"
   203    type              = "CNAME"
   204    ttl               = 60
   205  
   206    // meta {
   207    //   weight = 5
   208    //   connections = 3
   209    //   // up = false // Ignored by d.GetOk("meta.0.up") due to known issue
   210    // }
   211  
   212    answers {
   213      answer = "test1.terraform-record-test.io"
   214      region = "cal"
   215  
   216      // meta {
   217      //   weight = 10
   218      //   up = true
   219      // }
   220    }
   221  
   222    regions {
   223      name = "cal"
   224      // meta {
   225      //   up = true
   226      //   us_state = ["CA"]
   227      // }
   228    }
   229  
   230    filters {
   231      filter = "up"
   232    }
   233  
   234    filters {
   235      filter = "geotarget_country"
   236    }
   237  
   238    filters {
   239      filter = "select_first_n"
   240      config = {N=1}
   241    }
   242  }
   243  
   244  resource "ns1_zone" "test" {
   245    zone = "terraform-record-test.io"
   246  }
   247  `
   248  
   249  const testAccRecordUpdated = `
   250  resource "ns1_record" "it" {
   251    zone              = "${ns1_zone.test.zone}"
   252    domain            = "test.${ns1_zone.test.zone}"
   253    type              = "CNAME"
   254    ttl               = 120
   255    use_client_subnet = false
   256  
   257    // meta {
   258    //   weight = 5
   259    //   connections = 3
   260    //   // up = false // Ignored by d.GetOk("meta.0.up") due to known issue
   261    // }
   262  
   263    answers {
   264      answer = "test2.terraform-record-test.io"
   265      region = "ny"
   266  
   267      // meta {
   268      //   weight = 5
   269      //   up = true
   270      // }
   271    }
   272  
   273    regions {
   274      name = "wa"
   275      // meta {
   276      //   us_state = ["WA"]
   277      // }
   278    }
   279  
   280    regions {
   281      name = "ny"
   282      // meta {
   283      //   us_state = ["NY"]
   284      // }
   285    }
   286  
   287    filters {
   288      filter = "up"
   289    }
   290  
   291    filters {
   292      filter = "geotarget_country"
   293    }
   294  }
   295  
   296  resource "ns1_zone" "test" {
   297    zone = "terraform-record-test.io"
   298  }
   299  `