github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/google/resource_compute_forwarding_rule_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  )
    10  
    11  func TestAccComputeForwardingRule_basic(t *testing.T) {
    12  
    13  	resource.Test(t, resource.TestCase{
    14  		PreCheck:     func() { testAccPreCheck(t) },
    15  		Providers:    testAccProviders,
    16  		CheckDestroy: testAccCheckComputeForwardingRuleDestroy,
    17  		Steps: []resource.TestStep{
    18  			resource.TestStep{
    19  				Config: testAccComputeForwardingRule_basic,
    20  				Check: resource.ComposeTestCheckFunc(
    21  					testAccCheckComputeForwardingRuleExists(
    22  						"google_compute_forwarding_rule.foobar"),
    23  				),
    24  			},
    25  		},
    26  	})
    27  }
    28  
    29  func TestAccComputeForwardingRule_ip(t *testing.T) {
    30  
    31  	resource.Test(t, resource.TestCase{
    32  		PreCheck:     func() { testAccPreCheck(t) },
    33  		Providers:    testAccProviders,
    34  		CheckDestroy: testAccCheckComputeForwardingRuleDestroy,
    35  		Steps: []resource.TestStep{
    36  			resource.TestStep{
    37  				Config: testAccComputeForwardingRule_ip,
    38  				Check: resource.ComposeTestCheckFunc(
    39  					testAccCheckComputeForwardingRuleExists(
    40  						"google_compute_forwarding_rule.foobar"),
    41  				),
    42  			},
    43  		},
    44  	})
    45  }
    46  
    47  func testAccCheckComputeForwardingRuleDestroy(s *terraform.State) error {
    48  	config := testAccProvider.Meta().(*Config)
    49  
    50  	for _, rs := range s.RootModule().Resources {
    51  		if rs.Type != "google_compute_forwarding_rule" {
    52  			continue
    53  		}
    54  
    55  		_, err := config.clientCompute.ForwardingRules.Get(
    56  			config.Project, config.Region, rs.Primary.ID).Do()
    57  		if err == nil {
    58  			return fmt.Errorf("ForwardingRule still exists")
    59  		}
    60  	}
    61  
    62  	return nil
    63  }
    64  
    65  func testAccCheckComputeForwardingRuleExists(n string) resource.TestCheckFunc {
    66  	return func(s *terraform.State) error {
    67  		rs, ok := s.RootModule().Resources[n]
    68  		if !ok {
    69  			return fmt.Errorf("Not found: %s", n)
    70  		}
    71  
    72  		if rs.Primary.ID == "" {
    73  			return fmt.Errorf("No ID is set")
    74  		}
    75  
    76  		config := testAccProvider.Meta().(*Config)
    77  
    78  		found, err := config.clientCompute.ForwardingRules.Get(
    79  			config.Project, config.Region, rs.Primary.ID).Do()
    80  		if err != nil {
    81  			return err
    82  		}
    83  
    84  		if found.Name != rs.Primary.ID {
    85  			return fmt.Errorf("ForwardingRule not found")
    86  		}
    87  
    88  		return nil
    89  	}
    90  }
    91  
    92  const testAccComputeForwardingRule_basic = `
    93  resource "google_compute_target_pool" "foobar-tp" {
    94  	description = "Resource created for Terraform acceptance testing"
    95  	instances = ["us-central1-a/foo", "us-central1-b/bar"]
    96  	name = "terraform-test"
    97  }
    98  resource "google_compute_forwarding_rule" "foobar" {
    99  	description = "Resource created for Terraform acceptance testing"
   100  	ip_protocol = "UDP"
   101      name = "terraform-test"
   102      port_range = "80-81"
   103      target = "${google_compute_target_pool.foobar-tp.self_link}"
   104  }
   105  `
   106  
   107  const testAccComputeForwardingRule_ip = `
   108  resource "google_compute_address" "foo" {
   109      name = "foo"
   110  }
   111  resource "google_compute_target_pool" "foobar-tp" {
   112  	description = "Resource created for Terraform acceptance testing"
   113  	instances = ["us-central1-a/foo", "us-central1-b/bar"]
   114  	name = "terraform-test"
   115  }
   116  resource "google_compute_forwarding_rule" "foobar" {
   117  	description = "Resource created for Terraform acceptance testing"
   118  	ip_address = "${google_compute_address.foo.address}"
   119  	ip_protocol = "TCP"
   120      name = "terraform-test"
   121      port_range = "80-81"
   122      target = "${google_compute_target_pool.foobar-tp.self_link}"
   123  }
   124  `