Skip to content

Adding IP Collection To Forwarding Rule #12914

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

Merged
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 26 additions & 0 deletions mmv1/products/compute/ForwardingRule.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ examples:
network_name: 'website-net'
ignore_read_extra:
- 'port_range'
- name: 'forwarding_rule_externallb_byoipv6'
primary_resource_id: 'default'
vars:
forwarding_rule_name: 'byoipv6-forwarding-rule'
backend_name: 'website-backend'
network_name: 'website-net'
ip_address: '"2600:1901:4457:1::/96"'
ip_collection_url: '"projects/tf-static-byoip/regions/us-central1/publicDelegatedPrefixes/tf-test-forwarding-rule-mode-pdp"'
test_vars_overrides:
ip_address: 'fmt.Sprintf("2600:1901:4457:1:%d:%d::/96", acctest.RandIntRange(t, 0, 9999), acctest.RandIntRange(t, 0, 9999))'
ip_collection_url: '"projects/tf-static-byoip/regions/us-central1/publicDelegatedPrefixes/tf-test-forwarding-rule-mode-pdp"'
ignore_read_extra:
- 'port_range'
- name: 'forwarding_rule_global_internallb'
primary_resource_id: 'default'
vars:
Expand Down Expand Up @@ -653,3 +666,16 @@ properties:
enum_values:
- 'IPV4'
- 'IPV6'
- name: ipCollection
type: String
diff_suppress_func: 'IpCollectionDiffSuppress'
description: |
Resource reference of a PublicDelegatedPrefix. The PDP must be a sub-PDP
in EXTERNAL_IPV6_FORWARDING_RULE_CREATION mode.
Use one of the following formats to specify a sub-PDP when creating an
IPv6 NetLB forwarding rule using BYOIP:
Full resource URL, as in:
* `https://www.googleapis.com/compute/v1/projects/{{projectId}}/regions/{{region}}/publicDelegatedPrefixes/{{sub-pdp-name}}`
Partial URL, as in:
* `projects/{{projectId}}/regions/region/publicDelegatedPrefixes/{{sub-pdp-name}}`
* `regions/{{region}}/publicDelegatedPrefixes/{{sub-pdp-name}}`
27 changes: 27 additions & 0 deletions mmv1/templates/terraform/constants/compute_forwarding_rule.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ func PortRangeDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
return old == new+"-"+new
}

// Compare only the relative path from 'regions' of two IP collection links
func IpCollectionDiffSuppress(_, old, new string, d *schema.ResourceData) bool {
oldStripped, err := GetRelativePathFromRegions(old)
if err != nil {
return false
}

newStripped, err := GetRelativePathFromRegions(new)
if err != nil {
return false
}

if oldStripped == newStripped {
return true
}
return false
}

// Suppresses diff for IPv4 and IPv6 different formats.
// It also suppresses diffs if an IP is changing to a reference.
func InternalIpDiffSuppress(_, old, new string, _ *schema.ResourceData) bool {
Expand Down Expand Up @@ -65,3 +83,12 @@ func InternalIpDiffSuppress(_, old, new string, _ *schema.ResourceData) bool {

return addr_equality && netmask_equality
}

func GetRelativePathFromRegions(resourceLink string) (string, error) {
stringParts := strings.SplitAfterN(resourceLink, "regions/", 2)
if len(stringParts) != 2 {
return "", fmt.Errorf("String is not a valid link: %s", resourceLink)
}

return "regions/" + stringParts[1], nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Forwarding rule for External Network Load Balancing using Backend Services with IP Collection

resource "google_compute_forwarding_rule" "{{$.PrimaryResourceId}}" {
name = "{{index $.Vars "forwarding_rule_name"}}"
region = "us-central1"
port_range = 80
ip_protocol = "TCP"
ip_version = "IPV6"
load_balancing_scheme = "EXTERNAL"
ip_address = "{{index $.Vars "ip_address"}}"
network_tier = "PREMIUM"
backend_service = google_compute_region_backend_service.backend.id
ip_collection = "{{index $.Vars "ip_collection_url"}}"
}

resource "google_compute_region_backend_service" "backend" {
name = "{{index $.Vars "backend_name"}}"
region = "us-central1"
load_balancing_scheme = "EXTERNAL"
health_checks = [google_compute_region_health_check.hc.id]
}

resource "google_compute_region_health_check" "hc" {
name = "{{index $.Vars "backend_name"}}"
check_interval_sec = 1
timeout_sec = 1
region = "us-central1"

tcp_health_check {
port = "80"
}
}
15 changes: 15 additions & 0 deletions mmv1/third_party/terraform/acctest/test_utils.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,21 @@ func RandInt(t *testing.T) int {
return rand.New(s.source).Int()
}

func RandIntRange(t *testing.T, minInt int, maxInt int) int {
if !IsVcrEnabled() {
return acctest.RandIntRange(minInt, maxInt)
}
envPath := os.Getenv("VCR_PATH")
vcrMode := os.Getenv("VCR_MODE")
s, err := vcrSource(t, envPath, vcrMode)
if err != nil {
// At this point we haven't created any resources, so fail fast
t.Fatal(err)
}

return rand.New(s.source).Intn(maxInt-minInt) + minInt
}

// ProtoV5ProviderFactories returns a muxed ProviderServer that uses the provider code from this repo (SDK and plugin-framework).
// Used to set ProtoV5ProviderFactories in a resource.TestStep within an acceptance test.
func ProtoV5ProviderFactories(t *testing.T) map[string]func() (tfprotov5.ProviderServer, error) {
Expand Down
Loading