Skip to content

Commit 7bcf086

Browse files
Updating Subnetwork's IP Collection Field Docs & Fixing Internal IPv6 Prefix Field (#15552)
1 parent 1c41584 commit 7bcf086

File tree

5 files changed

+200
-4
lines changed

5 files changed

+200
-4
lines changed

mmv1/products/compute/Subnetwork.yaml

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,26 @@ examples:
152152
ip_collection_url: '"projects/tf-static-byoip/regions/us-central1/publicDelegatedPrefixes/tf-test-subnet-mode-pdp"'
153153
test_vars_overrides:
154154
ip_collection_url: '"projects/tf-static-byoip/regions/us-central1/publicDelegatedPrefixes/tf-test-subnet-mode-pdp"'
155+
- name: 'subnetwork_with_internal_subnet_mode_pdp'
156+
primary_resource_id: 'subnetwork-with-internal-subnet-mode-pdp'
157+
exclude_docs: true
158+
vars:
159+
subnetwork_name: 'internal-subnet-mode-pdp-subnet'
160+
network_name: 'network-byoipv6-internal'
161+
ip_collection_url: '"projects/tf-static-byoip/regions/us-central1/publicDelegatedPrefixes/internal-ipv6-subnet-mode-test-sub-pdp"'
162+
test_vars_overrides:
163+
ip_collection_url: '"projects/tf-static-byoip/regions/us-central1/publicDelegatedPrefixes/internal-ipv6-subnet-mode-test-sub-pdp"'
164+
- name: 'subnetwork_with_internal_subnet_mode_pdp_explicit_ip_prefix'
165+
primary_resource_id: 'subnetwork-with-internal-subnet-mode-pdp-explicit-ip-prefix'
166+
exclude_docs: true
167+
vars:
168+
subnetwork_name: 'subnet-mode-pdp-subnet-internal-prefix'
169+
network_name: 'network-byoipv6-internal-prefix'
170+
internal_ipv6_prefix: '"2001:db8:1::/64"'
171+
ip_collection_url: '"projects/tf-static-byoip/regions/us-central1/publicDelegatedPrefixes/internal-ipv6-subnet-mode-test-sub-pdp-explicit-prefix"'
172+
test_vars_overrides:
173+
internal_ipv6_prefix: 'fmt.Sprintf("2001:db8:1:%d::/64", acctest.RandIntRange(t, 0, 9999))'
174+
ip_collection_url: '"projects/tf-static-byoip/regions/us-central1/publicDelegatedPrefixes/internal-ipv6-subnet-mode-test-sub-pdp-explicit-prefix"'
155175
- name: 'subnetwork_ipv6_only_external'
156176
primary_resource_id: 'subnetwork-ipv6-only'
157177
exclude_docs: true
@@ -461,9 +481,12 @@ properties:
461481
output: true
462482
- name: 'internalIpv6Prefix'
463483
type: String
484+
diff_suppress_func: 'IpDiffSuppress'
464485
description: |
465486
The internal IPv6 address range that is assigned to this subnetwork.
466-
output: true
487+
default_from_api: true
488+
validation:
489+
function: 'verify.ValidateIpCidrRange'
467490
- name: 'externalIpv6Prefix'
468491
type: String
469492
description: |
@@ -475,9 +498,9 @@ properties:
475498
is_missing_in_cai: true
476499
description: |
477500
Resource reference of a PublicDelegatedPrefix. The PDP must be a sub-PDP
478-
in EXTERNAL_IPV6_SUBNETWORK_CREATION mode.
479-
Use one of the following formats to specify a sub-PDP when creating an
480-
IPv6 NetLB forwarding rule using BYOIP:
501+
in EXTERNAL_IPV6_SUBNETWORK_CREATION or INTERNAL_IPV6_SUBNETWORK_CREATION
502+
mode. Use one of the following formats to specify a sub-PDP when creating
503+
a dual stack or IPv6-only subnetwork using BYOIP:
481504
Full resource URL, as in:
482505
* `https://www.googleapis.com/compute/v1/projects/{{projectId}}/regions/{{region}}/publicDelegatedPrefixes/{{sub-pdp-name}}`
483506
Partial URL, as in:

