Skip to content

Commit

Permalink
Shared reservation update (GoogleCloudPlatform#5653)
Browse files Browse the repository at this point in the history
  • Loading branch information
simamGit authored and lcaggio committed Mar 18, 2022
1 parent 65e28ff commit 97e25f7
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 3 deletions.
4 changes: 2 additions & 2 deletions mmv1/products/compute/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14261,14 +14261,14 @@ objects:
A map of project number and project config. This is only valid when shareType's value is SPECIFIC_PROJECTS.
key_name: id
key_description: |
The project id/number which is deleting or adding to the project list. Only project number is acceptable.
The project id/number which is deleting or adding to the project list.
value_type: !ruby/object:Api::Type::NestedObject
name: projectConfig
properties:
- !ruby/object:Api::Type::String
name: 'projectId'
description: |
The project id/number, should be same as the key of this project config in the project map.
The project id/number should be the same as the key of this project config in the project map.
- !ruby/object:Api::Type::NestedObject
name: 'specificReservation'
required: true
Expand Down
92 changes: 91 additions & 1 deletion mmv1/templates/terraform/update_encoder/reservation.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,94 @@
firstProject := true
urlUpdateMask := ""

return newObj, nil
if d.HasChange("share_settings") {
// Get name.
nameProp, err := expandComputeReservationName(d.Get("name"), d, config)
if err != nil {
return nil, fmt.Errorf("Invalid value for name: %s", err)
} else if v, ok := d.GetOkExists("name"); !isEmptyValue(reflect.ValueOf(nameProp)) && (ok || !reflect.DeepEqual(v, nameProp)) {
newObj["name"] = nameProp
}
// Get zone.
zoneProp, err := expandComputeReservationZone(d.Get("zone"), d, config)
if err != nil {
return nil, fmt.Errorf("Invalid value for zone: %s", err)
} else if v, ok := d.GetOkExists("zone"); !isEmptyValue(reflect.ValueOf(zoneProp)) && (ok || !reflect.DeepEqual(v, zoneProp)) {
newObj["zone"] = zoneProp
}
transformed := make(map[string]interface{})
// Set shareType.
transformed["shareType"] = "SPECIFIC_PROJECTS"
// 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)

for _, raw := range after.Difference(before).List() {
original := raw.(map[string]interface{})
singleProject := make(map[string]interface{})
// set up project_map.
transformedProjectId := original["project_id"]
if val := reflect.ValueOf(transformedProjectId); val.IsValid() && !isEmptyValue(val) {
singleProject["projectId"] = transformedProjectId
}
transformedId, err := expandString(original["id"], d, config)
if err != nil {
return nil, fmt.Errorf("Invalid value for id: %s", err)
}
projectMap[transformedId] = singleProject
// add added projects to updateMask
if firstProject != true {
maskId = fmt.Sprintf("%s%s", "&paths=shareSettings.projectMap.", original["project_id"])
} else {
maskId = fmt.Sprintf("%s%s", "?paths=shareSettings.projectMap.", original["project_id"])
firstProject = false
}
decodedPath, _ := url.QueryUnescape(maskId)
urlUpdateMask = urlUpdateMask + decodedPath
}
transformed["projectMap"] = projectMap
newObj["shareSettings"] = transformed

// add removed projects to updateMask
firstProject = true
for _, raw := range before.Difference(after).List() {
original := raw.(map[string]interface{})
// To remove a project we need project number.
projectId := fmt.Sprintf("%s", original["project_id"])
projectIdOrNum := projectId
_, err := strconv.Atoi(projectId)
// convert id to number.
if err != nil {
config := meta.(*Config)
project, err := config.NewResourceManagerClient(config.userAgent).Projects.Get(projectId).Do()
if err != nil {
return nil, fmt.Errorf("Invalid value for projectId: %s", err)
}
projectNum := project.ProjectNumber
projectIdOrNum = fmt.Sprintf("%d", projectNum)
}
if firstProject != true {
maskId = fmt.Sprintf("%s%s", "&paths=shareSettings.projectMap.", projectIdOrNum)
} else {
maskId = fmt.Sprintf("%s%s", "?paths=shareSettings.projectMap.", projectIdOrNum)
firstProject = false
}
decodedPath, _ := url.QueryUnescape(maskId)
urlUpdateMask = urlUpdateMask + decodedPath
}
newObj["urlUpdateMask"] = urlUpdateMask
}

// Resize.
if obj["specificReservation"] != nil {
count := obj["specificReservation"].(map[string]interface{})["count"]
if count != nil {
newObj["specificSkuCount"] = obj["specificReservation"].(map[string]interface{})["count"]
}
}

return newObj, nil

0 comments on commit 97e25f7

Please sign in to comment.