provider/google: target http proxies resource + tests & documentation

This commit is contained in:
Lars Wander 2015-11-01 10:39:08 -05:00
parent 519756c42a
commit e7ebb0ac40
5 changed files with 458 additions and 0 deletions

View File

@ -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(),

View File

@ -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
}

View File

@ -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}"
}
}
`

View File

@ -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.

View File

@ -73,6 +73,10 @@
<a href="/docs/providers/google/r/compute_ssl_certificate.html">google_compute_ssl_certificate</a>
</li>
<li<%= sidebar_current("docs-google-compute-target-http-proxy") %>>
<a href="/docs/providers/google/r/compute_target_http_proxy.html">google_compute_target_http_proxy</a>
</li>
<li<%= sidebar_current("docs-google-compute-target-pool") %>>
<a href="/docs/providers/google/r/compute_target_pool.html">google_compute_target_pool</a>
</li>