Skip to content

Commit

Permalink
Add deadLetterPolicy to Pub/Sub Subscription resource (#3305) (#184)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
modular-magician authored Mar 30, 2020
1 parent 872d3da commit c41f599
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
95 changes: 95 additions & 0 deletions plugins/modules/gcp_pubsub_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,42 @@
- 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.
Expand Down Expand Up @@ -393,6 +429,41 @@
- 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
'''

################################################################################
Expand Down Expand Up @@ -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'))),
)
)

Expand Down Expand Up @@ -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)


Expand All @@ -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():
Expand Down Expand Up @@ -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(),
}


Expand Down Expand Up @@ -675,5 +751,24 @@ def from_response(self):
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()
35 changes: 35 additions & 0 deletions plugins/modules/gcp_pubsub_subscription_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,41 @@
- 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
'''

################################################################################
Expand Down

0 comments on commit c41f599

Please sign in to comment.