Ejemplo n.º 1
0
    def events_handler_put_targets(self):
        event_rules = EventsBackend.get().event_rules

        def is_rule_present(rule_name):
            for rule in event_rules.get(event_bus, []):
                if rule == rule_name:
                    return True
            return False

        rule_name = self._get_param("Rule")
        targets = self._get_param("Targets")
        event_bus = self._get_param("EventBusName") or DEFAULT_EVENT_BUS_NAME

        if not rule_name:
            return self.error("ValidationException",
                              "Parameter Rule is required.")

        if not targets:
            return self.error("ValidationException",
                              "Parameter Targets is required.")

        if not self.events_backend.put_targets(rule_name, event_bus, targets):
            if not is_rule_present(rule_name):
                return self.error(
                    "ResourceNotFoundException",
                    "Rule " + rule_name + " does not exist.",
                )

        return (
            json.dumps({
                "FailedEntryCount": 0,
                "FailedEntries": []
            }),
            self.response_headers,
        )
Ejemplo n.º 2
0
    def events_handler_put_targets(self):
        event_rules = EventsBackend.get().event_rules

        def is_rule_present(rule_name):
            for rule in event_rules.get(event_bus, []):
                if rule == rule_name:
                    return True
            return False

        rule_name = self._get_param('Rule')
        targets = self._get_param('Targets')
        event_bus = self._get_param('EventBusName') or DEFAULT_EVENT_BUS_NAME

        if not rule_name:
            return self.error('ValidationException',
                              'Parameter Rule is required.')

        if not targets:
            return self.error('ValidationException',
                              'Parameter Targets is required.')

        if not self.events_backend.put_targets(rule_name, event_bus, targets):
            if not is_rule_present(rule_name):
                return self.error('ResourceNotFoundException',
                                  'Rule ' + rule_name + ' does not exist.')

        return json.dumps({
            'FailedEntryCount': 0,
            'FailedEntries': []
        }), self.response_headers
Ejemplo n.º 3
0
    def events_handler_put_rule(self):
        name = self._get_param("Name")
        event_bus = self._get_param("EventBusName") or DEFAULT_EVENT_BUS_NAME

        event_rules = EventsBackend.get().event_rules
        event_rules.setdefault(event_bus, set())
        event_rules[event_bus].add(name)

        return events_handler_put_rule_orig(self)
Ejemplo n.º 4
0
    def events_handler_delete_rule(self):
        name = self._get_param('Name')
        event_bus = self._get_param('EventBusName') or DEFAULT_EVENT_BUS_NAME

        event_rules = EventsBackend.get().event_rules
        rules_set = event_rules.get(event_bus, set())
        if name not in rules_set:
            return self.error('ValidationException', 'Rule "%s" not found for event bus "%s"' % (name, event_bus))
        rules_set.remove(name)

        return events_handler_delete_rule_orig(self)
Ejemplo n.º 5
0
    def events_handler_put_events(self):
        entries = self._get_param("Entries")
        events = list(
            map(lambda event: {
                "event": event,
                "uuid": str(uuid.uuid4())
            }, entries))

        _create_and_register_temp_dir()
        _dump_events_to_files(events)
        event_rules = EventsBackend.get().event_rules

        for event_envelope in events:
            event = event_envelope["event"]
            event_bus = event.get("EventBusName") or DEFAULT_EVENT_BUS_NAME

            rules = event_rules.get(event_bus, [])

            formatted_event = {
                "version": "0",
                "id": event_envelope["uuid"],
                "detail-type": event.get("DetailType"),
                "source": event.get("Source"),
                "account": TEST_AWS_ACCOUNT_ID,
                "time":
                datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ"),
                "region": self.region,
                "resources": event.get("Resources", []),
                "detail": json.loads(event.get("Detail", "{}")),
            }

            targets = []
            for rule in rules:
                if filter_event_based_on_event_format(self, rule,
                                                      formatted_event):
                    targets.extend(
                        self.events_backend.list_targets_by_rule(rule)
                        ["Targets"])

            # process event
            process_events(formatted_event, targets)

        content = {
            "Entries":
            list(map(lambda event: {"EventId": event["uuid"]}, events))
        }

        self.response_headers.update({
            "Content-Type": APPLICATION_AMZ_JSON_1_1,
            "x-amzn-RequestId": short_uid()
        })

        return json.dumps(content), self.response_headers
Ejemplo n.º 6
0
    def events_handler_put_events(self):
        entries = self._get_param('Entries')
        events = list(
            map(lambda event: {
                'event': event,
                'uuid': str(uuid.uuid4())
            }, entries))

        _create_and_register_temp_dir()
        _dump_events_to_files(events)
        event_rules = EventsBackend.get().event_rules

        for event_envelope in events:
            event = event_envelope['event']
            event_bus = event.get('EventBusName') or DEFAULT_EVENT_BUS_NAME

            rules = event_rules.get(event_bus, [])

            formatted_event = {
                'version': '0',
                'id': event_envelope['uuid'],
                'detail-type': event.get('DetailType'),
                'source': event.get('Source'),
                'account': TEST_AWS_ACCOUNT_ID,
                'time':
                datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ'),
                'region': self.region,
                'resources': event.get('Resources', []),
                'detail': json.loads(event.get('Detail', '{}')),
            }

            targets = []
            for rule in rules:
                if filter_event_based_on_event_format(self, rule,
                                                      formatted_event):
                    targets.extend(
                        self.events_backend.list_targets_by_rule(rule)
                        ['Targets'])

            # process event
            process_events(formatted_event, targets)

        content = {
            'Entries':
            list(map(lambda event: {'EventId': event['uuid']}, events))
        }

        self.response_headers.update({
            'Content-Type': APPLICATION_AMZ_JSON_1_1,
            'x-amzn-RequestId': short_uid()
        })

        return json.dumps(content), self.response_headers