From 872d3daf12416a7050bbcc20b70a0bcf2057d2ce Mon Sep 17 00:00:00 2001 From: The Magician Date: Fri, 27 Mar 2020 14:55:40 -0700 Subject: [PATCH] Support compute reservation resize (#3308) (#181) * support compute reservations resize * remove if custom encoder * review comments Signed-off-by: Modular Magician --- plugins/modules/gcp_compute_reservation.py | 23 +++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/plugins/modules/gcp_compute_reservation.py b/plugins/modules/gcp_compute_reservation.py index b9fb91f62..5ba7d62d6 100644 --- a/plugins/modules/gcp_compute_reservation.py +++ b/plugins/modules/gcp_compute_reservation.py @@ -410,7 +410,7 @@ def main(): if fetch: if state == 'present': if is_different(module, fetch): - update(module, self_link(module)) + update(module, self_link(module), fetch) fetch = fetch_resource(module, self_link(module)) changed = True else: @@ -434,9 +434,22 @@ def create(module, link): return wait_for_operation(module, auth.post(link, resource_to_request(module))) -def update(module, link): - delete(module, self_link(module)) - create(module, collection(module)) +def update(module, link, fetch): + update_fields(module, resource_to_request(module), response_to_hash(module, fetch)) + return fetch_resource(module, self_link(module)) + + +def update_fields(module, request, response): + if response.get('specificReservation') != request.get('specificReservation'): + specific_reservation_update(module, request, response) + + +def specific_reservation_update(module, request, response): + auth = GcpSession(module, 'compute') + auth.post( + ''.join(["https://www.googleapis.com/compute/v1/", "projects/{project}/zones/{zone}/reservations/{name}/resize"]).format(**module.params), + {u'specificReservation': ReservationSpecificreservation(module.params.get('specific_reservation', {}), module).to_request()}, + ) def delete(module, link): @@ -582,7 +595,7 @@ def from_response(self): return remove_nones_from_dict( { u'count': self.request.get(u'count'), - u'instanceProperties': ReservationInstanceproperties(self.request.get(u'instanceProperties', {}), self.module).from_response(), + u'instanceProperties': ReservationInstanceproperties(self.module.params.get('instance_properties', {}), self.module).to_request(), } )