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 16, 2022
1 parent ee21532 commit 0867f96
Show file tree
Hide file tree
Showing 5 changed files with 363 additions and 6 deletions.
19 changes: 16 additions & 3 deletions mmv1/products/compute/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14149,8 +14149,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 @@ -14200,6 +14202,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 @@ -14210,6 +14213,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 @@ -14226,6 +14230,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 Down Expand Up @@ -14256,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 Expand Up @@ -14292,10 +14297,12 @@ objects:
- !ruby/object:Api::Type::String
name: 'machineType'
required: true
input: true
description: |
The name of the machine type to reserve.
- !ruby/object:Api::Type::String
name: 'minCpuPlatform'
input: true
description: |
The minimum CPU platform for the reservation. For example,
`"Intel Skylake"`. See
Expand All @@ -14305,11 +14312,13 @@ objects:
name: 'guestAccelerators'
description: |
Guest accelerator type and count.
input: true
item_type: !ruby/object:Api::Type::NestedObject
properties:
- !ruby/object:Api::Type::String
name: 'acceleratorType'
required: true
input: true
description: |
The full or partial URL of the accelerator type to
attach to this instance. For example:
Expand All @@ -14319,18 +14328,21 @@ objects:
- !ruby/object:Api::Type::Integer
name: 'acceleratorCount'
required: true
input: true
description: |
The number of the guest accelerator cards exposed to
this instance.
- !ruby/object:Api::Type::Array
name: 'localSsds'
input: true
description: |
The amount of local ssd to reserve with each instance. This
reserves disks of type `local-ssd`.
item_type: !ruby/object:Api::Type::NestedObject
properties:
- !ruby/object:Api::Type::Enum
name: 'interface'
input: true
default_value: :SCSI
values:
- :SCSI
Expand All @@ -14340,6 +14352,7 @@ objects:
- !ruby/object:Api::Type::Integer
name: 'diskSizeGb'
required: true
input: true
description: |
The size of the disk in base-2 GB.
- !ruby/object:Api::Resource
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 @@ -2293,6 +2293,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")
}
}
99 changes: 96 additions & 3 deletions mmv1/templates/terraform/update_encoder/reservation.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,100 @@
# See the License for the specific language governing permissions and
# limitations under the License.
-%>
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 := ""

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
Loading

0 comments on commit 0867f96

Please sign in to comment.