Add import support to global forwarding rule (#653)

This commit is contained in:
Vincent Roseberry 2017-10-31 11:28:55 -07:00 committed by GitHub
parent c3b54980f6
commit 0332208bc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 69 additions and 13 deletions

View File

@ -0,0 +1,34 @@
package google
import (
"fmt"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"testing"
)
func TestAccComputeGlobalForwardingRule_import(t *testing.T) {
t.Parallel()
fr := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10))
proxy1 := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10))
proxy2 := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10))
backend := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10))
hc := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10))
urlmap := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeGlobalForwardingRuleDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeGlobalForwardingRule_basic1(fr, proxy1, proxy2, backend, hc, urlmap),
},
resource.TestStep{
ResourceName: "google_compute_global_forwarding_rule.foobar",
ImportState: true,
ImportStateVerify: true,
},
}})
}

View File

@ -73,15 +73,10 @@ func resourceComputeForwardingRule() *schema.Resource {
},
"port_range": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if old == new+"-"+new {
return true
}
return false
},
Type: schema.TypeString,
Optional: true,
ForceNew: true,
DiffSuppressFunc: portRangeDiffSuppress,
},
"ports": &schema.Schema{

View File

@ -23,6 +23,10 @@ func resourceComputeGlobalForwardingRule() *schema.Resource {
Update: resourceComputeGlobalForwardingRuleUpdate,
Delete: resourceComputeGlobalForwardingRuleDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
@ -69,9 +73,10 @@ func resourceComputeGlobalForwardingRule() *schema.Resource {
},
"port_range": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Type: schema.TypeString,
Optional: true,
ForceNew: true,
DiffSuppressFunc: portRangeDiffSuppress,
},
"ip_version": &schema.Schema{
@ -273,6 +278,10 @@ func resourceComputeGlobalForwardingRuleRead(d *schema.ResourceData, meta interf
}
}
d.Set("name", frule.Name)
d.Set("description", frule.Description)
d.Set("target", frule.Target)
d.Set("port_range", frule.PortRange)
d.Set("ip_address", frule.IPAddress)
d.Set("ip_protocol", frule.IPProtocol)
d.Set("ip_version", frule.IpVersion)

View File

@ -283,6 +283,16 @@ func ipCidrRangeDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
return false
}
// Port range '80' and '80-80' is equivalent.
// `old` is read from the server and always has the full range format (e.g. '80-80', '1024-2048').
// `new` can be either a single port or a port range.
func portRangeDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
if old == new+"-"+new {
return true
}
return false
}
// expandLabels pulls the value of "labels" out of a schema.ResourceData as a map[string]string.
func expandLabels(d *schema.ResourceData) map[string]string {
return expandStringMap(d, "labels")

View File

@ -17,7 +17,7 @@ documentation](https://cloud.google.com/compute/docs/load-balancing/http/global-
```hcl
resource "google_compute_global_forwarding_rule" "default" {
name = "test"
name = "default-rule"
target = "${google_compute_target_http_proxy.default.self_link}"
port_range = "80"
}
@ -110,3 +110,11 @@ exported:
* `self_link` - The URI of the created resource.
* `label_fingerprint` - ([Beta](/docs/providers/google/index.html#beta-features)) The current label fingerprint.
## Import
Global forwarding rules can be imported using the `name`, e.g.
```
$ terraform import google_compute_global_forwarding_rule.default default-rule
```