|
|
|
@ -1,49 +1,46 @@
|
|
|
|
|
#!/usr/bin/python
|
|
|
|
|
# Copyright 2016 Google Inc.
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
# Copyright: (c) 2016, Google Inc.
|
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
|
|
|
|
'status': ['preview'],
|
|
|
|
|
'supported_by': 'community'}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
|
---
|
|
|
|
|
module: gcpubsub
|
|
|
|
|
version_added: "2.3"
|
|
|
|
|
short_description: Create and Delete Topics/Subscriptions, Publish and pull messages on PubSub.
|
|
|
|
|
short_description: Create and Delete Topics/Subscriptions, Publish and pull messages on PubSub
|
|
|
|
|
description:
|
|
|
|
|
- Create and Delete Topics/Subscriptions, Publish and pull messages on PubSub.
|
|
|
|
|
See U(https://cloud.google.com/pubsub/docs) for an overview.
|
|
|
|
|
requirements:
|
|
|
|
|
- "python >= 2.6"
|
|
|
|
|
- "google-auth >= 0.5.0"
|
|
|
|
|
- "google-cloud-pubsub >= 0.22.0"
|
|
|
|
|
- google-auth >= 0.5.0
|
|
|
|
|
- google-cloud-pubsub >= 0.22.0
|
|
|
|
|
notes:
|
|
|
|
|
- Subscription pull happens before publish. You cannot publish and pull in the same task.
|
|
|
|
|
author:
|
|
|
|
|
- "Tom Melendez (@supertom) <tom@supertom.com>"
|
|
|
|
|
- Tom Melendez (@supertom) <tom@supertom.com>
|
|
|
|
|
options:
|
|
|
|
|
topic:
|
|
|
|
|
description:
|
|
|
|
|
- GCP pubsub topic name. Only the name, not the full path, is required.
|
|
|
|
|
required: True
|
|
|
|
|
- GCP pubsub topic name.
|
|
|
|
|
- Only the name, not the full path, is required.
|
|
|
|
|
required: yes
|
|
|
|
|
subscription:
|
|
|
|
|
description:
|
|
|
|
|
- Dictionary containing a subscripton name associated with a topic (required), along with optional ack_deadline, push_endpoint and pull.
|
|
|
|
|
For pulling from a subscription, message_ack (bool), max_messages (int) and return_immediate are available as subfields.
|
|
|
|
|
See subfields name, push_endpoint and ack_deadline for more information.
|
|
|
|
|
required: False
|
|
|
|
|
name:
|
|
|
|
|
description: Subfield of subscription. Required if subscription is specified. See examples.
|
|
|
|
|
required: False
|
|
|
|
|
ack_deadline:
|
|
|
|
|
description: Subfield of subscription. Not required. Default deadline for subscriptions to ACK the message before it is resent. See examples.
|
|
|
|
|
required: False
|
|
|
|
|
pull:
|
|
|
|
|
description:
|
|
|
|
|
- Subfield of subscription. Not required. If specified, messages will be retrieved from topic via the provided subscription name.
|
|
|
|
@ -53,62 +50,60 @@ options:
|
|
|
|
|
description:
|
|
|
|
|
- Subfield of subscription. Not required. If specified, message will be sent to an endpoint.
|
|
|
|
|
See U(https://cloud.google.com/pubsub/docs/advanced#push_endpoints) for more information.
|
|
|
|
|
required: False
|
|
|
|
|
publish:
|
|
|
|
|
description:
|
|
|
|
|
- List of dictionaries describing messages and attributes to be published. Dictionary is in message(str):attributes(dict) format.
|
|
|
|
|
Only message is required.
|
|
|
|
|
required: False
|
|
|
|
|
state:
|
|
|
|
|
description:
|
|
|
|
|
- State of the topic or queue (absent, present). Applies to the most granular resource. Remove the most granular resource. If subcription is
|
|
|
|
|
specified we remove it. If only topic is specified, that is what is removed. Note that a topic can be removed without first removing the
|
|
|
|
|
subscription.
|
|
|
|
|
required: False
|
|
|
|
|
default: "present"
|
|
|
|
|
- State of the topic or queue.
|
|
|
|
|
- Applies to the most granular resource.
|
|
|
|
|
- If subscription isspecified we remove it.
|
|
|
|
|
- If only topic is specified, that is what is removed.
|
|
|
|
|
- NOTE - A topic can be removed without first removing the subscription.
|
|
|
|
|
choices: [ absent, present ]
|
|
|
|
|
default: present
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
|
# Create a topic and publish a message to it
|
|
|
|
|
# (Message will be pushed; there is no check to see if the message was pushed before
|
|
|
|
|
# Topics:
|
|
|
|
|
## Create Topic
|
|
|
|
|
- name: Create a topic and publish a message to it
|
|
|
|
|
gcpubsub:
|
|
|
|
|
topic: ansible-topic-example
|
|
|
|
|
state: present
|
|
|
|
|
|
|
|
|
|
## Delete Topic
|
|
|
|
|
### Subscriptions associated with topic are not deleted.
|
|
|
|
|
# Subscriptions associated with topic are not deleted.
|
|
|
|
|
- name: Delete Topic
|
|
|
|
|
gcpubsub:
|
|
|
|
|
topic: ansible-topic-example
|
|
|
|
|
state: absent
|
|
|
|
|
|
|
|
|
|
## Messages: publish multiple messages, with attributes (key:value available with the message)
|
|
|
|
|
### setting absent will keep the messages from being sent
|
|
|
|
|
# Setting absent will keep the messages from being sent
|
|
|
|
|
- name: Publish multiple messages, with attributes (key:value available with the message)
|
|
|
|
|
gcpubsub:
|
|
|
|
|
topic: "{{ topic_name }}"
|
|
|
|
|
topic: '{{ topic_name }}'
|
|
|
|
|
state: present
|
|
|
|
|
publish:
|
|
|
|
|
- message: "this is message 1"
|
|
|
|
|
- message: this is message 1
|
|
|
|
|
attributes:
|
|
|
|
|
mykey1: myvalue
|
|
|
|
|
mykey2: myvalu2
|
|
|
|
|
mykey3: myvalue3
|
|
|
|
|
- message: "this is message 2"
|
|
|
|
|
- message: this is message 2
|
|
|
|
|
attributes:
|
|
|
|
|
server: prod
|
|
|
|
|
sla: "99.9999"
|
|
|
|
|
owner: fred
|
|
|
|
|
|
|
|
|
|
# Subscriptions
|
|
|
|
|
## Create Subscription (pull)
|
|
|
|
|
- name: Create Subscription (pull)
|
|
|
|
|
gcpubsub:
|
|
|
|
|
topic: ansible-topic-example
|
|
|
|
|
subscription:
|
|
|
|
|
- name: mysub
|
|
|
|
|
state: present
|
|
|
|
|
|
|
|
|
|
## Create Subscription with ack_deadline and push endpoint
|
|
|
|
|
### pull is default, ack_deadline is not required
|
|
|
|
|
# pull is default, ack_deadline is not required
|
|
|
|
|
- name: Create Subscription with ack_deadline and push endpoint
|
|
|
|
|
gcpubsub:
|
|
|
|
|
topic: ansible-topic-example
|
|
|
|
|
subscription:
|
|
|
|
@ -117,24 +112,24 @@ gcpubsub:
|
|
|
|
|
push_endpoint: http://pushendpoint.example.com
|
|
|
|
|
state: present
|
|
|
|
|
|
|
|
|
|
## Subscription change from push to pull
|
|
|
|
|
### setting push_endpoint to "None" converts subscription to pull.
|
|
|
|
|
# Setting push_endpoint to "None" converts subscription to pull.
|
|
|
|
|
- name: Subscription change from push to pull
|
|
|
|
|
gcpubsub:
|
|
|
|
|
topic: ansible-topic-example
|
|
|
|
|
subscription:
|
|
|
|
|
name: mysub
|
|
|
|
|
push_endpoint: "None"
|
|
|
|
|
|
|
|
|
|
## Delete subscription
|
|
|
|
|
### Topic will not be deleted
|
|
|
|
|
- name: Delete subscription
|
|
|
|
|
gcpubsub:
|
|
|
|
|
topic: ansible-topic-example
|
|
|
|
|
subscription:
|
|
|
|
|
- name: mysub
|
|
|
|
|
state: absent
|
|
|
|
|
|
|
|
|
|
## Pull messages from subscription
|
|
|
|
|
### only pull keyword is required.
|
|
|
|
|
# only pull keyword is required.
|
|
|
|
|
- name: Pull messages from subscription
|
|
|
|
|
gcpubsub:
|
|
|
|
|
topic: ansible-topic-example
|
|
|
|
|
subscription:
|
|
|
|
@ -208,6 +203,7 @@ def publish_messages(message_list, topic):
|
|
|
|
|
batch.publish(bytes(msg), **attrs)
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def pull_messages(pull_params, sub):
|
|
|
|
|
"""
|
|
|
|
|
:rtype: tuple (output, changed)
|
|
|
|
@ -218,8 +214,7 @@ def pull_messages(pull_params, sub):
|
|
|
|
|
return_immediately = pull_params.get('return_immediately', False)
|
|
|
|
|
|
|
|
|
|
output = []
|
|
|
|
|
pulled = sub.pull(return_immediately=return_immediately,
|
|
|
|
|
max_messages=max_messages)
|
|
|
|
|
pulled = sub.pull(return_immediately=return_immediately, max_messages=max_messages)
|
|
|
|
|
|
|
|
|
|
for ack_id, msg in pulled:
|
|
|
|
|
msg_dict = {'message_id': msg.message_id,
|
|
|
|
@ -238,14 +233,17 @@ def pull_messages(pull_params, sub):
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
|
|
module = AnsibleModule(argument_spec=dict(
|
|
|
|
|
topic=dict(required=True),
|
|
|
|
|
state=dict(choices=['absent', 'present'], default='present'),
|
|
|
|
|
publish=dict(type='list', default=None),
|
|
|
|
|
subscription=dict(type='dict', default=None),
|
|
|
|
|
service_account_email=dict(),
|
|
|
|
|
credentials_file=dict(),
|
|
|
|
|
project_id=dict(), ),)
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
|
argument_spec=dict(
|
|
|
|
|
topic=dict(type='str', required=True),
|
|
|
|
|
state=dict(type='str', default='present', choices=['absent', 'present']),
|
|
|
|
|
publish=dict(type='list'),
|
|
|
|
|
subscription=dict(type='dict'),
|
|
|
|
|
service_account_email=dict(type='str'),
|
|
|
|
|
credentials_file=dict(type='str'),
|
|
|
|
|
project_id=dict(type='str'),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if not HAS_PYTHON26:
|
|
|
|
|
module.fail_json(
|
|
|
|
@ -326,11 +324,9 @@ def main():
|
|
|
|
|
if mod_params['publish'] and len(mod_params['publish']) > 0:
|
|
|
|
|
changed = publish_messages(mod_params['publish'], t)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
json_output['changed'] = changed
|
|
|
|
|
json_output.update(mod_params)
|
|
|
|
|
module.exit_json(**json_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
main()
|
|
|
|
|