| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = ''' |
| 12 |
--- |
| 13 |
module: azure_rm_servicebustopicsubscription |
| 14 |
version_added: "0.1.2" |
| 15 |
short_description: Manage Azure Service Bus subscription |
| 16 |
description: |
| 17 |
- Create, update or delete an Azure Service Bus subscriptions. |
| 18 |
options: |
| 19 |
resource_group: |
| 20 |
description: |
| 21 |
- Name of resource group. |
| 22 |
required: true |
| 23 |
type: str |
| 24 |
name: |
| 25 |
description: |
| 26 |
- Name of the servicebus subscription. |
| 27 |
required: true |
| 28 |
type: str |
| 29 |
state: |
| 30 |
description: |
| 31 |
- Assert the state of the servicebus subscription. Use C(present) to create or update and use C(absent) to delete. |
| 32 |
default: present |
| 33 |
type: str |
| 34 |
choices: |
| 35 |
- absent |
| 36 |
- present |
| 37 |
namespace: |
| 38 |
description: |
| 39 |
- Servicebus namespace name. |
| 40 |
- A namespace is a scoping container for all messaging components. |
| 41 |
- Multiple subscriptions and topics can reside within a single namespace, and namespaces often serve as application containers. |
| 42 |
required: true |
| 43 |
type: str |
| 44 |
topic: |
| 45 |
description: |
| 46 |
- Topic name which the subscription subscribe to. |
| 47 |
required: true |
| 48 |
type: str |
| 49 |
auto_delete_on_idle_in_seconds: |
| 50 |
description: |
| 51 |
- Time idle interval after which a subscription is automatically deleted. |
| 52 |
- The minimum duration is 5 minutes. |
| 53 |
type: int |
| 54 |
dead_lettering_on_message_expiration: |
| 55 |
description: |
| 56 |
- A value that indicates whether a subscription has dead letter support when a message expires. |
| 57 |
type: bool |
| 58 |
dead_lettering_on_filter_evaluation_exceptions: |
| 59 |
description: |
| 60 |
- Value that indicates whether a subscription has dead letter support on filter evaluation exceptions. |
| 61 |
type: bool |
| 62 |
default_message_time_to_live_seconds: |
| 63 |
description: |
| 64 |
- Default message timespan to live value. |
| 65 |
- This is the duration after which the message expires, starting from when the message is sent to Service Bus. |
| 66 |
- This is the default value used when TimeToLive is not set on a message itself. |
| 67 |
type: int |
| 68 |
enable_batched_operations: |
| 69 |
description: |
| 70 |
- Value that indicates whether server-side batched operations are enabled. |
| 71 |
type: bool |
| 72 |
forward_dead_lettered_messages_to: |
| 73 |
description: |
| 74 |
- Queue or topic name to forward the Dead Letter message for a subscription. |
| 75 |
type: str |
| 76 |
forward_to: |
| 77 |
description: |
| 78 |
- Queue or topic name to forward the messages for a subscription. |
| 79 |
type: str |
| 80 |
lock_duration_in_seconds: |
| 81 |
description: |
| 82 |
- Timespan duration of a peek-lock. |
| 83 |
- The amount of time that the message is locked for other receivers. |
| 84 |
- The maximum value for LockDuration is 5 minutes. |
| 85 |
type: int |
| 86 |
max_delivery_count: |
| 87 |
description: |
| 88 |
- he maximum delivery count. |
| 89 |
- A message is automatically deadlettered after this number of deliveries. |
| 90 |
type: int |
| 91 |
requires_session: |
| 92 |
description: |
| 93 |
- A value that indicates whether the subscription supports the concept of sessions. |
| 94 |
type: bool |
| 95 |
duplicate_detection_time_in_seconds: |
| 96 |
description: |
| 97 |
- TimeSpan structure that defines the duration of the duplicate detection history. |
| 98 |
type: int |
| 99 |
status: |
| 100 |
description: |
| 101 |
- Status of the entity. |
| 102 |
type: str |
| 103 |
choices: |
| 104 |
- active |
| 105 |
- disabled |
| 106 |
- send_disabled |
| 107 |
- receive_disabled |
| 108 |
|
| 109 |
extends_documentation_fragment: |
| 110 |
- azure.azcollection.azure |
| 111 |
|
| 112 |
author: |
| 113 |
- Yuwei Zhou (@yuwzho) |
| 114 |
|
| 115 |
''' |
| 116 |
|
| 117 |
EXAMPLES = ''' |
| 118 |
- name: Create a subscription |
| 119 |
azure_rm_servicebustopicsubscription: |
| 120 |
name: sbsub |
| 121 |
resource_group: myResourceGroup |
| 122 |
namespace: bar |
| 123 |
topic: subtopic |
| 124 |
''' |
| 125 |
RETURN = ''' |
| 126 |
id: |
| 127 |
description: |
| 128 |
- Current state of the subscription. |
| 129 |
returned: success |
| 130 |
type: str |
| 131 |
sample: "/subscriptions/xxx...xxx/resourceGroups/myResourceGroup/providers/Microsoft.ServiceBus/ |
| 132 |
namespaces/nsb57dc95979/topics/topicb57dc95979/subscriptions/subsb57dc95979" |
| 133 |
''' |
| 134 |
|
| 135 |
try: |
| 136 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 137 |
except ImportError: |
| 138 |
|
| 139 |
pass |
| 140 |
|
| 141 |
from ansible.module_utils.common.dict_transformations import _snake_to_camel, _camel_to_snake |
| 142 |
from ansible.module_utils._text import to_native |
| 143 |
from datetime import datetime, timedelta |
| 144 |
|
| 145 |
|
| 146 |
duration_spec_map = dict( |
| 147 |
default_message_time_to_live='default_message_time_to_live_seconds', |
| 148 |
duplicate_detection_history_time_window='duplicate_detection_time_in_seconds', |
| 149 |
auto_delete_on_idle='auto_delete_on_idle_in_seconds', |
| 150 |
lock_duration='lock_duration_in_seconds' |
| 151 |
) |
| 152 |
|
| 153 |
|
| 154 |
class AzureRMServiceSubscription(AzureRMModuleBase): |
| 155 |
|
| 156 |
def __init__(self): |
| 157 |
|
| 158 |
self.module_arg_spec = dict( |
| 159 |
auto_delete_on_idle_in_seconds=dict(type='int'), |
| 160 |
dead_lettering_on_filter_evaluation_exceptions=dict(type='bool'), |
| 161 |
dead_lettering_on_message_expiration=dict(type='bool'), |
| 162 |
default_message_time_to_live_seconds=dict(type='int'), |
| 163 |
duplicate_detection_time_in_seconds=dict(type='int'), |
| 164 |
enable_batched_operations=dict(type='bool'), |
| 165 |
forward_dead_lettered_messages_to=dict(type='str'), |
| 166 |
forward_to=dict(type='str'), |
| 167 |
lock_duration_in_seconds=dict(type='int'), |
| 168 |
max_delivery_count=dict(type='int'), |
| 169 |
name=dict(type='str', required=True), |
| 170 |
namespace=dict(type='str', required=True), |
| 171 |
requires_session=dict(type='bool'), |
| 172 |
resource_group=dict(type='str', required=True), |
| 173 |
state=dict(type='str', default='present', choices=['present', 'absent']), |
| 174 |
status=dict(type='str', |
| 175 |
choices=['active', 'disabled', 'send_disabled', 'receive_disabled']), |
| 176 |
topic=dict(type='str', required=True) |
| 177 |
) |
| 178 |
|
| 179 |
self.auto_delete_on_idle_in_seconds = None |
| 180 |
self.dead_lettering_on_filter_evaluation_exceptions = None |
| 181 |
self.dead_lettering_on_message_expiration = None |
| 182 |
self.default_message_time_to_live_seconds = None |
| 183 |
self.duplicate_detection_time_in_seconds = None |
| 184 |
self.enable_batched_operations = None |
| 185 |
self.forward_dead_lettered_messages_to = None |
| 186 |
self.forward_to = None |
| 187 |
self.lock_duration_in_seconds = None |
| 188 |
self.max_delivery_count = None |
| 189 |
self.name = None |
| 190 |
self.namespace = None |
| 191 |
self.requires_session = None |
| 192 |
self.resource_group = None |
| 193 |
self.state = None |
| 194 |
self.status = None |
| 195 |
self.topic = None |
| 196 |
|
| 197 |
self.results = dict( |
| 198 |
changed=False, |
| 199 |
id=None |
| 200 |
) |
| 201 |
|
| 202 |
super(AzureRMServiceSubscription, self).__init__(self.module_arg_spec, |
| 203 |
supports_tags=False, |
| 204 |
supports_check_mode=True) |
| 205 |
|
| 206 |
def exec_module(self, **kwargs): |
| 207 |
|
| 208 |
for key in list(self.module_arg_spec.keys()): |
| 209 |
setattr(self, key, kwargs[key]) |
| 210 |
|
| 211 |
changed = False |
| 212 |
|
| 213 |
original = self.get() |
| 214 |
if self.state == 'present': |
| 215 |
|
| 216 |
params = dict( |
| 217 |
dead_lettering_on_filter_evaluation_exceptions=self.dead_lettering_on_filter_evaluation_exceptions, |
| 218 |
dead_lettering_on_message_expiration=self.dead_lettering_on_message_expiration, |
| 219 |
enable_batched_operations=self.enable_batched_operations, |
| 220 |
forward_dead_lettered_messages_to=self.forward_dead_lettered_messages_to, |
| 221 |
forward_to=self.forward_to, |
| 222 |
max_delivery_count=self.max_delivery_count, |
| 223 |
requires_session=self.requires_session |
| 224 |
) |
| 225 |
if self.status: |
| 226 |
params['status'] = self.servicebus_models.EntityStatus(str.capitalize(_snake_to_camel(self.status))) |
| 227 |
for k, v in duration_spec_map.items(): |
| 228 |
seconds = getattr(self, v) |
| 229 |
if seconds: |
| 230 |
params[k] = timedelta(seconds=seconds) |
| 231 |
|
| 232 |
instance = self.servicebus_models.SBSubscription(**params) |
| 233 |
result = original |
| 234 |
if not original: |
| 235 |
changed = True |
| 236 |
result = instance |
| 237 |
else: |
| 238 |
result = original |
| 239 |
attribute_map_keys = set(self.servicebus_models.SBSubscription._attribute_map.keys()) |
| 240 |
validation_keys = set(self.servicebus_models.SBSubscription._validation.keys()) |
| 241 |
attribute_map = attribute_map_keys - validation_keys |
| 242 |
for attribute in attribute_map: |
| 243 |
value = getattr(instance, attribute) |
| 244 |
if value and value != getattr(original, attribute): |
| 245 |
changed = True |
| 246 |
if changed and not self.check_mode: |
| 247 |
result = self.create_or_update(instance) |
| 248 |
self.results = self.to_dict(result) |
| 249 |
elif original: |
| 250 |
changed = True |
| 251 |
if not self.check_mode: |
| 252 |
self.delete() |
| 253 |
self.results['deleted'] = True |
| 254 |
|
| 255 |
self.results['changed'] = changed |
| 256 |
return self.results |
| 257 |
|
| 258 |
def create_or_update(self, param): |
| 259 |
try: |
| 260 |
client = self._get_client() |
| 261 |
return client.create_or_update(self.resource_group, self.namespace, self.topic, self.name, param) |
| 262 |
except Exception as exc: |
| 263 |
self.fail("Error creating or updating servicebus subscription {0} - {1}".format(self.name, str(exc))) |
| 264 |
|
| 265 |
def delete(self): |
| 266 |
try: |
| 267 |
client = self._get_client() |
| 268 |
client.delete(self.resource_group, self.namespace, self.topic, self.name) |
| 269 |
return True |
| 270 |
except Exception as exc: |
| 271 |
self.fail("Error deleting servicebus subscription {0} - {1}".format(self.name, str(exc))) |
| 272 |
|
| 273 |
def _get_client(self): |
| 274 |
return self.servicebus_client.subscriptions |
| 275 |
|
| 276 |
def get(self): |
| 277 |
try: |
| 278 |
client = self._get_client() |
| 279 |
return client.get(self.resource_group, self.namespace, self.topic, self.name) |
| 280 |
except Exception: |
| 281 |
return None |
| 282 |
|
| 283 |
def to_dict(self, instance): |
| 284 |
result = dict() |
| 285 |
attribute_map = self.servicebus_models.SBSubscription._attribute_map |
| 286 |
for attribute in attribute_map.keys(): |
| 287 |
value = getattr(instance, attribute) |
| 288 |
if not value: |
| 289 |
continue |
| 290 |
if attribute_map[attribute]['type'] == 'duration': |
| 291 |
if is_valid_timedelta(value): |
| 292 |
key = duration_spec_map.get(attribute) or attribute |
| 293 |
result[key] = int(value.total_seconds()) |
| 294 |
elif attribute == 'status': |
| 295 |
result['status'] = _camel_to_snake(value) |
| 296 |
elif isinstance(value, self.servicebus_models.MessageCountDetails): |
| 297 |
result[attribute] = value.as_dict() |
| 298 |
elif isinstance(value, self.servicebus_models.SBSku): |
| 299 |
result[attribute] = value.name.lower() |
| 300 |
elif isinstance(value, datetime): |
| 301 |
result[attribute] = str(value) |
| 302 |
elif isinstance(value, str): |
| 303 |
result[attribute] = to_native(value) |
| 304 |
elif attribute == 'max_size_in_megabytes': |
| 305 |
result['max_size_in_mb'] = value |
| 306 |
else: |
| 307 |
result[attribute] = value |
| 308 |
return result |
| 309 |
|
| 310 |
|
| 311 |
def is_valid_timedelta(value): |
| 312 |
if value == timedelta(10675199, 10085, 477581): |
| 313 |
return None |
| 314 |
return value |
| 315 |
|
| 316 |
|
| 317 |
def main(): |
| 318 |
AzureRMServiceSubscription() |
| 319 |
|
| 320 |
|
| 321 |
if __name__ == '__main__': |
| 322 |
main() |