From e7ebb0ac40181d5e3e95adef8c772fb9e0c8921b Mon Sep 17 00:00:00 2001 From: Lars Wander Date: Sun, 1 Nov 2015 10:39:08 -0500 Subject: [PATCH] provider/google: target http proxies resource + tests & documentation --- builtin/providers/google/provider.go | 1 + .../resource_compute_target_http_proxy.go | 147 ++++++++++++ ...resource_compute_target_http_proxy_test.go | 226 ++++++++++++++++++ .../r/compute_target_http_proxy.html.markdown | 80 +++++++ website/source/layouts/google.erb | 4 + 5 files changed, 458 insertions(+) create mode 100644 builtin/providers/google/resource_compute_target_http_proxy.go create mode 100644 builtin/providers/google/resource_compute_target_http_proxy_test.go create mode 100644 website/source/docs/providers/google/r/compute_target_http_proxy.html.markdown diff --git a/builtin/providers/google/provider.go b/builtin/providers/google/provider.go index f1d5c17ce..6b9b88639 100644 --- a/builtin/providers/google/provider.go +++ b/builtin/providers/google/provider.go @@ -49,6 +49,7 @@ func Provider() terraform.ResourceProvider { "google_compute_project_metadata": resourceComputeProjectMetadata(), "google_compute_route": resourceComputeRoute(), "google_compute_ssl_certificate": resourceComputeSslCertificate(), + "google_compute_target_http_proxy": resourceComputeTargetHttpProxy(), "google_compute_target_pool": resourceComputeTargetPool(), "google_compute_url_map": resourceComputeUrlMap(), "google_compute_vpn_gateway": resourceComputeVpnGateway(), diff --git a/builtin/providers/google/resource_compute_target_http_proxy.go b/builtin/providers/google/resource_compute_target_http_proxy.go new file mode 100644 index 000000000..6cf2ccf5d --- /dev/null +++ b/builtin/providers/google/resource_compute_target_http_proxy.go @@ -0,0 +1,147 @@ +package google + +import ( + "fmt" + "log" + "strconv" + + "github.com/hashicorp/terraform/helper/schema" + "google.golang.org/api/compute/v1" + "google.golang.org/api/googleapi" +) + +func resourceComputeTargetHttpProxy() *schema.Resource { + return &schema.Resource{ + Create: resourceComputeTargetHttpProxyCreate, + Read: resourceComputeTargetHttpProxyRead, + Delete: resourceComputeTargetHttpProxyDelete, + Update: resourceComputeTargetHttpProxyUpdate, + + Schema: map[string]*schema.Schema{ + "name": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "description": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + + "self_link": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + + "id": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + + "url_map": &schema.Schema{ + Type: schema.TypeString, + Required: true, + }, + }, + } +} + +func resourceComputeTargetHttpProxyCreate(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + + proxy := &compute.TargetHttpProxy{ + Name: d.Get("name").(string), + UrlMap: d.Get("url_map").(string), + } + + if v, ok := d.GetOk("description"); ok { + proxy.Description = v.(string) + } + + log.Printf("[DEBUG] TargetHttpProxy insert request: %#v", proxy) + op, err := config.clientCompute.TargetHttpProxies.Insert( + config.Project, proxy).Do() + if err != nil { + return fmt.Errorf("Error creating TargetHttpProxy: %s", err) + } + + err = computeOperationWaitGlobal(config, op, "Creating Target Http Proxy") + if err != nil { + return err + } + + d.SetId(proxy.Name) + + return resourceComputeTargetHttpProxyRead(d, meta) +} + +func resourceComputeTargetHttpProxyUpdate(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + + d.Partial(true) + + if d.HasChange("url_map") { + url_map := d.Get("url_map").(string) + url_map_ref := &compute.UrlMapReference{UrlMap: url_map} + op, err := config.clientCompute.TargetHttpProxies.SetUrlMap( + config.Project, d.Id(), url_map_ref).Do() + if err != nil { + return fmt.Errorf("Error updating target: %s", err) + } + + err = computeOperationWaitGlobal(config, op, "Updating Target Http Proxy") + if err != nil { + return err + } + + d.SetPartial("url_map") + } + + d.Partial(false) + + return resourceComputeTargetHttpProxyRead(d, meta) +} + +func resourceComputeTargetHttpProxyRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + + proxy, err := config.clientCompute.TargetHttpProxies.Get( + config.Project, d.Id()).Do() + if err != nil { + if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { + // The resource doesn't exist anymore + d.SetId("") + + return nil + } + + return fmt.Errorf("Error reading TargetHttpProxy: %s", err) + } + + d.Set("self_link", proxy.SelfLink) + d.Set("id", strconv.FormatUint(proxy.Id, 10)) + + return nil +} + +func resourceComputeTargetHttpProxyDelete(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + + // Delete the TargetHttpProxy + log.Printf("[DEBUG] TargetHttpProxy delete request") + op, err := config.clientCompute.TargetHttpProxies.Delete( + config.Project, d.Id()).Do() + if err != nil { + return fmt.Errorf("Error deleting TargetHttpProxy: %s", err) + } + + err = computeOperationWaitGlobal(config, op, "Deleting Target Http Proxy") + if err != nil { + return err + } + + d.SetId("") + return nil +} diff --git a/builtin/providers/google/resource_compute_target_http_proxy_test.go b/builtin/providers/google/resource_compute_target_http_proxy_test.go new file mode 100644 index 000000000..6337ada57 --- /dev/null +++ b/builtin/providers/google/resource_compute_target_http_proxy_test.go @@ -0,0 +1,226 @@ +package google + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccComputeTargetHttpProxy_basic(t *testing.T) { + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckComputeTargetHttpProxyDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccComputeTargetHttpProxy_basic1, + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeTargetHttpProxyExists( + "google_compute_target_http_proxy.foobar"), + ), + }, + }, + }) +} + +func TestAccComputeTargetHttpProxy_update(t *testing.T) { + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckComputeTargetHttpProxyDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccComputeTargetHttpProxy_basic1, + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeTargetHttpProxyExists( + "google_compute_target_http_proxy.foobar"), + ), + }, + + resource.TestStep{ + Config: testAccComputeTargetHttpProxy_basic2, + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeTargetHttpProxyExists( + "google_compute_target_http_proxy.foobar"), + ), + }, + }, + }) +} + +func testAccCheckComputeTargetHttpProxyDestroy(s *terraform.State) error { + config := testAccProvider.Meta().(*Config) + + for _, rs := range s.RootModule().Resources { + if rs.Type != "google_compute_target_http_proxy" { + continue + } + + _, err := config.clientCompute.TargetHttpProxies.Get( + config.Project, rs.Primary.ID).Do() + if err == nil { + return fmt.Errorf("TargetHttpProxy still exists") + } + } + + return nil +} + +func testAccCheckComputeTargetHttpProxyExists(n string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No ID is set") + } + + config := testAccProvider.Meta().(*Config) + + found, err := config.clientCompute.TargetHttpProxies.Get( + config.Project, rs.Primary.ID).Do() + if err != nil { + return err + } + + if found.Name != rs.Primary.ID { + return fmt.Errorf("TargetHttpProxy not found") + } + + return nil + } +} + +const testAccComputeTargetHttpProxy_basic1 = ` +resource "google_compute_target_http_proxy" "foobar" { + description = "Resource created for Terraform acceptance testing" + name = "terraform-test" + url_map = "${google_compute_url_map.foobar1.self_link}" +} + +resource "google_compute_backend_service" "foobar" { + name = "service" + health_checks = ["${google_compute_http_health_check.zero.self_link}"] +} + +resource "google_compute_http_health_check" "zero" { + name = "tf-test-zero" + request_path = "/" + check_interval_sec = 1 + timeout_sec = 1 +} + +resource "google_compute_url_map" "foobar1" { + name = "myurlmap1" + default_service = "${google_compute_backend_service.foobar.self_link}" + host_rule { + hosts = ["mysite.com", "myothersite.com"] + path_matcher = "boop" + } + path_matcher { + default_service = "${google_compute_backend_service.foobar.self_link}" + name = "boop" + path_rule { + paths = ["/*"] + service = "${google_compute_backend_service.foobar.self_link}" + } + } + test { + host = "mysite.com" + path = "/*" + service = "${google_compute_backend_service.foobar.self_link}" + } +} + +resource "google_compute_url_map" "foobar2" { + name = "myurlmap2" + default_service = "${google_compute_backend_service.foobar.self_link}" + host_rule { + hosts = ["mysite.com", "myothersite.com"] + path_matcher = "boop" + } + path_matcher { + default_service = "${google_compute_backend_service.foobar.self_link}" + name = "boop" + path_rule { + paths = ["/*"] + service = "${google_compute_backend_service.foobar.self_link}" + } + } + test { + host = "mysite.com" + path = "/*" + service = "${google_compute_backend_service.foobar.self_link}" + } +} +` + +const testAccComputeTargetHttpProxy_basic2 = ` +resource "google_compute_target_http_proxy" "foobar" { + description = "Resource created for Terraform acceptance testing" + name = "terraform-test" + url_map = "${google_compute_url_map.foobar2.self_link}" +} + +resource "google_compute_backend_service" "foobar" { + name = "service" + health_checks = ["${google_compute_http_health_check.zero.self_link}"] +} + +resource "google_compute_http_health_check" "zero" { + name = "tf-test-zero" + request_path = "/" + check_interval_sec = 1 + timeout_sec = 1 +} + +resource "google_compute_url_map" "foobar1" { + name = "myurlmap1" + default_service = "${google_compute_backend_service.foobar.self_link}" + host_rule { + hosts = ["mysite.com", "myothersite.com"] + path_matcher = "boop" + } + path_matcher { + default_service = "${google_compute_backend_service.foobar.self_link}" + name = "boop" + path_rule { + paths = ["/*"] + service = "${google_compute_backend_service.foobar.self_link}" + } + } + test { + host = "mysite.com" + path = "/*" + service = "${google_compute_backend_service.foobar.self_link}" + } +} + +resource "google_compute_url_map" "foobar2" { + name = "myurlmap2" + default_service = "${google_compute_backend_service.foobar.self_link}" + host_rule { + hosts = ["mysite.com", "myothersite.com"] + path_matcher = "boop" + } + path_matcher { + default_service = "${google_compute_backend_service.foobar.self_link}" + name = "boop" + path_rule { + paths = ["/*"] + service = "${google_compute_backend_service.foobar.self_link}" + } + } + test { + host = "mysite.com" + path = "/*" + service = "${google_compute_backend_service.foobar.self_link}" + } +} +` diff --git a/website/source/docs/providers/google/r/compute_target_http_proxy.html.markdown b/website/source/docs/providers/google/r/compute_target_http_proxy.html.markdown new file mode 100644 index 000000000..9734f6ff8 --- /dev/null +++ b/website/source/docs/providers/google/r/compute_target_http_proxy.html.markdown @@ -0,0 +1,80 @@ +--- +layout: "google" +page_title: "Google: google_compute_target_http_proxy" +sidebar_current: "docs-google-compute-target-http-proxy" +description: |- + Creates a Target HTTP Proxy resource in GCE. +--- + +# google\_compute\_target\_http\_proxy + +Creates a target HTTP proxy resource in GCE. For more information see +[the official +documentation](http://cloud.google.com/compute/docs/load-balancing/http/target-proxies) and +[API](http://cloud.google.com/compute/docs/reference/latest/targetHttpProxies). + + +## Example Usage + +``` +resource "google_compute_target_http_proxy" "default" { + name = "test-proxy" + description = "a description" + url_map = "${google_compute_url_map.default.self_link}" +} + +resource "google_compute_url_map" "default" { + name = "url-map" + description = "a description" + default_service = "${google_compute_backend_service.default.self_link}" + + host_rule { + hosts = ["mysite.com"] + path_matcher = "allpaths" + } + + path_matcher { + default_service = "${google_compute_backend_service.default.self_link}" + name = "allpaths" + path_rule { + paths = ["/*"] + service = "${google_compute_backend_service.default.self_link}" + } + } +} + +resource "google_compute_backend_service" "default" { + name = "default-backend" + port_name = "http" + protocol = "HTTP" + timeout_sec = 10 + region = "us-central1" + + health_checks = ["${google_compute_http_health_check.default.self_link}"] +} + +resource "google_compute_http_health_check" "default" { + name = "test" + request_path = "/" + check_interval_sec = 1 + timeout_sec = 1 +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) A unique name for the resource, required by GCE. + Changing this forces a new resource to be created. +* `description` - (Optional) A description of this resource. + Changing this forces a new resource to be created. +* `url_map` - (Required) The URL of a URL Map resource that defines the + mapping from the URL to the BackendService. + +## Attributes Reference + +The following attributes are exported: + +* `self_link` - The URI of the created resource. +* `id` - A unique ID assigned by GCE. diff --git a/website/source/layouts/google.erb b/website/source/layouts/google.erb index f5f87d885..0a917dd22 100644 --- a/website/source/layouts/google.erb +++ b/website/source/layouts/google.erb @@ -73,6 +73,10 @@ google_compute_ssl_certificate + > + google_compute_target_http_proxy + + > google_compute_target_pool