Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

[Terraform] autogenerate Pubsub Topics/Subscriptions (+labels) #193

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 64 additions & 9 deletions lib/ansible/modules/cloud/google/gcp_pubsub_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,20 @@
name:
description:
- Name of the subscription.
required: false
required: true
topic:
description:
- A reference to a Topic resource.
- 'This field represents a link to a Topic resource in GCP. It can be specified
in two ways. First, you can place in the name of the resource here as a string
Alternatively, you can add `register: name-of-resource` to a gcp_pubsub_topic
task and then set this topic field to "{{ name-of-resource }}"'
required: true
labels:
description:
- A set of key/value label pairs to assign to this Subscription.
required: false
version_added: 2.8
push_config:
description:
- If push delivery is used with this subscription, this field is used to configure
Expand All @@ -72,6 +77,25 @@
- A URL locating the endpoint to which messages should be pushed.
- For example, a Webhook endpoint might use "U(https://example.com/push".)
required: false
attributes:
description:
- Endpoint configuration attributes.
- Every endpoint has a set of API supported attributes that can be used to
control different aspects of the message delivery.
- The currently supported attribute is x-goog-version, which you can use to
change the format of the pushed message. This attribute indicates the version
of the data expected by the endpoint. This controls the shape of the pushed
message (i.e., its fields and metadata). The endpoint version is based on
the version of the Pub/Sub API.
- If not present during the subscriptions.create call, it will default to
the version of the API used to make such call. If not present during a subscriptions.modifyPushConfig
call, its value will not be changed. subscriptions.get calls will always
return a valid version, even if the subscription was created without this
attribute.
- 'The possible values for this attribute are: - v1beta1: uses the push format
defined in the v1beta1 Pub/Sub API.'
- "- v1 or v1beta2: uses the push format defined in the v1 Pub/Sub API."
required: false
ack_deadline_seconds:
description:
- This value is the maximum time after a subscriber receives a message before
Expand All @@ -90,6 +114,9 @@
redeliver the message.
required: false
extends_documentation_fragment: gcp
notes:
- 'API Reference: U(https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions)'
- 'Managing Subscriptions: U(https://cloud.google.com/pubsub/docs/admin#managing_subscriptions)'
'''

EXAMPLES = '''
Expand Down Expand Up @@ -124,6 +151,11 @@
- A reference to a Topic resource.
returned: success
type: str
labels:
description:
- A set of key/value label pairs to assign to this Subscription.
returned: success
type: dict
pushConfig:
description:
- If push delivery is used with this subscription, this field is used to configure
Expand All @@ -138,6 +170,25 @@
- For example, a Webhook endpoint might use "U(https://example.com/push".)
returned: success
type: str
attributes:
description:
- Endpoint configuration attributes.
- Every endpoint has a set of API supported attributes that can be used to control
different aspects of the message delivery.
- The currently supported attribute is x-goog-version, which you can use to
change the format of the pushed message. This attribute indicates the version
of the data expected by the endpoint. This controls the shape of the pushed
message (i.e., its fields and metadata). The endpoint version is based on
the version of the Pub/Sub API.
- If not present during the subscriptions.create call, it will default to the
version of the API used to make such call. If not present during a subscriptions.modifyPushConfig
call, its value will not be changed. subscriptions.get calls will always return
a valid version, even if the subscription was created without this attribute.
- 'The possible values for this attribute are: - v1beta1: uses the push format
defined in the v1beta1 Pub/Sub API.'
- "- v1 or v1beta2: uses the push format defined in the v1 Pub/Sub API."
returned: success
type: dict
ackDeadlineSeconds:
description:
- This value is the maximum time after a subscriber receives a message before the
Expand Down Expand Up @@ -176,9 +227,10 @@ def main():
module = GcpModule(
argument_spec=dict(
state=dict(default='present', choices=['present', 'absent'], type='str'),
name=dict(type='str'),
topic=dict(),
push_config=dict(type='dict', options=dict(push_endpoint=dict(type='str'))),
name=dict(required=True, type='str'),
topic=dict(required=True),
labels=dict(type='dict'),
push_config=dict(type='dict', options=dict(push_endpoint=dict(type='str'), attributes=dict(type='dict'))),
ack_deadline_seconds=dict(type='int'),
)
)
Expand Down Expand Up @@ -219,7 +271,8 @@ def create(module, link):


def update(module, link):
module.fail_json(msg="Subscription cannot be edited")
auth = GcpSession(module, 'pubsub')
return return_if_object(module, auth.patch(link, resource_to_request(module)))


def delete(module, link):
Expand All @@ -231,6 +284,7 @@ def resource_to_request(module):
request = {
u'name': module.params.get('name'),
u'topic': replace_resource_dict(module.params.get(u'topic', {}), 'name'),
u'labels': module.params.get('labels'),
u'pushConfig': SubscriptionPushconfig(module.params.get('push_config', {}), module).to_request(),
u'ackDeadlineSeconds': module.params.get('ack_deadline_seconds'),
}
Expand Down Expand Up @@ -302,8 +356,9 @@ def is_different(module, response):
# This is for doing comparisons with Ansible's current parameters.
def response_to_hash(module, response):
return {
u'name': response.get(u'name'),
u'topic': response.get(u'topic'),
u'name': module.params.get('name'),
u'topic': replace_resource_dict(module.params.get(u'topic', {}), 'name'),
u'labels': response.get(u'labels'),
u'pushConfig': SubscriptionPushconfig(response.get(u'pushConfig', {}), module).from_response(),
u'ackDeadlineSeconds': response.get(u'ackDeadlineSeconds'),
}
Expand Down Expand Up @@ -335,10 +390,10 @@ def __init__(self, request, module):
self.request = {}

