Skip to content

Commit

Permalink
Add retaining unchanged alias ip ranges and handle permadiff caused b…
Browse files Browse the repository at this point in the history
…y the change (#11321) (#19015)

[upstream:8fbe154440c88dc24388746f2a1cd48929a293a3]

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Aug 6, 2024
1 parent 5ab7d44 commit 541d33b
Show file tree
Hide file tree
Showing 5 changed files with 303 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .changelog/11321.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
compute: fixed maintaining unchanged `alias_ip_range` in `compute_instance` in between PATCH API calls and handled permadiff caused by the change
```
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/hashstructure v1.1.0
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.9.0
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba
golang.org/x/exp v0.0.0-20240409090435-93d18d7e34b8
golang.org/x/net v0.27.0
Expand Down Expand Up @@ -90,6 +91,7 @@ require (
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/oklog/run v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
Expand All @@ -114,4 +116,5 @@ require (
google.golang.org/genproto v0.0.0-20240730163845-b1a4ccb954bf // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240725223205-93522f1f2a9f // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
23 changes: 18 additions & 5 deletions google/services/compute/compute_instance_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,28 @@ func expandAliasIpRanges(ranges []interface{}) []*compute.AliasIpRange {
return ipRanges
}

func flattenAliasIpRange(ranges []*compute.AliasIpRange) []map[string]interface{} {
rangesSchema := make([]map[string]interface{}, 0, len(ranges))
func flattenAliasIpRange(d *schema.ResourceData, ranges []*compute.AliasIpRange, i int) []map[string]interface{} {
prefix := fmt.Sprintf("network_interface.%d", i)

configData := []map[string]interface{}{}
for _, item := range d.Get(prefix + ".alias_ip_range").([]interface{}) {
configData = append(configData, item.(map[string]interface{}))
}

apiData := make([]map[string]interface{}, 0, len(ranges))
for _, ipRange := range ranges {
rangesSchema = append(rangesSchema, map[string]interface{}{
apiData = append(apiData, map[string]interface{}{
"ip_cidr_range": ipRange.IpCidrRange,
"subnetwork_range_name": ipRange.SubnetworkRangeName,
})
}
return rangesSchema

//permadiff fix
sorted, err := tpgresource.SortMapsByConfigOrder(configData, apiData, "ip_cidr_range")
if err != nil {
return apiData
}
return sorted
}

func expandScheduling(v interface{}) (*compute.Scheduling, error) {
Expand Down Expand Up @@ -363,7 +376,7 @@ func flattenNetworkInterfaces(d *schema.ResourceData, config *transport_tpg.Conf
"subnetwork": tpgresource.ConvertSelfLinkToV1(iface.Subnetwork),
"subnetwork_project": subnet.Project,
"access_config": ac,
"alias_ip_range": flattenAliasIpRange(iface.AliasIpRanges),
"alias_ip_range": flattenAliasIpRange(d, iface.AliasIpRanges, i),
"nic_type": iface.NicType,
"stack_type": iface.StackType,
"ipv6_access_config": flattenIpv6AccessConfigs(iface.Ipv6AccessConfigs),
Expand Down
20 changes: 20 additions & 0 deletions google/services/compute/resource_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -1988,6 +1988,9 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err
Fingerprint: instNetworkInterface.Fingerprint,
ForceSendFields: []string{"AliasIpRanges"},
}
if commonAliasIpRanges := CheckForCommonAliasIp(instNetworkInterface, networkInterface); len(commonAliasIpRanges) > 0 {
ni.AliasIpRanges = commonAliasIpRanges
}
op, err := config.NewComputeClient(userAgent).Instances.UpdateNetworkInterface(project, zone, instance.Name, networkName, ni).Do()
if err != nil {
return errwrap.Wrapf("Error removing alias_ip_range: {{err}}", err)
Expand Down Expand Up @@ -3011,3 +3014,20 @@ func isEmptyServiceAccountBlock(d *schema.ResourceData) bool {
}
return false
}

// Alias ip ranges cannot be removed and created at the same time. This checks if there are any unchanged alias ip ranges
// to be kept in between the PATCH operations on Network Interface
func CheckForCommonAliasIp(old, new *compute.NetworkInterface) []*compute.AliasIpRange {
newAliasIpMap := make(map[string]bool)
for _, ipRange := range new.AliasIpRanges {
newAliasIpMap[ipRange.IpCidrRange] = true
}

resultAliasIpRanges := make([]*compute.AliasIpRange, 0)
for _, val := range old.AliasIpRanges {
if newAliasIpMap[val.IpCidrRange] {
resultAliasIpRanges = append(resultAliasIpRanges, val)
}
}
return resultAliasIpRanges
}
Loading

0 comments on commit 541d33b

Please sign in to comment.