Beispiel #1
0
    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
Beispiel #2
0
    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
Beispiel #3
0
    def __call__(self, parser, namespace, values, option_string=None):
        if len(values) < 3:
            raise CLIError('usage error: --publisher-filter KEY[.INNERKEY] FILTEROPERATOR VALUE [VALUE ...]')

        key = values[0]
        operator = values[1]

# operators that support single value
        if operator.lower() == NUMBERLESSTHAN.lower():
            _validate_only_single_value_is_specified(NUMBERLESSTHAN, values)
            publisher_filter = NumberLessThanAdvancedFilter(key=key, value=float(values[2]))
        elif operator.lower() == NUMBERLESSTHANOREQUALS.lower():
            _validate_only_single_value_is_specified(NUMBERLESSTHANOREQUALS, values)
            publisher_filter = NumberLessThanOrEqualsAdvancedFilter(key=key, value=float(values[2]))
        elif operator.lower() == NUMBERGREATERTHAN.lower():
            _validate_only_single_value_is_specified(NUMBERGREATERTHAN, values)
            publisher_filter = NumberGreaterThanAdvancedFilter(key=key, value=float(values[2]))
        elif operator.lower() == NUMBERGREATERTHANOREQUALS.lower():
            _validate_only_single_value_is_specified(NUMBERGREATERTHANOREQUALS, values)
            publisher_filter = NumberGreaterThanOrEqualsAdvancedFilter(key=key, value=float(values[2]))
        elif operator.lower() == BOOLEQUALS.lower():
            _validate_only_single_value_is_specified(BOOLEQUALS, values)
            publisher_filter = BoolEqualsAdvancedFilter(key=key, value=bool(values[2]))

# operators that support multiple values
        elif operator.lower() == NUMBERIN.lower():
            float_values = [float(i) for i in values[2:]]
            publisher_filter = NumberInAdvancedFilter(key=key, values=float_values)
        elif operator.lower() == NUMBERNOTIN.lower():
            float_values = [float(i) for i in values[2:]]
            publisher_filter = NumberNotInAdvancedFilter(key=key, values=float_values)
        elif operator.lower() == STRINGIN.lower():
            publisher_filter = StringInAdvancedFilter(key=key, values=values[2:])
        elif operator.lower() == STRINGNOTIN.lower():
            publisher_filter = StringNotInAdvancedFilter(key=key, values=values[2:])
        elif operator.lower() == STRINGBEGINSWITH.lower():
            publisher_filter = StringBeginsWithAdvancedFilter(key=key, values=values[2:])
        elif operator.lower() == STRINGENDSWITH.lower():
            publisher_filter = StringEndsWithAdvancedFilter(key=key, values=values[2:])
        elif operator.lower() == STRINGCONTAINS.lower():
            publisher_filter = StringContainsAdvancedFilter(key=key, values=values[2:])
        else:
            raise CLIError("--publisher-filter: The specified filter operator '{}' is not"
                           " a valid operator. Supported values are ".format(operator) +
                           NUMBERIN + "," + NUMBERNOTIN + "," + STRINGIN + "," +
                           STRINGNOTIN + "," + STRINGBEGINSWITH + "," +
                           STRINGCONTAINS + "," + STRINGENDSWITH + "," +
                           NUMBERGREATERTHAN + "," + NUMBERGREATERTHANOREQUALS + "," +
                           NUMBERLESSTHAN + "," + NUMBERLESSTHANOREQUALS + "," + BOOLEQUALS + ".")
        if namespace.publisher_filter is None:
            namespace.publisher_filter = []
        namespace.publisher_filter.append(publisher_filter)
Beispiel #4
0
def _get_multi_value_advanced_filter(key, operator, values):
    if len(values) < 3:
        raise CLIError(
            "--advanced-filter: For '{}' operator at least one filter value "
            "must be specified.".format(operator))

    if operator.lower() == NUMBERIN.lower():
        float_values = [float(i) for i in values[2:]]
        advanced_filter = NumberInAdvancedFilter(key=key, values=float_values)
    elif operator.lower() == NUMBERNOTIN.lower():
        float_values = [float(i) for i in values[2:]]
        advanced_filter = NumberNotInAdvancedFilter(key=key,
                                                    values=float_values)
    elif operator.lower() == STRINGIN.lower():
        advanced_filter = StringInAdvancedFilter(key=key, values=values[2:])
    elif operator.lower() == STRINGNOTIN.lower():
        advanced_filter = StringNotInAdvancedFilter(key=key, values=values[2:])
    elif operator.lower() == STRINGBEGINSWITH.lower():
        advanced_filter = StringBeginsWithAdvancedFilter(key=key,
                                                         values=values[2:])
    elif operator.lower() == STRINGNOTBEGINSWITH.lower():
        advanced_filter = StringNotBeginsWithAdvancedFilter(key=key,
                                                            values=values[2:])
    elif operator.lower() == STRINGENDSWITH.lower():
        advanced_filter = StringEndsWithAdvancedFilter(key=key,
                                                       values=values[2:])
    elif operator.lower() == STRINGNOTENDSWITH.lower():
        advanced_filter = StringNotEndsWithAdvancedFilter(key=key,
                                                          values=values[2:])
    elif operator.lower() == STRINGCONTAINS.lower():
        advanced_filter = StringContainsAdvancedFilter(key=key,
                                                       values=values[2:])
    elif operator.lower() == STRINGNOTCONTAINS.lower():
        advanced_filter = StringNotContainsAdvancedFilter(key=key,
                                                          values=values[2:])
    else:
        raise CLIError(
            "--advanced-filter: The specified filter operator '{}' is not "
            " a multi-value operator. Supported operators are ".format(
                operator) + NUMBERIN + "," + NUMBERNOTIN + "," + STRINGIN +
            "," + STRINGNOTIN + "," + STRINGBEGINSWITH + "," +
            STRINGNOTBEGINSWITH + "," + STRINGENDSWITH + "," +
            STRINGNOTENDSWITH + "," + STRINGCONTAINS + "," +
            STRINGNOTCONTAINS + ".")

    return advanced_filter