Skip to content
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

Add internal IPV6 support for GoogleComputeAddress and ComputeInstance resources #6232

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
4 changes: 4 additions & 0 deletions .changelog/8432.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
```release-note:enhancement
compute: added `ipVersion` field to `google_compute_address` resource
compute: added `ipv6Address` field to `google_compute_instance` resource
```
2 changes: 2 additions & 0 deletions google-beta/services/compute/compute_instance_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ func flattenNetworkInterfaces(d *schema.ResourceData, config *transport_tpg.Conf
"nic_type": iface.NicType,
"stack_type": iface.StackType,
"ipv6_access_config": flattenIpv6AccessConfigs(iface.Ipv6AccessConfigs),
"ipv6_address": iface.Ipv6Address,
"queue_count": iface.QueueCount,
}
// Instance template interfaces never have names, so they're absent
Expand Down Expand Up @@ -455,6 +456,7 @@ func expandNetworkInterfaces(d tpgresource.TerraformResourceData, config *transp
StackType: data["stack_type"].(string),
QueueCount: int64(data["queue_count"].(int)),
Ipv6AccessConfigs: expandIpv6AccessConfigs(data["ipv6_access_config"].([]interface{})),
Ipv6Address: data["ipv6_address"].(string),
}
}
return ifaces, nil
Expand Down
50 changes: 50 additions & 0 deletions google-beta/services/compute/resource_compute_address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,53 @@ resource "google_compute_address" "foobar" {
}
`, i)
}

func TestAccComputeAddress_internalIpv6(t *testing.T) {
acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckComputeAddressDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeAddress_internalIpv6(acctest.RandString(t, 10)),
},
{
ResourceName: "google_compute_address.ipv6",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccComputeAddress_internalIpv6(i string) string {
return fmt.Sprintf(`
resource "google_compute_network" "default" {
name = "tf-test-network-test-%s"
enable_ula_internal_ipv6 = true
auto_create_subnetworks = false
}

resource "google_compute_subnetwork" "foo" {
name = "subnetwork-test-%s"
ip_cidr_range = "10.0.0.0/16"
region = "us-east1"
network = google_compute_network.default.self_link
stack_type = "IPV4_IPV6"
ipv6_access_type = "INTERNAL"
}

resource "google_compute_address" "ipv6" {
name = "tf-test-address-internal-ipv6-%s"
subnetwork = google_compute_subnetwork.foo.self_link
region = "us-east1"
address_type = "INTERNAL"
purpose = "GCE_ENDPOINT"
ip_version = "IPV6"
}
`,
i, // google_compute_network.default name
i, // google_compute_subnetwork.foo name
i, // google_compute_address.ipv6
)
}
57 changes: 57 additions & 0 deletions google-beta/services/compute/resource_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,21 @@ func ResourceComputeInstance() *schema.Resource {
},
},

"internal_ipv6_prefix_length": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: `The prefix length of the primary internal IPv6 range.`,
},

"ipv6_address": {
Type: schema.TypeString,
Optional: true,
Computed: true,
DiffSuppressFunc: ipv6RepresentationDiffSuppress,
Description: `An IPv6 internal network address for this network interface. If not specified, Google Cloud will automatically assign an internal IPv6 address from the instance's subnetwork.`,
},

"queue_count": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -1831,6 +1846,40 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err
}
}

if !updateDuringStop && d.HasChange(prefix+".ipv6_address") {

networkInterfacePatchObj := &compute.NetworkInterface{
Ipv6Address: d.Get(prefix + ".ipv6_address").(string),
Fingerprint: instNetworkInterface.Fingerprint,
}
updateCall := config.NewComputeClient(userAgent).Instances.UpdateNetworkInterface(project, zone, instance.Name, networkName, networkInterfacePatchObj).Do
op, err := updateCall()
if err != nil {
return errwrap.Wrapf("Error updating network interface: {{err}}", err)
}
opErr := ComputeOperationWaitTime(config, op, project, "network interface to update", userAgent, d.Timeout(schema.TimeoutUpdate))
if opErr != nil {
return opErr
}
}

if !updateDuringStop && d.HasChange(prefix+".internal_ipv6_prefix_length") {

networkInterfacePatchObj := &compute.NetworkInterface{
InternalIpv6PrefixLength: d.Get(prefix + ".internal_ipv6_prefix_length").(int64),
Fingerprint: instNetworkInterface.Fingerprint,
}
updateCall := config.NewComputeClient(userAgent).Instances.UpdateNetworkInterface(project, zone, instance.Name, networkName, networkInterfacePatchObj).Do
op, err := updateCall()
if err != nil {
return errwrap.Wrapf("Error updating network interface: {{err}}", err)
}
opErr := ComputeOperationWaitTime(config, op, project, "network interface to update", userAgent, d.Timeout(schema.TimeoutUpdate))
if opErr != nil {
return opErr
}
}

