From eecfb3050d67d060debdece762523333c9514166 Mon Sep 17 00:00:00 2001 From: Setyo Nugroho Date: Fri, 16 Sep 2022 00:11:04 +0700 Subject: [PATCH] handle event for new project --- README.md | 3 ++- core/component/project/event_handler.py | 15 +++++++++++++++ core/event_endpoint.py | 5 ++++- core/management/commands/event_monitor.py | 9 +++++---- 4 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 core/component/project/event_handler.py diff --git a/README.md b/README.md index 1a109cc..84fc068 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ You need to enable notification for this openstack service: - Nova (nova.conf) - Cinder (cinder.conf) - Neutron (neutron.conf) +- Keystone (keystone.conf) ### Nova Add configuration below on `[oslo_messaging_notifications]` @@ -51,7 +52,7 @@ notify_on_state_change = vm_and_task_state notification_format = unversioned ``` -### Cinder & Neutron +### Cinder & Neutron & Keystone Add configuration below on `[oslo_messaging_notifications]` diff --git a/core/component/project/event_handler.py b/core/component/project/event_handler.py new file mode 100644 index 0000000..a6e4478 --- /dev/null +++ b/core/component/project/event_handler.py @@ -0,0 +1,15 @@ +import logging + +from core.models import BillingProject + +LOG = logging.getLogger("yuyu_notification") + + +class ProjectEventHandler: + def handle(self, event_type, raw_payload): + if event_type == 'identity.project.created': + new_project_id = raw_payload['target']['id'] + LOG.info("Registering new project " + new_project_id) + project = BillingProject() + project.tenant_id = new_project_id + project.save() diff --git a/core/event_endpoint.py b/core/event_endpoint.py index da0925b..f6e4868 100644 --- a/core/event_endpoint.py +++ b/core/event_endpoint.py @@ -4,6 +4,7 @@ import traceback from oslo_messaging import NotificationResult from core.component import component +from core.component.project.event_handler import ProjectEventHandler from core.notification import send_notification from core.utils.dynamic_setting import get_dynamic_settings, BILLING_ENABLED, get_dynamic_setting from yuyu import settings @@ -17,6 +18,9 @@ class EventEndpoint(object): cls(component.INVOICE_HANDLER[label]) for label, cls in component.EVENT_HANDLER.items() ] + # Add handler for project event + self.event_handler.append(ProjectEventHandler()) + def info(self, ctxt, publisher_id, event_type, payload, metadata): LOG.info("=== Event Received ===") LOG.info("Event Type: " + str(event_type)) @@ -25,7 +29,6 @@ class EventEndpoint(object): if not get_dynamic_setting(BILLING_ENABLED): return NotificationResult.HANDLED - # TODO: Error Handling try: for handler in self.event_handler: handler.handle(event_type, payload) diff --git a/core/management/commands/event_monitor.py b/core/management/commands/event_monitor.py index a891403..58386d1 100644 --- a/core/management/commands/event_monitor.py +++ b/core/management/commands/event_monitor.py @@ -43,7 +43,7 @@ class Command(BaseCommand): def notify_server(self, transport, topics): endpoints = [EventEndpoint()] - targets = list(map(lambda t: messaging.Target(topic=t), topics)) + targets = list(map(lambda t: messaging.Target(topic=t, fanout=True), topics)) server = notify.get_notification_listener( transport, targets, @@ -54,13 +54,14 @@ class Command(BaseCommand): def handle(self, *args, **options): url = settings.YUYU_NOTIFICATION_URL - transport = messaging.get_notification_transport(cfg.CONF, - url=url) # oslo.config defaults cfg.CONF.heartbeat_interval = 5 cfg.CONF.prog = os.path.basename(__file__) - cfg.CONF.project = 'yuyu' + cfg.CONF.project = 'xxx' + + transport = messaging.get_notification_transport(cfg.CONF, + url=url) signal.signal(signal.SIGTERM, self.signal_handler) signal.signal(signal.SIGINT, self.signal_handler)