-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,169 @@ | ||
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, | ||
}, | ||
|
||
"proxy_id": &schema.Schema{ | ||
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), | ||
ProxyHeader: d.Get("proxy_header").(string), | ||
Description: d.Get("description").(string), | ||
} | ||
|
||
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) | ||
|
||
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("proxy_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 | ||
} |
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,156 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccComputeTargetTcpProxy_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{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How is this testing the update feature? You need at least two |
||
Config: testAccComputeTargetTcpProxy_basic1(target, backend, hc), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckComputeTargetTcpProxyExists( | ||
"google_compute_target_tcp_proxy.foobar"), | ||
), | ||
}, | ||
resource.TestStep{ | ||
Config: testAccComputeTargetTcpProxy_basic2(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}" | ||
proxy_header = "NONE" | ||
} | ||
|
||
resource "google_compute_backend_service" "foobar" { | ||
name = "%s" | ||
protocol = "TCP" | ||
health_checks = ["${google_compute_health_check.zero.self_link}"] | ||
} | ||
|
||
resource "google_compute_health_check" "zero" { | ||
name = "%s" | ||
check_interval_sec = 1 | ||
timeout_sec = 1 | ||
tcp_health_check { | ||
port = "443" | ||
} | ||
} | ||
`, target, backend, hc) | ||
} | ||
|
||
func testAccComputeTargetTcpProxy_basic2(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}" | ||
proxy_header = "PROXY_V1" | ||
} | ||
|
||
resource "google_compute_backend_service" "foobar" { | ||
name = "%s" | ||
protocol = "TCP" | ||
health_checks = ["${google_compute_health_check.zero.self_link}"] | ||
} | ||
|
||
resource "google_compute_health_check" "zero" { | ||
name = "%s" | ||
check_interval_sec = 1 | ||
timeout_sec = 1 | ||
tcp_health_check { | ||
port = "443" | ||
} | ||
} | ||
`, target, backend, hc) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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