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