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