| 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_iothub |
| 14 |
version_added: "0.1.2" |
| 15 |
short_description: Manage Azure IoT hub |
| 16 |
description: |
| 17 |
- Create, delete an Azure IoT hub. |
| 18 |
options: |
| 19 |
resource_group: |
| 20 |
description: |
| 21 |
- Name of resource group. |
| 22 |
type: str |
| 23 |
required: true |
| 24 |
name: |
| 25 |
description: |
| 26 |
- Name of the IoT hub. |
| 27 |
type: str |
| 28 |
required: true |
| 29 |
state: |
| 30 |
description: |
| 31 |
- State of the IoT hub. Use C(present) to create or update an IoT hub and C(absent) to delete an IoT hub. |
| 32 |
type: str |
| 33 |
default: present |
| 34 |
choices: |
| 35 |
- absent |
| 36 |
- present |
| 37 |
location: |
| 38 |
description: |
| 39 |
- Location of the IoT hub. |
| 40 |
type: str |
| 41 |
sku: |
| 42 |
description: |
| 43 |
- Pricing tier for Azure IoT Hub. |
| 44 |
- Note that only one free IoT hub instance is allowed in each subscription. Exception will be thrown if free instances exceed one. |
| 45 |
- Default is C(s1) when creation. |
| 46 |
type: str |
| 47 |
choices: |
| 48 |
- b1 |
| 49 |
- b2 |
| 50 |
- b3 |
| 51 |
- f1 |
| 52 |
- s1 |
| 53 |
- s2 |
| 54 |
- s3 |
| 55 |
unit: |
| 56 |
description: |
| 57 |
- Units in your IoT Hub. |
| 58 |
- Default is C(1). |
| 59 |
type: int |
| 60 |
event_endpoint: |
| 61 |
description: |
| 62 |
- The Event Hub-compatible endpoint property. |
| 63 |
type: dict |
| 64 |
suboptions: |
| 65 |
partition_count: |
| 66 |
description: |
| 67 |
- The number of partitions for receiving device-to-cloud messages in the Event Hub-compatible endpoint. |
| 68 |
- "See U(https://docs.microsoft.com/azure/iot-hub/iot-hub-devguide-messaging#device-to-cloud-messages)." |
| 69 |
- Default is C(2). |
| 70 |
type: int |
| 71 |
retention_time_in_days: |
| 72 |
description: |
| 73 |
- The retention time for device-to-cloud messages in days. |
| 74 |
- "See U(https://docs.microsoft.com/azure/iot-hub/iot-hub-devguide-messaging#device-to-cloud-messages)." |
| 75 |
- Default is C(1). |
| 76 |
type: int |
| 77 |
enable_file_upload_notifications: |
| 78 |
description: |
| 79 |
- File upload notifications are enabled if set to C(True). |
| 80 |
type: bool |
| 81 |
ip_filters: |
| 82 |
description: |
| 83 |
- Configure rules for rejecting or accepting traffic from specific IPv4 addresses. |
| 84 |
type: list |
| 85 |
elements: dict |
| 86 |
suboptions: |
| 87 |
name: |
| 88 |
description: |
| 89 |
- Name of the filter. |
| 90 |
type: str |
| 91 |
required: yes |
| 92 |
ip_mask: |
| 93 |
description: |
| 94 |
- A string that contains the IP address range in CIDR notation for the rule. |
| 95 |
type: str |
| 96 |
required: yes |
| 97 |
action: |
| 98 |
description: |
| 99 |
- The desired action for requests captured by this rule. |
| 100 |
type: str |
| 101 |
required: yes |
| 102 |
choices: |
| 103 |
- accept |
| 104 |
- reject |
| 105 |
routing_endpoints: |
| 106 |
description: |
| 107 |
- Custom endpoints. |
| 108 |
type: list |
| 109 |
elements: dict |
| 110 |
suboptions: |
| 111 |
name: |
| 112 |
description: |
| 113 |
- Name of the custom endpoint. |
| 114 |
type: str |
| 115 |
required: yes |
| 116 |
resource_group: |
| 117 |
description: |
| 118 |
- Resource group of the endpoint. |
| 119 |
- Default is the same as I(resource_group). |
| 120 |
type: str |
| 121 |
subscription: |
| 122 |
description: |
| 123 |
- Subscription id of the endpoint. |
| 124 |
- Default is the same as I(subscription). |
| 125 |
type: str |
| 126 |
resource_type: |
| 127 |
description: |
| 128 |
- Resource type of the custom endpoint. |
| 129 |
type: str |
| 130 |
choices: |
| 131 |
- eventhub |
| 132 |
- queue |
| 133 |
- storage |
| 134 |
- topic |
| 135 |
required: yes |
| 136 |
connection_string: |
| 137 |
description: |
| 138 |
- Connection string of the custom endpoint. |
| 139 |
- The connection string should have send privilege. |
| 140 |
type: str |
| 141 |
required: yes |
| 142 |
container_name: |
| 143 |
description: |
| 144 |
- Container name of the custom endpoint when I(resource_type=storage). |
| 145 |
type: str |
| 146 |
aliases: |
| 147 |
- container |
| 148 |
encoding: |
| 149 |
description: |
| 150 |
- Encoding of the message when I(resource_type=storage). |
| 151 |
type: str |
| 152 |
routes: |
| 153 |
description: |
| 154 |
- Route device-to-cloud messages to service-facing endpoints. |
| 155 |
type: list |
| 156 |
elements: dict |
| 157 |
suboptions: |
| 158 |
name: |
| 159 |
description: |
| 160 |
- Name of the route. |
| 161 |
type: str |
| 162 |
required: yes |
| 163 |
source: |
| 164 |
description: |
| 165 |
- The origin of the data stream to be acted upon. |
| 166 |
type: str |
| 167 |
choices: |
| 168 |
- device_messages |
| 169 |
- twin_change_events |
| 170 |
- device_lifecycle_events |
| 171 |
- device_job_lifecycle_events |
| 172 |
required: yes |
| 173 |
enabled: |
| 174 |
description: |
| 175 |
- Whether to enable the route. |
| 176 |
type: bool |
| 177 |
required: yes |
| 178 |
endpoint_name: |
| 179 |
description: |
| 180 |
- The name of the endpoint in I(routing_endpoints) where IoT Hub sends messages that match the query. |
| 181 |
type: str |
| 182 |
required: yes |
| 183 |
condition: |
| 184 |
description: |
| 185 |
- "The query expression for the routing query that is run against the message application properties, |
| 186 |
system properties, message body, device twin tags, and device twin properties to determine if it is a match for the endpoint." |
| 187 |
- "For more information about constructing a query, |
| 188 |
see U(https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-routing-query-syntax)" |
| 189 |
type: str |
| 190 |
extends_documentation_fragment: |
| 191 |
- azure.azcollection.azure |
| 192 |
- azure.azcollection.azure_tags |
| 193 |
- azure.azcollection.azure_identity_multiple_user |
| 194 |
|
| 195 |
author: |
| 196 |
- Yuwei Zhou (@yuwzho) |
| 197 |
|
| 198 |
''' |
| 199 |
|
| 200 |
EXAMPLES = ''' |
| 201 |
- name: Create a simplest IoT hub |
| 202 |
azure_rm_iothub: |
| 203 |
name: Testing |
| 204 |
resource_group: myResourceGroup |
| 205 |
- name: Create an IoT hub with route |
| 206 |
azure_rm_iothub: |
| 207 |
resource_group: myResourceGroup |
| 208 |
name: Testing |
| 209 |
routing_endpoints: |
| 210 |
- connection_string: "Endpoint=sb://qux.servicebus.windows.net/;SharedAccessKeyName=quux;SharedAccessKey=****;EntityPath=myQueue" |
| 211 |
name: foo |
| 212 |
resource_type: queue |
| 213 |
resource_group: myResourceGroup1 |
| 214 |
routes: |
| 215 |
- name: bar |
| 216 |
source: device_messages |
| 217 |
endpoint_name: foo |
| 218 |
enabled: true |
| 219 |
''' |
| 220 |
|
| 221 |
RETURN = ''' |
| 222 |
id: |
| 223 |
description: |
| 224 |
- Resource ID of the IoT hub. |
| 225 |
sample: "/subscriptions/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/resourceGroups/myResourceGroup/providers/Microsoft.Devices/IotHubs/Testing" |
| 226 |
returned: success |
| 227 |
type: str |
| 228 |
name: |
| 229 |
description: |
| 230 |
- Name of the IoT hub. |
| 231 |
sample: Testing |
| 232 |
returned: success |
| 233 |
type: str |
| 234 |
resource_group: |
| 235 |
description: |
| 236 |
- Resource group of the IoT hub. |
| 237 |
sample: myResourceGroup. |
| 238 |
returned: success |
| 239 |
type: str |
| 240 |
location: |
| 241 |
description: |
| 242 |
- Location of the IoT hub. |
| 243 |
sample: eastus |
| 244 |
returned: success |
| 245 |
type: str |
| 246 |
unit: |
| 247 |
description: |
| 248 |
- Units in the IoT Hub. |
| 249 |
sample: 1 |
| 250 |
returned: success |
| 251 |
type: int |
| 252 |
sku: |
| 253 |
description: |
| 254 |
- Pricing tier for Azure IoT Hub. |
| 255 |
sample: f1 |
| 256 |
returned: success |
| 257 |
type: str |
| 258 |
cloud_to_device: |
| 259 |
description: |
| 260 |
- Cloud to device message properties. |
| 261 |
contains: |
| 262 |
max_delivery_count: |
| 263 |
description: |
| 264 |
- The number of times the IoT hub attempts to deliver a message on the feedback queue. |
| 265 |
- "See U(https://docs.microsoft.com/azure/iot-hub/iot-hub-devguide-messaging#cloud-to-device-messages)." |
| 266 |
type: int |
| 267 |
returned: success |
| 268 |
sample: 10 |
| 269 |
ttl_as_iso8601: |
| 270 |
description: |
| 271 |
- The period of time for which a message is available to consume before it is expired by the IoT hub. |
| 272 |
- "See U(https://docs.microsoft.com/azure/iot-hub/iot-hub-devguide-messaging#cloud-to-device-messages)." |
| 273 |
type: str |
| 274 |
returned: success |
| 275 |
sample: "1:00:00" |
| 276 |
returned: success |
| 277 |
type: complex |
| 278 |
enable_file_upload_notifications: |
| 279 |
description: |
| 280 |
- Whether file upload notifications are enabled. |
| 281 |
sample: True |
| 282 |
returned: success |
| 283 |
type: bool |
| 284 |
event_endpoints: |
| 285 |
description: |
| 286 |
- Built-in endpoint where to deliver device message. |
| 287 |
contains: |
| 288 |
endpoint: |
| 289 |
description: |
| 290 |
- The Event Hub-compatible endpoint. |
| 291 |
type: str |
| 292 |
returned: success |
| 293 |
sample: "sb://iothub-ns-testing-1478811-9bbc4a15f0.servicebus.windows.net/" |
| 294 |
partition_count: |
| 295 |
description: |
| 296 |
- The number of partitions for receiving device-to-cloud messages in the Event Hub-compatible endpoint. |
| 297 |
- "See U(https://docs.microsoft.com/azure/iot-hub/iot-hub-devguide-messaging#device-to-cloud-messages)." |
| 298 |
type: int |
| 299 |
returned: success |
| 300 |
sample: 2 |
| 301 |
retention_time_in_days: |
| 302 |
description: |
| 303 |
- The retention time for device-to-cloud messages in days. |
| 304 |
- "See U(https://docs.microsoft.com/azure/iot-hub/iot-hub-devguide-messaging#device-to-cloud-messages)." |
| 305 |
type: int |
| 306 |
returned: success |
| 307 |
sample: 1 |
| 308 |
partition_ids: |
| 309 |
description: |
| 310 |
- List of the partition id for the event endpoint. |
| 311 |
type: list |
| 312 |
returned: success |
| 313 |
sample: ["0", "1"] |
| 314 |
returned: success |
| 315 |
type: complex |
| 316 |
host_name: |
| 317 |
description: |
| 318 |
- Host of the IoT hub. |
| 319 |
sample: "testing.azure-devices.net" |
| 320 |
returned: success |
| 321 |
type: str |
| 322 |
ip_filters: |
| 323 |
description: |
| 324 |
- Configure rules for rejecting or accepting traffic from specific IPv4 addresses. |
| 325 |
contains: |
| 326 |
name: |
| 327 |
description: |
| 328 |
- Name of the filter. |
| 329 |
type: str |
| 330 |
returned: success |
| 331 |
sample: filter |
| 332 |
ip_mask: |
| 333 |
description: |
| 334 |
- A string that contains the IP address range in CIDR notation for the rule. |
| 335 |
type: str |
| 336 |
returned: success |
| 337 |
sample: 40.54.7.3 |
| 338 |
action: |
| 339 |
description: |
| 340 |
- The desired action for requests captured by this rule. |
| 341 |
type: str |
| 342 |
returned: success |
| 343 |
sample: Reject |
| 344 |
returned: success |
| 345 |
type: complex |
| 346 |
routing_endpoints: |
| 347 |
description: |
| 348 |
- Custom endpoints. |
| 349 |
contains: |
| 350 |
event_hubs: |
| 351 |
description: |
| 352 |
- List of custom endpoints of event hubs. |
| 353 |
type: complex |
| 354 |
returned: success |
| 355 |
contains: |
| 356 |
name: |
| 357 |
description: |
| 358 |
- Name of the custom endpoint. |
| 359 |
type: str |
| 360 |
returned: success |
| 361 |
sample: foo |
| 362 |
resource_group: |
| 363 |
description: |
| 364 |
- Resource group of the endpoint. |
| 365 |
type: str |
| 366 |
returned: success |
| 367 |
sample: bar |
| 368 |
subscription: |
| 369 |
description: |
| 370 |
- Subscription id of the endpoint. |
| 371 |
type: str |
| 372 |
returned: success |
| 373 |
sample: "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" |
| 374 |
connection_string: |
| 375 |
description: |
| 376 |
- Connection string of the custom endpoint. |
| 377 |
type: str |
| 378 |
returned: success |
| 379 |
sample: "Endpoint=sb://quux.servicebus.windows.net:5671/;SharedAccessKeyName=qux;SharedAccessKey=****;EntityPath=foo" |
| 380 |
service_bus_queues: |
| 381 |
description: |
| 382 |
- List of custom endpoints of service bus queue. |
| 383 |
type: complex |
| 384 |
returned: always |
| 385 |
contains: |
| 386 |
name: |
| 387 |
description: |
| 388 |
- Name of the custom endpoint. |
| 389 |
type: str |
| 390 |
returned: success |
| 391 |
sample: foo |
| 392 |
resource_group: |
| 393 |
description: |
| 394 |
- Resource group of the endpoint. |
| 395 |
type: str |
| 396 |
returned: success |
| 397 |
sample: bar |
| 398 |
subscription: |
| 399 |
description: |
| 400 |
- Subscription ID of the endpoint. |
| 401 |
type: str |
| 402 |
returned: success |
| 403 |
sample: "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" |
| 404 |
connection_string: |
| 405 |
description: |
| 406 |
- Connection string of the custom endpoint. |
| 407 |
type: str |
| 408 |
returned: success |
| 409 |
sample: "Endpoint=sb://quux.servicebus.windows.net:5671/;SharedAccessKeyName=qux;SharedAccessKey=****;EntityPath=foo" |
| 410 |
service_bus_topics: |
| 411 |
description: |
| 412 |
- List of custom endpoints of service bus topic. |
| 413 |
type: complex |
| 414 |
returned: success |
| 415 |
contains: |
| 416 |
name: |
| 417 |
description: |
| 418 |
- Name of the custom endpoint. |
| 419 |
type: str |
| 420 |
returned: success |
| 421 |
sample: foo |
| 422 |
resource_group: |
| 423 |
description: |
| 424 |
- Resource group of the endpoint. |
| 425 |
type: str |
| 426 |
returned: success |
| 427 |
sample: bar |
| 428 |
subscription: |
| 429 |
description: |
| 430 |
- Subscription ID of the endpoint. |
| 431 |
type: str |
| 432 |
returned: success |
| 433 |
sample: "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" |
| 434 |
connection_string: |
| 435 |
description: |
| 436 |
- Connection string of the custom endpoint. |
| 437 |
type: str |
| 438 |
returned: success |
| 439 |
sample: "Endpoint=sb://quux.servicebus.windows.net:5671/;SharedAccessKeyName=qux;SharedAccessKey=****;EntityPath=foo" |
| 440 |
storage_containers: |
| 441 |
description: |
| 442 |
- List of custom endpoints of storage |
| 443 |
type: complex |
| 444 |
returned: success |
| 445 |
contains: |
| 446 |
name: |
| 447 |
description: |
| 448 |
- Name of the custom endpoint. |
| 449 |
type: str |
| 450 |
returned: success |
| 451 |
sample: foo |
| 452 |
resource_group: |
| 453 |
description: |
| 454 |
- Resource group of the endpoint. |
| 455 |
type: str |
| 456 |
returned: success |
| 457 |
sample: bar |
| 458 |
subscription: |
| 459 |
description: |
| 460 |
- Subscription ID of the endpoint. |
| 461 |
type: str |
| 462 |
returned: success |
| 463 |
sample: "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" |
| 464 |
connection_string: |
| 465 |
description: |
| 466 |
- Connection string of the custom endpoint. |
| 467 |
type: str |
| 468 |
returned: success |
| 469 |
sample: "Endpoint=sb://quux.servicebus.windows.net:5671/;SharedAccessKeyName=qux;SharedAccessKey=****;EntityPath=foo" |
| 470 |
returned: success |
| 471 |
type: complex |
| 472 |
routes: |
| 473 |
description: |
| 474 |
- Route device-to-cloud messages to service-facing endpoints. |
| 475 |
type: complex |
| 476 |
returned: success |
| 477 |
contains: |
| 478 |
name: |
| 479 |
description: |
| 480 |
- Name of the route. |
| 481 |
type: str |
| 482 |
returned: success |
| 483 |
sample: route1 |
| 484 |
source: |
| 485 |
description: |
| 486 |
- The origin of the data stream to be acted upon. |
| 487 |
type: str |
| 488 |
returned: success |
| 489 |
sample: device_messages |
| 490 |
enabled: |
| 491 |
description: |
| 492 |
- Whether to enable the route. |
| 493 |
type: str |
| 494 |
returned: success |
| 495 |
sample: true |
| 496 |
endpoint_name: |
| 497 |
description: |
| 498 |
- The name of the endpoint in C(routing_endpoints) where IoT Hub sends messages that match the query. |
| 499 |
type: str |
| 500 |
returned: success |
| 501 |
sample: foo |
| 502 |
condition: |
| 503 |
description: |
| 504 |
- "The query expression for the routing query that is run against the message application properties, |
| 505 |
system properties, message body, device twin tags, and device twin properties to determine if it is a match for the endpoint." |
| 506 |
- "For more information about constructing a query, |
| 507 |
see I(https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-routing-query-syntax)" |
| 508 |
type: bool |
| 509 |
returned: success |
| 510 |
sample: "true" |
| 511 |
''' |
| 512 |
|
| 513 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt |
| 514 |
from ansible.module_utils.common.dict_transformations import _snake_to_camel, _camel_to_snake |
| 515 |
import re |
| 516 |
|
| 517 |
try: |
| 518 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt |
| 519 |
from azure.mgmt.iothub.v2023_06_30_preview import models as IotHubModels |
| 520 |
except ImportError: |
| 521 |
|
| 522 |
pass |
| 523 |
|
| 524 |
|
| 525 |
ip_filter_spec = dict( |
| 526 |
name=dict(type='str', required=True), |
| 527 |
ip_mask=dict(type='str', required=True), |
| 528 |
action=dict(type='str', required=True, choices=['accept', 'reject']) |
| 529 |
) |
| 530 |
|
| 531 |
|
| 532 |
routing_endpoints_spec = dict( |
| 533 |
connection_string=dict(type='str', required=True), |
| 534 |
name=dict(type='str', required=True), |
| 535 |
resource_group=dict(type='str'), |
| 536 |
subscription=dict(type='str'), |
| 537 |
resource_type=dict(type='str', required=True, choices=['eventhub', 'queue', 'storage', 'topic']), |
| 538 |
container_name=dict(type='str', aliases=['container']), |
| 539 |
encoding=dict(type='str') |
| 540 |
) |
| 541 |
|
| 542 |
|
| 543 |
routing_endpoints_resource_type_mapping = { |
| 544 |
'eventhub': {'model': 'RoutingEventHubProperties', 'attribute': 'event_hubs'}, |
| 545 |
'queue': {'model': 'RoutingServiceBusQueueEndpointProperties', 'attribute': 'service_bus_queues'}, |
| 546 |
'topic': {'model': 'RoutingServiceBusTopicEndpointProperties', 'attribute': 'service_bus_topics'}, |
| 547 |
'storage': {'model': 'RoutingStorageContainerProperties', 'attribute': 'storage_containers'} |
| 548 |
} |
| 549 |
|
| 550 |
|
| 551 |
routes_spec = dict( |
| 552 |
name=dict(type='str', required=True), |
| 553 |
source=dict(type='str', required=True, choices=['device_messages', 'twin_change_events', 'device_lifecycle_events', 'device_job_lifecycle_events']), |
| 554 |
enabled=dict(type='bool', required=True), |
| 555 |
endpoint_name=dict(type='str', required=True), |
| 556 |
condition=dict(type='str') |
| 557 |
) |
| 558 |
|
| 559 |
|
| 560 |
event_endpoint_spec = dict( |
| 561 |
partition_count=dict(type='int'), |
| 562 |
retention_time_in_days=dict(type='int') |
| 563 |
) |
| 564 |
|
| 565 |
|
| 566 |
class AzureRMIoTHub(AzureRMModuleBaseExt): |
| 567 |
|
| 568 |
def __init__(self): |
| 569 |
|
| 570 |
self.module_arg_spec = dict( |
| 571 |
resource_group=dict(type='str', required=True), |
| 572 |
name=dict(type='str', required=True), |
| 573 |
state=dict(type='str', default='present', choices=['present', 'absent']), |
| 574 |
location=dict(type='str'), |
| 575 |
sku=dict(type='str', choices=['b1', 'b2', 'b3', 'f1', 's1', 's2', 's3']), |
| 576 |
unit=dict(type='int'), |
| 577 |
event_endpoint=dict(type='dict', options=event_endpoint_spec), |
| 578 |
enable_file_upload_notifications=dict(type='bool'), |
| 579 |
ip_filters=dict(type='list', elements='dict', options=ip_filter_spec), |
| 580 |
routing_endpoints=dict(type='list', elements='dict', options=routing_endpoints_spec), |
| 581 |
routes=dict(type='list', elements='dict', options=routes_spec), |
| 582 |
identity=dict( |
| 583 |
type='dict', |
| 584 |
options=self.managed_identity_multiple_user_assigned_spec |
| 585 |
), |
| 586 |
) |
| 587 |
|
| 588 |
self.results = dict( |
| 589 |
changed=False, |
| 590 |
id=None |
| 591 |
) |
| 592 |
|
| 593 |
self.resource_group = None |
| 594 |
self.name = None |
| 595 |
self.state = None |
| 596 |
self.location = None |
| 597 |
self.sku = None |
| 598 |
self.unit = None |
| 599 |
self.event_endpoint = None |
| 600 |
self.tags = None |
| 601 |
self.enable_file_upload_notifications = None |
| 602 |
self.ip_filters = None |
| 603 |
self.routing_endpoints = None |
| 604 |
self.routes = None |
| 605 |
self.identity = None |
| 606 |
self._managed_identity = None |
| 607 |
|
| 608 |
super(AzureRMIoTHub, self).__init__(self.module_arg_spec, supports_check_mode=True) |
| 609 |
|
| 610 |
@property |
| 611 |
def managed_identity(self): |
| 612 |
if not self._managed_identity: |
| 613 |
self._managed_identity = {"identity": IotHubModels.ArmIdentity, |
| 614 |
"user_assigned": IotHubModels.ManagedIdentity |
| 615 |
} |
| 616 |
return self._managed_identity |
| 617 |
|
| 618 |
def exec_module(self, **kwargs): |
| 619 |
|
| 620 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 621 |
setattr(self, key, kwargs[key]) |
| 622 |
|
| 623 |
changed = False |
| 624 |
|
| 625 |
if not self.location: |
| 626 |
|
| 627 |
resource_group = self.get_resource_group(self.resource_group) |
| 628 |
self.location = resource_group.location |
| 629 |
self.sku = str.capitalize(self.sku) if self.sku else None |
| 630 |
iothub = self.get_hub() |
| 631 |
if self.state == 'present': |
| 632 |
if not iothub: |
| 633 |
changed = True |
| 634 |
self.sku = self.sku or 'S1' |
| 635 |
self.unit = self.unit or 1 |
| 636 |
self.event_endpoint = self.event_endpoint or {} |
| 637 |
self.event_endpoint['partition_count'] = self.event_endpoint.get('partition_count') or 2 |
| 638 |
self.event_endpoint['retention_time_in_days'] = self.event_endpoint.get('retention_time_in_days') or 1 |
| 639 |
event_hub_properties = dict() |
| 640 |
event_hub_properties['events'] = self.IoThub_models.EventHubProperties(**self.event_endpoint) |
| 641 |
iothub_property = self.IoThub_models.IotHubProperties(event_hub_endpoints=event_hub_properties) |
| 642 |
if self.enable_file_upload_notifications: |
| 643 |
iothub_property.enable_file_upload_notifications = self.enable_file_upload_notifications |
| 644 |
if self.ip_filters: |
| 645 |
iothub_property.ip_filter_rules = self.construct_ip_filters() |
| 646 |
routing_endpoints = None |
| 647 |
routes = None |
| 648 |
if self.routing_endpoints: |
| 649 |
routing_endpoints = self.construct_routing_endpoint(self.routing_endpoints) |
| 650 |
if self.routes: |
| 651 |
routes = [self.construct_route(x) for x in self.routes] |
| 652 |
if routes or routing_endpoints: |
| 653 |
routing_property = self.IoThub_models.RoutingProperties(endpoints=routing_endpoints, |
| 654 |
routes=routes) |
| 655 |
iothub_property.routing = routing_property |
| 656 |
if self.identity: |
| 657 |
identities_changed, self.identity = self.update_managed_identity(new_identity=self.identity) |
| 658 |
iothub = self.IoThub_models.IotHubDescription(location=self.location, |
| 659 |
sku=self.IoThub_models.IotHubSkuInfo(name=self.sku, capacity=self.unit), |
| 660 |
properties=iothub_property, |
| 661 |
tags=self.tags, |
| 662 |
identity=self.identity) |
| 663 |
if not self.check_mode: |
| 664 |
iothub = self.create_or_update_hub(iothub) |
| 665 |
else: |
| 666 |
|
| 667 |
original_sku = iothub.sku |
| 668 |
if self.sku and self.sku != original_sku.name: |
| 669 |
self.log('SKU changed') |
| 670 |
iothub.sku.name = self.sku |
| 671 |
changed = True |
| 672 |
if self.unit and self.unit != original_sku.capacity: |
| 673 |
self.log('Unit count changed') |
| 674 |
iothub.sku.capacity = self.unit |
| 675 |
changed = True |
| 676 |
|
| 677 |
event_hub = iothub.properties.event_hub_endpoints or dict() |
| 678 |
if self.event_endpoint: |
| 679 |
item = self.event_endpoint |
| 680 |
original_item = event_hub.get('events') |
| 681 |
if not original_item: |
| 682 |
changed = True |
| 683 |
event_hub['events'] = self.IoThub_models.EventHubProperties(partition_count=item.get('partition_count') or 2, |
| 684 |
retention_time_in_days=item.get('retention_time_in_days') or 1) |
| 685 |
elif item.get('partition_count') and original_item.partition_count != item['partition_count']: |
| 686 |
changed = True |
| 687 |
original_item.partition_count = item['partition_count'] |
| 688 |
elif item.get('retention_time_in_days') and original_item.retention_time_in_days != item['retention_time_in_days']: |
| 689 |
changed = True |
| 690 |
original_item.retention_time_in_days = item['retention_time_in_days'] |
| 691 |
|
| 692 |
original_endpoints = iothub.properties.routing.endpoints |
| 693 |
endpoint_changed = False |
| 694 |
if self.routing_endpoints: |
| 695 |
|
| 696 |
total_length = 0 |
| 697 |
for item in routing_endpoints_resource_type_mapping.values(): |
| 698 |
attribute = item['attribute'] |
| 699 |
array = getattr(original_endpoints, attribute) |
| 700 |
total_length += len(array or []) |
| 701 |
if total_length != len(self.routing_endpoints): |
| 702 |
endpoint_changed = True |
| 703 |
else: |
| 704 |
for item in self.routing_endpoints: |
| 705 |
if not self.lookup_endpoint(item, original_endpoints): |
| 706 |
endpoint_changed = True |
| 707 |
break |
| 708 |
if endpoint_changed: |
| 709 |
iothub.properties.routing.endpoints = self.construct_routing_endpoint(self.routing_endpoints) |
| 710 |
changed = True |
| 711 |
|
| 712 |
original_routes = iothub.properties.routing.routes |
| 713 |
routes_changed = False |
| 714 |
if self.routes: |
| 715 |
if len(self.routes) != len(original_routes or []): |
| 716 |
routes_changed = True |
| 717 |
else: |
| 718 |
for item in self.routes: |
| 719 |
if not self.lookup_route(item, original_routes): |
| 720 |
routes_changed = True |
| 721 |
break |
| 722 |
if routes_changed: |
| 723 |
changed = True |
| 724 |
iothub.properties.routing.routes = [self.construct_route(x) for x in self.routes] |
| 725 |
|
| 726 |
ip_filter_changed = False |
| 727 |
original_ip_filter = iothub.properties.ip_filter_rules |
| 728 |
if self.ip_filters: |
| 729 |
if len(self.ip_filters) != len(original_ip_filter or []): |
| 730 |
ip_filter_changed = True |
| 731 |
else: |
| 732 |
for item in self.ip_filters: |
| 733 |
if not self.lookup_ip_filter(item, original_ip_filter): |
| 734 |
ip_filter_changed = True |
| 735 |
break |
| 736 |
if ip_filter_changed: |
| 737 |
changed = True |
| 738 |
iothub.properties.ip_filter_rules = self.construct_ip_filters() |
| 739 |
|
| 740 |
|
| 741 |
tag_changed, updated_tags = self.update_tags(iothub.tags) |
| 742 |
iothub.tags = updated_tags |
| 743 |
|
| 744 |
|
| 745 |
identity_changed = False |
| 746 |
if self.identity: |
| 747 |
identity_changed, iothub.identity = self.update_managed_identity(new_identity=self.identity, curr_identity=iothub.identity.as_dict()) |
| 748 |
|
| 749 |
if (changed or identity_changed) and not self.check_mode: |
| 750 |
changed = True |
| 751 |
iothub = self.create_or_update_hub(iothub) |
| 752 |
|
| 753 |
if not changed and tag_changed: |
| 754 |
changed = True |
| 755 |
if not self.check_mode: |
| 756 |
iothub = self.update_instance_tags(updated_tags) |
| 757 |
self.results = self.to_dict(iothub) |
| 758 |
elif iothub: |
| 759 |
changed = True |
| 760 |
if not self.check_mode: |
| 761 |
self.delete_hub() |
| 762 |
self.results['changed'] = changed |
| 763 |
return self.results |
| 764 |
|
| 765 |
def lookup_ip_filter(self, target, ip_filters): |
| 766 |
if not ip_filters or len(ip_filters) == 0: |
| 767 |
return False |
| 768 |
for item in ip_filters: |
| 769 |
if item.filter_name == target['name']: |
| 770 |
if item.ip_mask != target['ip_mask']: |
| 771 |
return False |
| 772 |
if item.action.lower() != target['action']: |
| 773 |
return False |
| 774 |
return True |
| 775 |
return False |
| 776 |
|
| 777 |
def lookup_route(self, target, routes): |
| 778 |
if not routes or len(routes) == 0: |
| 779 |
return False |
| 780 |
for item in routes: |
| 781 |
if item.name == target['name']: |
| 782 |
if target['source'] != _camel_to_snake(item.source): |
| 783 |
return False |
| 784 |
if target['enabled'] != item.is_enabled: |
| 785 |
return False |
| 786 |
if target['endpoint_name'] != item.endpoint_names[0]: |
| 787 |
return False |
| 788 |
if target.get('condition') and target['condition'] != item.condition: |
| 789 |
return False |
| 790 |
return True |
| 791 |
return False |
| 792 |
|
| 793 |
def lookup_endpoint(self, target, routing_endpoints): |
| 794 |
resource_type = target['resource_type'] |
| 795 |
attribute = routing_endpoints_resource_type_mapping[resource_type]['attribute'] |
| 796 |
endpoints = getattr(routing_endpoints, attribute) |
| 797 |
if not endpoints or len(endpoints) == 0: |
| 798 |
return False |
| 799 |
for item in endpoints: |
| 800 |
if item.name == target['name']: |
| 801 |
if target.get('resource_group') and target['resource_group'] != (item.resource_group or self.resource_group): |
| 802 |
return False |
| 803 |
if target.get('subscription_id') and target['subscription_id'] != (item.subscription_id or self.subscription_id): |
| 804 |
return False |
| 805 |
connection_string_regex = item.connection_string.replace('****', '.*') |
| 806 |
connection_string_regex = re.sub(r':\d+/;', '/;', connection_string_regex) |
| 807 |
if not re.search(connection_string_regex, target['connection_string']): |
| 808 |
return False |
| 809 |
if resource_type == 'storage': |
| 810 |
if target.get('container_name') and item.container_name != target['container_name']: |
| 811 |
return False |
| 812 |
if target.get('encoding') and item.encoding != target['encoding']: |
| 813 |
return False |
| 814 |
return True |
| 815 |
return False |
| 816 |
|
| 817 |
def construct_ip_filters(self): |
| 818 |
return [self.IoThub_models.IpFilterRule(filter_name=x['name'], |
| 819 |
action=self.IoThub_models.IpFilterActionType[x['action']], |
| 820 |
ip_mask=x['ip_mask']) for x in self.ip_filters] |
| 821 |
|
| 822 |
def construct_routing_endpoint(self, routing_endpoints): |
| 823 |
if not routing_endpoints or len(routing_endpoints) == 0: |
| 824 |
return None |
| 825 |
result = self.IoThub_models.RoutingEndpoints() |
| 826 |
for endpoint in routing_endpoints: |
| 827 |
resource_type_property = routing_endpoints_resource_type_mapping.get(endpoint['resource_type']) |
| 828 |
resource_type = getattr(self.IoThub_models, resource_type_property['model']) |
| 829 |
array = getattr(result, resource_type_property['attribute']) or [] |
| 830 |
array.append(resource_type(**endpoint)) |
| 831 |
setattr(result, resource_type_property['attribute'], array) |
| 832 |
return result |
| 833 |
|
| 834 |
def construct_route(self, route): |
| 835 |
if not route: |
| 836 |
return None |
| 837 |
return self.IoThub_models.RouteProperties(name=route['name'], |
| 838 |
source=_snake_to_camel(snake=route['source'], capitalize_first=True), |
| 839 |
is_enabled=route['enabled'], |
| 840 |
endpoint_names=[route['endpoint_name']], |
| 841 |
condition=route.get('condition')) |
| 842 |
|
| 843 |
def get_hub(self): |
| 844 |
try: |
| 845 |
return self.IoThub_client.iot_hub_resource.get(self.resource_group, self.name) |
| 846 |
except Exception: |
| 847 |
return None |
| 848 |
|
| 849 |
def create_or_update_hub(self, hub): |
| 850 |
try: |
| 851 |
poller = self.IoThub_client.iot_hub_resource.begin_create_or_update(self.resource_group, self.name, hub, if_match=hub.etag) |
| 852 |
return self.get_poller_result(poller) |
| 853 |
except Exception as exc: |
| 854 |
self.fail('Error creating or updating IoT Hub {0}: {1}'.format(self.name, exc.message or str(exc))) |
| 855 |
|
| 856 |
def update_instance_tags(self, tags): |
| 857 |
try: |
| 858 |
poller = self.IoThub_client.iot_hub_resource.begin_update(self.resource_group, self.name, tags=tags) |
| 859 |
return self.get_poller_result(poller) |
| 860 |
except Exception as exc: |
| 861 |
self.fail('Error updating IoT Hub {0}\'s tag: {1}'.format(self.name, exc.message or str(exc))) |
| 862 |
|
| 863 |
def delete_hub(self): |
| 864 |
try: |
| 865 |
self.IoThub_client.iot_hub_resource.begin_delete(self.resource_group, self.name) |
| 866 |
return True |
| 867 |
except Exception as exc: |
| 868 |
self.fail('Error deleting IoT Hub {0}: {1}'.format(self.name, exc.message or str(exc))) |
| 869 |
return False |
| 870 |
|
| 871 |
def route_to_dict(self, route): |
| 872 |
return dict( |
| 873 |
name=route.name, |
| 874 |
source=_camel_to_snake(route.source), |
| 875 |
endpoint_name=route.endpoint_names[0], |
| 876 |
enabled=route.is_enabled, |
| 877 |
condition=route.condition |
| 878 |
) |
| 879 |
|
| 880 |
def instance_dict_to_dict(self, instance_dict): |
| 881 |
result = dict() |
| 882 |
if not instance_dict: |
| 883 |
return result |
| 884 |
for key in instance_dict.keys(): |
| 885 |
result[key] = instance_dict[key].as_dict() |
| 886 |
return result |
| 887 |
|
| 888 |
def to_dict(self, hub): |
| 889 |
result = dict() |
| 890 |
properties = hub.properties |
| 891 |
result['id'] = hub.id |
| 892 |
result['name'] = hub.name |
| 893 |
result['resource_group'] = self.resource_group |
| 894 |
result['location'] = hub.location |
| 895 |
result['tags'] = hub.tags |
| 896 |
result['unit'] = hub.sku.capacity |
| 897 |
result['sku'] = hub.sku.name.lower() |
| 898 |
result['cloud_to_device'] = dict( |
| 899 |
max_delivery_count=properties.cloud_to_device.feedback.max_delivery_count, |
| 900 |
ttl_as_iso8601=str(properties.cloud_to_device.feedback.ttl_as_iso8601) |
| 901 |
) if properties.cloud_to_device else dict() |
| 902 |
result['enable_file_upload_notifications'] = properties.enable_file_upload_notifications |
| 903 |
result['event_endpoint'] = properties.event_hub_endpoints.get('events').as_dict() if properties.event_hub_endpoints.get('events') else None |
| 904 |
result['host_name'] = properties.host_name |
| 905 |
result['ip_filters'] = [x.as_dict() for x in properties.ip_filter_rules] |
| 906 |
if properties.routing: |
| 907 |
result['routing_endpoints'] = properties.routing.endpoints.as_dict() |
| 908 |
result['routes'] = [self.route_to_dict(x) for x in properties.routing.routes] |
| 909 |
result['fallback_route'] = self.route_to_dict(properties.routing.fallback_route) |
| 910 |
result['status'] = properties.state |
| 911 |
result['storage_endpoints'] = self.instance_dict_to_dict(properties.storage_endpoints) |
| 912 |
if hasattr(hub, 'identity'): |
| 913 |
result['identity'] = hub.identity.as_dict() if hub.identity else None |
| 914 |
return result |
| 915 |
|
| 916 |
|
| 917 |
def main(): |
| 918 |
AzureRMIoTHub() |
| 919 |
|
| 920 |
|
| 921 |
if __name__ == '__main__': |
| 922 |
main() |