 (defn perm
   "A permission interceptor on systems access, we check both env and owner persmissions.
   due to the way robert.hooke works we analyse args and not fn to decide what to verify on.
-  If we have a map we assume its a system if we have a number we assume its an id." 
+  If we have a map we assume its a system if we have a number we assume its an id."
   [f & args]
-  (let [system (first (filter map? args)) 
+  (let [system (first (filter map? args))
         id (first (filter #(or (number? %) (and (string? %) (re-find #"\d+" %))) args))
         skip (first (filter #{:skip-assert} args))]
     (when-not skip
       (trace "perm checking" f args)
-      (if (is-system? system) 
+      (if (is-system? system)
         (assert-access system)
-        (assert-access (get-system id :skip-assert)))) 
+        (assert-access (get-system id :skip-assert))))
     (if skip (apply f (butlast args)) (apply f args))))
 
-(defn decrease-quota 
-   "reducing usage quotas for owning user on delete" 
+(defn decrease-quota
+   "reducing usage quotas for owning user on delete"
    [f & args]
   (let [system (first (filter map? args))]
    (when (is-system? system) (q/decrease-use system)))
    (apply f args))
 
-(defn increase-quota 
-   "reducing usage quotas for owning user on delete" 
-   [f & args]
-   (if (map? (first args)) 
-     (let [id (apply f args) spec (first args)]  
+(defn increase-quota
+   "reducing usage quotas for owning user on delete"
+   [f & args]
+   (if (map? (first args))
+     (let [id (apply f args) spec (first args)]
        (q/quota-assert spec)
        (q/increase-use spec) id)
      (apply f args)))
 
 (defn es-put
-   "runs a specified es function on system fn call" 
-   [f & args]
-     (if (map? (first args)) 
-      (let [id (apply f args) spec (first args)]  
+   "runs a specified es function on system fn call"
+   [f & args]
+     (if (map? (first args))
+      (let [id (apply f args) spec (first args)]
         (es/put (str id) spec) id)
       (apply f args)))
 
 (defn es-delete
-   "reducing usage quotas for owning user on delete" 
+   "reducing usage quotas for owning user on delete"
    [f & args]
   (let [system (first (filter map? args)) id (first (filter number? args))]
-   (when-not (is-system? system) 
+   (when-not (is-system? system)
      (es/delete (str id) :flush? true)))
    (apply f args))
 

      (flatten (map #(get-system-index :env (keyword %)) envs))
      (get-system-index :owner username))))

(defn re-index 
   "Re-indexes all systems available to the current user under elasticsearch."
   [username]
   (es/re-index (map #(vector % (get-system %)) (systems-for username))))

  (validate! system system-base :error ::non-valid-system)
  (check-validity system))

(defn clone-system
  "clones an existing system"
  [id {:keys [hostname owner] :as spec}]
  (add-system
    (-> (get-system id)
      (assoc :owner owner)
      (assoc-in [:machine :hostname] hostname)
      (clone spec))))

