Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shared reservation update #5653

Merged
9 changes: 8 additions & 1 deletion mmv1/products/compute/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14092,8 +14092,10 @@ objects:
- !ruby/object:Api::Resource
name: 'Reservation'
base_url: projects/{{project}}/zones/{{zone}}/reservations
update_verb: :PATCH
update_url: projects/{{project}}/zones/{{zone}}/reservations/{{name}}
update_mask: true
collection_url_key: 'items'
input: true
has_self_link: true
description: |
Represents a reservation resource. A reservation ensures that capacity is
Expand Down Expand Up @@ -14143,6 +14145,7 @@ objects:
Creation timestamp in RFC3339 text format.
- !ruby/object:Api::Type::String
name: 'description'
input: true
description: |
An optional description of this resource.
- !ruby/object:Api::Type::Integer
Expand All @@ -14153,6 +14156,7 @@ objects:
- !ruby/object:Api::Type::String
name: 'name'
required: true
input: true
description: |
Name of the resource. Provided by the client when the resource is
created. The name must be 1-63 characters long, and comply with
Expand All @@ -14169,6 +14173,7 @@ objects:
reservations that are tied to a commitment.
- !ruby/object:Api::Type::Boolean
name: 'specificReservationRequired'
input: true
# Not a hard API default, but this should help avoid a unset/true/false
# trinary.
default_value: false
Expand All @@ -14183,6 +14188,8 @@ objects:
The status of the reservation.
- !ruby/object:Api::Type::NestedObject
name: 'shareSettings'
# update_verb: :PATCH
# update_url: projects/{{project}}/zones/{{zone}}/reservations/{{name}}
description: |
The share setting for reservations.
properties:
Expand Down
1 change: 1 addition & 0 deletions mmv1/products/compute/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2274,6 +2274,7 @@ overrides: !ruby/object:Overrides::ResourceOverrides
skip_vcr: true
custom_code: !ruby/object:Provider::Terraform::CustomCode
update_encoder: templates/terraform/update_encoder/reservation.go.erb
pre_update: templates/terraform/pre_update/shared_reservation_update.go.erb
properties:
id: !ruby/object:Overrides::Terraform::PropertyOverride
exclude: true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
if d.HasChange("share_settings") {
url, err = replaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/zones/{{zone}}/reservations/{{name}}")
if err != nil {
return err
}
urlUpdateMask := obj["urlUpdateMask"]
if urlUpdateMask != nil {
url = url + urlUpdateMask.(string)
delete(obj, "urlUpdateMask")
}
}
105 changes: 102 additions & 3 deletions mmv1/templates/terraform/update_encoder/reservation.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,107 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# newObj := make(map[string]interface{})
# newObj["shareType"] = "SPECIFIC_PROJECTS"
# newObj["projectMap"] = obj["shareSettings"].(map[string]interface{})["projectMap"]
#
# return newObj, nil

-%>
newObj := make(map[string]interface{})
newObj["specificSkuCount"] = obj["specificReservation"].(map[string]interface{})["count"]
newObj := make(map[string]interface{})
config := meta.(*Config)
maskId := ""
firstProject := true
urlUpdateMask := ""

if d.HasChange("share_settings") {
// Get name.
nameProp, err := expandComputeReservationName(d.Get("name"), d, config)
if err != nil {
return newObj, nil
} 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 newObj, nil
} 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 newObj, nil
}
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 newObj, nil
}
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
return newObj, nil
Loading