| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
from __future__ import absolute_import, division, print_function |
| 6 |
__metaclass__ = type |
| 7 |
|
| 8 |
|
| 9 |
DOCUMENTATION = ''' |
| 10 |
--- |
| 11 |
module: azure_rm_eventgrid_subscription |
| 12 |
version_added: "3.13.0" |
| 13 |
short_description: Manage Eventgrid Subscriptions |
| 14 |
description: |
| 15 |
- Create, update ,delete and replace Eventgrid Subscriptions. |
| 16 |
options: |
| 17 |
name: |
| 18 |
description: |
| 19 |
- Name of the subscription. |
| 20 |
required: true |
| 21 |
type: str |
| 22 |
source_resource_id: |
| 23 |
description: |
| 24 |
- Name of a resource id where the subscription exists or will be created. |
| 25 |
required: true |
| 26 |
type: str |
| 27 |
state: |
| 28 |
description: |
| 29 |
- State of the Eventgrid Event Subscription. Use C(present) to create |
| 30 |
or update and C(absent) to delete. |
| 31 |
default: present |
| 32 |
type: str |
| 33 |
choices: |
| 34 |
- absent |
| 35 |
- present |
| 36 |
|
| 37 |
|
| 38 |
extends_documentation_fragment: |
| 39 |
- azure.azcollection.azure |
| 40 |
- azure.azcollection.azure_eventgrid_subscription |
| 41 |
|
| 42 |
author: |
| 43 |
- Bill Peck (@p3ck) |
| 44 |
''' |
| 45 |
|
| 46 |
EXAMPLES = ''' |
| 47 |
- name: Subscribe to custom topic |
| 48 |
azure.azcollection.azure_rm_eventgrid_subscription: |
| 49 |
name: subscription-xxxxx20070 |
| 50 |
source_resource_id: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/MyResourceGroup/providers/Microsoft.EventGrid/topics/topic-xxxxx20070 |
| 51 |
endpoint: https://event-grid-site-xxxxx20070.azurewebsites.net/api/updates |
| 52 |
''' |
| 53 |
|
| 54 |
RETURN = ''' |
| 55 |
changed: |
| 56 |
description: |
| 57 |
- Whether the resource is changed. |
| 58 |
returned: always |
| 59 |
type: bool |
| 60 |
sample: true |
| 61 |
eventgrid_subscriptions: |
| 62 |
description: |
| 63 |
- List of Eventgrid subscriptions. |
| 64 |
returned: always |
| 65 |
type: complex |
| 66 |
contains: |
| 67 |
destination: |
| 68 |
description: |
| 69 |
- Details of the destination |
| 70 |
returned: always |
| 71 |
type: complex |
| 72 |
contains: |
| 73 |
endpoint_base_url: |
| 74 |
description: |
| 75 |
- The base URL of the endpoint |
| 76 |
returned: always |
| 77 |
type: str |
| 78 |
sample: https://event-grid-site-xxxxx20070.azurewebsites.net/api/updates |
| 79 |
endpoint_type: |
| 80 |
description: |
| 81 |
- The type of the endpoint |
| 82 |
returned: always |
| 83 |
type: str |
| 84 |
sample: WebHook |
| 85 |
max_events_per_batch: |
| 86 |
description: |
| 87 |
- How many events will be batched |
| 88 |
returned: always |
| 89 |
type: int |
| 90 |
sample: 1 |
| 91 |
preferred_batch_size_in_kilobytes: |
| 92 |
description: |
| 93 |
- The preferred batch size in kilobytes |
| 94 |
returned: always |
| 95 |
type: int |
| 96 |
sample: 64 |
| 97 |
event_delivery_schema: |
| 98 |
description: |
| 99 |
- The type of Eventgrid Schema |
| 100 |
returned: always |
| 101 |
type: str |
| 102 |
sample: EventGridSchema |
| 103 |
filter: |
| 104 |
description: |
| 105 |
- Filter settings for which events to publish to endpoint |
| 106 |
returned: always |
| 107 |
type: complex |
| 108 |
contains: |
| 109 |
subject_begins_with: |
| 110 |
description: |
| 111 |
- Filter on Subject beginning with this value |
| 112 |
returned: always |
| 113 |
type: str |
| 114 |
subject_ends_with: |
| 115 |
description: |
| 116 |
- Filter on Subject ending with this value |
| 117 |
returned: always |
| 118 |
type: str |
| 119 |
id: |
| 120 |
description: |
| 121 |
- The ID of the Eventgrid Subscription |
| 122 |
returned: always |
| 123 |
type: str |
| 124 |
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/MyResourceGroup/providers/Microsoft.EventGrid/topics/topic-xxxxx20070/providers/Microsoft.EventGrid/eventSubscriptions/subscription-xxxxx20070 # nopep8: E501 |
| 125 |
name: |
| 126 |
description: |
| 127 |
- The name of the Eventgrid Subscription |
| 128 |
returned: always |
| 129 |
type: str |
| 130 |
sample: subscription-xxxxx20070 |
| 131 |
provisioning_state: |
| 132 |
description: |
| 133 |
- The provisioning state of this Eventgrid Subscription |
| 134 |
returned: always |
| 135 |
type: str |
| 136 |
sample: Succeeded |
| 137 |
retry_policy: |
| 138 |
description: |
| 139 |
- Retry policy for this Eventgrid Subscription |
| 140 |
returned: always |
| 141 |
type: complex |
| 142 |
contains: |
| 143 |
event_time_to_live_in_minutes: |
| 144 |
description: |
| 145 |
- Event time to live in minutes |
| 146 |
returned: always |
| 147 |
type: int |
| 148 |
sample: 1440 |
| 149 |
max_delivery_attempts: |
| 150 |
description: |
| 151 |
- Max attempts to deliver to endpoint |
| 152 |
returned: always |
| 153 |
type: int |
| 154 |
sample: 30 |
| 155 |
topic: |
| 156 |
description: |
| 157 |
- The source resource id that the Eventgrid Subscription is subscribed to |
| 158 |
returned: always |
| 159 |
type: str |
| 160 |
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/MyResourceGroup/providers/microsoft.eventgrid/topics/topic-xxxxx20070 |
| 161 |
type: |
| 162 |
description: |
| 163 |
- The type of Eventgrid Subscription |
| 164 |
returned: always |
| 165 |
type: str |
| 166 |
sample: Microsoft.EventGrid/eventSubscriptions |
| 167 |
''' |
| 168 |
|
| 169 |
|
| 170 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt |
| 171 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_eventgrid import ( |
| 172 |
eventgrid_subscription_module_arg_spec, get_event_subscription_info, |
| 173 |
update_event_subscription_internal, ConfigError) |
| 174 |
try: |
| 175 |
from azure.mgmt.eventgrid import EventGridManagementClient |
| 176 |
from azure.core.polling import LROPoller |
| 177 |
from azure.core.exceptions import ResourceNotFoundError |
| 178 |
except ImportError: |
| 179 |
|
| 180 |
pass |
| 181 |
|
| 182 |
|
| 183 |
class AzureRMEventgridSubscription(AzureRMModuleBaseExt): |
| 184 |
|
| 185 |
@property |
| 186 |
def client(self): |
| 187 |
self.log('Getting client') |
| 188 |
if not self._client: |
| 189 |
self._client = self.get_mgmt_svc_client(EventGridManagementClient, |
| 190 |
base_url=self._cloud_environment.endpoints.resource_manager) |
| 191 |
return self._client |
| 192 |
|
| 193 |
def __init__(self): |
| 194 |
|
| 195 |
self.module_arg_spec = dict( |
| 196 |
name=dict( |
| 197 |
type='str', |
| 198 |
required=True |
| 199 |
), |
| 200 |
source_resource_id=dict( |
| 201 |
type='str', |
| 202 |
required=True, |
| 203 |
), |
| 204 |
state=dict( |
| 205 |
type='str', |
| 206 |
default='present', |
| 207 |
choices=['present', 'absent'] |
| 208 |
), |
| 209 |
) |
| 210 |
self.module_arg_spec.update(eventgrid_subscription_module_arg_spec) |
| 211 |
|
| 212 |
self._client = None |
| 213 |
|
| 214 |
self.name = None |
| 215 |
self.source_resource_id = None |
| 216 |
self.endpoint = None |
| 217 |
self.endpoint_type = None |
| 218 |
self.included_event_types = None |
| 219 |
self.subject_begins_with = None |
| 220 |
self.subject_ends_with = None |
| 221 |
self.is_subject_case_sensitive = None |
| 222 |
self.max_delivery_attempts = None |
| 223 |
self.event_ttl = None |
| 224 |
self.max_events_per_batch = None |
| 225 |
self.preferred_batch_size_in_kilobytes = None |
| 226 |
self.event_delivery_schema = None |
| 227 |
self.deadletter_endpoint = None |
| 228 |
self.labels = None |
| 229 |
self.expiration_date = None |
| 230 |
self.advanced_filter = None |
| 231 |
self.azure_active_directory_tenant_id = None |
| 232 |
self.azure_active_directory_application_id_or_uri = None |
| 233 |
self.storage_queue_msg_ttl = None |
| 234 |
self.enable_advanced_filtering_on_arrays = None |
| 235 |
self.delivery_attribute_mapping = None |
| 236 |
|
| 237 |
self.state = None |
| 238 |
|
| 239 |
self.results = dict( |
| 240 |
changed=False, |
| 241 |
eventgrid_event_subscription=dict() |
| 242 |
) |
| 243 |
|
| 244 |
required_together = None |
| 245 |
required_if = None |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
super(AzureRMEventgridSubscription, self).__init__(self.module_arg_spec, |
| 252 |
supports_tags=False, |
| 253 |
supports_check_mode=True, |
| 254 |
required_together=required_together, |
| 255 |
required_if=required_if) |
| 256 |
|
| 257 |
def exec_module(self, **kwargs): |
| 258 |
|
| 259 |
for key in list(self.module_arg_spec.keys()): |
| 260 |
setattr(self, key, kwargs[key]) |
| 261 |
|
| 262 |
changed = False |
| 263 |
|
| 264 |
if self.state == 'present': |
| 265 |
event_subscription_info = self.get_event_subscription(self.source_resource_id, |
| 266 |
self.name) |
| 267 |
if event_subscription_info: |
| 268 |
try: |
| 269 |
params = update_event_subscription_internal( |
| 270 |
instance=event_subscription_info, |
| 271 |
endpoint=self.endpoint, |
| 272 |
endpoint_type=self.update_endpoint_type, |
| 273 |
subject_begins_with=self.subject_begins_with, |
| 274 |
subject_ends_with=self.subject_ends_with, |
| 275 |
included_event_types=self.included_event_types, |
| 276 |
advanced_filter=self.advanced_filter, |
| 277 |
labels=self.labels, |
| 278 |
deadletter_endpoint=self.deadletter_endpoint, |
| 279 |
delivery_identity=None, |
| 280 |
delivery_identity_endpoint=None, |
| 281 |
delivery_identity_endpoint_type=None, |
| 282 |
deadletter_identity=None, |
| 283 |
deadletter_identity_endpoint=None, |
| 284 |
storage_queue_msg_ttl=self.storage_queue_msg_ttl, |
| 285 |
enable_advanced_filtering_on_arrays=self.enable_advanced_filtering_on_arrays, |
| 286 |
delivery_attribute_mapping=self.delivery_attribute_mapping) |
| 287 |
except ConfigError as exc: |
| 288 |
self.fail(str(exc)) |
| 289 |
|
| 290 |
self.results['compare'] = [] |
| 291 |
changed = not self.default_compare({}, |
| 292 |
self.format_event_subscription(params), |
| 293 |
self.format_event_subscription(event_subscription_info), |
| 294 |
'', |
| 295 |
self.results) |
| 296 |
if changed and not self.check_mode: |
| 297 |
event_subscription_info = self.begin_update_event_subscription(self.source_resource_id, |
| 298 |
self.name, |
| 299 |
params) |
| 300 |
response = self.format_event_subscription(event_subscription_info) |
| 301 |
|
| 302 |
else: |
| 303 |
changed = True |
| 304 |
|
| 305 |
try: |
| 306 |
event_subscription_info = get_event_subscription_info( |
| 307 |
endpoint=self.endpoint, |
| 308 |
endpoint_type=self.endpoint_type, |
| 309 |
included_event_types=self.included_event_types, |
| 310 |
subject_begins_with=self.subject_begins_with, |
| 311 |
subject_ends_with=self.subject_ends_with, |
| 312 |
is_subject_case_sensitive=self.is_subject_case_sensitive, |
| 313 |
max_delivery_attempts=self.max_delivery_attempts, |
| 314 |
event_ttl=self.event_ttl, |
| 315 |
max_events_per_batch=self.max_events_per_batch, |
| 316 |
preferred_batch_size_in_kilobytes=self.preferred_batch_size_in_kilobytes, |
| 317 |
event_delivery_schema=self.event_delivery_schema, |
| 318 |
deadletter_endpoint=self.deadletter_endpoint, |
| 319 |
labels=self.labels, |
| 320 |
expiration_date=self.expiration_date, |
| 321 |
advanced_filter=self.advanced_filter, |
| 322 |
azure_active_directory_tenant_id=self.azure_active_directory_tenant_id, |
| 323 |
azure_active_directory_application_id_or_uri=self.azure_active_directory_application_id_or_uri, |
| 324 |
delivery_identity=None, |
| 325 |
delivery_identity_endpoint=None, |
| 326 |
delivery_identity_endpoint_type=None, |
| 327 |
deadletter_identity=None, |
| 328 |
deadletter_identity_endpoint=None, |
| 329 |
storage_queue_msg_ttl=self.storage_queue_msg_ttl, |
| 330 |
enable_advanced_filtering_on_arrays=self.enable_advanced_filtering_on_arrays, |
| 331 |
delivery_attribute_mapping=self.delivery_attribute_mapping) |
| 332 |
except ConfigError as exc: |
| 333 |
self.fail(str(exc)) |
| 334 |
|
| 335 |
if not self.check_mode: |
| 336 |
event_subscription_info = self.begin_create_or_update_event_subscription(self.source_resource_id, |
| 337 |
self.name, |
| 338 |
event_subscription_info) |
| 339 |
response = self.format_event_subscription(event_subscription_info) |
| 340 |
else: |
| 341 |
event_subscription_info = self.get_event_subscription(self.source_resource_id, |
| 342 |
self.name) |
| 343 |
if event_subscription_info: |
| 344 |
changed = True |
| 345 |
if not self.check_mode: |
| 346 |
response = self.delete_event_subscription(self.source_resource_id, |
| 347 |
self.name) |
| 348 |
else: |
| 349 |
response = self.format_event_subscription(event_subscription_info) |
| 350 |
|
| 351 |
self.results['changed'] = changed |
| 352 |
self.results['eventgrid_event_subscription'] = response |
| 353 |
|
| 354 |
return self.results |
| 355 |
|
| 356 |
def begin_create_or_update_event_subscription(self, |
| 357 |
source_resource_id, |
| 358 |
event_subscription_name, |
| 359 |
event_subscription_info): |
| 360 |
self.log('Creates or updates the Eventgrid Event Subscription.') |
| 361 |
|
| 362 |
try: |
| 363 |
response = self.client.event_subscriptions.begin_create_or_update(source_resource_id, |
| 364 |
event_subscription_name, |
| 365 |
event_subscription_info) |
| 366 |
if isinstance(response, LROPoller): |
| 367 |
response = self.get_poller_result(response) |
| 368 |
except Exception as exc: |
| 369 |
self.fail('Creates or updates the Eventgrid Event Subscription got Exception as as {0}'.format(str(exc))) |
| 370 |
return response |
| 371 |
|
| 372 |
def begin_update_event_subscription(self, |
| 373 |
source_resource_id, |
| 374 |
event_subscription_name, |
| 375 |
event_subscription_update_parameters): |
| 376 |
self.log('Selectively updates the Event Subscription.') |
| 377 |
|
| 378 |
try: |
| 379 |
response = self.client.event_subscriptions.begin_update(source_resource_id, |
| 380 |
event_subscription_name, |
| 381 |
event_subscription_update_parameters) |
| 382 |
if isinstance(response, LROPoller): |
| 383 |
response = self.get_poller_result(response) |
| 384 |
except Exception as exc: |
| 385 |
self.fail('Selectively update the Eventgrid Event Subscription got Excption as {0}'.format(str(exc))) |
| 386 |
return response |
| 387 |
|
| 388 |
def delete_event_subscription(self, source_resource_id, event_subsription_name): |
| 389 |
self.log('Deletes the entire Eventgrid Event Subscription.') |
| 390 |
try: |
| 391 |
response = self.client.event_subscriptions.begin_delete(source_resource_id, event_subsription_name) |
| 392 |
if isinstance(response, LROPoller): |
| 393 |
response = self.get_poller_result(response) |
| 394 |
except Exception as exc: |
| 395 |
self.fail('Delete Event Subscription got Excetion as {0}'.format(str(exc))) |
| 396 |
|
| 397 |
def get_event_subscription(self, source_resource_id, event_subscription_name): |
| 398 |
self.log('Get properties for {0}'.format(source_resource_id)) |
| 399 |
try: |
| 400 |
return self.client.event_subscriptions.get(source_resource_id, event_subscription_name) |
| 401 |
except ResourceNotFoundError: |
| 402 |
self.log('Did not find the event_subscription.') |
| 403 |
return False |
| 404 |
except Exception as exc: |
| 405 |
self.fail('Error trying to get event_subscription got Excetion as {0}'.format(str(exc))) |
| 406 |
|
| 407 |
def format_event_subscription(self, event_subscription_data): |
| 408 |
results = event_subscription_data and event_subscription_data.as_dict() or {} |
| 409 |
return results |
| 410 |
|
| 411 |
|
| 412 |
def main(): |
| 413 |
AzureRMEventgridSubscription() |
| 414 |
|
| 415 |
|
| 416 |
if __name__ == '__main__': |
| 417 |
main() |