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