| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = ''' |
| 11 |
--- |
| 12 |
module: azure_rm_eventhub |
| 13 |
version_added: "1.6.0" |
| 14 |
short_description: Manage Event Hub |
| 15 |
description: |
| 16 |
- Create, update and delete instance of Event Hub. |
| 17 |
options: |
| 18 |
resource_group: |
| 19 |
description: |
| 20 |
- Name of the resource group to which the resource belongs. |
| 21 |
required: True |
| 22 |
type: str |
| 23 |
namespace_name: |
| 24 |
description: |
| 25 |
- Name of the namespace in which to create event hub. |
| 26 |
required: True |
| 27 |
type: str |
| 28 |
name: |
| 29 |
description: |
| 30 |
- Unique name of the Event Hub. |
| 31 |
required: False |
| 32 |
type: str |
| 33 |
message_retention_in_days: |
| 34 |
description: |
| 35 |
- Number of days to retain the events for this Event Hub. |
| 36 |
required: False |
| 37 |
type: int |
| 38 |
partition_count: |
| 39 |
description: |
| 40 |
- Number of partitions created for the Event Hub. |
| 41 |
- Range from 1 to 32. |
| 42 |
required: False |
| 43 |
type: int |
| 44 |
status: |
| 45 |
description: |
| 46 |
- Enumerates the possible values for the status of the Event hub. |
| 47 |
default: Active |
| 48 |
required: False |
| 49 |
type: str |
| 50 |
choices: |
| 51 |
- Active |
| 52 |
- Disabled |
| 53 |
- Restoring |
| 54 |
- SendDisabled |
| 55 |
- ReceiveDisabled |
| 56 |
- Creating |
| 57 |
- Deleting |
| 58 |
- Renaming |
| 59 |
- Unknown |
| 60 |
location: |
| 61 |
description: |
| 62 |
- Resource location. If not set, location from the resource group will be used as default. |
| 63 |
required: False |
| 64 |
type: str |
| 65 |
sku: |
| 66 |
description: |
| 67 |
- The name of the SKU. |
| 68 |
- Please see L(https://azure.microsoft.com/en-in/pricing/details/event-hubs/,). |
| 69 |
default: Basic |
| 70 |
choices: |
| 71 |
- Basic |
| 72 |
- Standard |
| 73 |
type: str |
| 74 |
state: |
| 75 |
description: |
| 76 |
- Assert the state of the Event Hub. |
| 77 |
- Use C(present) to create or update an event hub and C(absent) to delete it. |
| 78 |
default: present |
| 79 |
choices: |
| 80 |
- absent |
| 81 |
- present |
| 82 |
type: str |
| 83 |
|
| 84 |
extends_documentation_fragment: |
| 85 |
- azure.azcollection.azure |
| 86 |
- azure.azcollection.azure_tags |
| 87 |
- azure.azcollection.azure_identity_multiple |
| 88 |
|
| 89 |
author: |
| 90 |
- Praveen Ghuge (@praveenghuge) |
| 91 |
- Karl Dasan (@karldas30) |
| 92 |
- Saurabh Malpani(@saurabh3796) |
| 93 |
''' |
| 94 |
EXAMPLES = ''' |
| 95 |
|
| 96 |
- name: "Create Event Hub" |
| 97 |
azure_rm_eventhub: |
| 98 |
resource_group: testgroupans |
| 99 |
location: eastus |
| 100 |
namespace_name: myNamespace |
| 101 |
name: myhub |
| 102 |
tags: |
| 103 |
- a: b |
| 104 |
sku: free |
| 105 |
|
| 106 |
- name: Delete Event Hub |
| 107 |
azure_rm_eventhub: |
| 108 |
resource_group: testgroupans |
| 109 |
name: myNamespace |
| 110 |
state: absent |
| 111 |
|
| 112 |
- name: "Create Event Hub Namespace" |
| 113 |
azure_rm_eventhub: |
| 114 |
resource_group: testgroupans |
| 115 |
location: eastus |
| 116 |
namespace_name: myNamespace |
| 117 |
tags: |
| 118 |
a: b |
| 119 |
sku: free |
| 120 |
|
| 121 |
- name: Delete Event Hub Namespace |
| 122 |
azure_rm_eventhub: |
| 123 |
resource_group: testgroupans |
| 124 |
namespace_name: myNamespace |
| 125 |
state: absent |
| 126 |
''' |
| 127 |
|
| 128 |
RETURN = ''' |
| 129 |
state: |
| 130 |
description: |
| 131 |
- Current state of the Event Hub namesapce or Event Hub. |
| 132 |
returned: always |
| 133 |
type: dict |
| 134 |
sample: { |
| 135 |
"additional_properties": {"location": "East US"}, |
| 136 |
"critical": false, |
| 137 |
"enabled": true, |
| 138 |
"metric_id": null, |
| 139 |
"name": "testnaedd3d22d3w", |
| 140 |
"namespace_type": "eventHub", |
| 141 |
"status": "Active", |
| 142 |
"region": null, |
| 143 |
"scale_unit": null, |
| 144 |
"service_bus_endpoint": "https://testnaedd3d22d3w.servicebus.windows.net:443/", |
| 145 |
"sku": "Basic", |
| 146 |
"tags": { |
| 147 |
"a": "b" |
| 148 |
}, |
| 149 |
"message_retention_in_days": 7, |
| 150 |
"partition_count": 4, |
| 151 |
"partition_ids": ["0", "1", "2", "3"], |
| 152 |
"updated_at": "2021-04-29T10:05:24.000Z", |
| 153 |
"created_at": "2021-04-29T10:05:20.377Z", |
| 154 |
"type": "Microsoft.eventHubs/namespaces" |
| 155 |
} |
| 156 |
|
| 157 |
''' |
| 158 |
|
| 159 |
try: |
| 160 |
from azure.mgmt.eventhub.models import Eventhub, EHNamespace |
| 161 |
from azure.mgmt.eventhub.models import Sku, Identity, UserAssignedIdentity |
| 162 |
except ImportError: |
| 163 |
|
| 164 |
pass |
| 165 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt |
| 166 |
import time |
| 167 |
|
| 168 |
|
| 169 |
class AzureRMEventHub(AzureRMModuleBaseExt): |
| 170 |
|
| 171 |
def __init__(self): |
| 172 |
|
| 173 |
|
| 174 |
self.authorizations_spec = dict( |
| 175 |
name=dict(type='str', required=True) |
| 176 |
) |
| 177 |
|
| 178 |
self.module_arg_spec = dict( |
| 179 |
message_retention_in_days=dict(type='int'), |
| 180 |
name=dict(type='str'), |
| 181 |
namespace_name=dict(type='str', required=True), |
| 182 |
partition_count=dict(type='int'), |
| 183 |
resource_group=dict(type='str', required=True), |
| 184 |
sku=dict(type='str', choices=[ |
| 185 |
'Basic', 'Standard'], default='Basic'), |
| 186 |
status=dict(choices=["Active", "Disabled", "Restoring", "SendDisabled", "ReceiveDisabled", "Creating", "Deleting", "Renaming", "Unknown"], |
| 187 |
default='Active', type='str'), |
| 188 |
state=dict(choices=['present', 'absent'], |
| 189 |
default='present', type='str'), |
| 190 |
location=dict(type='str'), |
| 191 |
identity=dict( |
| 192 |
type="dict", |
| 193 |
options=self.managed_identity_multiple_spec |
| 194 |
) |
| 195 |
) |
| 196 |
required_if = [ |
| 197 |
('state', 'present', [ |
| 198 |
'partition_count', 'message_retention_in_days']) |
| 199 |
] |
| 200 |
self.sku = None |
| 201 |
self.resource_group = None |
| 202 |
self.namespace_name = None |
| 203 |
self.message_retention_in_days = None |
| 204 |
self.name = None |
| 205 |
self.location = None |
| 206 |
self.authorizations = None |
| 207 |
self.tags = None |
| 208 |
self.status = None |
| 209 |
self.partition_count = None |
| 210 |
self.results = dict( |
| 211 |
changed=False, |
| 212 |
state=dict() |
| 213 |
) |
| 214 |
self.state = None |
| 215 |
self.identity = None |
| 216 |
self._managed_identity = None |
| 217 |
|
| 218 |
super(AzureRMEventHub, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 219 |
supports_check_mode=True, |
| 220 |
supports_tags=True) |
| 221 |
|
| 222 |
@property |
| 223 |
def managed_identity(self): |
| 224 |
if not self._managed_identity: |
| 225 |
self._managed_identity = { |
| 226 |
"identity": Identity, |
| 227 |
"user_assigned": UserAssignedIdentity, |
| 228 |
} |
| 229 |
return self._managed_identity |
| 230 |
|
| 231 |
def exec_module(self, **kwargs): |
| 232 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 233 |
setattr(self, key, kwargs[key]) |
| 234 |
|
| 235 |
|
| 236 |
resource_group = self.get_resource_group(self.resource_group) |
| 237 |
if not self.location: |
| 238 |
|
| 239 |
self.location = resource_group.location |
| 240 |
|
| 241 |
results = dict() |
| 242 |
changed = False |
| 243 |
|
| 244 |
try: |
| 245 |
self.log( |
| 246 |
'Fetching Event Hub Namespace {0}'.format(self.name)) |
| 247 |
namespace = self.event_hub_client.namespaces.get( |
| 248 |
self.resource_group, self.namespace_name) |
| 249 |
|
| 250 |
results = namespace_to_dict(namespace) |
| 251 |
event_hub_results = None |
| 252 |
if self.name: |
| 253 |
self.log('Fetching event Hub {0}'.format(self.name)) |
| 254 |
event_hub = self.event_hub_client.event_hubs.get( |
| 255 |
self.resource_group, self.namespace_name, self.name) |
| 256 |
event_hub_results = event_hub_to_dict( |
| 257 |
event_hub) |
| 258 |
|
| 259 |
if self.state == 'present': |
| 260 |
changed = False |
| 261 |
|
| 262 |
update_tags, results['tags'] = self.update_tags( |
| 263 |
results['tags']) |
| 264 |
|
| 265 |
if update_tags: |
| 266 |
changed = True |
| 267 |
elif self.namespace_name and not self.name: |
| 268 |
if self.sku != results['sku']: |
| 269 |
changed = True |
| 270 |
elif self.namespace_name and self.name and event_hub_results: |
| 271 |
if results['sku'] != 'Basic' and self.message_retention_in_days != event_hub_results['message_retention_in_days']: |
| 272 |
self.sku = results['sku'] |
| 273 |
changed = True |
| 274 |
if self.identity: |
| 275 |
if self.namespace_name and not self.name: |
| 276 |
update_identity, self.identity = self.update_managed_identity(curr_identity=results['identity'], new_identity=self.identity) |
| 277 |
if update_identity: |
| 278 |
changed = True |
| 279 |
else: |
| 280 |
self.fail("Managed Identity shouldn't be used for event hub update") |
| 281 |
|
| 282 |
elif self.state == 'absent': |
| 283 |
changed = True |
| 284 |
|
| 285 |
except Exception: |
| 286 |
|
| 287 |
if self.state == 'present': |
| 288 |
changed = True |
| 289 |
|
| 290 |
if self.identity: |
| 291 |
if self.namespace_name and not self.name: |
| 292 |
update_identity, self.identity = self.update_managed_identity(new_identity=self.identity) |
| 293 |
else: |
| 294 |
self.fail("Managed Identity shouldn't be used for event hub creation") |
| 295 |
else: |
| 296 |
|
| 297 |
changed = False |
| 298 |
|
| 299 |
self.results['changed'] = changed |
| 300 |
|
| 301 |
if self.name and not changed: |
| 302 |
self.results['state'] = event_hub_results |
| 303 |
else: |
| 304 |
self.results['state'] = results |
| 305 |
|
| 306 |
|
| 307 |
if self.check_mode: |
| 308 |
return self.results |
| 309 |
|
| 310 |
if changed: |
| 311 |
if self.state == "present": |
| 312 |
if self.name is None: |
| 313 |
self.results['state'] = self.create_or_update_namespaces() |
| 314 |
elif self.namespace_name and self.name: |
| 315 |
self.results['state'] = self.create_or_update_event_hub() |
| 316 |
elif self.state == "absent": |
| 317 |
|
| 318 |
if self.name is None: |
| 319 |
self.delete_namespace() |
| 320 |
elif self.namespace_name and self.name: |
| 321 |
self.delete_event_hub() |
| 322 |
self.results['state']['status'] = 'Deleted' |
| 323 |
return self.results |
| 324 |
|
| 325 |
def create_or_update_namespaces(self): |
| 326 |
''' |
| 327 |
create or update namespaces |
| 328 |
''' |
| 329 |
try: |
| 330 |
namespace_params = EHNamespace( |
| 331 |
location=self.location, |
| 332 |
sku=Sku(name=self.sku), |
| 333 |
tags=self.tags, |
| 334 |
identity=self.identity |
| 335 |
) |
| 336 |
result = self.event_hub_client.namespaces.begin_create_or_update( |
| 337 |
self.resource_group, |
| 338 |
self.namespace_name, |
| 339 |
namespace_params) |
| 340 |
|
| 341 |
namespace = self.event_hub_client.namespaces.get( |
| 342 |
self.resource_group, |
| 343 |
self.namespace_name) |
| 344 |
while namespace.provisioning_state == "Created": |
| 345 |
time.sleep(30) |
| 346 |
namespace = self.event_hub_client.namespaces.get( |
| 347 |
self.resource_group, |
| 348 |
self.namespace_name, |
| 349 |
) |
| 350 |
except Exception as ex: |
| 351 |
self.fail("Failed to create namespace {0} in resource group {1}: {2}".format( |
| 352 |
self.namespace_name, self.resource_group, str(ex))) |
| 353 |
return namespace_to_dict(namespace) |
| 354 |
|
| 355 |
def create_or_update_event_hub(self): |
| 356 |
''' |
| 357 |
Create or update Event Hub. |
| 358 |
:return: create or update Event Hub instance state dictionary |
| 359 |
''' |
| 360 |
try: |
| 361 |
if self.sku == 'Basic': |
| 362 |
self.message_retention_in_days = 1 |
| 363 |
params = Eventhub( |
| 364 |
message_retention_in_days=self.message_retention_in_days, |
| 365 |
partition_count=self.partition_count, |
| 366 |
status=self.status, |
| 367 |
identity=self.identity |
| 368 |
) |
| 369 |
result = self.event_hub_client.event_hubs.create_or_update( |
| 370 |
self.resource_group, |
| 371 |
self.namespace_name, |
| 372 |
self.name, |
| 373 |
params) |
| 374 |
|
| 375 |
self.log("Response : {0}".format(result)) |
| 376 |
except Exception as ex: |
| 377 |
self.fail("Failed to create event hub {0} in resource group {1}: {2}".format( |
| 378 |
self.name, self.resource_group, str(ex))) |
| 379 |
return event_hub_to_dict(result) |
| 380 |
|
| 381 |
def delete_event_hub(self): |
| 382 |
''' |
| 383 |
Deletes specified event hub |
| 384 |
:return True |
| 385 |
''' |
| 386 |
self.log("Deleting the event hub {0}".format(self.name)) |
| 387 |
try: |
| 388 |
result = self.event_hub_client.event_hubs.delete( |
| 389 |
self.resource_group, self.namespace_name, self.name) |
| 390 |
except Exception as e: |
| 391 |
self.log('Error attempting to delete event hub.') |
| 392 |
self.fail( |
| 393 |
"Error deleting the event hub : {0}".format(str(e))) |
| 394 |
return True |
| 395 |
|
| 396 |
def delete_namespace(self): |
| 397 |
''' |
| 398 |
Deletes specified namespace |
| 399 |
:return True |
| 400 |
''' |
| 401 |
self.log("Deleting the namespace {0}".format(self.namespace_name)) |
| 402 |
try: |
| 403 |
result = self.event_hub_client.namespaces.begin_delete( |
| 404 |
self.resource_group, self.namespace_name) |
| 405 |
except Exception as e: |
| 406 |
self.log('Error attempting to delete namespace.') |
| 407 |
self.fail( |
| 408 |
"Error deleting the namespace : {0}".format(str(e))) |
| 409 |
return True |
| 410 |
|
| 411 |
|
| 412 |
def event_hub_to_dict(item): |
| 413 |
|
| 414 |
event_hub = item.as_dict() |
| 415 |
result = dict() |
| 416 |
if item.additional_properties: |
| 417 |
result['additional_properties'] = item.additional_properties |
| 418 |
result['name'] = event_hub.get('name', None) |
| 419 |
result['partition_ids'] = event_hub.get('partition_ids', None) |
| 420 |
result['created_at'] = event_hub.get('created_at', None) |
| 421 |
result['updated_at'] = event_hub.get('updated_at', None) |
| 422 |
result['message_retention_in_days'] = event_hub.get( |
| 423 |
'message_retention_in_days', None) |
| 424 |
result['partition_count'] = event_hub.get('partition_count', None) |
| 425 |
result['status'] = event_hub.get('status', None) |
| 426 |
result['tags'] = event_hub.get('tags', None) |
| 427 |
result['identity'] = event_hub.get('identity', None) |
| 428 |
return result |
| 429 |
|
| 430 |
|
| 431 |
def namespace_to_dict(item): |
| 432 |
|
| 433 |
namespace = item.as_dict() |
| 434 |
result = dict( |
| 435 |
additional_properties=namespace.get( |
| 436 |
'additional_properties', {}), |
| 437 |
name=namespace.get('name', None), |
| 438 |
type=namespace.get('type', None), |
| 439 |
location=namespace.get( |
| 440 |
'location', '').replace(' ', '').lower(), |
| 441 |
sku=namespace.get("sku").get("name"), |
| 442 |
tags=namespace.get('tags', None), |
| 443 |
provisioning_state=namespace.get( |
| 444 |
'provisioning_state', None), |
| 445 |
region=namespace.get('region', None), |
| 446 |
metric_id=namespace.get('metric_id', None), |
| 447 |
service_bus_endpoint=namespace.get( |
| 448 |
'service_bus_endpoint', None), |
| 449 |
scale_unit=namespace.get('scale_unit', None), |
| 450 |
enabled=namespace.get('enabled', None), |
| 451 |
critical=namespace.get('critical', None), |
| 452 |
data_center=namespace.get('data_center', None), |
| 453 |
namespace_type=namespace.get('namespace_type', None), |
| 454 |
updated_at=namespace.get('updated_at', None), |
| 455 |
created_at=namespace.get('created_at', None), |
| 456 |
is_auto_inflate_enabled=namespace.get( |
| 457 |
'is_auto_inflate_enabled', None), |
| 458 |
maximum_throughput_units=namespace.get( |
| 459 |
'maximum_throughput_units', None), |
| 460 |
identity=namespace.get('identity', None) |
| 461 |
) |
| 462 |
return result |
| 463 |
|
| 464 |
|
| 465 |
def main(): |
| 466 |
AzureRMEventHub() |
| 467 |
|
| 468 |
|
| 469 |
if __name__ == '__main__': |
| 470 |
main() |