From a1dfdc0b9d16fe3d43ff7186b8792a6fdded060a Mon Sep 17 00:00:00 2001
From: Jorrit Schaap <schaap@astron.nl>
Date: Thu, 27 Aug 2020 18:38:05 +0200
Subject: [PATCH] TMSS-272: added script for populating schemas

---
 SAS/TMSS/client/bin/CMakeLists.txt |  1 +
 SAS/TMSS/client/bin/tmss_populate  | 25 ++++++++++++++++++++++++
 SAS/TMSS/client/lib/CMakeLists.txt |  1 +
 SAS/TMSS/client/lib/populate.py    | 31 ++++++++++++++++++++++++++++++
 4 files changed, 58 insertions(+)
 create mode 100755 SAS/TMSS/client/bin/tmss_populate
 create mode 100644 SAS/TMSS/client/lib/populate.py

diff --git a/SAS/TMSS/client/bin/CMakeLists.txt b/SAS/TMSS/client/bin/CMakeLists.txt
index d2bd6170e88..34d5fafe0d1 100644
--- a/SAS/TMSS/client/bin/CMakeLists.txt
+++ b/SAS/TMSS/client/bin/CMakeLists.txt
@@ -7,3 +7,4 @@ lofar_add_bin_scripts(tmss_get_subtask_successors)
 lofar_add_bin_scripts(tmss_schedule_subtask)
 lofar_add_bin_scripts(tmss_get_setting)
 lofar_add_bin_scripts(tmss_set_setting)
+lofar_add_bin_scripts(tmss_populate)
diff --git a/SAS/TMSS/client/bin/tmss_populate b/SAS/TMSS/client/bin/tmss_populate
new file mode 100755
index 00000000000..b0dbfbb70ce
--- /dev/null
+++ b/SAS/TMSS/client/bin/tmss_populate
@@ -0,0 +1,25 @@
+#!/usr/bin/python3
+
+# Copyright (C) 2012-2015  ASTRON (Netherlands Institute for Radio Astronomy)
+# P.O. Box 2, 7990 AA Dwingeloo, The Netherlands
+#
+# This file is part of the LOFAR software suite.
+# The LOFAR software suite is free software: you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as published
+# by the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# The LOFAR software suite is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>.
+
+import logging
+from lofar.sas.tmss.client.populate import populate_schemas
+
+if __name__ == "__main__":
+    logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=logging.INFO)
+    populate_schemas()
diff --git a/SAS/TMSS/client/lib/CMakeLists.txt b/SAS/TMSS/client/lib/CMakeLists.txt
index 94606c74363..2281da0ee8a 100644
--- a/SAS/TMSS/client/lib/CMakeLists.txt
+++ b/SAS/TMSS/client/lib/CMakeLists.txt
@@ -4,6 +4,7 @@ include(PythonInstall)
 set(_py_files
     tmssbuslistener.py
     mains.py
+    populate.py
     tmss_http_rest_client.py
     )
 
diff --git a/SAS/TMSS/client/lib/populate.py b/SAS/TMSS/client/lib/populate.py
new file mode 100644
index 00000000000..d6b0d9a6622
--- /dev/null
+++ b/SAS/TMSS/client/lib/populate.py
@@ -0,0 +1,31 @@
+import json
+import argparse
+from pprint import pprint
+from lofar.sas.tmss.client.tmss_http_rest_client import TMSSsession
+from lofar.common.datetimeutils import parseDatetime
+
+
+def populate_schemas():
+    parser = argparse.ArgumentParser()
+    args = parser.parse_args()
+
+    try:
+        with TMSSsession.create_from_dbcreds_for_ldap() as session:
+            session.post_template(template_path='common_schema_template',
+                                  name="base",
+                                  description='email address schema',
+                                  version='1',
+                                  schema={
+                                      "$id": "http://127.0.0.1:8000/api/schemas/common/base/1/#",
+                                      "$schema": "http://json-schema.org/draft-06/schema#",
+                                      "definitions": {
+                                          "email": {
+                                              "type": "string",
+                                              "format": "email",
+                                              "pattern": "@example\\.com$",
+                                              "default": ""}
+                                      }})
+
+    except Exception as e:
+        print(e)
+        exit(1)
-- 
GitLab