From a40bfcd06f26f4facf25da61e89db2817d58edcf Mon Sep 17 00:00:00 2001
From: Jorrit Schaap <schaap@astron.nl>
Date: Tue, 12 Dec 2023 11:25:15 +0100
Subject: [PATCH] post reservation events on slack

---
 .../slack_webhook/lib/slack_webhook.py        | 22 +++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)

diff --git a/SAS/TMSS/backend/services/slack_webhook/lib/slack_webhook.py b/SAS/TMSS/backend/services/slack_webhook/lib/slack_webhook.py
index 4d2616a0a64..783e59e6c26 100644
--- a/SAS/TMSS/backend/services/slack_webhook/lib/slack_webhook.py
+++ b/SAS/TMSS/backend/services/slack_webhook/lib/slack_webhook.py
@@ -69,10 +69,28 @@ class TMSSEventMessageHandlerForSlackWebhooks(TMSSEventMessageHandler):
 
 
     def onSchedulingUnitBlueprintCreated(self, id: int):
-        scheduling_unit = self.tmss_client.get_path_as_json_object('scheduling_unit_blueprint/%s' % (id,))
+        scheduling_unit = self.tmss_client.get_path_as_json_object('scheduling_unit_blueprint_slim/%s' % (id,))
         self.post_to_slack_webhook("%s was created\ndescription: %s" % (self._get_formatted_project_scheduling_unit_string(id),
                                                                         scheduling_unit['description'] or "<no description>"))
 
+    def onReservationCreated(self, id: int):
+        self._onReservationCreatedUpdated(id, "created")
+
+    def onReservationUpdated(self, id: int):
+        self._onReservationCreatedUpdated(id, "updated")
+
+    def _onReservationCreatedUpdated(self, id: int, event: str):
+        from lofar.sas.tmss.tmss.tmssapp.models.specification import Reservation
+        reservation = Reservation.objects.get(id=id)
+        reservation_url = self.tmss_client.get_full_url_for_path('reservation/%s' % (id,))
+        reservation_ui_url = reservation_url.replace('/api/reservation/', '/reservation/view/')
+        msg = "reservation <%s|\'%s\' [%s]> was %s. start='%s' end='%s'" % (reservation_ui_url, reservation.name, id, event,
+                                                                            reservation.start_time, reservation.stop_time)
+        self.post_to_slack_webhook(msg)
+
+    def onReservationDeleted(self, id: int, start_time, stop_time):
+        self.post_to_slack_webhook("reservation %d from '%s' until '%s' was deleted" % (id, start_time, stop_time))
+
     def _get_formatted_project_scheduling_unit_string(self, scheduling_unit_blueprint_id: int) -> str:
         from lofar.sas.tmss.tmss.tmssapp.models.specification import SchedulingUnitBlueprint
         scheduling_unit = SchedulingUnitBlueprint.objects.get(id=scheduling_unit_blueprint_id)
@@ -117,7 +135,7 @@ def main():
     group = OptionGroup(parser, 'Django options')
     parser.add_option_group(group)
     group.add_option('-C', '--credentials', dest='dbcredentials', type='string', default=os.environ.get('TMSS_DBCREDENTIALS', 'TMSS'), help='django dbcredentials name, default: %default')
-    group.add_option('-R', '--rest_credentials', dest='rest_credentials', type='string', default='TMSSClient', help='django REST API credentials name, default: %default')
+    group.add_option('-R', '--rest_credentials', dest='rest_credentials', type='string', default=os.environ.get('TMSS_CLIENT_DBCREDENTIALS', 'TMSSClient'), help='django REST API credentials name, default: %default')
 
     group = OptionGroup(parser, 'Messaging options')
     group.add_option('-b', '--broker', dest='broker', type='string', default=DEFAULT_BROKER,
-- 
GitLab