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

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/acctest"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"google.golang.org/api/dns/v1"
    11  )
    12  
    13  func TestAccDnsManagedZone_basic(t *testing.T) {
    14  	var zone dns.ManagedZone
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckDnsManagedZoneDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccDnsManagedZone_basic,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckDnsManagedZoneExists(
    25  						"google_dns_managed_zone.foobar", &zone),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func testAccCheckDnsManagedZoneDestroy(s *terraform.State) error {
    33  	config := testAccProvider.Meta().(*Config)
    34  
    35  	for _, rs := range s.RootModule().Resources {
    36  		if rs.Type != "google_dns_zone" {
    37  			continue
    38  		}
    39  
    40  		_, err := config.clientDns.ManagedZones.Get(
    41  			config.Project, rs.Primary.ID).Do()
    42  		if err == nil {
    43  			return fmt.Errorf("DNS ManagedZone still exists")
    44  		}
    45  	}
    46  
    47  	return nil
    48  }
    49  
    50  func testAccCheckDnsManagedZoneExists(n string, zone *dns.ManagedZone) resource.TestCheckFunc {
    51  	return func(s *terraform.State) error {
    52  		rs, ok := s.RootModule().Resources[n]
    53  		if !ok {
    54  			return fmt.Errorf("Not found: %s", n)
    55  		}
    56  
    57  		if rs.Primary.ID == "" {
    58  			return fmt.Errorf("No ID is set")
    59  		}
    60  
    61  		config := testAccProvider.Meta().(*Config)
    62  
    63  		found, err := config.clientDns.ManagedZones.Get(
    64  			config.Project, rs.Primary.ID).Do()
    65  		if err != nil {
    66  			return err
    67  		}
    68  
    69  		if found.Name != rs.Primary.ID {
    70  			return fmt.Errorf("DNS Zone not found")
    71  		}
    72  
    73  		*zone = *found
    74  
    75  		return nil
    76  	}
    77  }
    78  
    79  var testAccDnsManagedZone_basic = fmt.Sprintf(`
    80  resource "google_dns_managed_zone" "foobar" {
    81  	name = "mzone-test-%s"
    82  	dns_name = "hashicorptest.com."
    83  }`, acctest.RandString(10))