handle event for new project

This commit is contained in:
Setyo Nugroho 2022-09-16 00:11:04 +07:00
parent 3c797950ed
commit eecfb3050d
4 changed files with 26 additions and 6 deletions

View file

@ -35,6 +35,7 @@ You need to enable notification for this openstack service:
- Nova (nova.conf) - Nova (nova.conf)
- Cinder (cinder.conf) - Cinder (cinder.conf)
- Neutron (neutron.conf) - Neutron (neutron.conf)
- Keystone (keystone.conf)
### Nova ### Nova
Add configuration below on `[oslo_messaging_notifications]` Add configuration below on `[oslo_messaging_notifications]`
@ -51,7 +52,7 @@ notify_on_state_change = vm_and_task_state
notification_format = unversioned notification_format = unversioned
``` ```
### Cinder & Neutron ### Cinder & Neutron & Keystone
Add configuration below on `[oslo_messaging_notifications]` Add configuration below on `[oslo_messaging_notifications]`

View file

@ -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()

View file

@ -4,6 +4,7 @@ import traceback
from oslo_messaging import NotificationResult from oslo_messaging import NotificationResult
from core.component import component from core.component import component
from core.component.project.event_handler import ProjectEventHandler
from core.notification import send_notification from core.notification import send_notification
from core.utils.dynamic_setting import get_dynamic_settings, BILLING_ENABLED, get_dynamic_setting from core.utils.dynamic_setting import get_dynamic_settings, BILLING_ENABLED, get_dynamic_setting
from yuyu import settings from yuyu import settings
@ -17,6 +18,9 @@ class EventEndpoint(object):
cls(component.INVOICE_HANDLER[label]) for label, cls in component.EVENT_HANDLER.items() 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): def info(self, ctxt, publisher_id, event_type, payload, metadata):
LOG.info("=== Event Received ===") LOG.info("=== Event Received ===")
LOG.info("Event Type: " + str(event_type)) LOG.info("Event Type: " + str(event_type))
@ -25,7 +29,6 @@ class EventEndpoint(object):
if not get_dynamic_setting(BILLING_ENABLED): if not get_dynamic_setting(BILLING_ENABLED):
return NotificationResult.HANDLED return NotificationResult.HANDLED
# TODO: Error Handling
try: try:
for handler in self.event_handler: for handler in self.event_handler:
handler.handle(event_type, payload) handler.handle(event_type, payload)

View file

@ -43,7 +43,7 @@ class Command(BaseCommand):
def notify_server(self, transport, topics): def notify_server(self, transport, topics):
endpoints = [EventEndpoint()] 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( server = notify.get_notification_listener(
transport, transport,
targets, targets,
@ -54,13 +54,14 @@ class Command(BaseCommand):
def handle(self, *args, **options): def handle(self, *args, **options):
url = settings.YUYU_NOTIFICATION_URL url = settings.YUYU_NOTIFICATION_URL
transport = messaging.get_notification_transport(cfg.CONF,
url=url)
# oslo.config defaults # oslo.config defaults
cfg.CONF.heartbeat_interval = 5 cfg.CONF.heartbeat_interval = 5
cfg.CONF.prog = os.path.basename(__file__) 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.SIGTERM, self.signal_handler)
signal.signal(signal.SIGINT, self.signal_handler) signal.signal(signal.SIGINT, self.signal_handler)