factsets.clj 2.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
(ns puppetlabs.puppetdb.factsets
  (:require [clojure.edn :as clj-edn]
            [clojure.set :as set]
            [clojure.string :as str]
            [puppetlabs.kitchensink.core :as kitchensink]
            [puppetlabs.puppetdb.cheshire :as json]
            [puppetlabs.puppetdb.facts :as facts]
            [puppetlabs.puppetdb.schema :as pls]
            [puppetlabs.puppetdb.scf.hash :as hash]
            [puppetlabs.puppetdb.scf.storage-utils :as sutils]
            [puppetlabs.puppetdb.utils :as utils]
            [schema.core :as s]))

;; SCHEMA

(def fact-query-schema
  "Schema for a single fact map"
  {:name s/Str
   :value s/Any
   (s/optional-key :environment) s/Str
   (s/optional-key :certname) s/Str})

(def facts-expanded-query-schema
  "Facts expanded data format."
  {(s/optional-key :data) [fact-query-schema]
   :href String})

(def factset-query-schema
  "Final schema for a single factset."
  {(s/optional-key :certname) String
   (s/optional-key :environment) (s/maybe s/Str)
   (s/optional-key :timestamp) pls/Timestamp
   (s/optional-key :producer_timestamp) (s/maybe pls/Timestamp)
   (s/optional-key :hash) (s/maybe s/Str)
   (s/optional-key :facts) facts-expanded-query-schema})

(def facts-wireformat-schema
  {:certname s/Str
   :values {s/Keyword s/Any}
   :environment (s/maybe s/Str)
41
   :producer_timestamp (s/cond-pre pls/Timestamp (s/maybe s/Str))})
42 43 44

;; TRANSFORMATIONS

45
(pls/defn-validated fact-query->wire-v5
46 47 48 49 50 51 52 53 54
  [fact :- fact-query-schema]
  (-> fact
      (dissoc :environment :certname)))

(pls/defn-validated facts-list-to-map :- {s/Keyword s/Any}
  [facts :- [fact-query-schema]]
  (zipmap (map (comp keyword :name) facts)
          (map :value facts)))

55
(pls/defn-validated facts-expanded->wire-v5 :- {s/Keyword s/Any}
56 57
  [facts :- facts-expanded-query-schema]
  (facts-list-to-map
58
   (map fact-query->wire-v5
59 60
        (:data facts))))

61
(defn factsets-query->wire-v5 [factsets]
62 63 64
  (map
   #(-> %
        (dissoc :hash :timestamp)
65
        (update :facts facts-expanded->wire-v5)
66 67
        (set/rename-keys {:facts :values}))
   factsets))