Skip to content

Commit

Permalink
Allow referencing IPs by reference in google_compute_forwarding_rule (#…
Browse files Browse the repository at this point in the history
…4673) (#8877)

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Apr 8, 2021
1 parent fccd353 commit 5669e1a
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 97 deletions.
3 changes: 3 additions & 0 deletions .changelog/4673.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
compute: added the ability to specify `google_compute_forwarding_rule.ip_address` by a reference in addition to raw IP address
```
50 changes: 21 additions & 29 deletions google/resource_compute_forwarding_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,35 +56,27 @@ characters must be a dash, lowercase letter, or digit, except the last
character, which cannot be a dash.`,
},
"ip_address": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
ValidateFunc: validateIpAddress,
Description: `The IP address that this forwarding rule is serving on behalf of.
Addresses are restricted based on the forwarding rule's load balancing
scheme (EXTERNAL or INTERNAL) and scope (global or regional).
When the load balancing scheme is EXTERNAL, for global forwarding
rules, the address must be a global IP, and for regional forwarding
rules, the address must live in the same region as the forwarding
rule. If this field is empty, an ephemeral IPv4 address from the same
scope (global or regional) will be assigned. A regional forwarding
rule supports IPv4 only. A global forwarding rule supports either IPv4
or IPv6.
When the load balancing scheme is INTERNAL, this can only be an RFC
1918 IP address belonging to the network/subnet configured for the
forwarding rule. By default, if this field is empty, an ephemeral
internal IP address will be automatically allocated from the IP range
of the subnet or network configured for this forwarding rule.
An address must be specified by a literal IP address. ~> **NOTE:** While
the API allows you to specify various resource paths for an address resource
instead, Terraform requires this to specifically be an IP address to
avoid needing to fetching the IP address from resource paths on refresh
or unnecessary diffs.`,
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
DiffSuppressFunc: internalIpDiffSuppress,
Description: `The IP address that this forwarding rule serves. When a client sends
traffic to this IP address, the forwarding rule directs the traffic to
the target that you specify in the forwarding rule. The
loadBalancingScheme and the forwarding rule's target determine the
type of IP address that you can use. For detailed information, refer
to [IP address specifications](https://cloud.google.com/load-balancing/docs/forwarding-rule-concepts#ip_address_specifications).
An address can be specified either by a literal IP address or a
reference to an existing Address resource. If you don't specify a
reserved IP address, an ephemeral IP address is assigned.
The value must be set to 0.0.0.0 when the target is a targetGrpcProxy
that has validateForProxyless field set to true.
For Private Service Connect forwarding rules that forward traffic to
Google APIs, IP address must be provided.`,
},
"ip_protocol": {
Type: schema.TypeString,
Expand Down
19 changes: 15 additions & 4 deletions google/resource_compute_forwarding_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,25 @@ func TestAccComputeForwardingRule_ip(t *testing.T) {
addrName := fmt.Sprintf("tf-%s", randString(t, 10))
poolName := fmt.Sprintf("tf-%s", randString(t, 10))
ruleName := fmt.Sprintf("tf-%s", randString(t, 10))
addressRefFieldRaw := "address"
addressRefFieldID := "id"

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeForwardingRuleDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeForwardingRule_ip(addrName, poolName, ruleName),
Config: testAccComputeForwardingRule_ip(addrName, poolName, ruleName, addressRefFieldID),
},
{
ResourceName: "google_compute_forwarding_rule.foobar",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"ip_address"}, // ignore ip_address because we've specified it by ID
},
{
Config: testAccComputeForwardingRule_ip(addrName, poolName, ruleName, addressRefFieldRaw),
},
{
ResourceName: "google_compute_forwarding_rule.foobar",
Expand Down Expand Up @@ -128,7 +139,7 @@ resource "google_compute_forwarding_rule" "foobar" {
`, poolName, poolName, ruleName)
}

func testAccComputeForwardingRule_ip(addrName, poolName, ruleName string) string {
func testAccComputeForwardingRule_ip(addrName, poolName, ruleName, addressRefFieldValue string) string {
return fmt.Sprintf(`
resource "google_compute_address" "foo" {
name = "%s"
Expand All @@ -142,13 +153,13 @@ resource "google_compute_target_pool" "foobar-tp" {
resource "google_compute_forwarding_rule" "foobar" {
description = "Resource created for Terraform acceptance testing"
ip_address = google_compute_address.foo.address
ip_address = google_compute_address.foo.%s
ip_protocol = "TCP"
name = "%s"
port_range = "80-81"
target = google_compute_target_pool.foobar-tp.self_link
}
`, addrName, poolName, ruleName)
`, addrName, poolName, addressRefFieldValue, ruleName)
}

func testAccComputeForwardingRule_networkTier(poolName, ruleName string) string {
Expand Down
40 changes: 16 additions & 24 deletions google/resource_compute_global_forwarding_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,30 +72,22 @@ addressType of INTERNAL, only "all-apis" and "vpc-sc" are valid.`,
Optional: true,
ForceNew: true,
DiffSuppressFunc: internalIpDiffSuppress,
Description: `The IP address that this forwarding rule is serving on behalf of.
Addresses are restricted based on the forwarding rule's load balancing
scheme (EXTERNAL or INTERNAL) and scope (global or regional).
When the load balancing scheme is EXTERNAL, for global forwarding
rules, the address must be a global IP, and for regional forwarding
rules, the address must live in the same region as the forwarding
rule. If this field is empty, an ephemeral IPv4 address from the same
scope (global or regional) will be assigned. A regional forwarding
rule supports IPv4 only. A global forwarding rule supports either IPv4
or IPv6.
When the load balancing scheme is INTERNAL, this can only be an RFC
1918 IP address belonging to the network/subnet configured for the
forwarding rule. By default, if this field is empty, an ephemeral
internal IP address will be automatically allocated from the IP range
of the subnet or network configured for this forwarding rule.
An address must be specified by a literal IP address. ~> **NOTE**: While
the API allows you to specify various resource paths for an address resource
instead, Terraform requires this to specifically be an IP address to
avoid needing to fetching the IP address from resource paths on refresh
or unnecessary diffs.`,
Description: `The IP address that this forwarding rule serves. When a client sends
traffic to this IP address, the forwarding rule directs the traffic to
the target that you specify in the forwarding rule. The
loadBalancingScheme and the forwarding rule's target determine the
type of IP address that you can use. For detailed information, refer
to [IP address specifications](https://cloud.google.com/load-balancing/docs/forwarding-rule-concepts#ip_address_specifications).
An address can be specified either by a literal IP address or a
reference to an existing Address resource. If you don't specify a
reserved IP address, an ephemeral IP address is assigned.
The value must be set to 0.0.0.0 when the target is a targetGrpcProxy
that has validateForProxyless field set to true.
For Private Service Connect forwarding rules that forward traffic to
Google APIs, IP address must be provided.`,
},
"ip_protocol": {
Type: schema.TypeString,
Expand Down
33 changes: 13 additions & 20 deletions website/docs/r/compute_forwarding_rule.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -416,26 +416,19 @@ The following arguments are supported:

* `ip_address` -
(Optional)
The IP address that this forwarding rule is serving on behalf of.
Addresses are restricted based on the forwarding rule's load balancing
scheme (EXTERNAL or INTERNAL) and scope (global or regional).
When the load balancing scheme is EXTERNAL, for global forwarding
rules, the address must be a global IP, and for regional forwarding
rules, the address must live in the same region as the forwarding
rule. If this field is empty, an ephemeral IPv4 address from the same
scope (global or regional) will be assigned. A regional forwarding
rule supports IPv4 only. A global forwarding rule supports either IPv4
or IPv6.
When the load balancing scheme is INTERNAL, this can only be an RFC
1918 IP address belonging to the network/subnet configured for the
forwarding rule. By default, if this field is empty, an ephemeral
internal IP address will be automatically allocated from the IP range
of the subnet or network configured for this forwarding rule.
An address must be specified by a literal IP address. ~> **NOTE:** While
the API allows you to specify various resource paths for an address resource
instead, Terraform requires this to specifically be an IP address to
avoid needing to fetching the IP address from resource paths on refresh
or unnecessary diffs.
The IP address that this forwarding rule serves. When a client sends
traffic to this IP address, the forwarding rule directs the traffic to
the target that you specify in the forwarding rule. The
loadBalancingScheme and the forwarding rule's target determine the
type of IP address that you can use. For detailed information, refer
to [IP address specifications](https://cloud.google.com/load-balancing/docs/forwarding-rule-concepts#ip_address_specifications).
An address can be specified either by a literal IP address or a
reference to an existing Address resource. If you don't specify a
reserved IP address, an ephemeral IP address is assigned.
The value must be set to 0.0.0.0 when the target is a targetGrpcProxy
that has validateForProxyless field set to true.
For Private Service Connect forwarding rules that forward traffic to
Google APIs, IP address must be provided.

* `ip_protocol` -
(Optional)
Expand Down
33 changes: 13 additions & 20 deletions website/docs/r/compute_global_forwarding_rule.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -276,26 +276,19 @@ The following arguments are supported:

* `ip_address` -
(Optional)
The IP address that this forwarding rule is serving on behalf of.
Addresses are restricted based on the forwarding rule's load balancing
scheme (EXTERNAL or INTERNAL) and scope (global or regional).
When the load balancing scheme is EXTERNAL, for global forwarding
rules, the address must be a global IP, and for regional forwarding
rules, the address must live in the same region as the forwarding
rule. If this field is empty, an ephemeral IPv4 address from the same
scope (global or regional) will be assigned. A regional forwarding
rule supports IPv4 only. A global forwarding rule supports either IPv4
or IPv6.
When the load balancing scheme is INTERNAL, this can only be an RFC
1918 IP address belonging to the network/subnet configured for the
forwarding rule. By default, if this field is empty, an ephemeral
internal IP address will be automatically allocated from the IP range
of the subnet or network configured for this forwarding rule.
An address must be specified by a literal IP address. ~> **NOTE**: While
the API allows you to specify various resource paths for an address resource
instead, Terraform requires this to specifically be an IP address to
avoid needing to fetching the IP address from resource paths on refresh
or unnecessary diffs.
The IP address that this forwarding rule serves. When a client sends
traffic to this IP address, the forwarding rule directs the traffic to
the target that you specify in the forwarding rule. The
loadBalancingScheme and the forwarding rule's target determine the
type of IP address that you can use. For detailed information, refer
to [IP address specifications](https://cloud.google.com/load-balancing/docs/forwarding-rule-concepts#ip_address_specifications).
An address can be specified either by a literal IP address or a
reference to an existing Address resource. If you don't specify a
reserved IP address, an ephemeral IP address is assigned.
The value must be set to 0.0.0.0 when the target is a targetGrpcProxy
that has validateForProxyless field set to true.
For Private Service Connect forwarding rules that forward traffic to
Google APIs, IP address must be provided.

* `ip_protocol` -
(Optional)
Expand Down

0 comments on commit 5669e1a

Please sign in to comment.