github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/ns1/resource_zone_test.go (about)

     1  package ns1
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  
    10  	ns1 "gopkg.in/ns1/ns1-go.v2/rest"
    11  	"gopkg.in/ns1/ns1-go.v2/rest/model/dns"
    12  )
    13  
    14  func TestAccZone_basic(t *testing.T) {
    15  	var zone dns.Zone
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckZoneDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccZoneBasic,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckZoneExists("ns1_zone.it", &zone),
    25  					testAccCheckZoneName(&zone, "terraform-test-zone.io"),
    26  					testAccCheckZoneTTL(&zone, 3600),
    27  					testAccCheckZoneRefresh(&zone, 43200),
    28  					testAccCheckZoneRetry(&zone, 7200),
    29  					testAccCheckZoneExpiry(&zone, 1209600),
    30  					testAccCheckZoneNxTTL(&zone, 3600),
    31  				),
    32  			},
    33  		},
    34  	})
    35  }
    36  
    37  func TestAccZone_updated(t *testing.T) {
    38  	var zone dns.Zone
    39  	resource.Test(t, resource.TestCase{
    40  		PreCheck:     func() { testAccPreCheck(t) },
    41  		Providers:    testAccProviders,
    42  		CheckDestroy: testAccCheckZoneDestroy,
    43  		Steps: []resource.TestStep{
    44  			resource.TestStep{
    45  				Config: testAccZoneBasic,
    46  				Check: resource.ComposeTestCheckFunc(
    47  					testAccCheckZoneExists("ns1_zone.it", &zone),
    48  					testAccCheckZoneName(&zone, "terraform-test-zone.io"),
    49  					testAccCheckZoneTTL(&zone, 3600),
    50  					testAccCheckZoneRefresh(&zone, 43200),
    51  					testAccCheckZoneRetry(&zone, 7200),
    52  					testAccCheckZoneExpiry(&zone, 1209600),
    53  					testAccCheckZoneNxTTL(&zone, 3600),
    54  				),
    55  			},
    56  			resource.TestStep{
    57  				Config: testAccZoneUpdated,
    58  				Check: resource.ComposeTestCheckFunc(
    59  					testAccCheckZoneExists("ns1_zone.it", &zone),
    60  					testAccCheckZoneName(&zone, "terraform-test-zone.io"),
    61  					testAccCheckZoneTTL(&zone, 10800),
    62  					testAccCheckZoneRefresh(&zone, 3600),
    63  					testAccCheckZoneRetry(&zone, 300),
    64  					testAccCheckZoneExpiry(&zone, 2592000),
    65  					testAccCheckZoneNxTTL(&zone, 3601),
    66  				),
    67  			},
    68  		},
    69  	})
    70  }
    71  
    72  func testAccCheckZoneExists(n string, zone *dns.Zone) resource.TestCheckFunc {
    73  	return func(s *terraform.State) error {
    74  		rs, ok := s.RootModule().Resources[n]
    75  
    76  		if !ok {
    77  			return fmt.Errorf("Not found: %s", n)
    78  		}
    79  
    80  		if rs.Primary.ID == "" {
    81  			return fmt.Errorf("NoID is set")
    82  		}
    83  
    84  		client := testAccProvider.Meta().(*ns1.Client)
    85  
    86  		foundZone, _, err := client.Zones.Get(rs.Primary.Attributes["zone"])
    87  
    88  		p := rs.Primary
    89  
    90  		if err != nil {
    91  			return err
    92  		}
    93  
    94  		if foundZone.ID != p.Attributes["id"] {
    95  			return fmt.Errorf("Zone not found")
    96  		}
    97  
    98  		*zone = *foundZone
    99  
   100  		return nil
   101  	}
   102  }
   103  
   104  func testAccCheckZoneDestroy(s *terraform.State) error {
   105  	client := testAccProvider.Meta().(*ns1.Client)
   106  
   107  	for _, rs := range s.RootModule().Resources {
   108  		if rs.Type != "ns1_zone" {
   109  			continue
   110  		}
   111  
   112  		zone, _, err := client.Zones.Get(rs.Primary.Attributes["zone"])
   113  
   114  		if err == nil {
   115  			return fmt.Errorf("Zone still exists: %#v: %#v", err, zone)
   116  		}
   117  	}
   118  
   119  	return nil
   120  }
   121  
   122  func testAccCheckZoneName(zone *dns.Zone, expected string) resource.TestCheckFunc {
   123  	return func(s *terraform.State) error {
   124  		if zone.Zone != expected {
   125  			return fmt.Errorf("Zone: got: %s want: %s", zone.Zone, expected)
   126  		}
   127  		return nil
   128  	}
   129  }
   130  
   131  func testAccCheckZoneTTL(zone *dns.Zone, expected int) resource.TestCheckFunc {
   132  	return func(s *terraform.State) error {
   133  		if zone.TTL != expected {
   134  			return fmt.Errorf("TTL: got: %d want: %d", zone.TTL, expected)
   135  		}
   136  		return nil
   137  	}
   138  }
   139  func testAccCheckZoneRefresh(zone *dns.Zone, expected int) resource.TestCheckFunc {
   140  	return func(s *terraform.State) error {
   141  		if zone.Refresh != expected {
   142  			return fmt.Errorf("Refresh: got: %d want: %d", zone.Refresh, expected)
   143  		}
   144  		return nil
   145  	}
   146  }
   147  func testAccCheckZoneRetry(zone *dns.Zone, expected int) resource.TestCheckFunc {
   148  	return func(s *terraform.State) error {
   149  		if zone.Retry != expected {
   150  			return fmt.Errorf("Retry: got: %d want: %d", zone.Retry, expected)
   151  		}
   152  		return nil
   153  	}
   154  }
   155  func testAccCheckZoneExpiry(zone *dns.Zone, expected int) resource.TestCheckFunc {
   156  	return func(s *terraform.State) error {
   157  		if zone.Expiry != expected {
   158  			return fmt.Errorf("Expiry: got: %d want: %d", zone.Expiry, expected)
   159  		}
   160  		return nil
   161  	}
   162  }
   163  func testAccCheckZoneNxTTL(zone *dns.Zone, expected int) resource.TestCheckFunc {
   164  	return func(s *terraform.State) error {
   165  		if zone.NxTTL != expected {
   166  			return fmt.Errorf("NxTTL: got: %d want: %d", zone.NxTTL, expected)
   167  		}
   168  		return nil
   169  	}
   170  }
   171  
   172  const testAccZoneBasic = `
   173  resource "ns1_zone" "it" {
   174    zone = "terraform-test-zone.io"
   175  }
   176  `
   177  
   178  const testAccZoneUpdated = `
   179  resource "ns1_zone" "it" {
   180    zone    = "terraform-test-zone.io"
   181    ttl     = 10800
   182    refresh = 3600
   183    retry   = 300
   184    expiry  = 2592000
   185    nx_ttl  = 3601
   186    # link    = "1.2.3.4.in-addr.arpa" # TODO
   187    # primary = "1.2.3.4.in-addr.arpa" # TODO
   188  }
   189  `