From c41f599aee62016fec098baf0363c987e0711b1a Mon Sep 17 00:00:00 2001 From: The Magician Date: Mon, 30 Mar 2020 11:44:47 -0700 Subject: [PATCH] Add deadLetterPolicy to Pub/Sub Subscription resource (#3305) (#184) * Add deadLetterPolicy to Pub/Sub subscription resource * fix: disable allow_empty_objects, fix docstring, add example * fix: set max_delivery_attempts * fix: block and topic name Signed-off-by: Modular Magician --- plugins/modules/gcp_pubsub_subscription.py | 95 +++++++++++++++++++ .../modules/gcp_pubsub_subscription_info.py | 35 +++++++ 2 files changed, 130 insertions(+) diff --git a/plugins/modules/gcp_pubsub_subscription.py b/plugins/modules/gcp_pubsub_subscription.py index 3f2b697..c016a92 100644 --- a/plugins/modules/gcp_pubsub_subscription.py +++ b/plugins/modules/gcp_pubsub_subscription.py @@ -190,6 +190,42 @@ options: - Example - "3.5s". required: true type: str + dead_letter_policy: + description: + - A policy that specifies the conditions for dead lettering messages in this subscription. + If dead_letter_policy is not set, dead lettering is disabled. + - The Cloud Pub/Sub service account associated with this subscriptions's parent + project (i.e., service-{project_number}@gcp-sa-pubsub.iam.gserviceaccount.com) + must have permission to Acknowledge() messages on this subscription. + required: false + type: dict + version_added: '2.10' + suboptions: + dead_letter_topic: + description: + - The name of the topic to which dead letter messages should be published. + - Format is `projects/{project}/topics/{topic}`. + - The Cloud Pub/Sub service\naccount associated with the enclosing subscription's + parent project (i.e., service-{project_number}@gcp-sa-pubsub.iam.gserviceaccount.com) + must have permission to Publish() to this topic. + - The operation will fail if the topic does not exist. + - Users should ensure that there is a subscription attached to this topic + since messages published to a topic with no subscriptions are lost. + required: false + type: str + max_delivery_attempts: + description: + - The maximum number of delivery attempts for any message. The value must + be between 5 and 100. + - The number of delivery attempts is defined as 1 + (the sum of number of + NACKs and number of times the acknowledgement deadline has been exceeded + for the message). + - A NACK is any call to ModifyAckDeadline with a 0 deadline. Note that client + libraries may automatically extend ack_deadlines. + - This field will be honored on a best effort basis. + - If this parameter is 0, a default value of 5 is used. + required: false + type: int project: description: - The Google Cloud Platform project to use. @@ -393,6 +429,41 @@ expirationPolicy: - Example - "3.5s". returned: success type: str +deadLetterPolicy: + description: + - A policy that specifies the conditions for dead lettering messages in this subscription. + If dead_letter_policy is not set, dead lettering is disabled. + - The Cloud Pub/Sub service account associated with this subscriptions's parent + project (i.e., service-{project_number}@gcp-sa-pubsub.iam.gserviceaccount.com) + must have permission to Acknowledge() messages on this subscription. + returned: success + type: complex + contains: + deadLetterTopic: + description: + - The name of the topic to which dead letter messages should be published. + - Format is `projects/{project}/topics/{topic}`. + - The Cloud Pub/Sub service\naccount associated with the enclosing subscription's + parent project (i.e., service-{project_number}@gcp-sa-pubsub.iam.gserviceaccount.com) + must have permission to Publish() to this topic. + - The operation will fail if the topic does not exist. + - Users should ensure that there is a subscription attached to this topic since + messages published to a topic with no subscriptions are lost. + returned: success + type: str + maxDeliveryAttempts: + description: + - The maximum number of delivery attempts for any message. The value must be + between 5 and 100. + - The number of delivery attempts is defined as 1 + (the sum of number of NACKs + and number of times the acknowledgement deadline has been exceeded for the + message). + - A NACK is any call to ModifyAckDeadline with a 0 deadline. Note that client + libraries may automatically extend ack_deadlines. + - This field will be honored on a best effort basis. + - If this parameter is 0, a default value of 5 is used. + returned: success + type: int ''' ################################################################################ @@ -436,6 +507,7 @@ def main(): message_retention_duration=dict(default='604800s', type='str'), retain_acked_messages=dict(type='bool'), expiration_policy=dict(type='dict', options=dict(ttl=dict(required=True, type='str'))), + dead_letter_policy=dict(type='dict', options=dict(dead_letter_topic=dict(type='str'), max_delivery_attempts=dict(type='int'))), ) ) @@ -496,6 +568,8 @@ def updateMask(request, response): update_mask.append('retainAckedMessages') if request.get('expirationPolicy') != response.get('expirationPolicy'): update_mask.append('expirationPolicy') + if request.get('deadLetterPolicy') != response.get('deadLetterPolicy'): + update_mask.append('deadLetterPolicy') return ','.join(update_mask) @@ -514,6 +588,7 @@ def resource_to_request(module): u'messageRetentionDuration': module.params.get('message_retention_duration'), u'retainAckedMessages': module.params.get('retain_acked_messages'), u'expirationPolicy': SubscriptionExpirationpolicy(module.params.get('expiration_policy', {}), module).to_request(), + u'deadLetterPolicy': SubscriptionDeadletterpolicy(module.params.get('dead_letter_policy', {}), module).to_request(), } return_vals = {} for k, v in request.items(): @@ -587,6 +662,7 @@ def response_to_hash(module, response): u'messageRetentionDuration': response.get(u'messageRetentionDuration'), u'retainAckedMessages': response.get(u'retainAckedMessages'), u'expirationPolicy': SubscriptionExpirationpolicy(response.get(u'expirationPolicy', {}), module).from_response(), + u'deadLetterPolicy': SubscriptionDeadletterpolicy(response.get(u'deadLetterPolicy', {}), module).from_response(), } @@ -675,5 +751,24 @@ class SubscriptionExpirationpolicy(object): return remove_nones_from_dict({u'ttl': self.request.get(u'ttl')}) +class SubscriptionDeadletterpolicy(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'deadLetterTopic': self.request.get('dead_letter_topic'), u'maxDeliveryAttempts': self.request.get('max_delivery_attempts')} + ) + + def from_response(self): + return remove_nones_from_dict( + {u'deadLetterTopic': self.request.get(u'deadLetterTopic'), u'maxDeliveryAttempts': self.request.get(u'maxDeliveryAttempts')} + ) + + if __name__ == '__main__': main() diff --git a/plugins/modules/gcp_pubsub_subscription_info.py b/plugins/modules/gcp_pubsub_subscription_info.py index 3281b0c..d29e3c7 100644 --- a/plugins/modules/gcp_pubsub_subscription_info.py +++ b/plugins/modules/gcp_pubsub_subscription_info.py @@ -236,6 +236,41 @@ resources: - Example - "3.5s". returned: success type: str + deadLetterPolicy: + description: + - A policy that specifies the conditions for dead lettering messages in this + subscription. If dead_letter_policy is not set, dead lettering is disabled. + - The Cloud Pub/Sub service account associated with this subscriptions's parent + project (i.e., service-{project_number}@gcp-sa-pubsub.iam.gserviceaccount.com) + must have permission to Acknowledge() messages on this subscription. + returned: success + type: complex + contains: + deadLetterTopic: + description: + - The name of the topic to which dead letter messages should be published. + - Format is `projects/{project}/topics/{topic}`. + - The Cloud Pub/Sub service\naccount associated with the enclosing subscription's + parent project (i.e., service-{project_number}@gcp-sa-pubsub.iam.gserviceaccount.com) + must have permission to Publish() to this topic. + - The operation will fail if the topic does not exist. + - Users should ensure that there is a subscription attached to this topic + since messages published to a topic with no subscriptions are lost. + returned: success + type: str + maxDeliveryAttempts: + description: + - The maximum number of delivery attempts for any message. The value must + be between 5 and 100. + - The number of delivery attempts is defined as 1 + (the sum of number of + NACKs and number of times the acknowledgement deadline has been exceeded + for the message). + - A NACK is any call to ModifyAckDeadline with a 0 deadline. Note that client + libraries may automatically extend ack_deadlines. + - This field will be honored on a best effort basis. + - If this parameter is 0, a default value of 5 is used. + returned: success + type: int ''' ################################################################################