| 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_servicebusqueue |
| 14 |
version_added: "0.1.2" |
| 15 |
short_description: Manage Azure Service Bus queue |
| 16 |
description: |
| 17 |
- Create, update or delete an Azure Service Bus queue. |
| 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 queue. |
| 27 |
required: true |
| 28 |
type: str |
| 29 |
namespace: |
| 30 |
description: |
| 31 |
- Servicebus namespace name. |
| 32 |
- A namespace is a scoping container for all messaging components. |
| 33 |
- Multiple queues and topics can reside within a single namespace, and namespaces often serve as application containers. |
| 34 |
required: true |
| 35 |
type: str |
| 36 |
state: |
| 37 |
description: |
| 38 |
- Assert the state of the queue. Use C(present) to create or update and use C(absent) to delete. |
| 39 |
default: present |
| 40 |
type: str |
| 41 |
choices: |
| 42 |
- absent |
| 43 |
- present |
| 44 |
auto_delete_on_idle_in_seconds: |
| 45 |
description: |
| 46 |
- Time idle interval after which a queue is automatically deleted. |
| 47 |
- The minimum duration is 5 minutes. |
| 48 |
type: int |
| 49 |
dead_lettering_on_message_expiration: |
| 50 |
description: |
| 51 |
- A value that indicates whether a queue has dead letter support when a message expires. |
| 52 |
type: bool |
| 53 |
default_message_time_to_live_seconds: |
| 54 |
description: |
| 55 |
- Default message timespan to live value. |
| 56 |
- This is the duration after which the message expires, starting from when the message is sent to Service Bus. |
| 57 |
- This is the default value used when TimeToLive is not set on a message itself. |
| 58 |
type: int |
| 59 |
enable_batched_operations: |
| 60 |
description: |
| 61 |
- Value that indicates whether server-side batched operations are enabled. |
| 62 |
type: bool |
| 63 |
enable_express: |
| 64 |
description: |
| 65 |
- Value that indicates whether Express Entities are enabled. |
| 66 |
- An express topic or queue holds a message in memory temporarily before writing it to persistent storage. |
| 67 |
type: bool |
| 68 |
enable_partitioning: |
| 69 |
description: |
| 70 |
- A value that indicates whether the topic or queue is to be partitioned across multiple message brokers. |
| 71 |
type: bool |
| 72 |
forward_dead_lettered_messages_to: |
| 73 |
description: |
| 74 |
- Queue or topic name to forward the Dead Letter message for a queue. |
| 75 |
type: str |
| 76 |
forward_to: |
| 77 |
description: |
| 78 |
- Queue or topic name to forward the messages for a queue. |
| 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 |
- The maximum delivery count. |
| 89 |
- A message is automatically deadlettered after this number of deliveries. |
| 90 |
type: int |
| 91 |
max_message_size_in_kb: |
| 92 |
description: |
| 93 |
- Maximum size (in KB) of the message payload that can be accepted by the queue. |
| 94 |
- This property is only used in Premium today and default is 1024. |
| 95 |
type: int |
| 96 |
max_size_in_mb: |
| 97 |
description: |
| 98 |
- The maximum size of the queue in megabytes, which is the size of memory allocated for the queue. |
| 99 |
type: int |
| 100 |
requires_duplicate_detection: |
| 101 |
description: |
| 102 |
- A value indicating if this queue or topic requires duplicate detection. |
| 103 |
type: bool |
| 104 |
duplicate_detection_time_in_seconds: |
| 105 |
description: |
| 106 |
- TimeSpan structure that defines the duration of the duplicate detection history. |
| 107 |
type: int |
| 108 |
requires_session: |
| 109 |
description: |
| 110 |
- A value that indicates whether the queue supports the concept of sessions. |
| 111 |
type: bool |
| 112 |
status: |
| 113 |
description: |
| 114 |
- Status of the entity. |
| 115 |
type: str |
| 116 |
choices: |
| 117 |
- active |
| 118 |
- disabled |
| 119 |
- send_disabled |
| 120 |
- receive_disabled |
| 121 |
extends_documentation_fragment: |
| 122 |
- azure.azcollection.azure |
| 123 |
|
| 124 |
author: |
| 125 |
- Yuwei Zhou (@yuwzho) |
| 126 |
|
| 127 |
''' |
| 128 |
|
| 129 |
EXAMPLES = ''' |
| 130 |
- name: Create a queue |
| 131 |
azure_rm_servicebusqueue: |
| 132 |
name: subqueue |
| 133 |
resource_group: myResourceGroup |
| 134 |
namespace: bar |
| 135 |
duplicate_detection_time_in_seconds: 600 |
| 136 |
''' |
| 137 |
RETURN = ''' |
| 138 |
id: |
| 139 |
description: |
| 140 |
- Current state of the queue. |
| 141 |
returned: success |
| 142 |
type: str |
| 143 |
sample: "/subscriptions/xxx...xxx/resourceGroups/v-xisuRG/providers/Microsoft.ServiceBus/namespaces/nsb57dc9561/queues/queueb57dc9561" |
| 144 |
''' |
| 145 |
|
| 146 |
try: |
| 147 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 148 |
except ImportError: |
| 149 |
|
| 150 |
pass |
| 151 |
|
| 152 |
from ansible.module_utils.common.dict_transformations import _snake_to_camel, _camel_to_snake |
| 153 |
from ansible.module_utils._text import to_native |
| 154 |
from datetime import datetime, timedelta |
| 155 |
|
| 156 |
|
| 157 |
duration_spec_map = dict( |
| 158 |
default_message_time_to_live='default_message_time_to_live_seconds', |
| 159 |
duplicate_detection_history_time_window='duplicate_detection_time_in_seconds', |
| 160 |
auto_delete_on_idle='auto_delete_on_idle_in_seconds', |
| 161 |
lock_duration='lock_duration_in_seconds' |
| 162 |
) |
| 163 |
|
| 164 |
|
| 165 |
sas_policy_spec = dict( |
| 166 |
state=dict(type='str', default='present', choices=['present', 'absent']), |
| 167 |
name=dict(type='str', required=True), |
| 168 |
regenerate_key=dict(type='bool'), |
| 169 |
rights=dict(type='str', choices=['manage', 'listen', 'send', 'listen_send']) |
| 170 |
) |
| 171 |
|
| 172 |
|
| 173 |
class AzureRMServiceBusQueue(AzureRMModuleBase): |
| 174 |
|
| 175 |
def __init__(self): |
| 176 |
|
| 177 |
self.module_arg_spec = dict( |
| 178 |
resource_group=dict(type='str', required=True), |
| 179 |
name=dict(type='str', required=True), |
| 180 |
state=dict(type='str', default='present', choices=['present', 'absent']), |
| 181 |
namespace=dict(type='str', required=True), |
| 182 |
auto_delete_on_idle_in_seconds=dict(type='int'), |
| 183 |
dead_lettering_on_message_expiration=dict(type='bool'), |
| 184 |
default_message_time_to_live_seconds=dict(type='int'), |
| 185 |
duplicate_detection_time_in_seconds=dict(type='int'), |
| 186 |
enable_batched_operations=dict(type='bool'), |
| 187 |
enable_express=dict(type='bool'), |
| 188 |
enable_partitioning=dict(type='bool'), |
| 189 |
forward_dead_lettered_messages_to=dict(type='str'), |
| 190 |
forward_to=dict(type='str'), |
| 191 |
lock_duration_in_seconds=dict(type='int'), |
| 192 |
max_delivery_count=dict(type='int'), |
| 193 |
max_message_size_in_kb=dict(type='int'), |
| 194 |
max_size_in_mb=dict(type='int'), |
| 195 |
requires_duplicate_detection=dict(type='bool'), |
| 196 |
requires_session=dict(type='bool'), |
| 197 |
status=dict(type='str', |
| 198 |
choices=['active', 'disabled', 'send_disabled', 'receive_disabled']) |
| 199 |
) |
| 200 |
|
| 201 |
self.resource_group = None |
| 202 |
self.name = None |
| 203 |
self.state = None |
| 204 |
self.namespace = None |
| 205 |
self.location = None |
| 206 |
self.type = None |
| 207 |
self.subscription_topic_name = None |
| 208 |
self.auto_delete_on_idle_in_seconds = None |
| 209 |
self.dead_lettering_on_message_expiration = None |
| 210 |
self.default_message_time_to_live_seconds = None |
| 211 |
self.enable_batched_operations = None |
| 212 |
self.enable_express = None |
| 213 |
self.enable_partitioning = None |
| 214 |
self.forward_dead_lettered_messages_to = None |
| 215 |
self.forward_to = None |
| 216 |
self.lock_duration_in_seconds = None |
| 217 |
self.max_delivery_count = None |
| 218 |
self.max_size_in_mb = None |
| 219 |
self.requires_duplicate_detection = None |
| 220 |
self.status = None |
| 221 |
self.requires_session = None |
| 222 |
self.max_message_size_in_kb = None |
| 223 |
|
| 224 |
self.results = dict( |
| 225 |
changed=False, |
| 226 |
id=None |
| 227 |
) |
| 228 |
|
| 229 |
super(AzureRMServiceBusQueue, self).__init__(self.module_arg_spec, |
| 230 |
supports_tags=False, |
| 231 |
supports_check_mode=True) |
| 232 |
|
| 233 |
def exec_module(self, **kwargs): |
| 234 |
|
| 235 |
for key in list(self.module_arg_spec.keys()): |
| 236 |
setattr(self, key, kwargs[key]) |
| 237 |
|
| 238 |
changed = False |
| 239 |
|
| 240 |
original = self.get() |
| 241 |
if self.state == 'present': |
| 242 |
|
| 243 |
params = dict( |
| 244 |
dead_lettering_on_message_expiration=self.dead_lettering_on_message_expiration, |
| 245 |
enable_batched_operations=self.enable_batched_operations, |
| 246 |
enable_express=self.enable_express, |
| 247 |
enable_partitioning=self.enable_partitioning, |
| 248 |
forward_dead_lettered_messages_to=self.forward_dead_lettered_messages_to, |
| 249 |
forward_to=self.forward_to, |
| 250 |
max_delivery_count=self.max_delivery_count, |
| 251 |
max_message_size_in_kilobytes=self.max_message_size_in_kb, |
| 252 |
max_size_in_megabytes=self.max_size_in_mb, |
| 253 |
requires_session=self.requires_session, |
| 254 |
requires_duplicate_detection=self.requires_duplicate_detection |
| 255 |
) |
| 256 |
if self.status: |
| 257 |
params['status'] = self.servicebus_models.EntityStatus(str.capitalize(_snake_to_camel(self.status))) |
| 258 |
for k, v in duration_spec_map.items(): |
| 259 |
seconds = getattr(self, v) |
| 260 |
if seconds: |
| 261 |
params[k] = timedelta(seconds=seconds) |
| 262 |
|
| 263 |
instance = self.servicebus_models.SBQueue(**params) |
| 264 |
result = original |
| 265 |
if not original: |
| 266 |
changed = True |
| 267 |
result = instance |
| 268 |
else: |
| 269 |
result = original |
| 270 |
attribute_map = set(self.servicebus_models.SBQueue._attribute_map.keys()) - set(self.servicebus_models.SBQueue._validation.keys()) |
| 271 |
for attribute in attribute_map: |
| 272 |
value = getattr(instance, attribute) |
| 273 |
if value and value != getattr(original, attribute): |
| 274 |
changed = True |
| 275 |
if changed and not self.check_mode: |
| 276 |
result = self.create_or_update(instance) |
| 277 |
self.results = self.to_dict(result) |
| 278 |
elif original: |
| 279 |
changed = True |
| 280 |
if not self.check_mode: |
| 281 |
self.delete() |
| 282 |
self.results['deleted'] = True |
| 283 |
|
| 284 |
self.results['changed'] = changed |
| 285 |
return self.results |
| 286 |
|
| 287 |
def create_or_update(self, param): |
| 288 |
|
| 289 |
try: |
| 290 |
client = self._get_client() |
| 291 |
return client.create_or_update(self.resource_group, self.namespace, self.name, param) |
| 292 |
except Exception as exc: |
| 293 |
self.fail('Error creating or updating queue {0} - {1}'.format(self.name, str(exc.inner_exception) or str(exc))) |
| 294 |
|
| 295 |
def delete(self): |
| 296 |
try: |
| 297 |
client = self._get_client() |
| 298 |
client.delete(self.resource_group, self.namespace, self.name) |
| 299 |
return True |
| 300 |
except Exception as exc: |
| 301 |
self.fail("Error deleting queue {0} - {1}".format(self.name, str(exc))) |
| 302 |
|
| 303 |
def _get_client(self): |
| 304 |
return self.servicebus_client.queues |
| 305 |
|
| 306 |
def get(self): |
| 307 |
try: |
| 308 |
client = self._get_client() |
| 309 |
return client.get(self.resource_group, self.namespace, self.name) |
| 310 |
except Exception: |
| 311 |
return None |
| 312 |
|
| 313 |
def to_dict(self, instance): |
| 314 |
result = dict() |
| 315 |
attribute_map = self.servicebus_models.SBQueue._attribute_map |
| 316 |
for attribute in attribute_map.keys(): |
| 317 |
value = getattr(instance, attribute) |
| 318 |
if not value: |
| 319 |
continue |
| 320 |
if attribute_map[attribute]['type'] == 'duration': |
| 321 |
if is_valid_timedelta(value): |
| 322 |
key = duration_spec_map.get(attribute) or attribute |
| 323 |
result[key] = int(value.total_seconds()) |
| 324 |
elif attribute == 'status': |
| 325 |
result['status'] = _camel_to_snake(value) |
| 326 |
elif isinstance(value, self.servicebus_models.MessageCountDetails): |
| 327 |
result[attribute] = value.as_dict() |
| 328 |
elif isinstance(value, self.servicebus_models.SBSku): |
| 329 |
result[attribute] = value.name.lower() |
| 330 |
elif isinstance(value, datetime): |
| 331 |
result[attribute] = str(value) |
| 332 |
elif isinstance(value, str): |
| 333 |
result[attribute] = to_native(value) |
| 334 |
elif attribute == 'max_size_in_megabytes': |
| 335 |
result['max_size_in_mb'] = value |
| 336 |
elif attribute == 'max_size_in_kilobytes': |
| 337 |
result['max_size_in_kb'] = value |
| 338 |
else: |
| 339 |
result[attribute] = value |
| 340 |
return result |
| 341 |
|
| 342 |
|
| 343 |
def is_valid_timedelta(value): |
| 344 |
if value == timedelta(10675199, 10085, 477581): |
| 345 |
return None |
| 346 |
return value |
| 347 |
|
| 348 |
|
| 349 |
def main(): |
| 350 |
AzureRMServiceBusQueue() |
| 351 |
|
| 352 |
|
| 353 |
if __name__ == '__main__': |
| 354 |
main() |