def to_request(self):
return remove_nones_from_dict({u'pushEndpoint': self.request.get('push_endpoint')})
return remove_nones_from_dict({u'pushEndpoint': self.request.get('push_endpoint'), u'attributes': self.request.get('attributes')})

def from_response(self):
return remove_nones_from_dict({u'pushEndpoint': self.request.get(u'pushEndpoint')})
return remove_nones_from_dict({u'pushEndpoint': self.request.get(u'pushEndpoint'), u'attributes': self.request.get(u'attributes')})


if __name__ == '__main__':
Expand Down
25 changes: 25 additions & 0 deletions lib/ansible/modules/cloud/google/gcp_pubsub_subscription_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
- A reference to a Topic resource.
returned: success
type: str
labels:
description:
- A set of key/value label pairs to assign to this Subscription.
returned: success
type: dict
pushConfig:
description:
- If push delivery is used with this subscription, this field is used to configure
Expand All @@ -81,6 +86,26 @@
- For example, a Webhook endpoint might use "U(https://example.com/push".)
returned: success
type: str
attributes:
description:
- Endpoint configuration attributes.
- Every endpoint has a set of API supported attributes that can be used
to control different aspects of the message delivery.
- The currently supported attribute is x-goog-version, which you can use
to change the format of the pushed message. This attribute indicates the
version of the data expected by the endpoint. This controls the shape
of the pushed message (i.e., its fields and metadata). The endpoint version
is based on the version of the Pub/Sub API.
- If not present during the subscriptions.create call, it will default to
the version of the API used to make such call. If not present during a
subscriptions.modifyPushConfig call, its value will not be changed. subscriptions.get
calls will always return a valid version, even if the subscription was
created without this attribute.
- 'The possible values for this attribute are: - v1beta1: uses the push
format defined in the v1beta1 Pub/Sub API.'
- "- v1 or v1beta2: uses the push format defined in the v1 Pub/Sub API."
returned: success
type: dict
ackDeadlineSeconds:
description:
- This value is the maximum time after a subscriber receives a message before
Expand Down
26 changes: 21 additions & 5 deletions lib/ansible/modules/cloud/google/gcp_pubsub_topic.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,16 @@
name:
description:
- Name of the topic.
required: true
labels:
description:
- A set of key/value label pairs to assign to this Topic.
required: false
version_added: 2.8
extends_documentation_fragment: gcp
notes:
- 'API Reference: U(https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.topics)'
- 'Managing Topics: U(https://cloud.google.com/pubsub/docs/admin#managing_topics)'
'''

EXAMPLES = '''
Expand All @@ -70,6 +78,11 @@
- Name of the topic.
returned: success
type: str
labels:
description:
- A set of key/value label pairs to assign to this Topic.
returned: success
type: dict
'''

################################################################################
Expand All @@ -87,7 +100,11 @@
def main():
"""Main function"""

module = GcpModule(argument_spec=dict(state=dict(default='present', choices=['present', 'absent'], type='str'), name=dict(type='str')))
module = GcpModule(
argument_spec=dict(
state=dict(default='present', choices=['present', 'absent'], type='str'), name=dict(required=True, type='str'), labels=dict(type='dict')
)
)

if not module.params['scopes']:
module.params['scopes'] = ['https://www.googleapis.com/auth/pubsub']
Expand Down Expand Up @@ -125,8 +142,7 @@ def create(module, link):


def update(module, link):
auth = GcpSession(module, 'pubsub')
return return_if_object(module, auth.put(link, resource_to_request(module)))
module.fail_json(msg="Topic cannot be edited")


def delete(module, link):
Expand All @@ -135,7 +151,7 @@ def delete(module, link):


def resource_to_request(module):
request = {u'name': module.params.get('name')}
request = {u'name': module.params.get('name'), u'labels': module.params.get('labels')}
request = encode_request(request, module)
return_vals = {}
for k, v in request.items():
Expand Down Expand Up @@ -203,7 +219,7 @@ def is_different(module, response):
# Remove unnecessary properties from the response.
# This is for doing comparisons with Ansible's current parameters.
def response_to_hash(module, response):
return {u'name': response.get(u'name')}
return {u'name': response.get(u'name'), u'labels': response.get(u'labels')}


def decode_request(response, module):
Expand Down
5 changes: 5 additions & 0 deletions lib/ansible/modules/cloud/google/gcp_pubsub_topic_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@
- Name of the topic.
returned: success
type: str
labels:
description:
- A set of key/value label pairs to assign to this Topic.
returned: success
type: dict
'''

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