-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provider/google: target http proxies resource + tests & documentation
- Loading branch information
Showing
5 changed files
with
458 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
builtin/providers/google/resource_compute_target_http_proxy.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
226 changes: 226 additions & 0 deletions
226
builtin/providers/google/resource_compute_target_http_proxy_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}" | ||
} | ||
} | ||
` |
Oops, something went wrong.