From 831577350dd20fe49e2abc93dfd11dcad280ce12 Mon Sep 17 00:00:00 2001 From: gruebel Date: Sun, 3 Nov 2019 20:42:31 +0100 Subject: [PATCH] Add events.list_event_buses --- IMPLEMENTATION_COVERAGE.md | 4 +- moto/events/models.py | 10 +++++ moto/events/responses.py | 18 ++++++++ tests/test_events/test_events.py | 70 +++++++++++++++++++++++++++++--- 4 files changed, 95 insertions(+), 7 deletions(-) diff --git a/IMPLEMENTATION_COVERAGE.md b/IMPLEMENTATION_COVERAGE.md index ab66bf016..6c1cb503b 100644 --- a/IMPLEMENTATION_COVERAGE.md +++ b/IMPLEMENTATION_COVERAGE.md @@ -2713,7 +2713,7 @@ - [ ] upgrade_elasticsearch_domain ## events -51% implemented +54% implemented - [ ] activate_event_source - [X] create_event_bus - [ ] create_partner_event_source @@ -2727,7 +2727,7 @@ - [X] describe_rule - [X] disable_rule - [X] enable_rule -- [ ] list_event_buses +- [X] list_event_buses - [ ] list_event_sources - [ ] list_partner_event_source_accounts - [ ] list_partner_event_sources diff --git a/moto/events/models.py b/moto/events/models.py index 005acc0c1..4e9ba0da2 100644 --- a/moto/events/models.py +++ b/moto/events/models.py @@ -344,6 +344,16 @@ class EventsBackend(BaseBackend): return self.event_buses[name] + def list_event_buses(self, name_prefix): + if name_prefix: + return [ + event_bus + for event_bus in self.event_buses.values() + if event_bus.name.startswith(name_prefix) + ] + + return list(self.event_buses.values()) + available_regions = boto3.session.Session().get_available_regions("events") events_backends = {region: EventsBackend(region) for region in available_regions} diff --git a/moto/events/responses.py b/moto/events/responses.py index 896278aec..dc1615b3c 100644 --- a/moto/events/responses.py +++ b/moto/events/responses.py @@ -278,3 +278,21 @@ class EventsHandler(BaseResponse): event_bus = self.events_backend.create_event_bus(name, event_source_name) return json.dumps({"EventBusArn": event_bus.arn}), self.response_headers + + def list_event_buses(self): + name_prefix = self._get_param("NamePrefix") + # ToDo: add 'NextToken' & 'Limit' parameters + + response = [] + for event_bus in self.events_backend.list_event_buses(name_prefix): + event_bus_response = { + "Name": event_bus.name, + "Arn": event_bus.arn, + } + + if event_bus.policy: + event_bus_response["Policy"] = event_bus.policy + + response.append(event_bus_response) + + return json.dumps({"EventBuses": response}), self.response_headers diff --git a/tests/test_events/test_events.py b/tests/test_events/test_events.py index 43ab8f310..5a2cb4f5d 100644 --- a/tests/test_events/test_events.py +++ b/tests/test_events/test_events.py @@ -213,14 +213,14 @@ def test_put_permission_errors(): client.put_permission.when.called_with( EventBusName="non-existing", Action="events:PutEvents", - Principal="762054135200", + Principal="111111111111", StatementId="test", ).should.throw(ClientError, "Event bus non-existing does not exist.") client.put_permission.when.called_with( EventBusName="test-bus", Action="events:PutPermission", - Principal="762054135200", + Principal="111111111111", StatementId="test", ).should.throw( ClientError, "Provided value in parameter 'action' is not supported." @@ -243,7 +243,7 @@ def test_remove_permission_errors(): client.put_permission( EventBusName="test-bus", Action="events:PutEvents", - Principal="762054135200", + Principal="111111111111", StatementId="test", ) @@ -322,7 +322,7 @@ def test_describe_event_bus(): client.put_permission( EventBusName="test-bus", Action="events:PutEvents", - Principal="762054135200", + Principal="111111111111", StatementId="test", ) @@ -339,7 +339,7 @@ def test_describe_event_bus(): { "Sid": "test", "Effect": "Allow", - "Principal": {"AWS": "arn:aws:iam::762054135200:root"}, + "Principal": {"AWS": "arn:aws:iam::111111111111:root"}, "Action": "events:PutEvents", "Resource": "arn:aws:events:us-east-1:123456789012:event-bus/test-bus", } @@ -355,3 +355,63 @@ def test_describe_event_bus_errors(): client.describe_event_bus.when.called_with(Name="non-existing").should.throw( ClientError, "Event bus non-existing does not exist." ) + + +@mock_events +def test_list_event_buses(): + client = boto3.client("events", "us-east-1") + client.create_event_bus(Name="test-bus-1") + client.put_permission( + EventBusName="test-bus-1", + Action="events:PutEvents", + Principal="111111111111", + StatementId="test", + ) + client.create_event_bus(Name="test-bus-2") + client.create_event_bus(Name="other-bus-1") + client.create_event_bus(Name="other-bus-2") + + response = client.list_event_buses() + + response["EventBuses"].should.have.length_of(5) + sorted(response["EventBuses"], key=lambda i: i["Name"]).should.equal( + [ + { + "Name": "default", + "Arn": "arn:aws:events:us-east-1:123456789012:event-bus/default", + }, + { + "Name": "other-bus-1", + "Arn": "arn:aws:events:us-east-1:123456789012:event-bus/other-bus-1", + }, + { + "Name": "other-bus-2", + "Arn": "arn:aws:events:us-east-1:123456789012:event-bus/other-bus-2", + }, + { + "Name": "test-bus-1", + "Arn": "arn:aws:events:us-east-1:123456789012:event-bus/test-bus-1", + "Policy": '{"Version": "2012-10-17", "Statement": [{"Sid": "test", "Effect": "Allow", "Principal": {"AWS": "arn:aws:iam::111111111111:root"}, "Action": "events:PutEvents", "Resource": "arn:aws:events:us-east-1:123456789012:event-bus/test-bus-1"}]}', + }, + { + "Name": "test-bus-2", + "Arn": "arn:aws:events:us-east-1:123456789012:event-bus/test-bus-2", + }, + ] + ) + + response = client.list_event_buses(NamePrefix="other-bus") + + response["EventBuses"].should.have.length_of(2) + response["EventBuses"].should.equal( + [ + { + "Name": "other-bus-1", + "Arn": "arn:aws:events:us-east-1:123456789012:event-bus/other-bus-1", + }, + { + "Name": "other-bus-2", + "Arn": "arn:aws:events:us-east-1:123456789012:event-bus/other-bus-2", + }, + ] + )