Skip to content

Commit

Permalink
Adding ula internal ipv6 support in compute_network and subnetwork (#…
Browse files Browse the repository at this point in the history
…6105) (#11842)

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician committed Jun 7, 2022
1 parent 77f0b70 commit 30d4fdf
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .changelog/6105.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
compute: added internal IPv6 support on `google_compute_network` and `google_compute_subnetwork`
```
52 changes: 52 additions & 0 deletions google/resource_compute_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,24 @@ the user can explicitly connect subnetwork resources.`,
ForceNew: true,
Description: `An optional description of this resource. The resource must be
recreated to modify this field.`,
},
"enable_ula_internal_ipv6": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: `Enable ULA internal ipv6 on this network. Enabling this feature will assign
a /48 from google defined ULA prefix fd20::/20.`,
},
"internal_ipv6_range": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
Description: `When enabling ula internal ipv6, caller optionally can specify the /48 range
they want from the google defined ULA prefix fd20::/20. The input must be a
valid /48 ULA IPv6 address and must be within the fd20::/20. Operation will
fail if the speficied /48 is already in used by another resource.
If the field is not speficied, then a /48 range will be randomly allocated from fd20::/20 and returned via this field.`,
},
"mtu": {
Type: schema.TypeInt,
Expand Down Expand Up @@ -157,6 +175,18 @@ func resourceComputeNetworkCreate(d *schema.ResourceData, meta interface{}) erro
} else if v, ok := d.GetOkExists("mtu"); !isEmptyValue(reflect.ValueOf(mtuProp)) && (ok || !reflect.DeepEqual(v, mtuProp)) {
obj["mtu"] = mtuProp
}
enableUlaInternalIpv6Prop, err := expandComputeNetworkEnableUlaInternalIpv6(d.Get("enable_ula_internal_ipv6"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("enable_ula_internal_ipv6"); !isEmptyValue(reflect.ValueOf(enableUlaInternalIpv6Prop)) && (ok || !reflect.DeepEqual(v, enableUlaInternalIpv6Prop)) {
obj["enableUlaInternalIpv6"] = enableUlaInternalIpv6Prop
}
internalIpv6RangeProp, err := expandComputeNetworkInternalIpv6Range(d.Get("internal_ipv6_range"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("internal_ipv6_range"); !isEmptyValue(reflect.ValueOf(internalIpv6RangeProp)) && (ok || !reflect.DeepEqual(v, internalIpv6RangeProp)) {
obj["internalIpv6Range"] = internalIpv6RangeProp
}

url, err := replaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/global/networks")
if err != nil {
Expand Down Expand Up @@ -306,6 +336,12 @@ func resourceComputeNetworkRead(d *schema.ResourceData, meta interface{}) error
if err := d.Set("mtu", flattenComputeNetworkMtu(res["mtu"], d, config)); err != nil {
return fmt.Errorf("Error reading Network: %s", err)
}
if err := d.Set("enable_ula_internal_ipv6", flattenComputeNetworkEnableUlaInternalIpv6(res["enableUlaInternalIpv6"], d, config)); err != nil {
return fmt.Errorf("Error reading Network: %s", err)
}
if err := d.Set("internal_ipv6_range", flattenComputeNetworkInternalIpv6Range(res["internalIpv6Range"], d, config)); err != nil {
return fmt.Errorf("Error reading Network: %s", err)
}
if err := d.Set("self_link", ConvertSelfLinkToV1(res["selfLink"].(string))); err != nil {
return fmt.Errorf("Error reading Network: %s", err)
}
Expand Down Expand Up @@ -490,6 +526,14 @@ func flattenComputeNetworkMtu(v interface{}, d *schema.ResourceData, config *Con
return v // let terraform core handle it otherwise
}

func flattenComputeNetworkEnableUlaInternalIpv6(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func flattenComputeNetworkInternalIpv6Range(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func expandComputeNetworkDescription(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down Expand Up @@ -521,3 +565,11 @@ func expandComputeNetworkRoutingConfigRoutingMode(v interface{}, d TerraformReso
func expandComputeNetworkMtu(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeNetworkEnableUlaInternalIpv6(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeNetworkInternalIpv6Range(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
4 changes: 2 additions & 2 deletions google/resource_compute_subnetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ creation time.`,
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validateEnum([]string{"EXTERNAL", ""}),
ValidateFunc: validateEnum([]string{"EXTERNAL", "INTERNAL", ""}),
Description: `The access type of IPv6 address this subnet holds. It's immutable and can only be specified during creation
or the first time the subnet is updated into IPV4_IPV6 dual stack. If the ipv6_type is EXTERNAL then this subnet
cannot enable direct path. Possible values: ["EXTERNAL"]`,
cannot enable direct path. Possible values: ["EXTERNAL", "INTERNAL"]`,
},
"log_config": {
Type: schema.TypeList,
Expand Down
47 changes: 47 additions & 0 deletions google/resource_compute_subnetwork_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,53 @@ resource "google_compute_network" "custom-test" {
`, context)
}

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

context := map[string]interface{}{
"random_suffix": randString(t, 10),
}

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeSubnetworkDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeSubnetwork_subnetworkInternalIpv6Example(context),
},
{
ResourceName: "google_compute_subnetwork.subnetwork-internal-ipv6",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"network", "region"},
},
},
})
}

func testAccComputeSubnetwork_subnetworkInternalIpv6Example(context map[string]interface{}) string {
return Nprintf(`
resource "google_compute_subnetwork" "subnetwork-internal-ipv6" {
name = "tf-test-internal-ipv6-test-subnetwork%{random_suffix}"
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 = "tf-test-internal-ipv6-test-network%{random_suffix}"
auto_create_subnetworks = false
enable_ula_internal_ipv6 = true
}
`, context)
}

func testAccCheckComputeSubnetworkDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
Expand Down
13 changes: 13 additions & 0 deletions website/docs/r/compute_network.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@ The following arguments are supported:
Maximum Transmission Unit in bytes. The minimum value for this field is 1460
and the maximum value is 1500 bytes.

* `enable_ula_internal_ipv6` -
(Optional)
Enable ULA internal ipv6 on this network. Enabling this feature will assign
a /48 from google defined ULA prefix fd20::/20.

* `internal_ipv6_range` -
(Optional)
When enabling ula internal ipv6, caller optionally can specify the /48 range
they want from the google defined ULA prefix fd20::/20. The input must be a
valid /48 ULA IPv6 address and must be within the fd20::/20. Operation will
fail if the speficied /48 is already in used by another resource.
If the field is not speficied, then a /48 range will be randomly allocated from fd20::/20 and returned via this field.

* `project` - (Optional) The ID of the project in which the resource belongs.
If it is not provided, the provider project is used.

Expand Down
29 changes: 28 additions & 1 deletion website/docs/r/compute_subnetwork.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,33 @@ resource "google_compute_network" "custom-test" {
auto_create_subnetworks = false
}
```
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgit.luolix.top%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=subnetwork_internal_ipv6&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
</a>
</div>
## Example Usage - Subnetwork Internal Ipv6


```hcl
resource "google_compute_subnetwork" "subnetwork-internal-ipv6" {
name = "internal-ipv6-test-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 = "internal-ipv6-test-network"
auto_create_subnetworks = false
enable_ula_internal_ipv6 = true
}
```

## Argument Reference

Expand Down Expand Up @@ -257,7 +284,7 @@ The following arguments are supported:
The access type of IPv6 address this subnet holds. It's immutable and can only be specified during creation
or the first time the subnet is updated into IPV4_IPV6 dual stack. If the ipv6_type is EXTERNAL then this subnet
cannot enable direct path.
Possible values are `EXTERNAL`.
Possible values are `EXTERNAL` and `INTERNAL`.

* `project` - (Optional) The ID of the project in which the resource belongs.
If it is not provided, the provider project is used.
Expand Down

0 comments on commit 30d4fdf

Please sign in to comment.