From 67631747aeb4f2b835dc8a2ff30a4279c427d1aa Mon Sep 17 00:00:00 2001 From: jem Date: Fri, 9 Jul 2021 16:39:36 +0200 Subject: [PATCH] mob --- README.md | 3 ++ shadow-cljs.edn | 1 + src/main/clj/dda/c4k_common/yaml.clj | 11 +++-- src/main/cljs/dda/c4k_common/yaml.cljs | 21 +++++--- .../dda/c4k_common/yaml_test.clj} | 9 ++-- src/test/cljs/dda/c4k_common/yaml_test.cljs | 48 +++++++++++++++++++ .../resources/{ => test}/ingress_test.yaml | 0 7 files changed, 78 insertions(+), 15 deletions(-) rename src/test/{cljc/dda/c4k_common/yaml_test.cljc => clj/dda/c4k_common/yaml_test.clj} (85%) create mode 100644 src/test/cljs/dda/c4k_common/yaml_test.cljs rename src/test/resources/{ => test}/ingress_test.yaml (100%) diff --git a/README.md b/README.md index 338f71a..a0a2952 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,9 @@ Our convention 4 kubernetes c4k-* tools combine the advantages of both approache * yaml / edn as input and output, no more magic * good validation, integration as api, cli or in the browser +## Usage + + ## License Copyright © 2021 meissa GmbH diff --git a/shadow-cljs.edn b/shadow-cljs.edn index a4db323..81d3960 100644 --- a/shadow-cljs.edn +++ b/shadow-cljs.edn @@ -2,6 +2,7 @@ "src/main/cljs" "src/main/resources" "src/test/cljc" + "src/test/cljs" "src/test/resources"] :dependencies [[aero "1.1.6"] [orchestra "2021.01.01-1"] diff --git a/src/main/clj/dda/c4k_common/yaml.clj b/src/main/clj/dda/c4k_common/yaml.clj index 7a4221f..95592ab 100644 --- a/src/main/clj/dda/c4k_common/yaml.clj +++ b/src/main/clj/dda/c4k_common/yaml.clj @@ -10,9 +10,6 @@ (into [] %) %) lazy-seq)) -(defmethod load-resource :clj [resource-name] - (slurp (io/resource resource-name))) - (defn from-string [input] (cast-lazy-seq-to-vec (yaml/parse-string input))) @@ -20,6 +17,10 @@ (yaml/generate-string edn :dumper-options {:flow-style :block})) (defn dispatch-by-resource-name - [resource]) + [resource] + :clj) -(defmulti load-resource dispatch-by-resource-name) \ No newline at end of file +(defmulti load-resource dispatch-by-resource-name) + +(defmethod load-resource :clj [resource-name] + (slurp (io/resource resource-name))) \ No newline at end of file diff --git a/src/main/cljs/dda/c4k_common/yaml.cljs b/src/main/cljs/dda/c4k_common/yaml.cljs index 78c3f0d..64596c6 100644 --- a/src/main/cljs/dda/c4k_common/yaml.cljs +++ b/src/main/cljs/dda/c4k_common/yaml.cljs @@ -1,16 +1,23 @@ (ns dda.c4k-common.yaml (:require ["js-yaml" :as yaml] - [shadow.resource :as rc])) - -(defn load-resource [resource-name] - (case resource-name - "ingress_test.yaml" (rc/inline "ingress_test.yaml") - (throw (js/Error. "Undefined Resource!")))) + [shadow.resource :as rc] + [clojure.string :as st])) (defn from-string [input] (js->clj (yaml/load input) :keywordize-keys true)) (defn to-string [edn] - (yaml/dump (clj->js edn))) \ No newline at end of file + (yaml/dump (clj->js edn))) + +(defn dispatch-by-resource-name + [resource] + (keyword (first (st/split resource #"/")))) + +(defmulti load-resource dispatch-by-resource-name) + +(defmethod load-resource :test [resource-name] + (case resource-name + "test/ingress_test.yaml" (rc/inline "test/ingress_test.yaml") + (throw (js/Error. "Undefined Resource!")))) \ No newline at end of file diff --git a/src/test/cljc/dda/c4k_common/yaml_test.cljc b/src/test/clj/dda/c4k_common/yaml_test.clj similarity index 85% rename from src/test/cljc/dda/c4k_common/yaml_test.cljc rename to src/test/clj/dda/c4k_common/yaml_test.clj index 2857070..6a16d50 100644 --- a/src/test/cljc/dda/c4k_common/yaml_test.cljc +++ b/src/test/clj/dda/c4k_common/yaml_test.clj @@ -1,9 +1,12 @@ (ns dda.c4k-common.yaml-test (:require - #?(:clj [clojure.test :refer [deftest is are testing run-tests]] - :cljs [cljs.test :refer-macros [deftest is are testing run-tests]]) + [clojure.test :refer [deftest is are testing run-tests]] [dda.c4k-common.yaml :as cut])) +(deftest should-dispatch-by-resource-name + (is (= :clj + (cut/dispatch-by-resource-name "postgres/etc")))) + (deftest should-parse-yaml-string (is (= {:hallo "welt"} (cut/from-string "hallo: welt")))) @@ -42,4 +45,4 @@ [{:backend {:serviceName "another_keycloak" :servicePort 8081}}]}}]}} - (cut/from-string (cut/load-resource "ingress_test.yaml"))))) + (cut/from-string (cut/load-resource "test/ingress_test.yaml"))))) diff --git a/src/test/cljs/dda/c4k_common/yaml_test.cljs b/src/test/cljs/dda/c4k_common/yaml_test.cljs new file mode 100644 index 0000000..65c4a1c --- /dev/null +++ b/src/test/cljs/dda/c4k_common/yaml_test.cljs @@ -0,0 +1,48 @@ +(ns dda.c4k-common.yaml-test + (:require + [cljs.test :refer-macros [deftest is are testing run-tests]] + [dda.c4k-common.yaml :as cut])) + +(deftest should-dispatch-by-resource-name + (is (= :postgres + (cut/dispatch-by-resource-name "postgres/etc")))) + +(deftest should-parse-yaml-string + (is (= {:hallo "welt"} + (cut/from-string "hallo: welt")))) + +(deftest should-generate-yaml-string + (is (= "hallo: welt +" + (cut/to-string {:hallo "welt"})))) + +(deftest should-convert-config-yml-to-map + (is (= {:apiVersion "networking.k8s.io/v1beta1" + :kind "Ingress" + :metadata + {:name "ingress-cloud" + :annotations + {:cert-manager.io/cluster-issuer + "letsencrypt-staging-issuer" + :nginx.ingress.kubernetes.io/proxy-body-size "256m" + :nginx.ingress.kubernetes.io/ssl-redirect "true" + :nginx.ingress.kubernetes.io/rewrite-target "/" + :nginx.ingress.kubernetes.io/proxy-connect-timeout "300" + :nginx.ingress.kubernetes.io/proxy-send-timeout "300" + :nginx.ingress.kubernetes.io/proxy-read-timeout "300"} + :namespace "default"} + :spec + {:tls [{:hosts ["fqdn"], :secretName "keycloak-secret"}] + :rules + [{:host "fqdn" + :http + {:paths + [{:backend + {:serviceName "keycloak", :servicePort 8080}}]}} + {:host "fqdn" + :http + {:paths + [{:backend + {:serviceName "another_keycloak" + :servicePort 8081}}]}}]}} + (cut/from-string (cut/load-resource "test/ingress_test.yaml"))))) diff --git a/src/test/resources/ingress_test.yaml b/src/test/resources/test/ingress_test.yaml similarity index 100% rename from src/test/resources/ingress_test.yaml rename to src/test/resources/test/ingress_test.yaml