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

Add support for google_compute_target_tcp_proxy #528

Merged
merged 2 commits into from
Oct 5, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func Provider() terraform.ResourceProvider {
"google_compute_subnetwork": resourceComputeSubnetwork(),
"google_compute_target_http_proxy": resourceComputeTargetHttpProxy(),
"google_compute_target_https_proxy": resourceComputeTargetHttpsProxy(),
"google_compute_target_tcp_proxy": resourceComputeTargetTcpProxy(),
"google_compute_target_pool": resourceComputeTargetPool(),
"google_compute_url_map": resourceComputeUrlMap(),
"google_compute_vpn_gateway": resourceComputeVpnGateway(),
Expand Down
171 changes: 171 additions & 0 deletions google/resource_compute_target_tcp_proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package google

import (
"fmt"
"log"
"strconv"

"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/compute/v1"
)

func resourceComputeTargetTcpProxy() *schema.Resource {
return &schema.Resource{
Create: resourceComputeTargetTcpProxyCreate,
Read: resourceComputeTargetTcpProxyRead,
Delete: resourceComputeTargetTcpProxyDelete,
Update: resourceComputeTargetTcpProxyUpdate,

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"backend_service": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"proxy_header": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "NONE",
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"id": &schema.Schema{
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you rename it to proxy_id? We are moving away from "id" field. See #399 for more details.

Type: schema.TypeString,
Computed: true,
},

"project": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"self_link": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceComputeTargetTcpProxyCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

project, err := getProject(d, config)
if err != nil {
return err
}

proxy := &compute.TargetTcpProxy{
Name: d.Get("name").(string),
Service: d.Get("backend_service").(string),
Copy link
Contributor

Choose a reason for hiding this comment

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

Have you forgot to set the ProxyHeader field?

}

if v, ok := d.GetOk("description"); ok {
proxy.Description = v.(string)
Copy link
Contributor

Choose a reason for hiding this comment

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

No need for the if statement around it. Move it inside &compute.TargetTcpProxy{...} above. d.Get("description") will return an empty string if not set which will yield the same behavior.

}

log.Printf("[DEBUG] TargetTcpProxy insert request: %#v", proxy)
op, err := config.clientCompute.TargetTcpProxies.Insert(
project, proxy).Do()
if err != nil {
return fmt.Errorf("Error creating TargetTcpProxy: %s", err)
}

err = computeOperationWait(config, op, project, "Creating Target Tcp Proxy")
if err != nil {
return err
}

d.SetId(proxy.Name)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should probably set this to proxy.Id.

Can two separate proxies have the same name?

Copy link
Contributor Author

@enxebre enxebre Oct 5, 2017

Choose a reason for hiding this comment

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

At least through the UI you can not have more than one with the same name. I think I'd be in favour of leaving it with name which is also in line with the others target resources


return resourceComputeTargetTcpProxyRead(d, meta)
}

func resourceComputeTargetTcpProxyUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

project, err := getProject(d, config)
if err != nil {
return err
}

d.Partial(true)

if d.HasChange("proxy_header") {
proxy_header := d.Get("proxy_header").(string)
proxy_header_payload := &compute.TargetTcpProxiesSetProxyHeaderRequest{
ProxyHeader: proxy_header,
}
op, err := config.clientCompute.TargetTcpProxies.SetProxyHeader(
project, d.Id(), proxy_header_payload).Do()
if err != nil {
return fmt.Errorf("Error updating target: %s", err)
}

err = computeOperationWait(config, op, project, "Updating Target Tcp Proxy")
if err != nil {
return err
}

d.SetPartial("proxy_header")
}

d.Partial(false)

return resourceComputeTargetTcpProxyRead(d, meta)
}

func resourceComputeTargetTcpProxyRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

project, err := getProject(d, config)
if err != nil {
return err
}

proxy, err := config.clientCompute.TargetTcpProxies.Get(
project, d.Id()).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Target TCP Proxy %q", d.Get("name").(string)))
}

d.Set("self_link", proxy.SelfLink)
d.Set("id", strconv.FormatUint(proxy.Id, 10))

return nil
}

func resourceComputeTargetTcpProxyDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

project, err := getProject(d, config)
if err != nil {
return err
}

// Delete the TargetTcpProxy
log.Printf("[DEBUG] TargetTcpProxy delete request")
op, err := config.clientCompute.TargetTcpProxies.Delete(
project, d.Id()).Do()
if err != nil {
return fmt.Errorf("Error deleting TargetTcpProxy: %s", err)
}

err = computeOperationWait(config, op, project, "Deleting Target Tcp Proxy")
if err != nil {
return err
}

