Skip to content

Commit

Permalink
vpc network custom mtu support (#4126) (#2617)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Oct 19, 2020
1 parent eaea0c1 commit 2fd3bea
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/4126.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
compute: added `mtu` field to `google_compute_network` resource
```
39 changes: 39 additions & 0 deletions google-beta/resource_compute_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"log"
"reflect"
"strconv"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -73,6 +74,14 @@ 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.`,
},
"mtu": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
ForceNew: true,
Description: `Maximum Transmission Unit in bytes. The minimum value for this field is 1460
and the maximum value is 1500 bytes.`,
},
"routing_mode": {
Type: schema.TypeString,
Expand Down Expand Up @@ -143,6 +152,12 @@ func resourceComputeNetworkCreate(d *schema.ResourceData, meta interface{}) erro
} else if !isEmptyValue(reflect.ValueOf(routingConfigProp)) {
obj["routingConfig"] = routingConfigProp
}
mtuProp, err := expandComputeNetworkMtu(d.Get("mtu"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("mtu"); !isEmptyValue(reflect.ValueOf(mtuProp)) && (ok || !reflect.DeepEqual(v, mtuProp)) {
obj["mtu"] = mtuProp
}

url, err := replaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/global/networks")
if err != nil {
Expand Down Expand Up @@ -289,6 +304,9 @@ 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("self_link", ConvertSelfLinkToV1(res["selfLink"].(string))); err != nil {
return fmt.Errorf("Error reading Network: %s", err)
}
Expand Down Expand Up @@ -458,6 +476,23 @@ func flattenComputeNetworkRoutingConfigRoutingMode(v interface{}, d *schema.Reso
return v
}

func flattenComputeNetworkMtu(v interface{}, d *schema.ResourceData, config *Config) interface{} {
// Handles the string fixed64 format
if strVal, ok := v.(string); ok {
if intVal, err := strconv.ParseInt(strVal, 10, 64); err == nil {
return intVal
}
}

// number values are represented as float64
if floatVal, ok := v.(float64); ok {
intVal := int(floatVal)
return intVal
}

return v // let terraform core handle it otherwise
}

func expandComputeNetworkDescription(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down Expand Up @@ -485,3 +520,7 @@ func expandComputeNetworkRoutingConfig(v interface{}, d TerraformResourceData, c
func expandComputeNetworkRoutingConfigRoutingMode(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeNetworkMtu(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
36 changes: 36 additions & 0 deletions google-beta/resource_compute_network_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,42 @@ resource "google_compute_network" "vpc_network" {
`, context)
}

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

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

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
ExternalProviders: map[string]resource.ExternalProvider{
"random": {},
},
CheckDestroy: testAccCheckComputeNetworkDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeNetwork_networkCustomMtuExample(context),
},
{
ResourceName: "google_compute_network.vpc_network",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccComputeNetwork_networkCustomMtuExample(context map[string]interface{}) string {
return Nprintf(`
resource "google_compute_network" "vpc_network" {
name = "tf-test-vpc-network%{random_suffix}"
mtu = 1500
}
`, context)
}

func testAccCheckComputeNetworkDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
Expand Down
19 changes: 19 additions & 0 deletions website/docs/r/compute_network.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ resource "google_compute_network" "vpc_network" {
name = "vpc-network"
}
```
<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=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">
<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 - Network Custom Mtu


```hcl
resource "google_compute_network" "vpc_network" {
name = "vpc-network"
mtu = 1500
}
```

## Argument Reference

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

* `mtu` -
(Optional)
Maximum Transmission Unit in bytes. The minimum value for this field is 1460
and the maximum value is 1500 bytes.

* `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 2fd3bea

Please sign in to comment.