mmv1/templates/terraform/constants/subnetwork.tmpl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,35 @@ func sendSecondaryIpRangeIfEmptyDiff(_ context.Context, diff *schema.ResourceDif
5353

5454
return nil
5555
}
56+
57+
func IpDiffSuppress(_, old, new string, d *schema.ResourceData) bool {
58+
if d.Id() == "" {
59+
return false
60+
}
61+
if old == "" || new == "" {
62+
return old == new
63+
}
64+
addr_equality := false
65+
netmask_equality := false
66+
67+
addr_netmask_old := strings.Split(old, "/")
68+
addr_netmask_new := strings.Split(new, "/")
69+
70+
if (!((len(addr_netmask_old)) == 2 && (len(addr_netmask_new) == 2))) {
71+
return false
72+
}
73+
74+
var addr_old net.IP = net.ParseIP(addr_netmask_old[0])
75+
if addr_old == nil {
76+
return false
77+
}
78+
var addr_new net.IP = net.ParseIP(addr_netmask_new[0])
79+
if addr_new == nil {
80+
return false
81+
}
82+
83+
addr_equality = net.IP.Equal(addr_old, addr_new)
84+
netmask_equality = addr_netmask_old[1] == addr_netmask_new[1]
85+
86+
return addr_equality && netmask_equality
87+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
resource "google_compute_subnetwork" "{{$.PrimaryResourceId}}" {
2+
name = "{{index $.Vars "subnetwork_name"}}"
3+
region = "us-central1"
4+
network = google_compute_network.custom-test-network.id
5+
stack_type = "IPV6_ONLY"
6+
ipv6_access_type = "INTERNAL"
7+
ip_collection = "{{index $.Vars "ip_collection_url"}}"
8+
}
9+
10+
resource "google_compute_network" "custom-test-network" {
11+
name = "{{index $.Vars "network_name"}}"
12+
auto_create_subnetworks = false
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
resource "google_compute_subnetwork" "{{$.PrimaryResourceId}}" {
2+
name = "{{index $.Vars "subnetwork_name"}}"
3+
region = "us-central1"
4+
network = google_compute_network.custom-test-network.id
5+
stack_type = "IPV6_ONLY"
6+
ipv6_access_type = "INTERNAL"
7+
ip_collection = "{{index $.Vars "ip_collection_url"}}"
8+
internal_ipv6_prefix = "{{index $.Vars "internal_ipv6_prefix"}}"
9+
}
10+
11+
resource "google_compute_network" "custom-test-network" {
12+
name = "{{index $.Vars "network_name"}}"
13+
auto_create_subnetworks = false
14+
}

mmv1/third_party/terraform/services/compute/resource_compute_subnetwork_test.go.tmpl

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"testing"
88

9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
910
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1011
"github.com/hashicorp/terraform-plugin-testing/terraform"
1112
"github.com/hashicorp/terraform-provider-google/google/acctest"
@@ -56,6 +57,119 @@ func TestIsShrinkageIpCidr(t *testing.T) {
5657
}
5758
}
5859

60+
func TestIpDiffSuppress(t *testing.T) {
61+
d := &schema.ResourceData{}
62+
d.SetId("test-id")
63+
64+
tests := []struct {
65+
name string
66+
old string
67+
new string
68+
want bool
69+
}{
70+
{
71+
name: "both empty",
72+
old: "",
73+
new: "",
74+
want: true,
75+
},
76+
{
77+
name: "old empty new not",
78+
old: "",
79+
new: "10.0.0.0/24",
80+
want: false,
81+
},
82+
{
83+
name: "new empty old not",
84+
old: "10.0.0.0/24",
85+
new: "",
86+
want: false,
87+
},
88+
{
89+
name: "identical CIDR",
90+
old: "10.0.0.0/24",
91+
new: "10.0.0.0/24",
92+
want: true,
93+
},
94+
{
95+
name: "different address same mask",
96+
old: "10.0.0.0/24",
97+
new: "10.0.1.0/24",
98+
want: false,
99+
},
100+
{
101+
name: "same address different mask",
102+
old: "10.0.0.0/24",
103+
new: "10.0.0.0/16",
104+
want: false,
105+
},
106+
{
107+
name: "different address different mask",
108+
old: "10.0.0.0/24",
109+
new: "192.168.0.0/16",
110+
want: false,
111+
},
112+
{
113+
name: "invalid old CIDR format no mask",
114+
old: "10.0.0.0",
115+
new: "10.0.0.0/24",
116+
want: false,
117+
},
118+
{
119+
name: "invalid new CIDR format no mask",
120+
old: "10.0.0.0/24",
121+
new: "10.0.0.0",
122+
want: false,
123+
},
124+
{
125+
name: "invalid old IP",
126+
old: "256.0.0.0/24",
127+
new: "10.0.0.0/24",
128+
want: false,
129+
},
130+
{
131+
name: "invalid new IP",
132+
old: "10.0.0.0/24",
133+
new: "10.0.0.256/24",
134+
want: false,
135+
},
136+
{
137+
name: "ipv6 identical CIDR",
138+
old: "2001:db8::/32",
139+
new: "2001:db8::/32",
140+
want: true,
141+
},
142+
{
143+
name: "ipv6 different address",
144+
old: "2001:db8::/32",
145+
new: "2001:db9::/32",
146+
want: false,
147+
},
148+
{
149+
name: "ipv6 different mask",
150+
old: "2001:db8::/32",
151+
new: "2001:db8::/48",
152+
want: false,
153+
},
154+
}
155+
156+
for _, tt := range tests {
157+
t.Run(tt.name, func(t *testing.T) {
158+
if got := tpgcompute.IpDiffSuppress("ip_cidr_range", tt.old, tt.new, d); got != tt.want {
159+
t.Errorf("IpDiffSuppress() = %v, want %v", got, tt.want)
160+
}
161+
})
162+
}
163+
}
164+
165+
func TestIpDiffSuppress_NoId(t *testing.T) {
166+
d := &schema.ResourceData{}
167+
168+
if tpgcompute.IpDiffSuppress("ip_cidr_range", "10.0.0.0/24", "10.0.0.0/24", d) != false {
169+
t.Errorf("IpDiffSuppress() with no ID should return false")
170+
}
171+
}
172+
59173
// Acceptance tests
60174

61175
func TestAccComputeSubnetwork_basic(t *testing.T) {

0 commit comments

Comments
 (0)