| 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_servicebus |
| 14 |
version_added: "0.1.2" |
| 15 |
short_description: Manage Azure Service Bus |
| 16 |
description: |
| 17 |
- Create, update or delete an Azure Service Bus namespaces. |
| 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 namespace. |
| 27 |
required: true |
| 28 |
type: str |
| 29 |
state: |
| 30 |
description: |
| 31 |
- Assert the state of the servicebus. Use C(present) to create or update and use C(absen) to delete. |
| 32 |
default: present |
| 33 |
type: str |
| 34 |
choices: |
| 35 |
- absent |
| 36 |
- present |
| 37 |
location: |
| 38 |
description: |
| 39 |
- The servicebus's location. |
| 40 |
type: str |
| 41 |
sku: |
| 42 |
description: |
| 43 |
- Namespace SKU. |
| 44 |
type: str |
| 45 |
choices: |
| 46 |
- standard |
| 47 |
- basic |
| 48 |
- premium |
| 49 |
default: standard |
| 50 |
minimum_tls_version: |
| 51 |
description: |
| 52 |
- The minimum TLS version for the cluster to support. |
| 53 |
type: str |
| 54 |
choices: |
| 55 |
- '1.0' |
| 56 |
- '1.1' |
| 57 |
- '1.2' |
| 58 |
zone_redundant: |
| 59 |
description: |
| 60 |
- Enabling this property creates a Premium Service Bus Namespace in regions supported availability zones. |
| 61 |
type: bool |
| 62 |
disable_local_auth: |
| 63 |
description: |
| 64 |
- This property disables SAS authentication for the Service Bus namespace. |
| 65 |
type: bool |
| 66 |
public_network_access: |
| 67 |
description: |
| 68 |
- This determines if traffic is allowed over public network. |
| 69 |
- By default it is C(Enabled). |
| 70 |
type: str |
| 71 |
default: Enabled |
| 72 |
choices: |
| 73 |
- Enabled |
| 74 |
- Disabled |
| 75 |
- SecuredByPerimeter |
| 76 |
premium_messaging_partitions: |
| 77 |
description: |
| 78 |
- The number of partitions of a Service Bus namespace. |
| 79 |
- This property is only applicable to C(premium) SKU namespaces. |
| 80 |
- It default value is C(1) for C(premium) namespaces. |
| 81 |
type: int |
| 82 |
choices: |
| 83 |
- 1 |
| 84 |
- 2 |
| 85 |
- 4 |
| 86 |
|
| 87 |
extends_documentation_fragment: |
| 88 |
- azure.azcollection.azure |
| 89 |
- azure.azcollection.azure_tags |
| 90 |
- azure.azcollection.azure_identity_multiple |
| 91 |
|
| 92 |
author: |
| 93 |
- Yuwei Zhou (@yuwzho) |
| 94 |
|
| 95 |
''' |
| 96 |
|
| 97 |
EXAMPLES = ''' |
| 98 |
- name: Create a namespace |
| 99 |
azure_rm_servicebus: |
| 100 |
name: deadbeef |
| 101 |
location: eastus |
| 102 |
tags: |
| 103 |
key1: value1 |
| 104 |
''' |
| 105 |
RETURN = ''' |
| 106 |
id: |
| 107 |
description: |
| 108 |
- Current state of the service bus. |
| 109 |
returned: success |
| 110 |
type: str |
| 111 |
sample: "/subscriptions/xxx...xxx/resourceGroups/myResourceGroup/providers/Microsoft.ServiceBus/namespaces/myServicebus" |
| 112 |
''' |
| 113 |
|
| 114 |
try: |
| 115 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt |
| 116 |
from azure.mgmt.servicebus.v2022_10_01_preview.models import (Identity, UserAssignedIdentity) |
| 117 |
except ImportError: |
| 118 |
|
| 119 |
pass |
| 120 |
|
| 121 |
from ansible.module_utils._text import to_native |
| 122 |
from datetime import datetime, timedelta |
| 123 |
|
| 124 |
|
| 125 |
class AzureRMServiceBus(AzureRMModuleBaseExt): |
| 126 |
|
| 127 |
def __init__(self): |
| 128 |
|
| 129 |
self.module_arg_spec = dict( |
| 130 |
resource_group=dict(type='str', required=True), |
| 131 |
name=dict(type='str', required=True), |
| 132 |
location=dict(type='str'), |
| 133 |
state=dict(type='str', default='present', choices=['present', 'absent']), |
| 134 |
sku=dict(type='str', choices=['basic', 'standard', 'premium'], default='standard'), |
| 135 |
identity=dict( |
| 136 |
type="dict", |
| 137 |
options=self.managed_identity_multiple_spec |
| 138 |
), |
| 139 |
minimum_tls_version=dict(type='str', choices=['1.0', '1.1', '1.2']), |
| 140 |
zone_redundant=dict(type='bool'), |
| 141 |
disable_local_auth=dict(type='bool'), |
| 142 |
public_network_access=dict(type='str', default='Enabled', choices=["Enabled", "Disabled", "SecuredByPerimeter"]), |
| 143 |
premium_messaging_partitions=dict(type='int', choices=[1, 2, 4]) |
| 144 |
) |
| 145 |
|
| 146 |
self.resource_group = None |
| 147 |
self.name = None |
| 148 |
self.state = None |
| 149 |
self.sku = None |
| 150 |
self.location = None |
| 151 |
self._managed_identity = None |
| 152 |
self.identity = None |
| 153 |
self.update_identity = False |
| 154 |
self.minimum_tls_version = None |
| 155 |
self.zone_redundant = None |
| 156 |
self.disable_local_auth = None |
| 157 |
self.public_network_access = None |
| 158 |
self.premium_messaging_partitions = None |
| 159 |
|
| 160 |
self.results = dict( |
| 161 |
changed=False, |
| 162 |
id=None |
| 163 |
) |
| 164 |
|
| 165 |
super(AzureRMServiceBus, self).__init__(self.module_arg_spec, |
| 166 |
supports_tags=True, |
| 167 |
supports_check_mode=True) |
| 168 |
|
| 169 |
@property |
| 170 |
def managed_identity(self): |
| 171 |
if not self._managed_identity: |
| 172 |
self._managed_identity = { |
| 173 |
"identity": Identity, |
| 174 |
"user_assigned": UserAssignedIdentity, |
| 175 |
} |
| 176 |
return self._managed_identity |
| 177 |
|
| 178 |
def exec_module(self, **kwargs): |
| 179 |
|
| 180 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 181 |
setattr(self, key, kwargs[key]) |
| 182 |
|
| 183 |
changed = False |
| 184 |
|
| 185 |
if not self.location: |
| 186 |
resource_group = self.get_resource_group(self.resource_group) |
| 187 |
self.location = resource_group.location |
| 188 |
|
| 189 |
original = self.get() |
| 190 |
|
| 191 |
if not original: |
| 192 |
self.check_name() |
| 193 |
|
| 194 |
curr_identity = original.identity.as_dict() if original and original.identity else None |
| 195 |
|
| 196 |
if self.identity: |
| 197 |
self.update_identity, identity_result = self.update_managed_identity(curr_identity=curr_identity, |
| 198 |
new_identity=self.identity) |
| 199 |
self.identity = identity_result.as_dict() |
| 200 |
|
| 201 |
if self.state == 'present': |
| 202 |
if self.sku != 'premium' and self.premium_messaging_partitions is not None: |
| 203 |
self.fail("The premium_messaging_partitions is only applicable to C(premium) SKU namespaces.") |
| 204 |
|
| 205 |
if not self.check_mode: |
| 206 |
if original: |
| 207 |
update_tags, new_tags = self.update_tags(original.tags) |
| 208 |
if update_tags: |
| 209 |
changed = True |
| 210 |
self.tags = new_tags |
| 211 |
if self.update_identity: |
| 212 |
changed = True |
| 213 |
if self.minimum_tls_version is not None and self.minimum_tls_version != original.minimum_tls_version: |
| 214 |
changed = True |
| 215 |
else: |
| 216 |
self.minimum_tls_version = original.minimum_tls_version |
| 217 |
if self.premium_messaging_partitions is not None and self.premium_messaging_partitions != original.premium_messaging_partitions: |
| 218 |
changed = True |
| 219 |
else: |
| 220 |
self.premium_messaging_partitions = original.premium_messaging_partitions |
| 221 |
if self.public_network_access is not None and self.public_network_access != original.public_network_access: |
| 222 |
changed = True |
| 223 |
else: |
| 224 |
self.public_network_access = original.public_network_access |
| 225 |
if self.zone_redundant is not None and bool(self.zone_redundant) != bool(original.zone_redundant): |
| 226 |
|
| 227 |
|
| 228 |
self.log("The default value of zone_redundant is True and cannot be set") |
| 229 |
else: |
| 230 |
self.zone_redundant = original.zone_redundant |
| 231 |
if self.disable_local_auth is not None and bool(self.disable_local_auth) != bool(original.disable_local_auth): |
| 232 |
changed = True |
| 233 |
else: |
| 234 |
self.disable_local_auth = original.disable_local_auth |
| 235 |
if changed: |
| 236 |
original = self.create() |
| 237 |
else: |
| 238 |
changed = True |
| 239 |
original = self.create() |
| 240 |
else: |
| 241 |
changed = True |
| 242 |
elif self.state == 'absent' and original: |
| 243 |
changed = True |
| 244 |
original = None |
| 245 |
if not self.check_mode: |
| 246 |
self.delete() |
| 247 |
self.results['deleted'] = True |
| 248 |
|
| 249 |
if original: |
| 250 |
self.results = self.to_dict(original) |
| 251 |
self.results['changed'] = changed |
| 252 |
return self.results |
| 253 |
|
| 254 |
def check_name(self): |
| 255 |
try: |
| 256 |
check_name = self.servicebus_client.namespaces.check_name_availability(parameters={'name': self.name}) |
| 257 |
if not check_name or not check_name.name_available: |
| 258 |
self.fail("Error creating namespace {0} - {1}".format(self.name, check_name.message or str(check_name))) |
| 259 |
except Exception as exc: |
| 260 |
self.fail("Error creating namespace {0} - {1}".format(self.name, exc.message or str(exc))) |
| 261 |
|
| 262 |
def create(self): |
| 263 |
self.log('Cannot find namespace, creating a one') |
| 264 |
try: |
| 265 |
sku = self.servicebus_models.SBSku(name=str.capitalize(self.sku)) |
| 266 |
parameters = self.servicebus_models.SBNamespace(location=self.location, |
| 267 |
tags=self.tags, |
| 268 |
sku=sku, |
| 269 |
minimum_tls_version=self.minimum_tls_version, |
| 270 |
zone_redundant=self.zone_redundant, |
| 271 |
disable_local_auth=self.disable_local_auth, |
| 272 |
public_network_access=self.public_network_access, |
| 273 |
premium_messaging_partitions=self.premium_messaging_partitions, |
| 274 |
identity=self.identity) |
| 275 |
poller = self.servicebus_client.namespaces.begin_create_or_update(self.resource_group, self.name, parameters) |
| 276 |
ns = self.get_poller_result(poller) |
| 277 |
except Exception as exc: |
| 278 |
self.fail('Error creating namespace {0} - {1}'.format(self.name, str(exc))) |
| 279 |
return ns |
| 280 |
|
| 281 |
def delete(self): |
| 282 |
try: |
| 283 |
self.servicebus_client.namespaces.begin_delete(self.resource_group, self.name) |
| 284 |
return True |
| 285 |
except Exception as exc: |
| 286 |
self.fail("Error deleting route {0} - {1}".format(self.name, str(exc))) |
| 287 |
|
| 288 |
def get(self): |
| 289 |
try: |
| 290 |
return self.servicebus_client.namespaces.get(self.resource_group, self.name) |
| 291 |
except Exception: |
| 292 |
return None |
| 293 |
|
| 294 |
def to_dict(self, instance): |
| 295 |
result = dict() |
| 296 |
attribute_map = self.servicebus_models.SBNamespace._attribute_map |
| 297 |
for attribute in attribute_map.keys(): |
| 298 |
value = getattr(instance, attribute) |
| 299 |
if not value: |
| 300 |
continue |
| 301 |
if isinstance(value, self.servicebus_models.SBSku): |
| 302 |
result[attribute] = value.name.lower() |
| 303 |
elif isinstance(value, datetime): |
| 304 |
result[attribute] = str(value) |
| 305 |
elif isinstance(value, str): |
| 306 |
result[attribute] = to_native(value) |
| 307 |
elif attribute == 'max_size_in_megabytes': |
| 308 |
result['max_size_in_mb'] = value |
| 309 |
elif isinstance(value, Identity): |
| 310 |
result['identity'] = value.as_dict() |
| 311 |
else: |
| 312 |
result[attribute] = value |
| 313 |
return result |
| 314 |
|
| 315 |
|
| 316 |
def is_valid_timedelta(value): |
| 317 |
if value == timedelta(10675199, 10085, 477581): |
| 318 |
return None |
| 319 |
return value |
| 320 |
|
| 321 |
|
| 322 |
def main(): |
| 323 |
AzureRMServiceBus() |
| 324 |
|
| 325 |
|
| 326 |
if __name__ == '__main__': |
| 327 |
main() |