| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: Ansible Project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
module: sns_topic |
| 9 |
short_description: Manages AWS SNS topics and subscriptions |
| 10 |
version_added: 1.0.0 |
| 11 |
description: |
| 12 |
- The M(community.aws.sns_topic) module allows you to create, delete, and manage subscriptions for AWS SNS topics. |
| 13 |
author: |
| 14 |
- "Joel Thompson (@joelthompson)" |
| 15 |
- "Fernando Jose Pando (@nand0p)" |
| 16 |
- "Will Thames (@willthames)" |
| 17 |
options: |
| 18 |
name: |
| 19 |
description: |
| 20 |
- The name or ARN of the SNS topic to manage. |
| 21 |
required: true |
| 22 |
type: str |
| 23 |
topic_type: |
| 24 |
description: |
| 25 |
- The type of topic that should be created. Either Standard for FIFO (first-in, first-out). |
| 26 |
- Some regions, including GovCloud regions do not support FIFO topics. |
| 27 |
Use a default value of 'standard' or omit the option if the region |
| 28 |
does not support FIFO topics. |
| 29 |
choices: ["standard", "fifo"] |
| 30 |
default: 'standard' |
| 31 |
type: str |
| 32 |
version_added: 2.0.0 |
| 33 |
state: |
| 34 |
description: |
| 35 |
- Whether to create or destroy an SNS topic. |
| 36 |
default: present |
| 37 |
choices: ["absent", "present"] |
| 38 |
type: str |
| 39 |
display_name: |
| 40 |
description: |
| 41 |
- Display name of the topic. |
| 42 |
type: str |
| 43 |
policy: |
| 44 |
description: |
| 45 |
- Policy to apply to the SNS topic. |
| 46 |
- Policy body can be YAML or JSON. |
| 47 |
- This is required for certain use cases for example with S3 bucket notifications. |
| 48 |
type: dict |
| 49 |
delivery_policy: |
| 50 |
description: |
| 51 |
- Delivery policy to apply to the SNS topic. |
| 52 |
type: dict |
| 53 |
suboptions: |
| 54 |
http: |
| 55 |
description: |
| 56 |
- Delivery policy for HTTP(S) messages. |
| 57 |
- See U(https://docs.aws.amazon.com/sns/latest/dg/sns-message-delivery-retries.html) |
| 58 |
for more information. |
| 59 |
type: dict |
| 60 |
required: false |
| 61 |
suboptions: |
| 62 |
disableSubscriptionOverrides: |
| 63 |
description: |
| 64 |
- Applies this policy to all subscriptions, even if they have their own policies. |
| 65 |
type: bool |
| 66 |
required: false |
| 67 |
defaultThrottlePolicy: |
| 68 |
description: |
| 69 |
- Throttle the rate of messages sent to subsriptions. |
| 70 |
type: dict |
| 71 |
suboptions: |
| 72 |
maxReceivesPerSecond: |
| 73 |
description: |
| 74 |
- The maximum number of deliveries per second per subscription. |
| 75 |
type: int |
| 76 |
required: true |
| 77 |
required: false |
| 78 |
defaultHealthyRetryPolicy: |
| 79 |
description: |
| 80 |
- Retry policy for HTTP(S) messages. |
| 81 |
type: dict |
| 82 |
required: true |
| 83 |
suboptions: |
| 84 |
minDelayTarget: |
| 85 |
description: |
| 86 |
- The minimum delay for a retry. |
| 87 |
type: int |
| 88 |
required: true |
| 89 |
maxDelayTarget: |
| 90 |
description: |
| 91 |
- The maximum delay for a retry. |
| 92 |
type: int |
| 93 |
required: true |
| 94 |
numRetries: |
| 95 |
description: |
| 96 |
- The total number of retries. |
| 97 |
type: int |
| 98 |
required: true |
| 99 |
numMaxDelayRetries: |
| 100 |
description: |
| 101 |
- The number of retries with the maximum delay between them. |
| 102 |
type: int |
| 103 |
required: true |
| 104 |
numMinDelayRetries: |
| 105 |
description: |
| 106 |
- The number of retries with just the minimum delay between them. |
| 107 |
type: int |
| 108 |
required: true |
| 109 |
numNoDelayRetries: |
| 110 |
description: |
| 111 |
- The number of retries to be performmed immediately. |
| 112 |
type: int |
| 113 |
required: true |
| 114 |
backoffFunction: |
| 115 |
description: |
| 116 |
- The function for backoff between retries. |
| 117 |
type: str |
| 118 |
required: true |
| 119 |
choices: ['arithmetic', 'exponential', 'geometric', 'linear'] |
| 120 |
subscriptions: |
| 121 |
description: |
| 122 |
- List of subscriptions to apply to the topic. Note that AWS requires |
| 123 |
subscriptions to be confirmed, so you will need to confirm any new |
| 124 |
subscriptions. |
| 125 |
suboptions: |
| 126 |
endpoint: |
| 127 |
description: Endpoint of subscription. |
| 128 |
required: true |
| 129 |
protocol: |
| 130 |
description: Protocol of subscription. |
| 131 |
required: true |
| 132 |
attributes: |
| 133 |
description: Attributes of subscription. Only supports RawMessageDelievery for SQS endpoints. |
| 134 |
default: {} |
| 135 |
version_added: "4.1.0" |
| 136 |
type: list |
| 137 |
elements: dict |
| 138 |
default: [] |
| 139 |
purge_subscriptions: |
| 140 |
description: |
| 141 |
- "Whether to purge any subscriptions not listed here. NOTE: AWS does not |
| 142 |
allow you to purge any PendingConfirmation subscriptions, so if any |
| 143 |
exist and would be purged, they are silently skipped. This means that |
| 144 |
somebody could come back later and confirm the subscription. Sorry. |
| 145 |
Blame Amazon." |
| 146 |
default: true |
| 147 |
type: bool |
| 148 |
content_based_deduplication: |
| 149 |
description: |
| 150 |
- Whether to enable content-based deduplication for this topic. |
| 151 |
- Ignored unless I(topic_type=fifo). |
| 152 |
- Defaults to C(disabled). |
| 153 |
choices: ["disabled", "enabled"] |
| 154 |
type: str |
| 155 |
version_added: 5.3.0 |
| 156 |
notes: |
| 157 |
- Support for I(tags) and I(purge_tags) was added in release 5.3.0. |
| 158 |
extends_documentation_fragment: |
| 159 |
- amazon.aws.common.modules |
| 160 |
- amazon.aws.region.modules |
| 161 |
- amazon.aws.tags.modules |
| 162 |
- amazon.aws.boto3 |
| 163 |
""" |
| 164 |
|
| 165 |
EXAMPLES = r""" |
| 166 |
|
| 167 |
- name: Create alarm SNS topic |
| 168 |
community.aws.sns_topic: |
| 169 |
name: "alarms" |
| 170 |
state: present |
| 171 |
display_name: "alarm SNS topic" |
| 172 |
delivery_policy: |
| 173 |
http: |
| 174 |
defaultHealthyRetryPolicy: |
| 175 |
minDelayTarget: 2 |
| 176 |
maxDelayTarget: 4 |
| 177 |
numRetries: 9 |
| 178 |
numMaxDelayRetries: 5 |
| 179 |
numMinDelayRetries: 2 |
| 180 |
numNoDelayRetries: 2 |
| 181 |
backoffFunction: "linear" |
| 182 |
disableSubscriptionOverrides: true |
| 183 |
defaultThrottlePolicy: |
| 184 |
maxReceivesPerSecond: 10 |
| 185 |
subscriptions: |
| 186 |
- endpoint: "[email protected]" |
| 187 |
protocol: "email" |
| 188 |
- endpoint: "my_mobile_number" |
| 189 |
protocol: "sms" |
| 190 |
|
| 191 |
- name: Create a topic permitting S3 bucket notifications |
| 192 |
community.aws.sns_topic: |
| 193 |
name: "S3Notifications" |
| 194 |
state: present |
| 195 |
display_name: "S3 notifications SNS topic" |
| 196 |
policy: |
| 197 |
Id: s3-topic-policy |
| 198 |
Version: 2012-10-17 |
| 199 |
Statement: |
| 200 |
- Sid: Statement-id |
| 201 |
Effect: Allow |
| 202 |
Resource: "arn:aws:sns:*:*:S3Notifications" |
| 203 |
Principal: |
| 204 |
Service: s3.amazonaws.com |
| 205 |
Action: sns:Publish |
| 206 |
Condition: |
| 207 |
ArnLike: |
| 208 |
aws:SourceArn: "arn:aws:s3:*:*:SomeBucket" |
| 209 |
|
| 210 |
- name: Example deleting a topic |
| 211 |
community.aws.sns_topic: |
| 212 |
name: "ExampleTopic" |
| 213 |
state: absent |
| 214 |
""" |
| 215 |
|
| 216 |
RETURN = r""" |
| 217 |
sns_arn: |
| 218 |
description: The ARN of the topic you are modifying |
| 219 |
type: str |
| 220 |
returned: always |
| 221 |
sample: "arn:aws:sns:us-east-2:123456789012:my_topic_name" |
| 222 |
sns_topic: |
| 223 |
description: Dict of sns topic details |
| 224 |
type: complex |
| 225 |
returned: always |
| 226 |
contains: |
| 227 |
attributes_set: |
| 228 |
description: list of attributes set during this run |
| 229 |
returned: always |
| 230 |
type: list |
| 231 |
sample: [] |
| 232 |
check_mode: |
| 233 |
description: whether check mode was on |
| 234 |
returned: always |
| 235 |
type: bool |
| 236 |
sample: false |
| 237 |
content_based_deduplication: |
| 238 |
description: Whether or not content_based_deduplication was set |
| 239 |
returned: always |
| 240 |
type: str |
| 241 |
sample: disabled |
| 242 |
version_added: 5.3.0 |
| 243 |
delivery_policy: |
| 244 |
description: Delivery policy for the SNS topic |
| 245 |
returned: when topic is owned by this AWS account |
| 246 |
type: str |
| 247 |
sample: > |
| 248 |
{"http":{"defaultHealthyRetryPolicy":{"minDelayTarget":20,"maxDelayTarget":20,"numRetries":3,"numMaxDelayRetries":0, |
| 249 |
"numNoDelayRetries":0,"numMinDelayRetries":0,"backoffFunction":"linear"},"disableSubscriptionOverrides":false}} |
| 250 |
display_name: |
| 251 |
description: Display name for SNS topic |
| 252 |
returned: when topic is owned by this AWS account |
| 253 |
type: str |
| 254 |
sample: My topic name |
| 255 |
name: |
| 256 |
description: Topic name |
| 257 |
returned: always |
| 258 |
type: str |
| 259 |
sample: ansible-test-dummy-topic |
| 260 |
owner: |
| 261 |
description: AWS account that owns the topic |
| 262 |
returned: when topic is owned by this AWS account |
| 263 |
type: str |
| 264 |
sample: '123456789012' |
| 265 |
policy: |
| 266 |
description: Policy for the SNS topic |
| 267 |
returned: when topic is owned by this AWS account |
| 268 |
type: str |
| 269 |
sample: > |
| 270 |
{"Version":"2012-10-17","Id":"SomePolicyId","Statement":[{"Sid":"ANewSid","Effect":"Allow","Principal":{"AWS":"arn:aws:iam::123456789012:root"}, |
| 271 |
"Action":"sns:Subscribe","Resource":"arn:aws:sns:us-east-2:123456789012:ansible-test-dummy-topic","Condition":{"StringEquals":{"sns:Protocol":"email"}}}]} |
| 272 |
state: |
| 273 |
description: whether the topic is present or absent |
| 274 |
returned: always |
| 275 |
type: str |
| 276 |
sample: present |
| 277 |
subscriptions: |
| 278 |
description: List of subscribers to the topic in this AWS account |
| 279 |
returned: always |
| 280 |
type: list |
| 281 |
sample: [] |
| 282 |
subscriptions_added: |
| 283 |
description: List of subscribers added in this run |
| 284 |
returned: always |
| 285 |
type: list |
| 286 |
sample: [] |
| 287 |
subscriptions_confirmed: |
| 288 |
description: Count of confirmed subscriptions |
| 289 |
returned: when topic is owned by this AWS account |
| 290 |
type: str |
| 291 |
sample: '0' |
| 292 |
subscriptions_deleted: |
| 293 |
description: Count of deleted subscriptions |
| 294 |
returned: when topic is owned by this AWS account |
| 295 |
type: str |
| 296 |
sample: '0' |
| 297 |
subscriptions_existing: |
| 298 |
description: List of existing subscriptions |
| 299 |
returned: always |
| 300 |
type: list |
| 301 |
sample: [] |
| 302 |
subscriptions_new: |
| 303 |
description: List of new subscriptions |
| 304 |
returned: always |
| 305 |
type: list |
| 306 |
sample: [] |
| 307 |
subscriptions_pending: |
| 308 |
description: Count of pending subscriptions |
| 309 |
returned: when topic is owned by this AWS account |
| 310 |
type: str |
| 311 |
sample: '0' |
| 312 |
subscriptions_purge: |
| 313 |
description: Whether or not purge_subscriptions was set |
| 314 |
returned: always |
| 315 |
type: bool |
| 316 |
sample: true |
| 317 |
topic_arn: |
| 318 |
description: ARN of the SNS topic (equivalent to sns_arn) |
| 319 |
returned: when topic is owned by this AWS account |
| 320 |
type: str |
| 321 |
sample: arn:aws:sns:us-east-2:123456789012:ansible-test-dummy-topic |
| 322 |
topic_created: |
| 323 |
description: Whether the topic was created |
| 324 |
returned: always |
| 325 |
type: bool |
| 326 |
sample: false |
| 327 |
topic_deleted: |
| 328 |
description: Whether the topic was deleted |
| 329 |
returned: always |
| 330 |
type: bool |
| 331 |
sample: false |
| 332 |
""" |
| 333 |
|
| 334 |
import json |
| 335 |
|
| 336 |
try: |
| 337 |
import botocore |
| 338 |
except ImportError: |
| 339 |
pass # handled by AnsibleAWSModule |
| 340 |
|
| 341 |
from ansible_collections.amazon.aws.plugins.module_utils.arn import parse_aws_arn |
| 342 |
from ansible_collections.amazon.aws.plugins.module_utils.policy import compare_policies |
| 343 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import ansible_dict_to_boto3_tag_list |
| 344 |
from ansible_collections.amazon.aws.plugins.module_utils.transformation import scrub_none_parameters |
| 345 |
|
| 346 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 347 |
from ansible_collections.community.aws.plugins.module_utils.sns import canonicalize_endpoint |
| 348 |
from ansible_collections.community.aws.plugins.module_utils.sns import compare_delivery_policies |
| 349 |
from ansible_collections.community.aws.plugins.module_utils.sns import get_info |
| 350 |
from ansible_collections.community.aws.plugins.module_utils.sns import list_topic_subscriptions |
| 351 |
from ansible_collections.community.aws.plugins.module_utils.sns import list_topics |
| 352 |
from ansible_collections.community.aws.plugins.module_utils.sns import topic_arn_lookup |
| 353 |
from ansible_collections.community.aws.plugins.module_utils.sns import update_tags |
| 354 |
|
| 355 |
|
| 356 |
class SnsTopicManager(object): |
| 357 |
"""Handles SNS Topic creation and destruction""" |
| 358 |
|
| 359 |
def __init__( |
| 360 |
self, |
| 361 |
module, |
| 362 |
name, |
| 363 |
topic_type, |
| 364 |
state, |
| 365 |
display_name, |
| 366 |
policy, |
| 367 |
delivery_policy, |
| 368 |
subscriptions, |
| 369 |
purge_subscriptions, |
| 370 |
tags, |
| 371 |
purge_tags, |
| 372 |
content_based_deduplication, |
| 373 |
check_mode, |
| 374 |
): |
| 375 |
self.connection = module.client("sns") |
| 376 |
self.module = module |
| 377 |
self.name = name |
| 378 |
self.topic_type = topic_type |
| 379 |
self.state = state |
| 380 |
self.display_name = display_name |
| 381 |
self.policy = policy |
| 382 |
self.delivery_policy = scrub_none_parameters(delivery_policy) if delivery_policy else None |
| 383 |
self.subscriptions = subscriptions |
| 384 |
self.subscriptions_existing = [] |
| 385 |
self.subscriptions_deleted = [] |
| 386 |
self.subscriptions_added = [] |
| 387 |
self.subscriptions_attributes_set = [] |
| 388 |
self.desired_subscription_attributes = dict() |
| 389 |
self.purge_subscriptions = purge_subscriptions |
| 390 |
self.content_based_deduplication = content_based_deduplication |
| 391 |
self.check_mode = check_mode |
| 392 |
self.topic_created = False |
| 393 |
self.topic_deleted = False |
| 394 |
self.topic_arn = None |
| 395 |
self.attributes_set = [] |
| 396 |
self.tags = tags |
| 397 |
self.purge_tags = purge_tags |
| 398 |
|
| 399 |
def _create_topic(self): |
| 400 |
attributes = {} |
| 401 |
tags = [] |
| 402 |
|
| 403 |
# NOTE: Never set FifoTopic = False. Some regions (including GovCloud) |
| 404 |
# don't support the attribute being set, even to False. |
| 405 |
if self.topic_type == "fifo": |
| 406 |
attributes["FifoTopic"] = "true" |
| 407 |
if not self.name.endswith(".fifo"): |
| 408 |
self.name = self.name + ".fifo" |
| 409 |
|
| 410 |
if self.tags: |
| 411 |
tags = ansible_dict_to_boto3_tag_list(self.tags) |
| 412 |
|
| 413 |
if not self.check_mode: |
| 414 |
try: |
| 415 |
response = self.connection.create_topic(Name=self.name, Attributes=attributes, Tags=tags) |
| 416 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 417 |
self.module.fail_json_aws(e, msg=f"Couldn't create topic {self.name}") |
| 418 |
self.topic_arn = response["TopicArn"] |
| 419 |
return True |
| 420 |
|
| 421 |
def _set_topic_attrs(self): |
| 422 |
changed = False |
| 423 |
try: |
| 424 |
topic_attributes = self.connection.get_topic_attributes(TopicArn=self.topic_arn)["Attributes"] |
| 425 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 426 |
self.module.fail_json_aws(e, msg=f"Couldn't get topic attributes for topic {self.topic_arn}") |
| 427 |
|
| 428 |
if self.display_name and self.display_name != topic_attributes["DisplayName"]: |
| 429 |
changed = True |
| 430 |
self.attributes_set.append("display_name") |
| 431 |
if not self.check_mode: |
| 432 |
try: |
| 433 |
self.connection.set_topic_attributes( |
| 434 |
TopicArn=self.topic_arn, AttributeName="DisplayName", AttributeValue=self.display_name |
| 435 |
) |
| 436 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 437 |
self.module.fail_json_aws(e, msg="Couldn't set display name") |
| 438 |
|
| 439 |
if self.policy and compare_policies(self.policy, json.loads(topic_attributes["Policy"])): |
| 440 |
changed = True |
| 441 |
self.attributes_set.append("policy") |
| 442 |
if not self.check_mode: |
| 443 |
try: |
| 444 |
self.connection.set_topic_attributes( |
| 445 |
TopicArn=self.topic_arn, AttributeName="Policy", AttributeValue=json.dumps(self.policy) |
| 446 |
) |
| 447 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 448 |
self.module.fail_json_aws(e, msg="Couldn't set topic policy") |
| 449 |
|
| 450 |
# Set content-based deduplication attribute. Ignore if topic_type is not fifo. |
| 451 |
if ( |
| 452 |
"FifoTopic" in topic_attributes and topic_attributes["FifoTopic"] == "true" |
| 453 |
) and self.content_based_deduplication: |
| 454 |
enabled = "true" if self.content_based_deduplication in "enabled" else "false" |
| 455 |
if enabled != topic_attributes["ContentBasedDeduplication"]: |
| 456 |
changed = True |
| 457 |
self.attributes_set.append("content_based_deduplication") |
| 458 |
if not self.check_mode: |
| 459 |
try: |
| 460 |
self.connection.set_topic_attributes( |
| 461 |
TopicArn=self.topic_arn, AttributeName="ContentBasedDeduplication", AttributeValue=enabled |
| 462 |
) |
| 463 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 464 |
self.module.fail_json_aws(e, msg="Couldn't set content-based deduplication") |
| 465 |
|
| 466 |
if self.delivery_policy and ( |
| 467 |
"DeliveryPolicy" not in topic_attributes |
| 468 |
or compare_delivery_policies(self.delivery_policy, json.loads(topic_attributes["DeliveryPolicy"])) |
| 469 |
): |
| 470 |
changed = True |
| 471 |
self.attributes_set.append("delivery_policy") |
| 472 |
if not self.check_mode: |
| 473 |
try: |
| 474 |
self.connection.set_topic_attributes( |
| 475 |
TopicArn=self.topic_arn, |
| 476 |
AttributeName="DeliveryPolicy", |
| 477 |
AttributeValue=json.dumps(self.delivery_policy), |
| 478 |
) |
| 479 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 480 |
self.module.fail_json_aws(e, msg="Couldn't set topic delivery policy") |
| 481 |
return changed |
| 482 |
|
| 483 |
def _set_topic_subs(self): |
| 484 |
changed = False |
| 485 |
subscriptions_existing_list = set() |
| 486 |
desired_subscriptions = [ |
| 487 |
(sub["protocol"], canonicalize_endpoint(sub["protocol"], sub["endpoint"])) for sub in self.subscriptions |
| 488 |
] |
| 489 |
|
| 490 |
for sub in list_topic_subscriptions(self.connection, self.module, self.topic_arn): |
| 491 |
sub_key = (sub["Protocol"], sub["Endpoint"]) |
| 492 |
subscriptions_existing_list.add(sub_key) |
| 493 |
if ( |
| 494 |
self.purge_subscriptions |
| 495 |
and sub_key not in desired_subscriptions |
| 496 |
and sub["SubscriptionArn"] not in ("PendingConfirmation", "Deleted") |
| 497 |
): |
| 498 |
changed = True |
| 499 |
self.subscriptions_deleted.append(sub_key) |
| 500 |
if not self.check_mode: |
| 501 |
try: |
| 502 |
self.connection.unsubscribe(SubscriptionArn=sub["SubscriptionArn"]) |
| 503 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 504 |
self.module.fail_json_aws(e, msg="Couldn't unsubscribe from topic") |
| 505 |
|
| 506 |
for protocol, endpoint in set(desired_subscriptions).difference(subscriptions_existing_list): |
| 507 |
changed = True |
| 508 |
self.subscriptions_added.append((protocol, endpoint)) |
| 509 |
if not self.check_mode: |
| 510 |
try: |
| 511 |
self.connection.subscribe(TopicArn=self.topic_arn, Protocol=protocol, Endpoint=endpoint) |
| 512 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 513 |
self.module.fail_json_aws(e, msg=f"Couldn't subscribe to topic {self.topic_arn}") |
| 514 |
return changed |
| 515 |
|
| 516 |
def _init_desired_subscription_attributes(self): |
| 517 |
for sub in self.subscriptions: |
| 518 |
sub_key = (sub["protocol"], canonicalize_endpoint(sub["protocol"], sub["endpoint"])) |
| 519 |
tmp_dict = sub.get("attributes", {}) |
| 520 |
# aws sdk expects values to be strings |
| 521 |
# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sns.html#SNS.Client.set_subscription_attributes |
| 522 |
for k, v in tmp_dict.items(): |
| 523 |
tmp_dict[k] = str(v) |
| 524 |
|
| 525 |
self.desired_subscription_attributes[sub_key] = tmp_dict |
| 526 |
|
| 527 |
def _set_topic_subs_attributes(self): |
| 528 |
changed = False |
| 529 |
for sub in list_topic_subscriptions(self.connection, self.module, self.topic_arn): |
| 530 |
sub_key = (sub["Protocol"], sub["Endpoint"]) |
| 531 |
sub_arn = sub["SubscriptionArn"] |
| 532 |
if not self.desired_subscription_attributes.get(sub_key): |
| 533 |
# subscription attributes aren't defined in desired, skipping |
| 534 |
continue |
| 535 |
|
| 536 |
try: |
| 537 |
sub_current_attributes = self.connection.get_subscription_attributes(SubscriptionArn=sub_arn)[ |
| 538 |
"Attributes" |
| 539 |
] |
| 540 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 541 |
self.module.fail_json_aws(e, f"Couldn't get subscription attributes for subscription {sub_arn}") |
| 542 |
|
| 543 |
raw_message = self.desired_subscription_attributes[sub_key].get("RawMessageDelivery") |
| 544 |
if raw_message is not None and "RawMessageDelivery" in sub_current_attributes: |
| 545 |
if sub_current_attributes["RawMessageDelivery"].lower() != raw_message.lower(): |
| 546 |
changed = True |
| 547 |
if not self.check_mode: |
| 548 |
try: |
| 549 |
self.connection.set_subscription_attributes( |
| 550 |
SubscriptionArn=sub_arn, AttributeName="RawMessageDelivery", AttributeValue=raw_message |
| 551 |
) |
| 552 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 553 |
self.module.fail_json_aws(e, "Couldn't set RawMessageDelivery subscription attribute") |
| 554 |
|
| 555 |
return changed |
| 556 |
|
| 557 |
def _delete_subscriptions(self): |
| 558 |
# NOTE: subscriptions in 'PendingConfirmation' timeout in 3 days |
| 559 |
# https://forums.aws.amazon.com/thread.jspa?threadID=85993 |
| 560 |
subscriptions = list_topic_subscriptions(self.connection, self.module, self.topic_arn) |
| 561 |
if not subscriptions: |
| 562 |
return False |
| 563 |
for sub in subscriptions: |
| 564 |
if sub["SubscriptionArn"] not in ("PendingConfirmation", "Deleted"): |
| 565 |
self.subscriptions_deleted.append(sub["SubscriptionArn"]) |
| 566 |
if not self.check_mode: |
| 567 |
try: |
| 568 |
self.connection.unsubscribe(SubscriptionArn=sub["SubscriptionArn"]) |
| 569 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 570 |
self.module.fail_json_aws(e, msg="Couldn't unsubscribe from topic") |
| 571 |
return True |
| 572 |
|
| 573 |
def _delete_topic(self): |
| 574 |
self.topic_deleted = True |
| 575 |
if not self.check_mode: |
| 576 |
try: |
| 577 |
self.connection.delete_topic(TopicArn=self.topic_arn) |
| 578 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 579 |
self.module.fail_json_aws(e, msg=f"Couldn't delete topic {self.topic_arn}") |
| 580 |
return True |
| 581 |
|
| 582 |
def _name_is_arn(self): |
| 583 |
return bool(parse_aws_arn(self.name)) |
| 584 |
|
| 585 |
def ensure_ok(self): |
| 586 |
changed = False |
| 587 |
self.populate_topic_arn() |
| 588 |
if not self.topic_arn: |
| 589 |
changed = self._create_topic() |
| 590 |
if self.topic_arn in list_topics(self.connection, self.module): |
| 591 |
changed |= self._set_topic_attrs() |
| 592 |
elif self.display_name or self.policy or self.delivery_policy: |
| 593 |
self.module.fail_json( |
| 594 |
msg="Cannot set display name, policy or delivery policy for SNS topics not owned by this account" |
| 595 |
) |
| 596 |
changed |= self._set_topic_subs() |
| 597 |
self._init_desired_subscription_attributes() |
| 598 |
if self.topic_arn in list_topics(self.connection, self.module): |
| 599 |
changed |= self._set_topic_subs_attributes() |
| 600 |
elif any(self.desired_subscription_attributes.values()): |
| 601 |
self.module.fail_json(msg="Cannot set subscription attributes for SNS topics not owned by this account") |
| 602 |
# Check tagging |
| 603 |
changed |= update_tags(self.connection, self.module, self.topic_arn) |
| 604 |
|
| 605 |
return changed |
| 606 |
|
| 607 |
def ensure_gone(self): |
| 608 |
changed = False |
| 609 |
self.populate_topic_arn() |
| 610 |
if self.topic_arn: |
| 611 |
if self.topic_arn not in list_topics(self.connection, self.module): |
| 612 |
self.module.fail_json( |
| 613 |
msg="Cannot use state=absent with third party ARN. Use subscribers=[] to unsubscribe" |
| 614 |
) |
| 615 |
changed = self._delete_subscriptions() |
| 616 |
changed |= self._delete_topic() |
| 617 |
return changed |
| 618 |
|
| 619 |
def populate_topic_arn(self): |
| 620 |
if self._name_is_arn(): |
| 621 |
self.topic_arn = self.name |
| 622 |
return |
| 623 |
|
| 624 |
name = self.name |
| 625 |
if self.topic_type == "fifo" and not name.endswith(".fifo"): |
| 626 |
name += ".fifo" |
| 627 |
self.topic_arn = topic_arn_lookup(self.connection, self.module, name) |
| 628 |
|
| 629 |
|
| 630 |
def main(): |
| 631 |
# We're kinda stuck with CamelCase here, it would be nice to switch to |
| 632 |
# snake_case, but we'd need to purge out the alias entries |
| 633 |
http_retry_args = dict( |
| 634 |
minDelayTarget=dict(type="int", required=True), |
| 635 |
maxDelayTarget=dict(type="int", required=True), |
| 636 |
numRetries=dict(type="int", required=True), |
| 637 |
numMaxDelayRetries=dict(type="int", required=True), |
| 638 |
numMinDelayRetries=dict(type="int", required=True), |
| 639 |
numNoDelayRetries=dict(type="int", required=True), |
| 640 |
backoffFunction=dict(type="str", required=True, choices=["arithmetic", "exponential", "geometric", "linear"]), |
| 641 |
) |
| 642 |
http_delivery_args = dict( |
| 643 |
defaultHealthyRetryPolicy=dict(type="dict", required=True, options=http_retry_args), |
| 644 |
disableSubscriptionOverrides=dict(type="bool", required=False), |
| 645 |
defaultThrottlePolicy=dict( |
| 646 |
type="dict", |
| 647 |
required=False, |
| 648 |
options=dict( |
| 649 |
maxReceivesPerSecond=dict(type="int", required=True), |
| 650 |
), |
| 651 |
), |
| 652 |
) |
| 653 |
delivery_args = dict( |
| 654 |
http=dict(type="dict", required=False, options=http_delivery_args), |
| 655 |
) |
| 656 |
|
| 657 |
argument_spec = dict( |
| 658 |
name=dict(required=True), |
| 659 |
topic_type=dict(type="str", default="standard", choices=["standard", "fifo"]), |
| 660 |
state=dict(default="present", choices=["present", "absent"]), |
| 661 |
display_name=dict(), |
| 662 |
policy=dict(type="dict"), |
| 663 |
delivery_policy=dict(type="dict", options=delivery_args), |
| 664 |
subscriptions=dict(default=[], type="list", elements="dict"), |
| 665 |
purge_subscriptions=dict(type="bool", default=True), |
| 666 |
tags=dict(type="dict", aliases=["resource_tags"]), |
| 667 |
purge_tags=dict(type="bool", default=True), |
| 668 |
content_based_deduplication=dict(choices=["enabled", "disabled"]), |
| 669 |
) |
| 670 |
|
| 671 |
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) |
| 672 |
|
| 673 |
name = module.params.get("name") |
| 674 |
topic_type = module.params.get("topic_type") |
| 675 |
state = module.params.get("state") |
| 676 |
display_name = module.params.get("display_name") |
| 677 |
policy = module.params.get("policy") |
| 678 |
delivery_policy = module.params.get("delivery_policy") |
| 679 |
subscriptions = module.params.get("subscriptions") |
| 680 |
purge_subscriptions = module.params.get("purge_subscriptions") |
| 681 |
content_based_deduplication = module.params.get("content_based_deduplication") |
| 682 |
check_mode = module.check_mode |
| 683 |
tags = module.params.get("tags") |
| 684 |
purge_tags = module.params.get("purge_tags") |
| 685 |
|
| 686 |
sns_topic = SnsTopicManager( |
| 687 |
module, |
| 688 |
name, |
| 689 |
topic_type, |
| 690 |
state, |
| 691 |
display_name, |
| 692 |
policy, |
| 693 |
delivery_policy, |
| 694 |
subscriptions, |
| 695 |
purge_subscriptions, |
| 696 |
tags, |
| 697 |
purge_tags, |
| 698 |
content_based_deduplication, |
| 699 |
check_mode, |
| 700 |
) |
| 701 |
|
| 702 |
if state == "present": |
| 703 |
changed = sns_topic.ensure_ok() |
| 704 |
elif state == "absent": |
| 705 |
changed = sns_topic.ensure_gone() |
| 706 |
|
| 707 |
sns_facts = dict( |
| 708 |
changed=changed, |
| 709 |
sns_arn=sns_topic.topic_arn, |
| 710 |
sns_topic=get_info(sns_topic.connection, module, sns_topic.topic_arn), |
| 711 |
) |
| 712 |
|
| 713 |
module.exit_json(**sns_facts) |
| 714 |
|
| 715 |
|
| 716 |
if __name__ == "__main__": |
| 717 |
main() |