Switch devops environments via namespaces

61 views
Skip to first unread message

Felix Dorner

unread,
May 18, 2023, 1:02:37 PM5/18/23
to Clojure
Clojure noob here, I might be totally off the track or committing crimes, so please advise.

For ops, I have to often call rest services in different environments, say prod and nonprod. My coworkers use postman and clicky clicky stuff, well, not my style, I want to use emacs and cider. I've thus written a set of functions that call these services, taking the prod/nonprod server as an argument:

(ns services)
(defn rest [server arg1 arg2] ...)

I'm thinking how it would feel to get rid of the server parameter, and resolve it inside the function, depending on the current namespace. I can then just leave my cider open, use Cider shortcuts to switch to the prod/nonprod namespaces quickly and run my stuff. Also, I would be constantly aware of which environment I'm in by looking at the repl prompt. Sounds neat somehow. But I feel I am venturing off the track somehow, but this seems to work:

(ns prod
  (:require
   [services :refer :all]))

(def server "http://prod-server.com")

(ns nonprod
  (:require
   [services :refer :all]))


(ns services)
;; no more server arg...
(defn call-a [arg1 arg2]
  (let [server @(resolve 'server)]
     ;; ... do stuff with server))

What do I miss? Complete nonsense? Better solutions?
Thanks again for comments,
Felix



Joe R. Smith

unread,
May 18, 2023, 2:18:11 PM5/18/23
to clo...@googlegroups.com
I mean, it works, it’s clever, … is it more or less “safe” is my question. 


On May 18, 2023, at 12:02 PM, Felix Dorner <felix....@gmail.com> wrote:


--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/clojure/f16c7ac6-107c-48fd-a6af-c9e91687fe18n%40googlegroups.com.

aditya....@gmail.com

unread,
May 18, 2023, 11:31:17 PM5/18/23
to Clojure
So from an ops point of view, I like your first approach; viz. pass the environment parameter explicitly.

I prefer being explicit about every single invocation of any ops task.

The namespace solution, while being "legal", causes the configuration to become implicit, one has to look at the REPL prompt to "know", rather than be confident that we told the function exactly where to call.

From a Clojure sensibilities point of view, what was stateless (explicit param) becomes stateful (implicit global state with def server). This can cause all manner of subtle issues, e.g. accidentally referring to the var instead of the resolved var in some service function.

Personally, I would go with your original approach, because it forces me to slow down a little and pay attention, while I'm typing, every single time. This is in service of human error mitigation. I would say the explicit workflow is close to the "pointing and calling" method https://en.wikipedia.org/wiki/Pointing_and_calling
Reply all
Reply to author
Forward
0 new messages