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