def create_eventgrid(self) -> None: logger.info("creating eventgrid subscription") src_resource_id = self.results["deploy"]["fuzz-storage"]["value"] dst_resource_id = self.results["deploy"]["func-storage"]["value"] client = get_client_from_cli_profile( StorageManagementClient, subscription_id=self.get_subscription_id()) event_subscription_info = EventSubscription( destination=StorageQueueEventSubscriptionDestination( resource_id=dst_resource_id, queue_name="file-changes"), filter=EventSubscriptionFilter(included_event_types=[ "Microsoft.Storage.BlobCreated", "Microsoft.Storage.BlobDeleted", ]), retry_policy=RetryPolicy( max_delivery_attempts=30, event_time_to_live_in_minutes=1440, ), ) client = get_client_from_cli_profile( EventGridManagementClient, subscription_id=self.get_subscription_id()) result = client.event_subscriptions.create_or_update( src_resource_id, "onefuzz1", event_subscription_info).result() if result.provisioning_state != "Succeeded": raise Exception( "eventgrid subscription failed: %s" % json.dumps(result.as_dict(), indent=4, sort_keys=True), )
def cli_eventgrid_event_subscription_create( # pylint: disable=too-many-locals cmd, client, event_subscription_name, endpoint, resource_id=None, source_resource_id=None, resource_group_name=None, topic_name=None, endpoint_type=WEBHOOK_DESTINATION, included_event_types=None, subject_begins_with=None, subject_ends_with=None, is_subject_case_sensitive=False, max_delivery_attempts=30, event_ttl=1440, deadletter_endpoint=None, labels=None): scope = _get_scope_for_event_subscription( cli_ctx=cmd.cli_ctx, source_resource_id=source_resource_id, resource_id=resource_id, topic_name=topic_name, resource_group_name=resource_group_name) # Construct RetryPolicy based on max_delivery_attempts and event_ttl max_delivery_attempts = int(max_delivery_attempts) event_ttl = int(event_ttl) _validate_retry_policy(max_delivery_attempts, event_ttl) retry_policy = RetryPolicy(max_delivery_attempts=max_delivery_attempts, event_time_to_live_in_minutes=event_ttl) destination = _get_endpoint_destination(endpoint_type, endpoint) event_subscription_filter = EventSubscriptionFilter( subject_begins_with=subject_begins_with, subject_ends_with=subject_ends_with, included_event_types=included_event_types, is_subject_case_sensitive=is_subject_case_sensitive) deadletter_destination = None if deadletter_endpoint is not None: deadletter_destination = _get_deadletter_destination(deadletter_endpoint) event_subscription_info = EventSubscription( destination=destination, filter=event_subscription_filter, labels=labels, retry_policy=retry_policy, dead_letter_destination=deadletter_destination) _warn_if_manual_handshake_needed(endpoint_type, endpoint) return client.create_or_update( scope, event_subscription_name, event_subscription_info).result()
def test_input_mappings_and_queue_destination(self, resource_group, location): topic_name = "kalspython2" eventsubscription_name = "kalspythonEventSubscription3" input_schema = InputSchema.cloud_event_v01_schema # Create a new topic and verify that it is created successfully topic = Topic(location="westcentralus", tags=None, input_schema=input_schema, input_schema_mapping=None) topic_result_create = self.eventgrid_client.topics.create_or_update( resource_group.name, topic_name, topic) topic = topic_result_create.result() self.assertEqual(topic.name, topic_name) self.assertEqual(topic.input_schema, "CloudEventV01Schema") # Create a new event subscription to this topic # Use this for recording mode # scope = "/subscriptions/55f3dcd4-cac7-43b4-990b-a139d62a1eb2/resourceGroups/" + resource_group.name + "/providers/Microsoft.EventGrid/topics/" + topic_name scope = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/" + resource_group.name + "/providers/Microsoft.EventGrid/topics/" + topic_name destination = StorageQueueEventSubscriptionDestination( resource_id= "/subscriptions/55f3dcd4-cac7-43b4-990b-a139d62a1eb2/resourceGroups/kalstest/providers/Microsoft.Storage/storageAccounts/kalsdemo", queue_name="kalsdemoqueue") deadletter_destination = StorageBlobDeadLetterDestination( resource_id= "/subscriptions/55f3dcd4-cac7-43b4-990b-a139d62a1eb2/resourceGroups/kalstest/providers/Microsoft.Storage/storageAccounts/kalsdemo", blob_container_name="dlq") filter = EventSubscriptionFilter() event_subscription_info = EventSubscription( destination=destination, filter=filter, dead_letter_destination=deadletter_destination, event_delivery_schema=EventDeliverySchema.cloud_event_v01_schema, retry_policy=RetryPolicy(event_time_to_live_in_minutes=5, max_delivery_attempts=10)) es_result_create = self.eventgrid_client.event_subscriptions.create_or_update( scope, eventsubscription_name, event_subscription_info) event_subscription = es_result_create.result() self.assertEqual(eventsubscription_name, event_subscription.name) # Delete the event subscription self.eventgrid_client.event_subscriptions.delete( scope, eventsubscription_name).wait() # Delete the topic self.eventgrid_client.topics.delete(resource_group.name, topic_name).wait()
def cli_eventgrid_event_subscription_create( # pylint: disable=too-many-locals cmd, client, event_subscription_name, endpoint, resource_id=None, source_resource_id=None, resource_group_name=None, topic_name=None, endpoint_type=WEBHOOK_DESTINATION, included_event_types=None, subject_begins_with=None, subject_ends_with=None, is_subject_case_sensitive=False, max_delivery_attempts=30, event_ttl=1440, deadletter_endpoint=None, labels=None, expiration_date=None, advanced_filter=None): if included_event_types is not None and len( included_event_types) == 1 and included_event_types[0].lower( ) == 'all': logger.warning( 'The usage of \"All\" for --included-event-types is not allowed starting from Azure Event Grid' ' API Version 2019-02-01-preview. However, the call here is still permitted by replacing' ' \"All\" with None in order to return all the event types (for the custom topics and' ' domains case) or default event types (for other topic types case). In any future calls,' ' please consider leaving --included-event-types unspecified or use None instead.' ) included_event_types = None scope = _get_scope_for_event_subscription( cli_ctx=cmd.cli_ctx, source_resource_id=source_resource_id, resource_id=resource_id, topic_name=topic_name, resource_group_name=resource_group_name) # Construct RetryPolicy based on max_delivery_attempts and event_ttl max_delivery_attempts = int(max_delivery_attempts) event_ttl = int(event_ttl) _validate_retry_policy(max_delivery_attempts, event_ttl) retry_policy = RetryPolicy(max_delivery_attempts=max_delivery_attempts, event_time_to_live_in_minutes=event_ttl) destination = _get_endpoint_destination(endpoint_type, endpoint) event_subscription_filter = EventSubscriptionFilter( subject_begins_with=subject_begins_with, subject_ends_with=subject_ends_with, included_event_types=included_event_types, is_subject_case_sensitive=is_subject_case_sensitive, advanced_filters=advanced_filter) deadletter_destination = None if deadletter_endpoint is not None: deadletter_destination = _get_deadletter_destination( deadletter_endpoint) if expiration_date is not None: expiration_date = parse(expiration_date) event_subscription_info = EventSubscription( destination=destination, filter=event_subscription_filter, labels=labels, retry_policy=retry_policy, expiration_time_utc=expiration_date, dead_letter_destination=deadletter_destination) _warn_if_manual_handshake_needed(endpoint_type, endpoint) return client.create_or_update(scope, event_subscription_name, event_subscription_info)