-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compute - Add update support for Network IP when changing network/sub…
…network (#4030) (#7515) * Compute - Add update support for Network IP when changing network/subnetwork * add field required by beta provider * error check for d.ForceNew * refactor functions into own file. introduce network-interface-helper * refresh instance after deleting alias config * correct bad merge * spelling fix * check error * Add forcenew unit test * resolve build issue * Update third_party/terraform/tests/resource_compute_instance_test.go.erb Co-authored-by: Cameron Thornton <[email protected]> * Update third_party/terraform/tests/resource_compute_instance_test.go.erb Co-authored-by: Cameron Thornton <[email protected]> * error couldn't convert to string * to type string * Refactored some test code for cleaner style and condensed some if statements * resolve merge issue with megan's change * update network interface helper to promote better go readability * fixed instances where incorrect name was referenced * removed extraneous dependencies and extrapolated self mutating operations * scrap refactor. only extrapolate functions when needed. * error check * resolved some fomatting and comment concerns. * find a comment a nice cozy new home on the hill side amongst all its comment friends. Truly truly a beautiful site to behold. Please be well and safe comment because the world needs you. YOU TOO are important * a comma boi * another commma for the party Co-authored-by: Cameron Thornton <[email protected]> Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Cameron Thornton <[email protected]>
- Loading branch information
1 parent
d6e2454
commit c3dbb2d
Showing
6 changed files
with
308 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
```release-note:enhancement | ||
compute: added support for updating `network_interface.[d].network_ip` on `google_compute_instance` when changing network or subnetwork | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/errwrap" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
computeBeta "google.golang.org/api/compute/v0.beta" | ||
) | ||
|
||
func computeInstanceDeleteAccessConfigs(d *schema.ResourceData, config *Config, instNetworkInterface *computeBeta.NetworkInterface, project, zone, userAgent, instanceName string) error { | ||
// Delete any accessConfig that currently exists in instNetworkInterface | ||
for _, ac := range instNetworkInterface.AccessConfigs { | ||
op, err := config.NewComputeClient(userAgent).Instances.DeleteAccessConfig( | ||
project, zone, instanceName, ac.Name, instNetworkInterface.Name).Do() | ||
if err != nil { | ||
return fmt.Errorf("Error deleting old access_config: %s", err) | ||
} | ||
opErr := computeOperationWaitTime(config, op, project, "old access_config to delete", userAgent, d.Timeout(schema.TimeoutUpdate)) | ||
if opErr != nil { | ||
return opErr | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func computeInstanceAddAccessConfigs(d *schema.ResourceData, config *Config, instNetworkInterface *computeBeta.NetworkInterface, accessConfigs []*computeBeta.AccessConfig, project, zone, userAgent, instanceName string) error { | ||
// Create new ones | ||
for _, ac := range accessConfigs { | ||
op, err := config.NewComputeBetaClient(userAgent).Instances.AddAccessConfig(project, zone, instanceName, instNetworkInterface.Name, ac).Do() | ||
if err != nil { | ||
return fmt.Errorf("Error adding new access_config: %s", err) | ||
} | ||
opErr := computeOperationWaitTime(config, op, project, "new access_config to add", userAgent, d.Timeout(schema.TimeoutUpdate)) | ||
if opErr != nil { | ||
return opErr | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func computeInstanceCreateUpdateWhileStoppedCall(d *schema.ResourceData, config *Config, networkInterfacePatchObj *computeBeta.NetworkInterface, accessConfigs []*computeBeta.AccessConfig, accessConfigsHaveChanged bool, index int, project, zone, userAgent, instanceName string) func(inst *computeBeta.Instance) error { | ||
|
||
// Access configs' ip changes when the instance stops invalidating our fingerprint | ||
// expect caller to re-validate instance before calling patch this is why we expect | ||
// instance to be passed in | ||
return func(instance *computeBeta.Instance) error { | ||
|
||
instNetworkInterface := instance.NetworkInterfaces[index] | ||
networkInterfacePatchObj.Fingerprint = instNetworkInterface.Fingerprint | ||
|
||
// Access config can run into some issues since we can't tell the difference between | ||
// the users declared intent (config within their hcl file) and what we have inferred from the | ||
// server (terraform state). Access configs contain an ip subproperty that can be incompatible | ||
// with the subnetwork/network we are transitioning to. Due to this we only change access | ||
// configs if we notice the configuration (user intent) changes. | ||
if accessConfigsHaveChanged { | ||
err := computeInstanceDeleteAccessConfigs(d, config, instNetworkInterface, project, zone, userAgent, instanceName) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
op, err := config.NewComputeBetaClient(userAgent).Instances.UpdateNetworkInterface(project, zone, instanceName, instNetworkInterface.Name, networkInterfacePatchObj).Do() | ||
if err != nil { | ||
return errwrap.Wrapf("Error updating network interface: {{err}}", err) | ||
} | ||
opErr := computeOperationWaitTime(config, op, project, "network interface to update", userAgent, d.Timeout(schema.TimeoutUpdate)) | ||
if opErr != nil { | ||
return opErr | ||
} | ||
|
||
if accessConfigsHaveChanged { | ||
err := computeInstanceAddAccessConfigs(d, config, instNetworkInterface, accessConfigs, project, zone, userAgent, instanceName) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.