From a3b7cfe68564218c97d3545de044ec5e9b9cf0f8 Mon Sep 17 00:00:00 2001
From: OZGCloud <ozgcloud@mgm-tp.com>
Date: Tue, 4 Jan 2022 17:55:47 +0100
Subject: [PATCH] create kc user

---
 Jenkinsfile                                   | 50 ++++++++++++++++---
 .../deployment-values/goofy/user/user.yaml    | 15 ++----
 .../src/fixtures/user/user_emil.json          |  3 +-
 .../src/fixtures/user/user_sabine.json        |  3 +-
 .../src/fixtures/user/user_zonk.json          |  3 +-
 5 files changed, 51 insertions(+), 23 deletions(-)

diff --git a/Jenkinsfile b/Jenkinsfile
index 85957c4965..4ab8e499fa 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -157,8 +157,8 @@ pipeline {
                 script {
                     FAILED_STAGE=env.STAGE_NAME
 
-                    namespace = createNamespaceName(env.STAGE_NAME)
-                    createKEycloakUser(env.STAGE_NAME)
+                    namespace = generateNamespaceName(env.STAGE_NAME)
+                    createKeycloakUser(env.STAGE_NAME)
 
                     container("k8s") {
                         println(namespace)
@@ -333,17 +333,51 @@ pipeline {
     // }
 }
 
-String createNamespaceName(String stage) {
+String generateNamespaceName(String stage) {
     branchName = env.BRANCH_NAME.replaceAll(/[^a-zA-Z0-9]+/, "").toLowerCase()
     stageName = stage.replaceAll(/[^a-zA-Z0-9]+/, "").toLowerCase()
     
     return "e2e-${branchName}${stageName}-dev"
 }
 
-Void createKEycloakUser(String stage) {
-    namespace = createNamespaceName(stage)
-    dir('goofy-client/apps/goofy-e2e/deployment-values/goofy') {
-        userYaml = readYaml file: "user.yaml"
-        println(userYaml)
+Void createKeycloakUser(String stage) {
+    namespace = generateNamespaceName(stage)
+	
+    e2eUserFiles = sh (script: 'ls goofy-client/apps/goofy-e2e/src/fixtures/user', returnStdout: true)
+
+    baseUserYaml = readYaml file: "goofy-client/apps/goofy-e2e/deployment-values/goofy/user/user.yaml"
+
+    e2eUserFiles.split("\\n").each { user ->
+    	dir('goofy-client/apps/goofy-e2e/src/fixtures/user') {
+    		userJson = readJSON file: user
+
+    		baseUserYaml.metadata.name = namespace + "-" + userJson.name
+    		baseUserYaml.metadata.labels.realm = namespace
+    		baseUserYaml.spec.realmSelector.matchLabels.realm = namespace
+
+    		baseUserYaml.spec.user.username = userJson.name
+    		baseUserYaml.spec.user.credentials = [[type: 'password', value: userJson.password]]
+
+            if(userJson.firstName) {
+        		baseUserYaml.spec.user.firstName = userJson.firstName
+            }
+
+            if(userJson.lastName) {
+        		baseUserYaml.spec.user.lastName = userJson.lastName
+            }
+
+            if(userJson.einheitlicherAnsprechpartner) {
+                baseUserYaml.spec.user.clientRoles = [namespace: ['EINHEITLICHER_ANSPRECHPARTNER']]
+            }
+
+            if(userJson.verwaltungUser) {
+                baseUserYaml.spec.user.clientRoles = [namespace: ['VERWALTUNG_USER']]
+            }
+    	}
+
+        writeYaml file: "kc-user.yaml", data: baseUserYaml
+
+        sh "kubectl apply -f kc-user.yaml"
+        sh "rm kc-user.yaml"
     }
 }
\ No newline at end of file
diff --git a/goofy-client/apps/goofy-e2e/deployment-values/goofy/user/user.yaml b/goofy-client/apps/goofy-e2e/deployment-values/goofy/user/user.yaml
index befa449a97..48734a2a6f 100644
--- a/goofy-client/apps/goofy-e2e/deployment-values/goofy/user/user.yaml
+++ b/goofy-client/apps/goofy-e2e/deployment-values/goofy/user/user.yaml
@@ -1,26 +1,17 @@
 apiVersion: keycloak.org/v1alpha1
 kind: KeycloakUser
 metadata:
-  name: e2e-ozg1784e2eeakc-dev-emil
+  name: null
   namespace: keycloak
   labels:
-    realm: e2e-ozg1784e2eeakc-dev
+    realm: null
 spec:
   realmSelector:
     matchLabels:
-      realm: e2e-ozg1784e2eeakc-dev
+      realm: null
   user:
-    username: "emil"
-    firstName: "Emil"
-    lastName: "Ernst"
     enabled: True
     emailVerified: False
-    credentials:
-      - type: "password"
-        value: "Einfallsreich"
     realmRoles:
       - "offline_access"
       - "uma_authorization"
-    clientRoles:
-      e2e-ozg1784e2eeakc-dev:
-        - "VERWALTUNG_USER"
diff --git a/goofy-client/apps/goofy-e2e/src/fixtures/user/user_emil.json b/goofy-client/apps/goofy-e2e/src/fixtures/user/user_emil.json
index e2c1804a7d..b5e05e2c6f 100644
--- a/goofy-client/apps/goofy-e2e/src/fixtures/user/user_emil.json
+++ b/goofy-client/apps/goofy-e2e/src/fixtures/user/user_emil.json
@@ -5,5 +5,6 @@
 	"lastName": "Ernst",
 	"fullName": "Emil Ernst",
 	"initials": "EE",
-	"dataTestId": "Emil_Ernst"
+	"dataTestId": "Emil_Ernst",
+	"einheitlicherAnsprechpartner": true
 }
\ No newline at end of file
diff --git a/goofy-client/apps/goofy-e2e/src/fixtures/user/user_sabine.json b/goofy-client/apps/goofy-e2e/src/fixtures/user/user_sabine.json
index 4bdb6c1763..65897a1c18 100644
--- a/goofy-client/apps/goofy-e2e/src/fixtures/user/user_sabine.json
+++ b/goofy-client/apps/goofy-e2e/src/fixtures/user/user_sabine.json
@@ -6,5 +6,6 @@
 	"lastName": "Solala",
 	"fullName": "Sabine Solala",
 	"initials": "SS",
-	"dataTestId": "Sabine_Solala"
+	"dataTestId": "Sabine_Solala",
+	"verwaltungUser": true
 }
\ No newline at end of file
diff --git a/goofy-client/apps/goofy-e2e/src/fixtures/user/user_zonk.json b/goofy-client/apps/goofy-e2e/src/fixtures/user/user_zonk.json
index 3aa593b0cd..eab922b306 100644
--- a/goofy-client/apps/goofy-e2e/src/fixtures/user/user_zonk.json
+++ b/goofy-client/apps/goofy-e2e/src/fixtures/user/user_zonk.json
@@ -1,4 +1,5 @@
 {
 	"name": "zonk",
-	"password": "Zonk4Nothing"
+	"password": "Zonk4Nothing",
+	"verwaltungUser": true
 }
\ No newline at end of file
-- 
GitLab