Skip to content

Commit

Permalink
Fix resource_compute_shared_reservation_update encoder (#11007) (#18498)
Browse files Browse the repository at this point in the history
[upstream:12c48c9980c5001d2cae723de54485a324bd990c]

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician committed Jun 20, 2024
1 parent d3f1dbe commit f53ee08
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .changelog/11007.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
compute: fixed a crash in `google_compute_reservation` resource where `share_settings` field has changes
```
34 changes: 26 additions & 8 deletions google/services/compute/resource_compute_reservation.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ for information on available CPU platforms.`,
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
ValidateFunc: verify.ValidateEnum([]string{"LOCAL", "SPECIFIC_PROJECTS", ""}),
Description: `Type of sharing for this shared-reservation Possible values: ["LOCAL", "SPECIFIC_PROJECTS"]`,
},
Expand Down Expand Up @@ -1128,10 +1129,27 @@ func resourceComputeReservationUpdateEncoder(d *schema.ResourceData, meta interf
// Set project_map.
projectMap := make(map[string]interface{})
old, new := d.GetChange("share_settings")
oldMap := old.([]interface{})[0].(map[string]interface{})["project_map"]
newMap := new.([]interface{})[0].(map[string]interface{})["project_map"]
before := oldMap.(*schema.Set)
after := newMap.(*schema.Set)

var before *schema.Set
if oldSlice, ok := old.([]interface{}); ok && len(oldSlice) > 0 {
if oldMap, ok := oldSlice[0].(map[string]interface{})["project_map"]; ok {
before = oldMap.(*schema.Set)
} else {
before = schema.NewSet(schema.HashString, []interface{}{})
}
} else {
before = schema.NewSet(schema.HashString, []interface{}{})
}
var after *schema.Set
if newSlice, ok := new.([]interface{}); ok && len(newSlice) > 0 {
if newMap, ok := newSlice[0].(map[string]interface{})["project_map"]; ok {
after = newMap.(*schema.Set)
} else {
after = schema.NewSet(schema.HashString, []interface{}{})
}
} else {
after = schema.NewSet(schema.HashString, []interface{}{})
}

for _, raw := range after.Difference(before).List() {
original := raw.(map[string]interface{})
Expand All @@ -1147,10 +1165,10 @@ func resourceComputeReservationUpdateEncoder(d *schema.ResourceData, meta interf
}
projectMap[transformedId] = singleProject
// add added projects to updateMask
if firstProject != true {
maskId = fmt.Sprintf("%s%s", "&paths=shareSettings.projectMap.", original["project_id"])
if !firstProject {
maskId = fmt.Sprintf("%s%s", "&paths=shareSettings.projectMap.", original["id"])
} else {
maskId = fmt.Sprintf("%s%s", "?paths=shareSettings.projectMap.", original["project_id"])
maskId = fmt.Sprintf("%s%s", "?paths=shareSettings.projectMap.", original["id"])
firstProject = false
}
decodedPath, _ := url.QueryUnescape(maskId)
Expand All @@ -1177,7 +1195,7 @@ func resourceComputeReservationUpdateEncoder(d *schema.ResourceData, meta interf
projectNum := project.ProjectNumber
projectIdOrNum = fmt.Sprintf("%d", projectNum)
}
if firstProject != true {
if !firstProject {
maskId = fmt.Sprintf("%s%s", "&paths=shareSettings.projectMap.", projectIdOrNum)
} else {
maskId = fmt.Sprintf("%s%s", "?paths=shareSettings.projectMap.", projectIdOrNum)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
)

func TestAccComputeSharedReservation_update(t *testing.T) {
acctest.SkipIfVcr(t) // large number of parallel resources.
t.Parallel()

context := map[string]interface{}{
Expand Down

0 comments on commit f53ee08

Please sign in to comment.