Skip to content

Commit 6ba4678

Browse files
vpc network custom mtu support (#4126) (#7567)
Signed-off-by: Modular Magician <[email protected]>
1 parent e5f4bd2 commit 6ba4678

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

.changelog/4126.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
compute: added `mtu` field to `google_compute_network` resource
3+
```

google/resource_compute_network.go

+39
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919
"log"
2020
"reflect"
21+
"strconv"
2122
"time"
2223

2324
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -73,6 +74,14 @@ the user can explicitly connect subnetwork resources.`,
7374
ForceNew: true,
7475
Description: `An optional description of this resource. The resource must be
7576
recreated to modify this field.`,
77+
},
78+
"mtu": {
79+
Type: schema.TypeInt,
80+
Computed: true,
81+
Optional: true,
82+
ForceNew: true,
83+
Description: `Maximum Transmission Unit in bytes. The minimum value for this field is 1460
84+
and the maximum value is 1500 bytes.`,
7685
},
7786
"routing_mode": {
7887
Type: schema.TypeString,
@@ -143,6 +152,12 @@ func resourceComputeNetworkCreate(d *schema.ResourceData, meta interface{}) erro
143152
} else if !isEmptyValue(reflect.ValueOf(routingConfigProp)) {
144153
obj["routingConfig"] = routingConfigProp
145154
}
155+
mtuProp, err := expandComputeNetworkMtu(d.Get("mtu"), d, config)
156+
if err != nil {
157+
return err
158+
} else if v, ok := d.GetOkExists("mtu"); !isEmptyValue(reflect.ValueOf(mtuProp)) && (ok || !reflect.DeepEqual(v, mtuProp)) {
159+
obj["mtu"] = mtuProp
160+
}
146161

147162
url, err := replaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/global/networks")
148163
if err != nil {
@@ -289,6 +304,9 @@ func resourceComputeNetworkRead(d *schema.ResourceData, meta interface{}) error
289304
}
290305
}
291306
}
307+
if err := d.Set("mtu", flattenComputeNetworkMtu(res["mtu"], d, config)); err != nil {
308+
return fmt.Errorf("Error reading Network: %s", err)
309+
}
292310
if err := d.Set("self_link", ConvertSelfLinkToV1(res["selfLink"].(string))); err != nil {
293311
return fmt.Errorf("Error reading Network: %s", err)
294312
}
@@ -458,6 +476,23 @@ func flattenComputeNetworkRoutingConfigRoutingMode(v interface{}, d *schema.Reso
458476
return v
459477
}
460478

479+
func flattenComputeNetworkMtu(v interface{}, d *schema.ResourceData, config *Config) interface{} {
480+
// Handles the string fixed64 format
481+
if strVal, ok := v.(string); ok {
482+
if intVal, err := strconv.ParseInt(strVal, 10, 64); err == nil {
483+
return intVal
484+
}
485+
}
486+
487+
// number values are represented as float64
488+
if floatVal, ok := v.(float64); ok {
489+
intVal := int(floatVal)
490+
return intVal
491+
}
492+
493+
return v // let terraform core handle it otherwise
494+
}
495+
461496
func expandComputeNetworkDescription(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
462497
return v, nil
463498
}
@@ -485,3 +520,7 @@ func expandComputeNetworkRoutingConfig(v interface{}, d TerraformResourceData, c
485520
func expandComputeNetworkRoutingConfigRoutingMode(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
486521
return v, nil
487522
}
523+
524+
func expandComputeNetworkMtu(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
525+
return v, nil
526+
}

google/resource_compute_network_generated_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,42 @@ resource "google_compute_network" "vpc_network" {
5858
`, context)
5959
}
6060

61+
func TestAccComputeNetwork_networkCustomMtuExample(t *testing.T) {
62+
t.Parallel()
63+
64+
context := map[string]interface{}{
65+
"random_suffix": randString(t, 10),
66+
}
67+
68+
vcrTest(t, resource.TestCase{
69+
PreCheck: func() { testAccPreCheck(t) },
70+
Providers: testAccProviders,
71+
ExternalProviders: map[string]resource.ExternalProvider{
72+
"random": {},
73+
},
74+
CheckDestroy: testAccCheckComputeNetworkDestroyProducer(t),
75+
Steps: []resource.TestStep{
76+
{
77+
Config: testAccComputeNetwork_networkCustomMtuExample(context),
78+
},
79+
{
80+
ResourceName: "google_compute_network.vpc_network",
81+
ImportState: true,
82+
ImportStateVerify: true,
83+
},
84+
},
85+
})
86+
}
87+
88+
func testAccComputeNetwork_networkCustomMtuExample(context map[string]interface{}) string {
89+
return Nprintf(`
90+
resource "google_compute_network" "vpc_network" {
91+
name = "tf-test-vpc-network%{random_suffix}"
92+
mtu = 1500
93+
}
94+
`, context)
95+
}
96+
6197
func testAccCheckComputeNetworkDestroyProducer(t *testing.T) func(s *terraform.State) error {
6298
return func(s *terraform.State) error {
6399
for name, rs := range s.RootModule().Resources {

website/docs/r/compute_network.html.markdown

+19
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ resource "google_compute_network" "vpc_network" {
4444
name = "vpc-network"
4545
}
4646
```
47+
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
48+
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.jpy.wang%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=network_custom_mtu&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
49+
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
50+
</a>
51+
</div>
52+
## Example Usage - Network Custom Mtu
53+
54+
55+
```hcl
56+
resource "google_compute_network" "vpc_network" {
57+
name = "vpc-network"
58+
mtu = 1500
59+
}
60+
```
4761

4862
## Argument Reference
4963

@@ -86,6 +100,11 @@ The following arguments are supported:
86100
subnetworks of this network, across regions.
87101
Possible values are `REGIONAL` and `GLOBAL`.
88102

103+
* `mtu` -
104+
(Optional)
105+
Maximum Transmission Unit in bytes. The minimum value for this field is 1460
106+
and the maximum value is 1500 bytes.
107+
89108
* `project` - (Optional) The ID of the project in which the resource belongs.
90109
If it is not provided, the provider project is used.
91110

0 commit comments

Comments
 (0)