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 test_get_event_operations_one_event_object(self): event_dictionary = { 'resourceProvider': 'Microsoft.Compute/virtualMachines', 'event': 'write' } event_operations = AzureEvents.get_event_operations([event_dictionary]) self.assertEqual(len(event_operations), 1) self.assertEqual(event_operations[0], 'Microsoft.Compute/virtualMachines/write')
def test_get_event_operations_both_event_types(self): event_string = 'AppServicePlanWrite' event_dict = { 'resourceProvider': 'Microsoft.Compute/virtualMachines', 'event': 'write' } event_operations = AzureEvents.get_event_operations([event_string, event_dict]) self.assertEqual(len(event_operations), 2) self.assertTrue('Microsoft.Compute/virtualMachines/write' in event_operations) self.assertTrue('Microsoft.Web/serverFarms/write' in event_operations)
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]) try: AzureEventSubscription.create(destination, queue_name, session, event_filter) 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_event_mode_is_subscribed_to_event_false(self): p = self.load_policy({ 'name': 'test-azure-event', 'resource': 'azure.vm', 'mode': {'type': FUNCTION_EVENT_TRIGGER_MODE, 'events': ['VmWrite']}, }) subscribed_events = AzureEvents.get_event_operations(p.data['mode']['events']) event = { 'data': { 'operationName': 'Microsoft.Compute/virtualMachineScaleSets/write' } } event_mode = AzureEventGridMode(p) self.assertFalse(event_mode._is_subscribed_to_event(event, subscribed_events))
def run(self, event=None, lambda_context=None): """Run the actual policy.""" subscribed_events = AzureEvents.get_event_operations( self.policy.data['mode'].get('events')) resource_ids = list( set([ e['subject'] for e in event if self._is_subscribed_to_event(e, subscribed_events) ])) resources = self.policy.resource_manager.get_resources(resource_ids) if not resources: self.policy.log.info( "policy: %s resources: %s no resources found" % (self.policy.name, self.policy.resource_type)) return with self.policy.ctx: self.policy.ctx.metrics.put_metric('ResourceCount', len(resources), 'Count', Scope="Policy", buffer=False) self.policy._write_file('resources.json', utils.dumps(resources, indent=2)) for action in self.policy.resource_manager.actions: self.policy.log.info( "policy: %s invoking action: %s resources: %d", self.policy.name, action.name, len(resources)) if isinstance(action, EventAction): results = action.process(resources, event) else: results = action.process(resources) self.policy._write_file("action-%s" % action.name, utils.dumps(results)) return resources
def run_policies_for_event(self, message): """ Find all policies subscribed to this event type and schedule them for immediate execution. """ # Load up the event event = json.loads(base64.b64decode(message.content).decode('utf-8')) operation_name = event['data']['operationName'] # Execute all policies matching the event type for k, v in self.policies.items(): events = v['policy'].data.get('mode', {}).get('events') if not events: continue events = AzureEvents.get_event_operations(events) if operation_name in events: self.scheduler.add_job(Host.run_policy, id=k + event['id'], name=k, args=[v['policy'], event, None], misfire_grace_time=60 * 3)
def run(self, event=None, lambda_context=None): """Run the actual policy.""" subscribed_events = AzureEvents.get_event_operations( self.policy.data['mode'].get('events')) resource_ids = list(set( [e['subject'] for e in event if self._is_subscribed_to_event(e, subscribed_events)])) resources = self.policy.resource_manager.get_resources(resource_ids) if not resources: self.policy.log.info( "policy: %s resources: %s no resources found" % ( self.policy.name, self.policy.resource_type)) return with self.policy.ctx: self.policy.ctx.metrics.put_metric( 'ResourceCount', len(resources), 'Count', Scope="Policy", buffer=False) self.policy._write_file( 'resources.json', utils.dumps(resources, indent=2)) for action in self.policy.resource_manager.actions: self.policy.log.info( "policy: %s invoking action: %s resources: %d", self.policy.name, action.name, len(resources)) if isinstance(action, EventAction): results = action.process(resources, event) else: results = action.process(resources) self.policy._write_file( "action-%s" % action.name, utils.dumps(results)) return resources
def __init__(self, policy): super(AzureEventGridMode, self).__init__(policy) self.subscribed_events = AzureEvents.get_event_operations( self.policy.data['mode'].get('events'))
def test_get_event_operations_one_string(self): event_string = 'VmWrite' event_operations = AzureEvents.get_event_operations([event_string]) self.assertEqual(len(event_operations), 1) self.assertEqual(event_operations[0], 'Microsoft.Compute/virtualMachines/write')