diff --git a/.changelog/4126.txt b/.changelog/4126.txt new file mode 100644 index 0000000000..f2ef94a6ea --- /dev/null +++ b/.changelog/4126.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +compute: added `mtu` field to `google_compute_network` resource +``` diff --git a/google-beta/resource_compute_network.go b/google-beta/resource_compute_network.go index cb1cb3d19a..51cbfb27ce 100644 --- a/google-beta/resource_compute_network.go +++ b/google-beta/resource_compute_network.go @@ -18,6 +18,7 @@ import ( "fmt" "log" "reflect" + "strconv" "time" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -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, @@ -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 { @@ -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) } @@ -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 } @@ -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 +} diff --git a/google-beta/resource_compute_network_generated_test.go b/google-beta/resource_compute_network_generated_test.go index 9aadebd184..4324d04e91 100644 --- a/google-beta/resource_compute_network_generated_test.go +++ b/google-beta/resource_compute_network_generated_test.go @@ -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 { diff --git a/website/docs/r/compute_network.html.markdown b/website/docs/r/compute_network.html.markdown index c58617a3fa..d844f4a2c6 100644 --- a/website/docs/r/compute_network.html.markdown +++ b/website/docs/r/compute_network.html.markdown @@ -44,6 +44,20 @@ resource "google_compute_network" "vpc_network" { name = "vpc-network" } ``` +
+## Example Usage - Network Custom Mtu + + +```hcl +resource "google_compute_network" "vpc_network" { + name = "vpc-network" + mtu = 1500 +} +``` ## Argument Reference @@ -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.