From 3c68ccab51bcc4f698da3a9e97bdf13466156c4e Mon Sep 17 00:00:00 2001
From: Tim Jaacks <tim.jaacks@seco.com>
Date: Wed, 7 Dec 2022 17:28:47 +0100
Subject: [PATCH] Alphaplan: add script for importing alphaplan data

The script has mostly been copied from alphaplan_fwr.py, but with
different input data.

Adding a job to the CI-test environment for testing this.
---
 build-common.yml                | 19 +++++++++
 build-jobs-ci-test.yml.jinja2   |  8 ++++
 scripts/alphaplan_fwr_import.py | 75 +++++++++++++++++++++++++++++++++
 3 files changed, 102 insertions(+)
 create mode 100755 scripts/alphaplan_fwr_import.py

diff --git a/build-common.yml b/build-common.yml
index c7e8ee7f..33e56570 100644
--- a/build-common.yml
+++ b/build-common.yml
@@ -215,3 +215,22 @@ workflow:
   artifacts:
     paths:
       - alphaplan-import*.json
+
+.import-alphaplan-data:
+  extends:
+    - .infrastructure
+  rules:
+    - when: manual
+  variables:
+    # Most AP_WEBSERVICE_* variables are set in GitLab CI variables. We're defining the
+    # URL here, though, so that it can be overridden in a job's variables block.
+    AP_WEBSERVICE_URL: >
+      https://SRV06.hamburg.garz-fricke.de/Alphaplan-API/Artikel/CreateFirmware
+  script:
+    - echo "${AP_WEBSERVICE_CERT}" > GarzFrickeGmbH-CA.cer
+    - .gitlab-ci/scripts/alphaplan_fwr_import.py
+        --url=${AP_WEBSERVICE_URL}
+        --user=${AP_WEBSERVICE_USR}
+        --password=${AP_WEBSERVICE_PW}
+        --cert-file=GarzFrickeGmbH-CA.cer
+        --file=alphaplan-import*.json
diff --git a/build-jobs-ci-test.yml.jinja2 b/build-jobs-ci-test.yml.jinja2
index f7760ffa..771d2999 100644
--- a/build-jobs-ci-test.yml.jinja2
+++ b/build-jobs-ci-test.yml.jinja2
@@ -295,3 +295,11 @@ generate-alphaplan-data-seco-mx6:
   stage: Alphaplan
   needs:
     - deploy-seco-mx6
+
+import-alphaplan-data-seco-mx6:
+  extends: .import-alphaplan-data
+  stage: Alphaplan
+  variables:
+    AP_WEBSERVICE_URL: https://SRV06.hamburg.garz-fricke.de/Test/Alphaplan-API/Artikel/CreateFirmware
+  needs:
+    - generate-alphaplan-data-seco-mx6
diff --git a/scripts/alphaplan_fwr_import.py b/scripts/alphaplan_fwr_import.py
new file mode 100755
index 00000000..9583cec4
--- /dev/null
+++ b/scripts/alphaplan_fwr_import.py
@@ -0,0 +1,75 @@
+#!/usr/bin/env python3
+import argparse
+import glob
+import json
+import sys
+
+import requests
+
+
+def ap_send_json(jsonobj: dict, url: str, user: str, password: str, cert_file: str):
+    """Sends the generated files to the Alphaplan webservice"""
+    msg = requests.post(
+        url, json=jsonobj, auth=(user, password), verify=cert_file, timeout=10
+    )
+    msg_json = msg.json()
+    if msg_json["Status"] != "Ok":
+        sys.exit("ERROR: AlphaPlan webservice post request failed")
+    print("AlphaPlan webservice response: {}".format(msg_json["Meldung"]))
+
+
+def main():
+    parser = argparse.ArgumentParser()
+    parser.add_argument(
+        "--file",
+        help="""JSON file for Alphaplan FWR import""",
+        dest="file",
+        required=True,
+    )
+    parser.add_argument(
+        "--url",
+        help="""URL to the Alphaplan webservice""",
+        dest="url",
+        required=True,
+    )
+    parser.add_argument(
+        "--user",
+        help="""User for the Alphaplan webservice""",
+        dest="user",
+        required=True,
+    )
+    parser.add_argument(
+        "--password",
+        help="""Password for the Alphaplan webservice""",
+        dest="password",
+        required=True,
+    )
+    parser.add_argument(
+        "--cert-file",
+        help="""Certificate file for the Alphaplan webservice""",
+        dest="cert_file",
+        required=True,
+    )
+    args, _ = parser.parse_known_args()
+
+    files = glob.glob(args.file, recursive=True)
+    if not files:
+        sys.exit("ERROR: no file(s) matching '%s' found" % args.file)
+
+    print("Sending data to Alphaplan FWR webservice at %s" % args.url)
+
+    # Get files from passed glob
+    for filename in files:
+        print("Importing JSON file %s" % filename)
+        with open(filename, "r", encoding="utf-8") as f:
+            ap_send_json(
+                json.load(f),
+                args.url,
+                args.user,
+                args.password,
+                args.cert_file,
+            )
+
+
+if __name__ == "__main__":
+    main()
-- 
GitLab