if updateDuringStop {
// Lets be explicit about what we are changing in the patch call
networkInterfacePatchObj := &compute.NetworkInterface{
Expand All @@ -1845,6 +1894,14 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err
networkInterfacePatchObj.NetworkIP = networkInterface.NetworkIP
}

if d.HasChange(prefix + ".internal_ipv6_prefix_length") {
networkInterfacePatchObj.Ipv6Address = networkInterface.Ipv6Address
}

if d.HasChange(prefix + ".ipv6_address") {
networkInterfacePatchObj.Ipv6Address = networkInterface.Ipv6Address
}

// Access config can run into some issues since we can't tell the difference between
// the users declared intent (config within their hcl file) and what we have inferred from the
// server (terraform state). Access configs contain an ip subproperty that can be incompatible
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,19 @@ Google Cloud KMS.`,
},
},
},
"internal_ipv6_prefix_length": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: `The prefix length of the primary internal IPv6 range.`,
},
"ipv6_address": {
Type: schema.TypeString,
Optional: true,
Computed: true,
DiffSuppressFunc: ipv6RepresentationDiffSuppress,
Description: `An IPv6 internal network address for this network interface. If not specified, Google Cloud will automatically assign an internal IPv6 address from the instance's subnetwork.`,
},
"queue_count": {
Type: schema.TypeInt,
Optional: true,
Expand Down
96 changes: 96 additions & 0 deletions google-beta/services/compute/resource_compute_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,31 @@ func TestAccComputeInstance_ipv6ExternalReservation(t *testing.T) {
})
}

func TestAccComputeInstance_internalIPv6(t *testing.T) {
t.Parallel()

var instance compute.Instance
var ipName = fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
var instanceName = fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckComputeInstanceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeInstance_internalIpv6(ipName, instanceName),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceExists(
t, "google_compute_instance.foobar", &instance),
testAccCheckComputeInstanceIpv6AccessConfigHasInternalIPv6(&instance),
),
},
computeInstanceImportStep("us-west2-a", instanceName, []string{}),
},
})
}

func TestAccComputeInstance_PTRRecord(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -2714,6 +2739,18 @@ func testAccCheckComputeInstanceIpv6AccessConfigHasExternalIPv6(instance *comput
}
}

func testAccCheckComputeInstanceIpv6AccessConfigHasInternalIPv6(instance *compute.Instance) resource.TestCheckFunc {
return func(s *terraform.State) error {
for _, i := range instance.NetworkInterfaces {
if i.Ipv6Address == "" {
return fmt.Errorf("no internal IPv6 address")
}
}

return nil
}
}

func testAccCheckComputeInstanceAccessConfigHasPTR(instance *compute.Instance) resource.TestCheckFunc {
return func(s *terraform.State) error {
for _, i := range instance.NetworkInterfaces {
Expand Down Expand Up @@ -3720,6 +3757,65 @@ resource "google_compute_instance" "foobar" {
`, instance, instance, ip, instance, record)
}

func testAccComputeInstance_internalIpv6(ip, instance string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
family = "debian-11"
project = "debian-cloud"
}

resource "google_compute_subnetwork" "subnetwork_ipv6" {
name = "%s-subnetwork"

ip_cidr_range = "10.0.0.0/22"
region = "us-west2"

stack_type = "IPV4_IPV6"
ipv6_access_type = "INTERNAL"

network = google_compute_network.custom-test.id
}

resource "google_compute_network" "custom-test" {
name = "%s-network"
enable_ula_internal_ipv6 = true
auto_create_subnetworks = false
}

resource "google_compute_address" "ipv6" {
name = "%s"
region = "us-west2"
address_type = "INTERNAL"
purpose = "GCE_ENDPOINT"
subnetwork = google_compute_subnetwork.subnetwork_ipv6.id
ip_version = "IPV6"
}

resource "google_compute_instance" "foobar" {
name = "%s"
machine_type = "e2-medium"
zone = "us-west2-a"
tags = ["foo", "bar"]

boot_disk {
initialize_params {
image = data.google_compute_image.my_image.self_link
}
}

network_interface {
subnetwork = google_compute_subnetwork.subnetwork_ipv6.name
stack_type = "IPV4_IPV6"
ipv6_address = google_compute_address.ipv6.address
}

metadata = {
foo = "bar"
}
}
`, instance, instance, ip, instance)
}

func testAccComputeInstance_ipv6ExternalReservation(instance string) string {
return fmt.Sprintf(`
resource "google_compute_address" "ipv6-address" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,19 @@ Google Cloud KMS.`,
},
},
},
"internal_ipv6_prefix_length": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: `The prefix length of the primary internal IPv6 range.`,
},
"ipv6_address": {
Type: schema.TypeString,
Optional: true,
Computed: true,
DiffSuppressFunc: ipv6RepresentationDiffSuppress,
Description: `An IPv6 internal network address for this network interface. If not specified, Google Cloud will automatically assign an internal IPv6 address from the instance's subnetwork.`,
},
"queue_count": {
Type: schema.TypeInt,
Optional: true,
Expand Down