github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/google/resource_compute_global_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 TestAccComputeGlobalForwardingRule_basic(t *testing.T) {
    12  	resource.Test(t, resource.TestCase{
    13  		PreCheck:     func() { testAccPreCheck(t) },
    14  		Providers:    testAccProviders,
    15  		CheckDestroy: testAccCheckComputeGlobalForwardingRuleDestroy,
    16  		Steps: []resource.TestStep{
    17  			resource.TestStep{
    18  				Config: testAccComputeGlobalForwardingRule_basic1,
    19  				Check: resource.ComposeTestCheckFunc(
    20  					testAccCheckComputeGlobalForwardingRuleExists(
    21  						"google_compute_global_forwarding_rule.foobar"),
    22  				),
    23  			},
    24  		},
    25  	})
    26  }
    27  
    28  func TestAccComputeGlobalForwardingRule_update(t *testing.T) {
    29  	resource.Test(t, resource.TestCase{
    30  		PreCheck:     func() { testAccPreCheck(t) },
    31  		Providers:    testAccProviders,
    32  		CheckDestroy: testAccCheckComputeGlobalForwardingRuleDestroy,
    33  		Steps: []resource.TestStep{
    34  			resource.TestStep{
    35  				Config: testAccComputeGlobalForwardingRule_basic1,
    36  				Check: resource.ComposeTestCheckFunc(
    37  					testAccCheckComputeGlobalForwardingRuleExists(
    38  						"google_compute_global_forwarding_rule.foobar"),
    39  				),
    40  			},
    41  
    42  			resource.TestStep{
    43  				Config: testAccComputeGlobalForwardingRule_basic2,
    44  				Check: resource.ComposeTestCheckFunc(
    45  					testAccCheckComputeGlobalForwardingRuleExists(
    46  						"google_compute_global_forwarding_rule.foobar"),
    47  				),
    48  			},
    49  		},
    50  	})
    51  }
    52  
    53  func testAccCheckComputeGlobalForwardingRuleDestroy(s *terraform.State) error {
    54  	config := testAccProvider.Meta().(*Config)
    55  
    56  	for _, rs := range s.RootModule().Resources {
    57  		if rs.Type != "google_compute_global_forwarding_rule" {
    58  			continue
    59  		}
    60  
    61  		_, err := config.clientCompute.GlobalForwardingRules.Get(
    62  			config.Project, rs.Primary.ID).Do()
    63  		if err == nil {
    64  			return fmt.Errorf("Global Forwarding Rule still exists")
    65  		}
    66  	}
    67  
    68  	return nil
    69  }
    70  
    71  func testAccCheckComputeGlobalForwardingRuleExists(n string) resource.TestCheckFunc {
    72  	return func(s *terraform.State) error {
    73  		rs, ok := s.RootModule().Resources[n]
    74  		if !ok {
    75  			return fmt.Errorf("Not found: %s", n)
    76  		}
    77  
    78  		if rs.Primary.ID == "" {
    79  			return fmt.Errorf("No ID is set")
    80  		}
    81  
    82  		config := testAccProvider.Meta().(*Config)
    83  
    84  		found, err := config.clientCompute.GlobalForwardingRules.Get(
    85  			config.Project, rs.Primary.ID).Do()
    86  		if err != nil {
    87  			return err
    88  		}
    89  
    90  		if found.Name != rs.Primary.ID {
    91  			return fmt.Errorf("Global Forwarding Rule not found")
    92  		}
    93  
    94  		return nil
    95  	}
    96  }
    97  
    98  const testAccComputeGlobalForwardingRule_basic1 = `
    99  resource "google_compute_global_forwarding_rule" "foobar" {
   100  	description = "Resource created for Terraform acceptance testing"
   101  	ip_protocol = "TCP"
   102  	name = "terraform-test"
   103  	port_range = "80"
   104  	target = "${google_compute_target_http_proxy.foobar1.self_link}"
   105  }
   106  
   107  resource "google_compute_target_http_proxy" "foobar1" {
   108  	description = "Resource created for Terraform acceptance testing"
   109  	name = "terraform-test1"
   110  	url_map = "${google_compute_url_map.foobar.self_link}"
   111  }
   112  
   113  resource "google_compute_target_http_proxy" "foobar2" {
   114  	description = "Resource created for Terraform acceptance testing"
   115  	name = "terraform-test2"
   116  	url_map = "${google_compute_url_map.foobar.self_link}"
   117  }
   118  
   119  resource "google_compute_backend_service" "foobar" {
   120  	name = "service"
   121  	health_checks = ["${google_compute_http_health_check.zero.self_link}"]
   122  }
   123  
   124  resource "google_compute_http_health_check" "zero" {
   125  	name = "tf-test-zero"
   126  	request_path = "/"
   127  	check_interval_sec = 1
   128  	timeout_sec = 1
   129  }
   130  
   131  resource "google_compute_url_map" "foobar" {
   132  	name = "myurlmap"
   133  	default_service = "${google_compute_backend_service.foobar.self_link}"
   134  	host_rule {
   135  		hosts = ["mysite.com", "myothersite.com"]
   136  		path_matcher = "boop"
   137  	}
   138  	path_matcher {
   139  		default_service = "${google_compute_backend_service.foobar.self_link}"
   140  		name = "boop"
   141  		path_rule {
   142  			paths = ["/*"]
   143  			service = "${google_compute_backend_service.foobar.self_link}"
   144  		}
   145  	}
   146  	test {
   147  		host = "mysite.com"
   148  		path = "/*"
   149  		service = "${google_compute_backend_service.foobar.self_link}"
   150  	}
   151  }
   152  `
   153  
   154  const testAccComputeGlobalForwardingRule_basic2 = `
   155  resource "google_compute_global_forwarding_rule" "foobar" {
   156  	description = "Resource created for Terraform acceptance testing"
   157  	ip_protocol = "TCP"
   158  	name = "terraform-test"
   159  	port_range = "80"
   160  	target = "${google_compute_target_http_proxy.foobar2.self_link}"
   161  }
   162  
   163  resource "google_compute_target_http_proxy" "foobar1" {
   164  	description = "Resource created for Terraform acceptance testing"
   165  	name = "terraform-test1"
   166  	url_map = "${google_compute_url_map.foobar.self_link}"
   167  }
   168  
   169  resource "google_compute_target_http_proxy" "foobar2" {
   170  	description = "Resource created for Terraform acceptance testing"
   171  	name = "terraform-test2"
   172  	url_map = "${google_compute_url_map.foobar.self_link}"
   173  }
   174  
   175  resource "google_compute_backend_service" "foobar" {
   176  	name = "service"
   177  	health_checks = ["${google_compute_http_health_check.zero.self_link}"]
   178  }
   179  
   180  resource "google_compute_http_health_check" "zero" {
   181  	name = "tf-test-zero"
   182  	request_path = "/"
   183  	check_interval_sec = 1
   184  	timeout_sec = 1
   185  }
   186  
   187  resource "google_compute_url_map" "foobar" {
   188  	name = "myurlmap"
   189  	default_service = "${google_compute_backend_service.foobar.self_link}"
   190  	host_rule {
   191  		hosts = ["mysite.com", "myothersite.com"]
   192  		path_matcher = "boop"
   193  	}
   194  	path_matcher {
   195  		default_service = "${google_compute_backend_service.foobar.self_link}"
   196  		name = "boop"
   197  		path_rule {
   198  			paths = ["/*"]
   199  			service = "${google_compute_backend_service.foobar.self_link}"
   200  		}
   201  	}
   202  	test {
   203  		host = "mysite.com"
   204  		path = "/*"
   205  		service = "${google_compute_backend_service.foobar.self_link}"
   206  	}
   207  }
   208  `