def update_event_subscriptions(self): """ Find unique list of all subscribed events and update a single event subscription to channel them to an Azure Queue. """ log.info('Updating event grid subscriptions') destination = \ StorageQueueEventSubscriptionDestination(resource_id=self.queue_storage_account.id, queue_name=self.event_queue_name) # Get total unique event list to use in event subscription policy_items = self.policies.items() events_lists = [ v['policy'].data.get('mode', {}).get('events') for n, v in policy_items ] flat_events = [e for l in events_lists if l for e in l if e] resolved_events = AzureEvents.get_event_operations(flat_events) unique_events = set(resolved_events) # Build event filter strings advance_filter = StringInAdvancedFilter(key='Data.OperationName', values=unique_events) event_filter = EventSubscriptionFilter( advanced_filters=[advance_filter]) # Update event subscription AzureEventSubscription.create(destination, self.event_queue_name, self.session.get_subscription_id(), self.session, event_filter) self.require_event_update = False
def _create_event_subscription(self, storage_account, queue_name, session): self.log.info('Creating event grid subscription') destination = StorageQueueEventSubscriptionDestination( resource_id=storage_account.id, queue_name=queue_name) # filter specific events subscribed_events = AzureEvents.get_event_operations( self.policy.data['mode'].get('events')) advance_filter = StringInAdvancedFilter(key='Data.OperationName', values=subscribed_events) event_filter = EventSubscriptionFilter( advanced_filters=[advance_filter]) for subscription_id in self.target_subscription_ids: try: AzureEventSubscription.create(destination, queue_name, subscription_id, session, event_filter) self.log.info( 'Event grid subscription creation succeeded: subscription_id=%s' % subscription_id) except Exception as e: self.log.error( 'Event Subscription creation failed with error: %s' % e) raise SystemExit
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 createEventGridSubscription(self, eventSubscriptionName, storageAccountId, queueName, subscriptionId): logging.info("Creating new event grid subscription with name: " + eventSubscriptionName + " in subscription: " + subscriptionId) eventGridClient = self.getEventGridManagementClient(subscriptionId) scope = storageAccountId destination = StorageQueueEventSubscriptionDestination( resource_id=storageAccountId, queue_name=queueName) subscriptionFilter = EventSubscriptionFilter( # By default, "All" event types are included subject_begins_with= '/blobServices/default/containers/insights-operational-logs/', included_event_types=['Microsoft.Storage.BlobCreated']) event_subscription_info = EventSubscription(destination=destination, filter=subscriptionFilter) event_subscription_async = eventGridClient.event_subscriptions.create_or_update( scope, eventSubscriptionName, event_subscription_info) event_subscription = event_subscription_async.result() logging.info( "Event grid subscription created successfully with name: " + eventSubscriptionName + " in subscription: " + subscriptionId) self.__log_item(event_subscription)
def setUp(self): super(AzureEventSubscriptionsTest, self).setUp() account = self.setup_account() queue_name = 'cctesteventsub' StorageUtilities.create_queue_from_storage_account(account, queue_name) event_sub_destination = StorageQueueEventSubscriptionDestination( resource_id=account.id, queue_name=queue_name) AzureEventSubscription.create(event_sub_destination, self.event_sub_name)
def test_create_azure_event_subscription(self): account = self.setup_account() queue_name = 'cctestevensub' StorageUtilities.create_queue_from_storage_account(account, queue_name, self.session) sub_destination = StorageQueueEventSubscriptionDestination(resource_id=account.id, queue_name=queue_name) sub_name = 'custodiantestsubscription' event_subscription = AzureEventSubscription.create(sub_destination, sub_name) self.assertEqual(event_subscription.name, sub_name) self.assertEqual(event_subscription.destination.endpoint_type, 'StorageQueue')
def _create_event_subscription(self, storage_account, queue_name, session): self.log.info('Creating event grid subscription') destination = StorageQueueEventSubscriptionDestination(resource_id=storage_account.id, queue_name=queue_name) try: AzureEventSubscription.create(destination, queue_name, session) self.log.info('Event grid subscription creation succeeded') except Exception as e: self.log.error('Event Subscription creation failed with error: %s' % e) raise SystemExit
def test_create_azure_event_subscription(self, create_mock): sub_destination = StorageQueueEventSubscriptionDestination( resource_id="cctestid", queue_name="cctestevensub") sub_name = 'custodiantestsubscription' sub_id = self.session.get_subscription_id() AzureEventSubscription.create(sub_destination, sub_name, sub_id) args = create_mock.mock_calls[0].args self.assertTrue(sub_id in args[0]) self.assertEqual(sub_name, args[1]) self.assertEqual(sub_destination, args[2].destination)
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 _get_storage_queue_destination(endpoint): # Supplied endpoint would be in the following format: # /subscriptions/.../storageAccounts/sa1/queueServices/default/queues/{queueName})) # and we need to break it up into: # /subscriptions/.../storageAccounts/sa1 and queueName queue_items = re.split( "/queueServices/default/queues/", endpoint, flags=re.IGNORECASE) if len(queue_items) != 2 or queue_items[0] is None or queue_items[1] is None: raise CLIError('Argument Error: Expected format of --endpoint for storage queue is:' + '/subscriptions/id/resourceGroups/rg/providers/Microsoft.Storage/' + 'storageAccounts/sa1/queueServices/default/queues/queueName') return StorageQueueEventSubscriptionDestination(resource_id=queue_items[0], queue_name=queue_items[1])
def update_event_subscription(self): """ Create a single event subscription to channel all events to an Azure Queue. """ log.info('Updating event grid subscriptions') destination = StorageQueueEventSubscriptionDestination( resource_id=self.queue_storage_account.id, queue_name=self.event_queue_name) # Build event filter event_filter = EventSubscriptionFilter( included_event_types=['Microsoft.Resources.ResourceWriteSuccess']) # Update event subscription AzureEventSubscription.create(destination, self.event_queue_name, self.session.get_subscription_id(), self.session, event_filter)
def run_example(): """Resource Group management example.""" # # Create the Resource Manager Client with an Application (service principal) token provider # subscription_id = os.environ.get( 'AZURE_SUBSCRIPTION_ID', '11111111-1111-1111-1111-111111111111') # your Azure Subscription Id credentials = ServicePrincipalCredentials( client_id=os.environ['AZURE_CLIENT_ID'], secret=os.environ['AZURE_CLIENT_SECRET'], tenant=os.environ['AZURE_TENANT_ID']) resource_client = ResourceManagementClient(credentials, subscription_id) event_grid_client = EventGridManagementClient(credentials, subscription_id) # Create Resource group print('\nCreating a Resource Group...') resource_group = resource_client.resource_groups.create_or_update( GROUP_NAME, {'location': LOCATION}) print_item(resource_group) # Creating an event subscription to storage account {StorageAccountResourceId} with destination as {HybridConnectionResourceId} print( '\nCreating an event subscription to storage account {} with destination as {}' .format(STORAGE_ACOUNT_RESOURCE_ID, QUEUE_NAME)) # Scope could be any ARM resource ID that supports EventGrid # https://docs.microsoft.com/azure/event-grid/event-sources scope = "/subscriptions/{}".format(subscription_id) event_subscription_name = 'EventSubscription1' destination = StorageQueueEventSubscriptionDestination( resource_id=STORAGE_ACOUNT_RESOURCE_ID, queue_name=QUEUE_NAME) filter = EventSubscriptionFilter( # By default, "All" event types are included is_subject_case_sensitive=False, subject_begins_with='', subject_ends_with='') event_subscription_info = EventSubscription(destination=destination, filter=filter) event_subscription_async_poller = event_grid_client.event_subscriptions.create_or_update( scope, event_subscription_name, event_subscription_info, ) # Blocking call for the EventSubscription to be created event_subscription = event_subscription_async_poller.result( ) # type: EventSubscription print_item(event_subscription) input("Press enter to delete all created resources.") # Delete the EventSubscription print('\nDeleting the event subscription') delete_async_operation = event_grid_client.event_subscriptions.delete( scope, event_subscription_name) delete_async_operation.wait() print("\nDeleted: {}".format(event_subscription_name)) # Delete Resource group and everything in it print('\nDelete Resource Group') delete_async_operation = resource_client.resource_groups.delete(GROUP_NAME) delete_async_operation.wait() print("\nDeleted: {}".format(GROUP_NAME))