From 314bebe2e875b85f2c8473e9609cdaf436dd37fd Mon Sep 17 00:00:00 2001 From: Edward Samuel Pasaribu Date: Wed, 1 Sep 2021 23:00:22 +0800 Subject: [PATCH] Add support for reservationAffinity to Instance and InstanceTemplate --- plugins/modules/gcp_compute_instance.py | 79 +++++++++++++++++++ plugins/modules/gcp_compute_instance_info.py | 26 ++++++ .../modules/gcp_compute_instance_template.py | 79 +++++++++++++++++++ .../gcp_compute_instance_template_info.py | 27 +++++++ 4 files changed, 211 insertions(+) diff --git a/plugins/modules/gcp_compute_instance.py b/plugins/modules/gcp_compute_instance.py index b62286d..6740643 100644 --- a/plugins/modules/gcp_compute_instance.py +++ b/plugins/modules/gcp_compute_instance.py @@ -388,6 +388,35 @@ options: field to "{{ name-of-resource }}"' required: false type: dict + reservation_affinity: + description: + - Specifies the reservations that this instance can consume from. + required: false + type: dict + suboptions: + consume_reservation_type: + description: + - 'Specifies the type of reservation from which this instance can consume + resources: ANY_RESERVATION (default), SPECIFIC_RESERVATION, or NO_RESERVATION.' + - 'Some valid choices include: "ANY_RESERVATION", "SPECIFIC_RESERVATION", + "NO_RESERVATION"' + required: false + type: str + key: + description: + - Corresponds to the label key of a reservation resource. To target a SPECIFIC_RESERVATION + by name, specify googleapis.com/reservation-name as the key and specify + the name of your reservation as its value. + required: false + type: str + values: + description: + - Corresponds to the label values of a reservation resource. This can be either + a name to a reservation in the same project or "projects/different-project/reservations/some-reservation-name" + to target a shared reservation in the same zone but in a different project. + elements: str + required: false + type: list scheduling: description: - Sets the scheduling options for this instance. @@ -956,6 +985,32 @@ networkInterfaces: If the network is in custom subnet mode, then this field should be specified. returned: success type: dict +reservationAffinity: + description: + - Specifies the reservations that this instance can consume from. + returned: success + type: complex + contains: + consumeReservationType: + description: + - 'Specifies the type of reservation from which this instance can consume resources: + ANY_RESERVATION (default), SPECIFIC_RESERVATION, or NO_RESERVATION.' + returned: success + type: str + key: + description: + - Corresponds to the label key of a reservation resource. To target a SPECIFIC_RESERVATION + by name, specify googleapis.com/reservation-name as the key and specify the + name of your reservation as its value. + returned: success + type: str + values: + description: + - Corresponds to the label values of a reservation resource. This can be either + a name to a reservation in the same project or "projects/different-project/reservations/some-reservation-name" + to target a shared reservation in the same zone but in a different project. + returned: success + type: list scheduling: description: - Sets the scheduling options for this instance. @@ -1160,6 +1215,9 @@ def main(): subnetwork=dict(type='dict'), ), ), + reservation_affinity=dict( + type='dict', options=dict(consume_reservation_type=dict(type='str'), key=dict(type='str'), values=dict(type='list', elements='str')) + ), scheduling=dict( type='dict', options=dict(automatic_restart=dict(type='bool'), on_host_maintenance=dict(type='str'), preemptible=dict(type='bool')) ), @@ -1266,6 +1324,7 @@ def resource_to_request(module): u'minCpuPlatform': module.params.get('min_cpu_platform'), u'name': module.params.get('name'), u'networkInterfaces': InstanceNetworkinterfacesArray(module.params.get('network_interfaces', []), module).to_request(), + u'reservationAffinity': InstanceReservationaffinity(module.params.get('reservation_affinity', {}), module).to_request(), u'scheduling': InstanceScheduling(module.params.get('scheduling', {}), module).to_request(), u'serviceAccounts': InstanceServiceaccountsArray(module.params.get('service_accounts', []), module).to_request(), u'shieldedInstanceConfig': InstanceShieldedinstanceconfig(module.params.get('shielded_instance_config', {}), module).to_request(), @@ -1356,6 +1415,7 @@ def response_to_hash(module, response): u'minCpuPlatform': response.get(u'minCpuPlatform'), u'name': response.get(u'name'), u'networkInterfaces': InstanceNetworkinterfacesArray(response.get(u'networkInterfaces', []), module).from_response(), + u'reservationAffinity': InstanceReservationaffinity(response.get(u'reservationAffinity', {}), module).from_response(), u'scheduling': InstanceScheduling(response.get(u'scheduling', {}), module).from_response(), u'serviceAccounts': InstanceServiceaccountsArray(response.get(u'serviceAccounts', []), module).from_response(), u'shieldedInstanceConfig': InstanceShieldedinstanceconfig(response.get(u'shieldedInstanceConfig', {}), module).from_response(), @@ -1784,6 +1844,25 @@ class InstanceAliasiprangesArray(object): return remove_nones_from_dict({u'ipCidrRange': item.get(u'ipCidrRange'), u'subnetworkRangeName': item.get(u'subnetworkRangeName')}) +class InstanceReservationaffinity(object): + def __init__(self, request, module): + self.module = module + if request: + self.request = request + else: + self.request = {} + + def to_request(self): + return remove_nones_from_dict( + {u'consumeReservationType': self.request.get('consume_reservation_type'), u'key': self.request.get('key'), u'values': self.request.get('values')} + ) + + def from_response(self): + return remove_nones_from_dict( + {u'consumeReservationType': self.request.get(u'consumeReservationType'), u'key': self.request.get(u'key'), u'values': self.request.get(u'values')} + ) + + class InstanceScheduling(object): def __init__(self, request, module): self.module = module diff --git a/plugins/modules/gcp_compute_instance_info.py b/plugins/modules/gcp_compute_instance_info.py index 5b402fc..1d9085d 100644 --- a/plugins/modules/gcp_compute_instance_info.py +++ b/plugins/modules/gcp_compute_instance_info.py @@ -467,6 +467,32 @@ resources: If the network is in custom subnet mode, then this field should be specified. returned: success type: dict + reservationAffinity: + description: + - Specifies the reservations that this instance can consume from. + returned: success + type: complex + contains: + consumeReservationType: + description: + - 'Specifies the type of reservation from which this instance can consume + resources: ANY_RESERVATION (default), SPECIFIC_RESERVATION, or NO_RESERVATION.' + returned: success + type: str + key: + description: + - Corresponds to the label key of a reservation resource. To target a SPECIFIC_RESERVATION + by name, specify googleapis.com/reservation-name as the key and specify + the name of your reservation as its value. + returned: success + type: str + values: + description: + - Corresponds to the label values of a reservation resource. This can be + either a name to a reservation in the same project or "projects/different-project/reservations/some-reservation-name" + to target a shared reservation in the same zone but in a different project. + returned: success + type: list scheduling: description: - Sets the scheduling options for this instance. diff --git a/plugins/modules/gcp_compute_instance_template.py b/plugins/modules/gcp_compute_instance_template.py index 0785692..cd00502 100644 --- a/plugins/modules/gcp_compute_instance_template.py +++ b/plugins/modules/gcp_compute_instance_template.py @@ -399,6 +399,35 @@ options: and then set this subnetwork field to "{{ name-of-resource }}"' required: false type: dict + reservation_affinity: + description: + - Specifies the reservations that this instance can consume from. + required: false + type: dict + suboptions: + consume_reservation_type: + description: + - 'Specifies the type of reservation from which this instance can consume + resources: ANY_RESERVATION (default), SPECIFIC_RESERVATION, or NO_RESERVATION.' + - 'Some valid choices include: "ANY_RESERVATION", "SPECIFIC_RESERVATION", + "NO_RESERVATION"' + required: false + type: str + key: + description: + - Corresponds to the label key of a reservation resource. To target a + SPECIFIC_RESERVATION by name, specify googleapis.com/reservation-name + as the key and specify the name of your reservation as its value. + required: false + type: str + values: + description: + - Corresponds to the label values of a reservation resource. This can + be either a name to a reservation in the same project or "projects/different-project/reservations/some-reservation-name" + to target a shared reservation in the same zone but in a different project. + elements: str + required: false + type: list scheduling: description: - Sets the scheduling options for this instance. @@ -901,6 +930,32 @@ properties: If the network is in custom subnet mode, then this field should be specified. returned: success type: dict + reservationAffinity: + description: + - Specifies the reservations that this instance can consume from. + returned: success + type: complex + contains: + consumeReservationType: + description: + - 'Specifies the type of reservation from which this instance can consume + resources: ANY_RESERVATION (default), SPECIFIC_RESERVATION, or NO_RESERVATION.' + returned: success + type: str + key: + description: + - Corresponds to the label key of a reservation resource. To target a SPECIFIC_RESERVATION + by name, specify googleapis.com/reservation-name as the key and specify + the name of your reservation as its value. + returned: success + type: str + values: + description: + - Corresponds to the label values of a reservation resource. This can be + either a name to a reservation in the same project or "projects/different-project/reservations/some-reservation-name" + to target a shared reservation in the same zone but in a different project. + returned: success + type: list scheduling: description: - Sets the scheduling options for this instance. @@ -1061,6 +1116,9 @@ def main(): subnetwork=dict(type='dict'), ), ), + reservation_affinity=dict( + type='dict', options=dict(consume_reservation_type=dict(type='str'), key=dict(type='str'), values=dict(type='list', elements='str')) + ), scheduling=dict( type='dict', options=dict(automatic_restart=dict(type='bool'), on_host_maintenance=dict(type='str'), preemptible=dict(type='bool')) ), @@ -1319,6 +1377,7 @@ class InstanceTemplateProperties(object): u'metadata': self.request.get('metadata'), u'guestAccelerators': InstanceTemplateGuestacceleratorsArray(self.request.get('guest_accelerators', []), self.module).to_request(), u'networkInterfaces': InstanceTemplateNetworkinterfacesArray(self.request.get('network_interfaces', []), self.module).to_request(), + u'reservationAffinity': InstanceTemplateReservationaffinity(self.request.get('reservation_affinity', {}), self.module).to_request(), u'scheduling': InstanceTemplateScheduling(self.request.get('scheduling', {}), self.module).to_request(), u'serviceAccounts': InstanceTemplateServiceaccountsArray(self.request.get('service_accounts', []), self.module).to_request(), u'tags': InstanceTemplateTags(self.request.get('tags', {}), self.module).to_request(), @@ -1337,6 +1396,7 @@ class InstanceTemplateProperties(object): u'metadata': self.request.get(u'metadata'), u'guestAccelerators': InstanceTemplateGuestacceleratorsArray(self.request.get(u'guestAccelerators', []), self.module).from_response(), u'networkInterfaces': InstanceTemplateNetworkinterfacesArray(self.request.get(u'networkInterfaces', []), self.module).from_response(), + u'reservationAffinity': InstanceTemplateReservationaffinity(self.request.get(u'reservationAffinity', {}), self.module).from_response(), u'scheduling': InstanceTemplateScheduling(self.request.get(u'scheduling', {}), self.module).from_response(), u'serviceAccounts': InstanceTemplateServiceaccountsArray(self.request.get(u'serviceAccounts', []), self.module).from_response(), u'tags': InstanceTemplateTags(self.request.get(u'tags', {}), self.module).from_response(), @@ -1604,6 +1664,25 @@ class InstanceTemplateAliasiprangesArray(object): return remove_nones_from_dict({u'ipCidrRange': item.get(u'ipCidrRange'), u'subnetworkRangeName': item.get(u'subnetworkRangeName')}) +class InstanceTemplateReservationaffinity(object): + def __init__(self, request, module): + self.module = module + if request: + self.request = request + else: + self.request = {} + + def to_request(self): + return remove_nones_from_dict( + {u'consumeReservationType': self.request.get('consume_reservation_type'), u'key': self.request.get('key'), u'values': self.request.get('values')} + ) + + def from_response(self): + return remove_nones_from_dict( + {u'consumeReservationType': self.request.get(u'consumeReservationType'), u'key': self.request.get(u'key'), u'values': self.request.get(u'values')} + ) + + class InstanceTemplateScheduling(object): def __init__(self, request, module): self.module = module diff --git a/plugins/modules/gcp_compute_instance_template_info.py b/plugins/modules/gcp_compute_instance_template_info.py index b08cdfa..4fd50b8 100644 --- a/plugins/modules/gcp_compute_instance_template_info.py +++ b/plugins/modules/gcp_compute_instance_template_info.py @@ -468,6 +468,33 @@ resources: should be specified. returned: success type: dict + reservationAffinity: + description: + - Specifies the reservations that this instance can consume from. + returned: success + type: complex + contains: + consumeReservationType: + description: + - 'Specifies the type of reservation from which this instance can consume + resources: ANY_RESERVATION (default), SPECIFIC_RESERVATION, or NO_RESERVATION.' + returned: success + type: str + key: + description: + - Corresponds to the label key of a reservation resource. To target + a SPECIFIC_RESERVATION by name, specify googleapis.com/reservation-name + as the key and specify the name of your reservation as its value. + returned: success + type: str + values: + description: + - Corresponds to the label values of a reservation resource. This can + be either a name to a reservation in the same project or "projects/different-project/reservations/some-reservation-name" + to target a shared reservation in the same zone but in a different + project. + returned: success + type: list scheduling: description: - Sets the scheduling options for this instance.