provider/google: global forwarding rule tests & documentation
This commit is contained in:
parent
0097d7105b
commit
d344d3e39a
|
@ -41,6 +41,7 @@ func Provider() terraform.ResourceProvider {
|
|||
"google_compute_firewall": resourceComputeFirewall(),
|
||||
"google_compute_forwarding_rule": resourceComputeForwardingRule(),
|
||||
"google_compute_global_address": resourceComputeGlobalAddress(),
|
||||
"google_compute_global_forwarding_rule": resourceComputeGlobalForwardingRule(),
|
||||
"google_compute_http_health_check": resourceComputeHttpHealthCheck(),
|
||||
"google_compute_instance": resourceComputeInstance(),
|
||||
"google_compute_instance_group_manager": resourceComputeInstanceGroupManager(),
|
||||
|
|
|
@ -0,0 +1,168 @@
|
|||
package google
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
"google.golang.org/api/compute/v1"
|
||||
"google.golang.org/api/googleapi"
|
||||
)
|
||||
|
||||
func resourceComputeGlobalForwardingRule() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Create: resourceComputeGlobalForwardingRuleCreate,
|
||||
Read: resourceComputeGlobalForwardingRuleRead,
|
||||
Update: resourceComputeGlobalForwardingRuleUpdate,
|
||||
Delete: resourceComputeGlobalForwardingRuleDelete,
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
"ip_address": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
ForceNew: true,
|
||||
Computed: true,
|
||||
},
|
||||
|
||||
"ip_protocol": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
ForceNew: true,
|
||||
Computed: true,
|
||||
},
|
||||
|
||||
"description": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
ForceNew: true,
|
||||
},
|
||||
|
||||
"name": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
ForceNew: true,
|
||||
},
|
||||
|
||||
"port_range": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
ForceNew: true,
|
||||
},
|
||||
|
||||
"region": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
ForceNew: true,
|
||||
},
|
||||
|
||||
"self_link": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
|
||||
"target": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func resourceComputeGlobalForwardingRuleCreate(d *schema.ResourceData, meta interface{}) error {
|
||||
config := meta.(*Config)
|
||||
|
||||
frule := &compute.ForwardingRule{
|
||||
IPAddress: d.Get("ip_address").(string),
|
||||
IPProtocol: d.Get("ip_protocol").(string),
|
||||
Description: d.Get("description").(string),
|
||||
Name: d.Get("name").(string),
|
||||
PortRange: d.Get("port_range").(string),
|
||||
Target: d.Get("target").(string),
|
||||
}
|
||||
|
||||
op, err := config.clientCompute.GlobalForwardingRules.Insert(
|
||||
config.Project, frule).Do()
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error creating Global Forwarding Rule: %s", err)
|
||||
}
|
||||
|
||||
// It probably maybe worked, so store the ID now
|
||||
d.SetId(frule.Name)
|
||||
|
||||
err = computeOperationWaitGlobal(config, op, "Creating Global Fowarding Rule")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return resourceComputeGlobalForwardingRuleRead(d, meta)
|
||||
}
|
||||
|
||||
func resourceComputeGlobalForwardingRuleUpdate(d *schema.ResourceData, meta interface{}) error {
|
||||
config := meta.(*Config)
|
||||
|
||||
d.Partial(true)
|
||||
|
||||
if d.HasChange("target") {
|
||||
target_name := d.Get("target").(string)
|
||||
target_ref := &compute.TargetReference{Target: target_name}
|
||||
op, err := config.clientCompute.GlobalForwardingRules.SetTarget(
|
||||
config.Project, d.Id(), target_ref).Do()
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error updating target: %s", err)
|
||||
}
|
||||
|
||||
err = computeOperationWaitGlobal(config, op, "Updating Global Forwarding Rule")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d.SetPartial("target")
|
||||
}
|
||||
|
||||
d.Partial(false)
|
||||
|
||||
return resourceComputeGlobalForwardingRuleRead(d, meta)
|
||||
}
|
||||
|
||||
func resourceComputeGlobalForwardingRuleRead(d *schema.ResourceData, meta interface{}) error {
|
||||
config := meta.(*Config)
|
||||
|
||||
frule, err := config.clientCompute.GlobalForwardingRules.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 GlobalForwardingRule: %s", err)
|
||||
}
|
||||
|
||||
d.Set("ip_address", frule.IPAddress)
|
||||
d.Set("ip_protocol", frule.IPProtocol)
|
||||
d.Set("self_link", frule.SelfLink)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func resourceComputeGlobalForwardingRuleDelete(d *schema.ResourceData, meta interface{}) error {
|
||||
config := meta.(*Config)
|
||||
|
||||
// Delete the GlobalForwardingRule
|
||||
log.Printf("[DEBUG] GlobalForwardingRule delete request")
|
||||
op, err := config.clientCompute.GlobalForwardingRules.Delete(
|
||||
config.Project, d.Id()).Do()
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error deleting GlobalForwardingRule: %s", err)
|
||||
}
|
||||
|
||||
err = computeOperationWaitGlobal(config, op, "Deleting GlobalForwarding Rule")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d.SetId("")
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,208 @@
|
|||
package google
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
)
|
||||
|
||||
func TestAccComputeGlobalForwardingRule_basic(t *testing.T) {
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckComputeGlobalForwardingRuleDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: testAccComputeGlobalForwardingRule_basic1,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckComputeGlobalForwardingRuleExists(
|
||||
"google_compute_global_forwarding_rule.foobar"),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccComputeGlobalForwardingRule_update(t *testing.T) {
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckComputeGlobalForwardingRuleDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: testAccComputeGlobalForwardingRule_basic1,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckComputeGlobalForwardingRuleExists(
|
||||
"google_compute_global_forwarding_rule.foobar"),
|
||||
),
|
||||
},
|
||||
|
||||
resource.TestStep{
|
||||
Config: testAccComputeGlobalForwardingRule_basic2,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckComputeGlobalForwardingRuleExists(
|
||||
"google_compute_global_forwarding_rule.foobar"),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func testAccCheckComputeGlobalForwardingRuleDestroy(s *terraform.State) error {
|
||||
config := testAccProvider.Meta().(*Config)
|
||||
|
||||
for _, rs := range s.RootModule().Resources {
|
||||
if rs.Type != "google_compute_global_forwarding_rule" {
|
||||
continue
|
||||
}
|
||||
|
||||
_, err := config.clientCompute.GlobalForwardingRules.Get(
|
||||
config.Project, rs.Primary.ID).Do()
|
||||
if err == nil {
|
||||
return fmt.Errorf("Global Forwarding Rule still exists")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func testAccCheckComputeGlobalForwardingRuleExists(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.GlobalForwardingRules.Get(
|
||||
config.Project, rs.Primary.ID).Do()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if found.Name != rs.Primary.ID {
|
||||
return fmt.Errorf("Global Forwarding Rule not found")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
const testAccComputeGlobalForwardingRule_basic1 = `
|
||||
resource "google_compute_global_forwarding_rule" "foobar" {
|
||||
description = "Resource created for Terraform acceptance testing"
|
||||
ip_protocol = "TCP"
|
||||
name = "terraform-test"
|
||||
port_range = "80"
|
||||
target = "${google_compute_target_http_proxy.foobar1.self_link}"
|
||||
}
|
||||
|
||||
resource "google_compute_target_http_proxy" "foobar1" {
|
||||
description = "Resource created for Terraform acceptance testing"
|
||||
name = "terraform-test1"
|
||||
url_map = "${google_compute_url_map.foobar.self_link}"
|
||||
}
|
||||
|
||||
resource "google_compute_target_http_proxy" "foobar2" {
|
||||
description = "Resource created for Terraform acceptance testing"
|
||||
name = "terraform-test2"
|
||||
url_map = "${google_compute_url_map.foobar.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" "foobar" {
|
||||
name = "myurlmap"
|
||||
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 testAccComputeGlobalForwardingRule_basic2 = `
|
||||
resource "google_compute_global_forwarding_rule" "foobar" {
|
||||
description = "Resource created for Terraform acceptance testing"
|
||||
ip_protocol = "TCP"
|
||||
name = "terraform-test"
|
||||
port_range = "80"
|
||||
target = "${google_compute_target_http_proxy.foobar2.self_link}"
|
||||
}
|
||||
|
||||
resource "google_compute_target_http_proxy" "foobar1" {
|
||||
description = "Resource created for Terraform acceptance testing"
|
||||
name = "terraform-test1"
|
||||
url_map = "${google_compute_url_map.foobar.self_link}"
|
||||
}
|
||||
|
||||
resource "google_compute_target_http_proxy" "foobar2" {
|
||||
description = "Resource created for Terraform acceptance testing"
|
||||
name = "terraform-test2"
|
||||
url_map = "${google_compute_url_map.foobar.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" "foobar" {
|
||||
name = "myurlmap"
|
||||
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}"
|
||||
}
|
||||
}
|
||||
`
|
|
@ -0,0 +1,93 @@
|
|||
---
|
||||
layout: "google"
|
||||
page_title: "Google: google_compute_global_forwarding_rule"
|
||||
sidebar_current: "docs-google-compute-global-forwarding_rule"
|
||||
description: |-
|
||||
Manages a Target Pool within GCE.
|
||||
---
|
||||
|
||||
# google\_compute\_global\_forwarding\_rule
|
||||
|
||||
Manages a Global Forwarding Rule within GCE. This binds an ip and port to a target HTTP(s) proxy. For more
|
||||
information see [the official
|
||||
documentation](https://cloud.google.com/compute/docs/load-balancing/network/forwarding-rules) and
|
||||
[API](https://cloud.google.com/compute/docs/reference/latest/globalForwardingRules).
|
||||
|
||||
## Example Usage
|
||||
|
||||
```
|
||||
resource "google_compute_global_forwarding_rule" "default" {
|
||||
name = "test"
|
||||
target = "${google_compute_target_http_proxy.default.self_link}"
|
||||
port_range = "80"
|
||||
}
|
||||
|
||||
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:
|
||||
|
||||
* `description` - (Optional) Textual description field.
|
||||
|
||||
* `ip_address` - (Optional) The static IP. (if not set, an ephemeral IP is used).
|
||||
|
||||
* `ip_protocol` - (Optional) The IP protocol to route, one of "TCP" "UDP" "AH" "ESP" or "SCTP". (default "TCP").
|
||||
|
||||
* `name` - (Required) A unique name for the resource, required by GCE. Changing
|
||||
this forces a new resource to be created.
|
||||
|
||||
* `port_range` - (Optional) A range e.g. "1024-2048" or a single port "1024"
|
||||
(defaults to all ports!).
|
||||
|
||||
* `target` - URL of target HTTP or HTTPS proxy.
|
||||
|
||||
## Attributes Reference
|
||||
|
||||
The following attributes are exported:
|
||||
|
||||
* `self_link` - The URL of the created resource.
|
||||
|
||||
* `ip_address` - The IP address that was chosen (or specified).
|
|
@ -41,6 +41,10 @@
|
|||
<a href="/docs/providers/google/r/compute_global_address.html">google_compute_global_address</a>
|
||||
</li>
|
||||
|
||||
<li<%= sidebar_current("docs-google-compute-global-forwarding-rule") %>>
|
||||
<a href="/docs/providers/google/r/compute_global_forwarding_rule.html">google_compute_global_forwarding_rule</a>
|
||||
</li>
|
||||
|
||||
<li<%= sidebar_current("docs-google-compute-http-health-check") %>>
|
||||
<a href="/docs/providers/google/r/compute_http_health_check.html">google_compute_http_health_check</a>
|
||||
</li>
|
||||
|
|
Loading…
Reference in New Issue