Skip to content

Commit 86a5a7a

Browse files
Allow updates to the mtu property in the Network resource. (#13995) (#22956)
[upstream:a6159afa29a77caf590505ba9afa117acc3b6e24] Signed-off-by: Modular Magician <[email protected]>
1 parent 1ce6c68 commit 86a5a7a

File tree

2 files changed

+146
-1
lines changed

2 files changed

+146
-1
lines changed

google/services/compute/resource_compute_network.go

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ If the field is not speficied, then a /48 range will be randomly allocated from
112112
Type: schema.TypeInt,
113113
Computed: true,
114114
Optional: true,
115-
ForceNew: true,
116115
Description: `Maximum Transmission Unit in bytes. The default value is 1460 bytes.
117116
The minimum value for this field is 1300 and the maximum value is 8896 bytes (jumbo frames).
118117
Note that packets larger than 1500 bytes (standard Ethernet) can be subject to TCP-MSS clamping or dropped
@@ -552,6 +551,79 @@ func resourceComputeNetworkUpdate(d *schema.ResourceData, meta interface{}) erro
552551
return err
553552
}
554553
}
554+
if d.HasChange("mtu") {
555+
obj := make(map[string]interface{})
556+
557+
getUrl, err := tpgresource.ReplaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/global/networks/{{name}}")
558+
if err != nil {
559+
return err
560+
}
561+
562+
// err == nil indicates that the billing_project value was found
563+
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
564+
billingProject = bp
565+
}
566+
567+
getRes, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
568+
Config: config,
569+
Method: "GET",
570+
Project: billingProject,
571+
RawURL: getUrl,
572+
UserAgent: userAgent,
573+
})
574+
if err != nil {
575+
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("ComputeNetwork %q", d.Id()))
576+
}
577+
578+
obj["fingerprint"] = getRes["fingerprint"]
579+
580+
mtuProp, err := expandComputeNetworkMtu(d.Get("mtu"), d, config)
581+
if err != nil {
582+
return err
583+
} else if v, ok := d.GetOkExists("mtu"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, mtuProp)) {
584+
obj["mtu"] = mtuProp
585+
}
586+
587+
obj, err = resourceComputeNetworkUpdateEncoder(d, meta, obj)
588+
if err != nil {
589+
return err
590+
}
591+
592+
url, err := tpgresource.ReplaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/global/networks/{{name}}")
593+
if err != nil {
594+
return err
595+
}
596+
597+
headers := make(http.Header)
598+
599+
// err == nil indicates that the billing_project value was found
600+
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
601+
billingProject = bp
602+
}
603+
604+
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
605+
Config: config,
606+
Method: "PATCH",
607+
Project: billingProject,
608+
RawURL: url,
609+
UserAgent: userAgent,
610+
Body: obj,
611+
Timeout: d.Timeout(schema.TimeoutUpdate),
612+
Headers: headers,
613+
})
614+
if err != nil {
615+
return fmt.Errorf("Error updating Network %q: %s", d.Id(), err)
616+
} else {
617+
log.Printf("[DEBUG] Finished updating Network %q: %#v", d.Id(), res)
618+
}
619+
620+
err = ComputeOperationWaitTime(
621+
config, res, project, "Updating Network", userAgent,
622+
d.Timeout(schema.TimeoutUpdate))
623+
if err != nil {
624+
return err
625+
}
626+
}
555627

556628
d.Partial(false)
557629

google/services/compute/resource_compute_network_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,41 @@ func TestAccComputeNetwork_customSubnet(t *testing.T) {
9090
})
9191
}
9292

93+
func TestAccComputeNetwork_mtuAndUpdate(t *testing.T) {
94+
t.Parallel()
95+
96+
var network compute.Network
97+
suffixName := acctest.RandString(t, 10)
98+
networkName := fmt.Sprintf("tf-test-network-routing-mode-%s", suffixName)
99+
100+
acctest.VcrTest(t, resource.TestCase{
101+
PreCheck: func() { acctest.AccTestPreCheck(t) },
102+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
103+
CheckDestroy: testAccCheckComputeNetworkDestroyProducer(t),
104+
Steps: []resource.TestStep{
105+
{
106+
Config: testAccComputeNetwork_mtu(networkName, 1460),
107+
Check: resource.ComposeTestCheckFunc(
108+
testAccCheckComputeNetworkExists(
109+
t, "google_compute_network.acc_network_mtu", &network),
110+
testAccCheckComputeNetworkHasMtu(
111+
t, "google_compute_network.acc_network_mtu", &network, 1460),
112+
),
113+
},
114+
// Test updating the mtu field from 1460 to 1500.
115+
{
116+
Config: testAccComputeNetwork_mtu(networkName, 1500),
117+
Check: resource.ComposeTestCheckFunc(
118+
testAccCheckComputeNetworkExists(
119+
t, "google_compute_network.acc_network_mtu", &network),
120+
testAccCheckComputeNetworkHasMtu(
121+
t, "google_compute_network.acc_network_mtu", &network, 1500),
122+
),
123+
},
124+
},
125+
})
126+
}
127+
93128
func TestAccComputeNetwork_routingModeAndUpdate(t *testing.T) {
94129
t.Parallel()
95130

@@ -537,6 +572,35 @@ func testAccCheckComputeNetworkIsCustomSubnet(t *testing.T, n string, network *c
537572
}
538573
}
539574

575+
func testAccCheckComputeNetworkHasMtu(t *testing.T, n string, network *compute.Network, mtu int32) resource.TestCheckFunc {
576+
return func(s *terraform.State) error {
577+
config := acctest.GoogleProviderConfig(t)
578+
579+
rs, ok := s.RootModule().Resources[n]
580+
if !ok {
581+
return fmt.Errorf("Not found: %s", n)
582+
}
583+
584+
if rs.Primary.Attributes["mtu"] == "" {
585+
return fmt.Errorf("Routing mode not found on resource")
586+
}
587+
588+
found, err := config.NewComputeClient(config.UserAgent).Networks.Get(
589+
config.Project, network.Name).Do()
590+
if err != nil {
591+
return err
592+
}
593+
594+
foundMtu := found.Mtu
595+
596+
if int64(mtu) != foundMtu {
597+
return fmt.Errorf("Expected mtu %d to match actual routing mode %d", mtu, foundMtu)
598+
}
599+
600+
return nil
601+
}
602+
}
603+
540604
func testAccCheckComputeNetworkHasRoutingMode(t *testing.T, n string, network *compute.Network, routingMode string) resource.TestCheckFunc {
541605
return func(s *terraform.State) error {
542606
config := acctest.GoogleProviderConfig(t)
@@ -651,6 +715,15 @@ resource "google_compute_network" "baz" {
651715
`, networkName)
652716
}
653717

718+
func testAccComputeNetwork_mtu(networkName string, mtu int32) string {
719+
return fmt.Sprintf(`
720+
resource "google_compute_network" "acc_network_mtu" {
721+
name = "%s"
722+
mtu = %d
723+
}
724+
`, networkName, mtu)
725+
}
726+
654727
func testAccComputeNetwork_routing_mode(networkName, routingMode string) string {
655728
return fmt.Sprintf(`
656729
resource "google_compute_network" "acc_network_routing_mode" {

0 commit comments

Comments
 (0)