| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
import re |
| 13 |
try: |
| 14 |
from dateutil.parser import parse |
| 15 |
from azure.mgmt.eventgrid.models import ( |
| 16 |
EventSubscription, |
| 17 |
EventSubscriptionUpdateParameters, |
| 18 |
WebHookEventSubscriptionDestination, |
| 19 |
RetryPolicy, |
| 20 |
EventHubEventSubscriptionDestination, |
| 21 |
HybridConnectionEventSubscriptionDestination, |
| 22 |
ServiceBusQueueEventSubscriptionDestination, |
| 23 |
ServiceBusTopicEventSubscriptionDestination, |
| 24 |
AzureFunctionEventSubscriptionDestination, |
| 25 |
StorageBlobDeadLetterDestination, |
| 26 |
EventSubscriptionFilter, |
| 27 |
EventSubscriptionIdentity, |
| 28 |
DeliveryWithResourceIdentity, |
| 29 |
DeadLetterWithResourceIdentity, |
| 30 |
StorageQueueEventSubscriptionDestination) |
| 31 |
except ImportError: |
| 32 |
|
| 33 |
pass |
| 34 |
|
| 35 |
|
| 36 |
IDENTITY_NO_IDENTITY = "NoIdentity" |
| 37 |
IDENTITY_NONE = "None" |
| 38 |
IDENTITY_SYSTEM_ASSIGNED = "SystemAssigned" |
| 39 |
IDENTITY_USER_ASSIGNED = "UserAssigned" |
| 40 |
IDENTITY_MIXED_MODE = "SystemAssigned, UserAssigned" |
| 41 |
|
| 42 |
WEBHOOK_DESTINATION = "webhook" |
| 43 |
EVENTHUB_DESTINATION = "eventhub" |
| 44 |
STORAGEQUEUE_DESTINATION = "storagequeue" |
| 45 |
HYBRIDCONNECTION_DESTINATION = "hybridconnection" |
| 46 |
SERVICEBUSQUEUE_DESTINATION = "servicebusqueue" |
| 47 |
SERVICEBUSTOPIC_DESTINATION = "servicebustopic" |
| 48 |
AZUREFUNCTION_DESTINATION = "azurefunction" |
| 49 |
|
| 50 |
|
| 51 |
class ConfigError(Exception): |
| 52 |
def __init__(self, message, **kwargs): |
| 53 |
self.__dict__.update(kwargs) |
| 54 |
super(ConfigError, self).__init__(message) |
| 55 |
|
| 56 |
|
| 57 |
eventgrid_subscription_module_arg_spec = dict( |
| 58 |
azure_active_directory_application_id_or_uri=dict( |
| 59 |
type='str', |
| 60 |
required=False, |
| 61 |
), |
| 62 |
azure_active_directory_tenant_id=dict( |
| 63 |
type='str', |
| 64 |
required=False, |
| 65 |
), |
| 66 |
deadletter_endpoint=dict( |
| 67 |
type='str', |
| 68 |
required=False, |
| 69 |
), |
| 70 |
delivery_attribute_mapping=dict( |
| 71 |
type='list', |
| 72 |
elements='str', |
| 73 |
required=False, |
| 74 |
), |
| 75 |
endpoint=dict( |
| 76 |
type='str', |
| 77 |
required=False, |
| 78 |
), |
| 79 |
endpoint_type=dict( |
| 80 |
type='str', |
| 81 |
choices=['azurefunction', |
| 82 |
'eventhub', |
| 83 |
'hybridconnection', |
| 84 |
'servicebusqueue', |
| 85 |
'servicebustopic', |
| 86 |
'storagequeue', |
| 87 |
'webhook'], |
| 88 |
default='webhook', |
| 89 |
), |
| 90 |
event_delivery_schema=dict( |
| 91 |
type='str', |
| 92 |
choices=['CloudEventSchemaV1_0', 'CustomInputSchema', 'EventGridSchema'], |
| 93 |
required=False, |
| 94 |
), |
| 95 |
event_ttl=dict( |
| 96 |
type='int', |
| 97 |
default=1440, |
| 98 |
), |
| 99 |
expiration_date=dict( |
| 100 |
type='str', |
| 101 |
required=False, |
| 102 |
), |
| 103 |
labels=dict( |
| 104 |
type='list', |
| 105 |
elements='str', |
| 106 |
required=False, |
| 107 |
), |
| 108 |
max_delivery_attempts=dict( |
| 109 |
type='int', |
| 110 |
default=30, |
| 111 |
required=False, |
| 112 |
), |
| 113 |
max_events_per_batch=dict( |
| 114 |
type='int', |
| 115 |
required=False, |
| 116 |
), |
| 117 |
preferred_batch_size_in_kilobytes=dict( |
| 118 |
type='int', |
| 119 |
required=False, |
| 120 |
), |
| 121 |
storage_queue_msg_ttl=dict( |
| 122 |
type='int', |
| 123 |
required=False, |
| 124 |
), |
| 125 |
advanced_filter=dict( |
| 126 |
type='list', |
| 127 |
elements='str', |
| 128 |
required=False, |
| 129 |
), |
| 130 |
enable_advanced_filtering_on_arrays=dict( |
| 131 |
type='bool', |
| 132 |
required=False, |
| 133 |
), |
| 134 |
included_event_types=dict( |
| 135 |
type='list', |
| 136 |
elements='str', |
| 137 |
required=False, |
| 138 |
), |
| 139 |
subject_begins_with=dict( |
| 140 |
type='str', |
| 141 |
required=False, |
| 142 |
), |
| 143 |
subject_case_sensitive=dict( |
| 144 |
type='bool', |
| 145 |
required=False, |
| 146 |
), |
| 147 |
subject_ends_with=dict( |
| 148 |
type='str', |
| 149 |
required=False, |
| 150 |
), |
| 151 |
) |
| 152 |
|
| 153 |
|
| 154 |
def _validate_retry_policy(max_delivery_attempts, event_ttl): |
| 155 |
if max_delivery_attempts < 1 or max_delivery_attempts > 30: |
| 156 |
raise ConfigError('max_delivery_attempts should be a number between 1 and 30.') |
| 157 |
|
| 158 |
if event_ttl < 1 or event_ttl > 1440: |
| 159 |
raise ConfigError('event_ttl should be a number between 1 and 1440.') |
| 160 |
|
| 161 |
|
| 162 |
def _validate_destination_attribute(endpoint_type, storage_queue_msg_ttl=None, delivery_attribute_mapping=None): |
| 163 |
isStorageQueueDestination = endpoint_type is not None and endpoint_type.lower() == STORAGEQUEUE_DESTINATION.lower() |
| 164 |
|
| 165 |
if not isStorageQueueDestination and storage_queue_msg_ttl is not None: |
| 166 |
raise ConfigError('usage error: storage_queue_msg_ttl is only applicable for endpoint type StorageQueue.') |
| 167 |
|
| 168 |
if isStorageQueueDestination and delivery_attribute_mapping is not None: |
| 169 |
raise ConfigError('usage error: delivery_attribute_mapping is not applicable for endpoint type StorageQueue.') |
| 170 |
|
| 171 |
|
| 172 |
def _get_storage_queue_destination(endpoint, storage_queue_msg_ttl): |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
queue_items = re.split( |
| 178 |
"/queueServices/default/queues/", endpoint, flags=re.IGNORECASE) |
| 179 |
|
| 180 |
if len(queue_items) != 2 or queue_items[0] is None or queue_items[1] is None: |
| 181 |
raise ConfigError('Argument Error: Expected format of endpoint for storage queue is:' + |
| 182 |
'/subscriptions/id/resourceGroups/rg/providers/Microsoft.Storage/' + |
| 183 |
'storageAccounts/sa1/queueServices/default/queues/queueName') |
| 184 |
|
| 185 |
if storage_queue_msg_ttl is not None: |
| 186 |
destination = StorageQueueEventSubscriptionDestination( |
| 187 |
resource_id=queue_items[0], |
| 188 |
queue_name=queue_items[1], |
| 189 |
queue_message_time_to_live_in_seconds=storage_queue_msg_ttl) |
| 190 |
else: |
| 191 |
destination = StorageQueueEventSubscriptionDestination( |
| 192 |
resource_id=queue_items[0], |
| 193 |
queue_name=queue_items[1]) |
| 194 |
return destination |
| 195 |
|
| 196 |
|
| 197 |
def _get_endpoint_destination( |
| 198 |
endpoint_type, |
| 199 |
endpoint, |
| 200 |
max_events_per_batch, |
| 201 |
preferred_batch_size_in_kilobytes, |
| 202 |
azure_active_directory_tenant_id, |
| 203 |
azure_active_directory_application_id_or_uri, |
| 204 |
storage_queue_msg_ttl, |
| 205 |
delivery_attribute_mapping): |
| 206 |
|
| 207 |
if endpoint_type.lower() == WEBHOOK_DESTINATION.lower(): |
| 208 |
destination = WebHookEventSubscriptionDestination( |
| 209 |
endpoint_url=endpoint, |
| 210 |
max_events_per_batch=max_events_per_batch, |
| 211 |
preferred_batch_size_in_kilobytes=preferred_batch_size_in_kilobytes, |
| 212 |
azure_active_directory_tenant_id=azure_active_directory_tenant_id, |
| 213 |
azure_active_directory_application_id_or_uri=azure_active_directory_application_id_or_uri, |
| 214 |
delivery_attribute_mappings=delivery_attribute_mapping) |
| 215 |
elif endpoint_type.lower() == EVENTHUB_DESTINATION.lower(): |
| 216 |
destination = EventHubEventSubscriptionDestination( |
| 217 |
resource_id=endpoint, |
| 218 |
delivery_attribute_mappings=delivery_attribute_mapping) |
| 219 |
elif endpoint_type.lower() == HYBRIDCONNECTION_DESTINATION.lower(): |
| 220 |
destination = HybridConnectionEventSubscriptionDestination( |
| 221 |
resource_id=endpoint, |
| 222 |
delivery_attribute_mappings=delivery_attribute_mapping) |
| 223 |
elif endpoint_type.lower() == STORAGEQUEUE_DESTINATION.lower(): |
| 224 |
destination = _get_storage_queue_destination(endpoint, storage_queue_msg_ttl) |
| 225 |
elif endpoint_type.lower() == SERVICEBUSQUEUE_DESTINATION.lower(): |
| 226 |
destination = ServiceBusQueueEventSubscriptionDestination( |
| 227 |
resource_id=endpoint, |
| 228 |
delivery_attribute_mappings=delivery_attribute_mapping) |
| 229 |
elif endpoint_type.lower() == SERVICEBUSTOPIC_DESTINATION.lower(): |
| 230 |
destination = ServiceBusTopicEventSubscriptionDestination( |
| 231 |
resource_id=endpoint, |
| 232 |
delivery_attribute_mappings=delivery_attribute_mapping) |
| 233 |
elif endpoint_type.lower() == AZUREFUNCTION_DESTINATION.lower(): |
| 234 |
destination = AzureFunctionEventSubscriptionDestination( |
| 235 |
resource_id=endpoint, |
| 236 |
max_events_per_batch=max_events_per_batch, |
| 237 |
preferred_batch_size_in_kilobytes=preferred_batch_size_in_kilobytes, |
| 238 |
delivery_attribute_mappings=delivery_attribute_mapping) |
| 239 |
return destination |
| 240 |
|
| 241 |
|
| 242 |
def _get_event_subscription_identity_type(identity_type_name): |
| 243 |
result = None |
| 244 |
if identity_type_name.lower() == IDENTITY_SYSTEM_ASSIGNED.lower(): |
| 245 |
result = IDENTITY_SYSTEM_ASSIGNED |
| 246 |
|
| 247 |
return result |
| 248 |
|
| 249 |
|
| 250 |
def _get_deadletter_destination(deadletter_endpoint): |
| 251 |
blob_items = re.split( |
| 252 |
"/blobServices/default/containers/", deadletter_endpoint, flags=re.IGNORECASE) |
| 253 |
|
| 254 |
if len(blob_items) != 2 or blob_items[0] is None or blob_items[1] is None: |
| 255 |
raise ConfigError('Argument Error: Expected format of deadletter_endpoint is:' + |
| 256 |
'/subscriptions/id/resourceGroups/rg/providers/Microsoft.Storage/' + |
| 257 |
'storageAccounts/sa1/blobServices/default/containers/containerName') |
| 258 |
|
| 259 |
return StorageBlobDeadLetterDestination(resource_id=blob_items[0], blob_container_name=blob_items[1]) |
| 260 |
|
| 261 |
|
| 262 |
def _get_tenant_id(destination, destination_with_resource_identity): |
| 263 |
tenant_id = None |
| 264 |
|
| 265 |
if destination is not None and hasattr(destination, 'azure_active_directory_tenant_id'): |
| 266 |
tenant_id = destination.azure_active_directory_tenant_id |
| 267 |
elif destination_with_resource_identity is not None and hasattr(destination_with_resource_identity, 'azure_active_directory_tenant_id'): |
| 268 |
tenant_id = destination_with_resource_identity.azure_active_directory_tenant_id |
| 269 |
|
| 270 |
return tenant_id |
| 271 |
|
| 272 |
|
| 273 |
def _get_application_id(destination, destination_with_resource_identity): |
| 274 |
application_id = None |
| 275 |
|
| 276 |
if destination is not None and hasattr(destination, 'azure_active_directory_application_id_or_uri'): |
| 277 |
application_id = destination.azure_active_directory_application_id_or_uri |
| 278 |
elif destination_with_resource_identity is not None and hasattr(destination_with_resource_identity, 'azure_active_directory_application_id_or_uri'): |
| 279 |
application_id = destination_with_resource_identity.azure_active_directory_application_id_or_uri |
| 280 |
return application_id |
| 281 |
|
| 282 |
|
| 283 |
def _set_event_subscription_destination( |
| 284 |
destination, |
| 285 |
storage_queue_msg_ttl=None, |
| 286 |
delivery_attribute_mapping=None): |
| 287 |
|
| 288 |
endpoint_type = destination.endpoint_type |
| 289 |
if endpoint_type.lower() == WEBHOOK_DESTINATION.lower(): |
| 290 |
if delivery_attribute_mapping is not None: |
| 291 |
destination.delivery_attribute_mappings = delivery_attribute_mapping |
| 292 |
elif endpoint_type.lower() == EVENTHUB_DESTINATION.lower(): |
| 293 |
if delivery_attribute_mapping is not None: |
| 294 |
destination.delivery_attribute_mappings = delivery_attribute_mapping |
| 295 |
elif endpoint_type.lower() == HYBRIDCONNECTION_DESTINATION.lower(): |
| 296 |
if delivery_attribute_mapping is not None: |
| 297 |
destination.delivery_attribute_mappings = delivery_attribute_mapping |
| 298 |
elif endpoint_type.lower() == STORAGEQUEUE_DESTINATION.lower(): |
| 299 |
if storage_queue_msg_ttl is not None: |
| 300 |
destination.queue_message_time_to_live_in_seconds = storage_queue_msg_ttl |
| 301 |
elif endpoint_type.lower() == SERVICEBUSQUEUE_DESTINATION.lower(): |
| 302 |
if delivery_attribute_mapping is not None: |
| 303 |
destination.delivery_attribute_mappings = delivery_attribute_mapping |
| 304 |
elif endpoint_type.lower() == SERVICEBUSTOPIC_DESTINATION.lower(): |
| 305 |
if delivery_attribute_mapping is not None: |
| 306 |
destination.delivery_attribute_mappings = delivery_attribute_mapping |
| 307 |
elif endpoint_type.lower() == AZUREFUNCTION_DESTINATION.lower(): |
| 308 |
if delivery_attribute_mapping is not None: |
| 309 |
destination.delivery_attribute_mappings = delivery_attribute_mapping |
| 310 |
return destination |
| 311 |
|
| 312 |
|
| 313 |
def _validate_and_update_destination(endpoint_type, destination, storage_queue_msg_ttl, delivery_attribute_mapping): |
| 314 |
_validate_destination_attribute( |
| 315 |
endpoint_type, |
| 316 |
storage_queue_msg_ttl, |
| 317 |
delivery_attribute_mapping) |
| 318 |
|
| 319 |
_set_event_subscription_destination( |
| 320 |
destination, |
| 321 |
storage_queue_msg_ttl, |
| 322 |
delivery_attribute_mapping) |
| 323 |
|
| 324 |
|
| 325 |
def _update_event_subscription_filter( |
| 326 |
event_subscription_filter, |
| 327 |
subject_begins_with=None, |
| 328 |
subject_ends_with=None, |
| 329 |
included_event_types=None, |
| 330 |
enable_advanced_filtering_on_arrays=None, |
| 331 |
advanced_filter=None): |
| 332 |
|
| 333 |
if subject_begins_with is not None: |
| 334 |
event_subscription_filter.subject_begins_with = subject_begins_with |
| 335 |
|
| 336 |
if subject_ends_with is not None: |
| 337 |
event_subscription_filter.subject_ends_with = subject_ends_with |
| 338 |
|
| 339 |
if included_event_types is not None: |
| 340 |
event_subscription_filter.included_event_types = included_event_types |
| 341 |
|
| 342 |
if enable_advanced_filtering_on_arrays is not None: |
| 343 |
event_subscription_filter.enable_advanced_filtering_on_arrays = enable_advanced_filtering_on_arrays |
| 344 |
|
| 345 |
if advanced_filter is not None: |
| 346 |
event_subscription_filter.advanced_filters = advanced_filter |
| 347 |
|
| 348 |
|
| 349 |
def get_event_subscription_info(endpoint=None, |
| 350 |
endpoint_type=WEBHOOK_DESTINATION, |
| 351 |
included_event_types=None, |
| 352 |
subject_begins_with=None, |
| 353 |
subject_ends_with=None, |
| 354 |
is_subject_case_sensitive=False, |
| 355 |
max_delivery_attempts=30, |
| 356 |
event_ttl=1440, |
| 357 |
max_events_per_batch=None, |
| 358 |
preferred_batch_size_in_kilobytes=None, |
| 359 |
event_delivery_schema=None, |
| 360 |
deadletter_endpoint=None, |
| 361 |
labels=None, |
| 362 |
expiration_date=None, |
| 363 |
advanced_filter=None, |
| 364 |
azure_active_directory_tenant_id=None, |
| 365 |
azure_active_directory_application_id_or_uri=None, |
| 366 |
delivery_identity=None, |
| 367 |
delivery_identity_endpoint=None, |
| 368 |
delivery_identity_endpoint_type=None, |
| 369 |
deadletter_identity=None, |
| 370 |
deadletter_identity_endpoint=None, |
| 371 |
storage_queue_msg_ttl=None, |
| 372 |
enable_advanced_filtering_on_arrays=None, |
| 373 |
delivery_attribute_mapping=None): |
| 374 |
|
| 375 |
normalized_endpoint_type = endpoint_type.lower() |
| 376 |
normalized_webhook_destination = WEBHOOK_DESTINATION.lower() |
| 377 |
normalized_azure_function_destination = AZUREFUNCTION_DESTINATION.lower() |
| 378 |
|
| 379 |
if deadletter_endpoint is not None and deadletter_identity_endpoint is not None: |
| 380 |
raise ConfigError('usage error: either deadletter_endpoint or deadletter_identity_endpoint ' |
| 381 |
'should be specified at one time, not both') |
| 382 |
|
| 383 |
if included_event_types is not None and len(included_event_types) == 1 and included_event_types[0].lower() == 'all': |
| 384 |
raise ConfigError('The usage of \"All\" for included_event_types is not allowed starting from Azure Event Grid' |
| 385 |
' API Version 2019-02-01-preview.') |
| 386 |
|
| 387 |
|
| 388 |
_validate_retry_policy(max_delivery_attempts, event_ttl) |
| 389 |
retry_policy = RetryPolicy(max_delivery_attempts=max_delivery_attempts, event_time_to_live_in_minutes=event_ttl) |
| 390 |
|
| 391 |
if max_events_per_batch is not None: |
| 392 |
if normalized_endpoint_type not in (normalized_webhook_destination, normalized_azure_function_destination): |
| 393 |
raise ConfigError('usage error: max_events_per_batch is applicable only for ' |
| 394 |
'endpoint types WebHook and AzureFunction.') |
| 395 |
if max_events_per_batch > 5000: |
| 396 |
raise ConfigError('usage error: max_events_per_batch must be a number between 1 and 5000.') |
| 397 |
|
| 398 |
if preferred_batch_size_in_kilobytes is not None: |
| 399 |
if normalized_endpoint_type not in (normalized_webhook_destination, normalized_azure_function_destination): |
| 400 |
raise ConfigError('usage error: preferred_batch_size_in_kilobytes is applicable only for ' |
| 401 |
'endpoint types WebHook and AzureFunction.') |
| 402 |
if preferred_batch_size_in_kilobytes > 1024: |
| 403 |
raise ConfigError('usage error: preferred_batch_size_in_kilobytes must be a number ' |
| 404 |
'between 1 and 1024.') |
| 405 |
|
| 406 |
if azure_active_directory_tenant_id is not None: |
| 407 |
if normalized_endpoint_type != normalized_webhook_destination: |
| 408 |
raise ConfigError('usage error: azure_active_directory_tenant_id is applicable only for ' |
| 409 |
'endpoint types WebHook.') |
| 410 |
if azure_active_directory_application_id_or_uri is None: |
| 411 |
raise ConfigError('usage error: azure_active_directory_application_id_or_uri is missing. ' |
| 412 |
'It should include an Azure Active Directory Application Id or Uri.') |
| 413 |
|
| 414 |
if azure_active_directory_application_id_or_uri is not None: |
| 415 |
if normalized_endpoint_type != normalized_webhook_destination: |
| 416 |
raise ConfigError('usage error: azure_active_directory_application_id_or_uri is applicable only for ' |
| 417 |
'endpoint types WebHook.') |
| 418 |
if azure_active_directory_tenant_id is None: |
| 419 |
raise ConfigError('usage error: azure_active_directory_tenant_id is missing. ' |
| 420 |
'It should include an Azure Active Directory Tenant Id.') |
| 421 |
|
| 422 |
condition1 = deadletter_identity is not None and deadletter_identity_endpoint is None |
| 423 |
condition2 = deadletter_identity is None and deadletter_identity_endpoint is not None |
| 424 |
if condition1 or condition2: |
| 425 |
raise ConfigError('usage error: one or more deadletter identity information is missing. If ' |
| 426 |
'deadletter_identity is specified, deadletter_identity_endpoint should be specified.') |
| 427 |
|
| 428 |
tennant_id = None |
| 429 |
application_id = None |
| 430 |
|
| 431 |
condition1 = endpoint_type is not None and normalized_endpoint_type == normalized_webhook_destination |
| 432 |
condition2 = delivery_identity_endpoint_type is not None and \ |
| 433 |
delivery_identity_endpoint_type.lower() == normalized_webhook_destination |
| 434 |
if condition1 or condition2: |
| 435 |
tennant_id = azure_active_directory_tenant_id |
| 436 |
application_id = azure_active_directory_application_id_or_uri |
| 437 |
|
| 438 |
destination = None |
| 439 |
delivery_with_resource_identity = None |
| 440 |
if endpoint is not None: |
| 441 |
_validate_destination_attribute(endpoint_type, storage_queue_msg_ttl, delivery_attribute_mapping) |
| 442 |
destination = _get_endpoint_destination( |
| 443 |
endpoint_type, |
| 444 |
endpoint, |
| 445 |
max_events_per_batch, |
| 446 |
preferred_batch_size_in_kilobytes, |
| 447 |
tennant_id, |
| 448 |
application_id, |
| 449 |
storage_queue_msg_ttl, |
| 450 |
delivery_attribute_mapping) |
| 451 |
elif delivery_identity_endpoint is not None: |
| 452 |
identity_type_name = _get_event_subscription_identity_type(delivery_identity) |
| 453 |
delivery_identity_info = EventSubscriptionIdentity(type=identity_type_name) |
| 454 |
_validate_destination_attribute( |
| 455 |
delivery_identity_endpoint_type, |
| 456 |
storage_queue_msg_ttl, |
| 457 |
delivery_attribute_mapping) |
| 458 |
destination_with_identity = _get_endpoint_destination( |
| 459 |
delivery_identity_endpoint_type, |
| 460 |
delivery_identity_endpoint, |
| 461 |
max_events_per_batch, |
| 462 |
preferred_batch_size_in_kilobytes, |
| 463 |
tennant_id, |
| 464 |
application_id, |
| 465 |
storage_queue_msg_ttl, |
| 466 |
delivery_attribute_mapping) |
| 467 |
delivery_with_resource_identity = DeliveryWithResourceIdentity( |
| 468 |
identity=delivery_identity_info, |
| 469 |
destination=destination_with_identity) |
| 470 |
|
| 471 |
event_subscription_filter = EventSubscriptionFilter( |
| 472 |
subject_begins_with=subject_begins_with, |
| 473 |
subject_ends_with=subject_ends_with, |
| 474 |
included_event_types=included_event_types, |
| 475 |
is_subject_case_sensitive=is_subject_case_sensitive, |
| 476 |
enable_advanced_filtering_on_arrays=enable_advanced_filtering_on_arrays, |
| 477 |
advanced_filters=advanced_filter) |
| 478 |
|
| 479 |
deadletter_destination = None |
| 480 |
if deadletter_endpoint is not None: |
| 481 |
deadletter_destination = _get_deadletter_destination(deadletter_endpoint) |
| 482 |
|
| 483 |
deadletter_with_resource_identity = None |
| 484 |
|
| 485 |
if deadletter_identity_endpoint is not None: |
| 486 |
deadletter_destination_with_identity = _get_deadletter_destination(deadletter_identity_endpoint) |
| 487 |
deadletter_identity_type_name = _get_event_subscription_identity_type(deadletter_identity) |
| 488 |
deadletter_delivery_identity_info = EventSubscriptionIdentity(type=deadletter_identity_type_name) |
| 489 |
deadletter_with_resource_identity = DeadLetterWithResourceIdentity( |
| 490 |
identity=deadletter_delivery_identity_info, |
| 491 |
dead_letter_destination=deadletter_destination_with_identity) |
| 492 |
|
| 493 |
if expiration_date is not None: |
| 494 |
expiration_date = parse(expiration_date) |
| 495 |
|
| 496 |
event_subscription_info = EventSubscription( |
| 497 |
destination=destination, |
| 498 |
filter=event_subscription_filter, |
| 499 |
labels=labels, |
| 500 |
event_delivery_schema=event_delivery_schema, |
| 501 |
retry_policy=retry_policy, |
| 502 |
expiration_time_utc=expiration_date, |
| 503 |
dead_letter_destination=deadletter_destination, |
| 504 |
delivery_with_resource_identity=delivery_with_resource_identity, |
| 505 |
dead_letter_with_resource_identity=deadletter_with_resource_identity) |
| 506 |
|
| 507 |
return event_subscription_info |
| 508 |
|
| 509 |
|
| 510 |
def update_event_subscription_internal(instance, |
| 511 |
endpoint=None, |
| 512 |
endpoint_type=WEBHOOK_DESTINATION, |
| 513 |
subject_begins_with=None, |
| 514 |
subject_ends_with=None, |
| 515 |
included_event_types=None, |
| 516 |
advanced_filter=None, |
| 517 |
labels=None, |
| 518 |
deadletter_endpoint=None, |
| 519 |
delivery_identity=None, |
| 520 |
delivery_identity_endpoint=None, |
| 521 |
delivery_identity_endpoint_type=None, |
| 522 |
deadletter_identity=None, |
| 523 |
deadletter_identity_endpoint=None, |
| 524 |
storage_queue_msg_ttl=None, |
| 525 |
enable_advanced_filtering_on_arrays=None, |
| 526 |
delivery_attribute_mapping=None): |
| 527 |
|
| 528 |
if (endpoint_type is not None and |
| 529 |
endpoint_type.lower() != WEBHOOK_DESTINATION.lower() and |
| 530 |
endpoint is None): |
| 531 |
raise ConfigError('Invalid usage: Since endpoint_type is specified, a valid endpoint must also be specified.') |
| 532 |
|
| 533 |
current_destination = instance.destination |
| 534 |
current_filter = instance.filter |
| 535 |
current_event_delivery_schema = instance.event_delivery_schema |
| 536 |
current_retry_policy = instance.retry_policy |
| 537 |
current_destination_with_resource_identity = None |
| 538 |
current_destination2 = None |
| 539 |
|
| 540 |
if instance.delivery_with_resource_identity is not None: |
| 541 |
current_destination2 = instance.delivery_with_resource_identity |
| 542 |
current_destination_with_resource_identity = instance.delivery_with_resource_identity.destination |
| 543 |
|
| 544 |
tenant_id = _get_tenant_id(current_destination, current_destination_with_resource_identity) |
| 545 |
application_id = _get_application_id(current_destination, current_destination_with_resource_identity) |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
current_max_events_per_batch = 0 |
| 550 |
current_preferred_batch_size_in_kilobytes = 0 |
| 551 |
|
| 552 |
if current_destination is not None and (current_destination.endpoint_type.lower() == WEBHOOK_DESTINATION.lower() or current_destination.endpoint_type.lower() == AZUREFUNCTION_DESTINATION.lower()): |
| 553 |
current_max_events_per_batch = current_destination.max_events_per_batch |
| 554 |
current_preferred_batch_size_in_kilobytes = current_destination.preferred_batch_size_in_kilobytes |
| 555 |
elif current_destination_with_resource_identity is not None and (current_destination_with_resource_identity.endpoint_type.lower() == WEBHOOK_DESTINATION.lower() or current_destination_with_resource_identity.endpoint_type.lower() == AZUREFUNCTION_DESTINATION.lower()): |
| 556 |
current_max_events_per_batch = current_destination_with_resource_identity.max_events_per_batch |
| 557 |
current_preferred_batch_size_in_kilobytes = current_destination_with_resource_identity.preferred_batch_size_in_kilobytes |
| 558 |
|
| 559 |
updated_destination = None |
| 560 |
updated_delivery_with_resource_identity = None |
| 561 |
|
| 562 |
|
| 563 |
if endpoint is None and delivery_identity_endpoint is None: |
| 564 |
if current_destination is not None: |
| 565 |
_validate_and_update_destination( |
| 566 |
current_destination.endpoint_type, |
| 567 |
current_destination, |
| 568 |
storage_queue_msg_ttl, |
| 569 |
delivery_attribute_mapping) |
| 570 |
updated_destination = current_destination |
| 571 |
elif current_destination_with_resource_identity is not None: |
| 572 |
_validate_and_update_destination( |
| 573 |
current_destination_with_resource_identity.endpoint_type, |
| 574 |
current_destination_with_resource_identity, |
| 575 |
storage_queue_msg_ttl, |
| 576 |
delivery_attribute_mapping) |
| 577 |
updated_delivery_with_resource_identity = current_destination2 |
| 578 |
elif endpoint is not None: |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
if endpoint_type is None: |
| 583 |
endpoint_type = current_destination.endpoint_type |
| 584 |
|
| 585 |
_validate_destination_attribute( |
| 586 |
endpoint_type, |
| 587 |
storage_queue_msg_ttl, |
| 588 |
delivery_attribute_mapping) |
| 589 |
updated_destination = _get_endpoint_destination( |
| 590 |
endpoint_type, |
| 591 |
endpoint, |
| 592 |
current_max_events_per_batch, |
| 593 |
current_preferred_batch_size_in_kilobytes, |
| 594 |
tenant_id, |
| 595 |
application_id, |
| 596 |
storage_queue_msg_ttl, |
| 597 |
delivery_attribute_mapping) |
| 598 |
elif delivery_identity_endpoint is not None: |
| 599 |
destination_with_identity = _get_endpoint_destination( |
| 600 |
delivery_identity_endpoint_type, |
| 601 |
delivery_identity_endpoint, |
| 602 |
0, |
| 603 |
0, |
| 604 |
tenant_id, |
| 605 |
application_id, |
| 606 |
storage_queue_msg_ttl, |
| 607 |
delivery_attribute_mapping) |
| 608 |
|
| 609 |
identity_type_name = _get_event_subscription_identity_type(delivery_identity) |
| 610 |
delivery_identity_info = EventSubscriptionIdentity(type=identity_type_name) |
| 611 |
|
| 612 |
updated_delivery_with_resource_identity = DeliveryWithResourceIdentity( |
| 613 |
identity=delivery_identity_info, |
| 614 |
destination=destination_with_identity) |
| 615 |
|
| 616 |
updated_deadletter_destination = None |
| 617 |
if deadletter_endpoint is not None: |
| 618 |
updated_deadletter_destination = _get_deadletter_destination(deadletter_endpoint) |
| 619 |
|
| 620 |
updated_deadletter_with_resource_identity = None |
| 621 |
if deadletter_identity_endpoint is not None: |
| 622 |
deadletter_destination_with_identity = _get_deadletter_destination(deadletter_identity_endpoint) |
| 623 |
deadletter_identity_type_name = _get_event_subscription_identity_type(deadletter_identity) |
| 624 |
deadletter_delivery_identity_info = EventSubscriptionIdentity(type=deadletter_identity_type_name) |
| 625 |
updated_deadletter_with_resource_identity = DeadLetterWithResourceIdentity( |
| 626 |
identity=deadletter_delivery_identity_info, |
| 627 |
dead_letter_destination=deadletter_destination_with_identity) |
| 628 |
|
| 629 |
_update_event_subscription_filter( |
| 630 |
current_filter, |
| 631 |
subject_begins_with, |
| 632 |
subject_ends_with, |
| 633 |
included_event_types, |
| 634 |
enable_advanced_filtering_on_arrays, |
| 635 |
advanced_filter) |
| 636 |
updated_filter = current_filter |
| 637 |
|
| 638 |
updated_labels = None |
| 639 |
if labels is not None: |
| 640 |
updated_labels = labels |
| 641 |
|
| 642 |
params = EventSubscriptionUpdateParameters( |
| 643 |
destination=updated_destination, |
| 644 |
filter=updated_filter, |
| 645 |
labels=updated_labels, |
| 646 |
retry_policy=current_retry_policy, |
| 647 |
dead_letter_destination=updated_deadletter_destination, |
| 648 |
event_delivery_schema=current_event_delivery_schema, |
| 649 |
delivery_with_resource_identity=updated_delivery_with_resource_identity, |
| 650 |
dead_letter_with_resource_identity=updated_deadletter_with_resource_identity) |
| 651 |
|
| 652 |
return params |