d.SetId("")
return nil
}
119 changes: 119 additions & 0 deletions google/resource_compute_target_tcp_proxy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package google

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccComputeTargeTcpProxy_basic(t *testing.T) {
target := fmt.Sprintf("ttcp-test-%s", acctest.RandString(10))
backend := fmt.Sprintf("ttcp-test-%s", acctest.RandString(10))
hc := fmt.Sprintf("ttcp-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeTargetTcpProxyDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeTargetTcpProxy_basic1(target, backend, hc),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeTargetTcpProxyExists(
"google_compute_target_tcp_proxy.foobar"),
),
},
},
})
}

func TestAccComputeTargetTcpProxy_update(t *testing.T) {
target := fmt.Sprintf("ttcp-test-%s", acctest.RandString(10))
backend := fmt.Sprintf("ttcp-test-%s", acctest.RandString(10))
hc := fmt.Sprintf("ttcp-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeTargetTcpProxyDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Copy link
Contributor

Choose a reason for hiding this comment

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

How is this testing the update feature? You need at least two TestStep. Make sure to test the update with the proxyHeader field (only updateable field) otherwise it will do a Delete/Create operation which won't test the update method.

Config: testAccComputeTargetTcpProxy_basic1(target, backend, hc),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeTargetTcpProxyExists(
"google_compute_target_tcp_proxy.foobar"),
),
},
},
})
}

func testAccCheckComputeTargetTcpProxyDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)

for _, rs := range s.RootModule().Resources {
if rs.Type != "google_compute_target_tcp_proxy" {
continue
}

_, err := config.clientCompute.TargetTcpProxies.Get(
config.Project, rs.Primary.ID).Do()
if err == nil {
return fmt.Errorf("TargetTcpProxy still exists")
}
}

return nil
}

func testAccCheckComputeTargetTcpProxyExists(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.TargetTcpProxies.Get(
config.Project, rs.Primary.ID).Do()
if err != nil {
return err
}

if found.Name != rs.Primary.ID {
return fmt.Errorf("TargetTcpProxy not found")
}

return nil
}
}

func testAccComputeTargetTcpProxy_basic1(target, backend, hc string) string {
return fmt.Sprintf(`
resource "google_compute_target_tcp_proxy" "foobar" {
description = "Resource created for Terraform acceptance testing"
name = "%s"
backend_service = "${google_compute_backend_service.foobar.self_link}"
}

resource "google_compute_backend_service" "foobar" {
name = "%s"
health_checks = ["${google_compute_http_health_check.zero.self_link}"]
}

resource "google_compute_http_health_check" "zero" {
name = "%s"
request_path = "/"
check_interval_sec = 1
timeout_sec = 1
}
`, target, backend, hc)
}
72 changes: 72 additions & 0 deletions website/docs/r/compute_target_tcp_proxy.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
layout: "google"
page_title: "Google: google_compute_target_tcp_proxy"
sidebar_current: "docs-google-compute-target-tcp-proxy"
description: |-
Creates a Target TCP Proxy resource in GCE.
---

# google\_compute\_target\_tcp\_proxy

Creates a target TCP proxy resource in GCE. For more information see
[the official
documentation](https://cloud.google.com/compute/docs/load-balancing/tcp-ssl/tcp-proxy) and
[API](https://cloud.google.com/compute/docs/reference/latest/targetTcpProxies).


## Example Usage

```hcl
resource "google_compute_target_tcp_proxy" "default" {
name = "test"
description = "test"
backend_service = "${google_compute_backend_service.default.self_link}"
}

resource "google_compute_backend_service" "default" {
name = "default-backend"
protocol = "TCP"
timeout_sec = 10

health_checks = ["${google_compute_health_check.default.self_link}"]
}

resource "google_compute_health_check" "default" {
name = "default"
timeout_sec = 1
check_interval_sec = 1

tcp_health_check {
port = "443"
}
}
```

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

* `backend_service` - (Required) The URL of a Backend Service resource to receive the matched traffic.

* `proxy_header` - (Optional) Type of proxy header to append before sending
data to the backend, either NONE or PROXY_V1 (default NONE).

- - -

* `description` - (Optional) A description of this resource. Changing this
forces a new resource to be created.

* `project` - (Optional) The project in which the resource belongs. If it
is not provided, the provider project is used.

## Attributes Reference

In addition to the arguments listed above, the following computed attributes are
exported:

* `id` - A unique ID assigned by GCE.
Copy link
Contributor

Choose a reason for hiding this comment

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

Rename to proxy_id like suggested in a comment above.


* `self_link` - The URI of the created resource.
4 changes: 4 additions & 0 deletions website/google.erb
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@
<a href="/docs/providers/google/r/compute_target_https_proxy.html">google_compute_target_https_proxy</a>
</li>

<li<%= sidebar_current("docs-google-compute-target-tcp-proxy") %>>
<a href="/docs/providers/google/r/compute_target_tcp_proxy.html">google_compute_target_tcp_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>
Expand Down