Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provider/google: Target HTTP proxy resource + tests & documentation #3727

Merged
merged 1 commit into from
Nov 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions builtin/providers/google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
147 changes: 147 additions & 0 deletions builtin/providers/google/resource_compute_target_http_proxy.go
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 builtin/providers/google/resource_compute_target_http_proxy_test.go
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 = `
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spaces / tabs consistency

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