Skip to content

Instantly share code, notes, and snippets.

@rjsheperd
Created April 2, 2022 12:26
Show Gist options
  • Save rjsheperd/c925d9c7d45819339be9629430686b06 to your computer and use it in GitHub Desktop.
Save rjsheperd/c925d9c7d45819339be9629430686b06 to your computer and use it in GitHub Desktop.
Update All GeoServer Layers to Style
#!/usr/bin/env bb
(def gs-creds {:url "https://<url-to-geoserver>/geoserver/rest"
:user "admin"
:pass "<password>"})
(def style "special-style")
(def auth {:basic-auth [(:user gs-creds) (:pass gs-creds)]})
(defn get-layers []
(-> (curl/get (str (:url gs-creds) "/layers.json") auth)
(:body)
(json/parse-string true)
(get-in [:layers :layer])))
(defn get-style [layer]
(-> (curl/get (:href layer) auth)
(:body)
(json/parse-string true)
(get-in [:layer :defaultStyle :name])))
(def style-json (json/generate-string {:layer {:defaultStyle {:name style :href (str (:url gs-creds) "/styles/" style ".json")}}}))
(defn update-style! [layer]
(when (not= style (get-style layer))
(println "Updating layer" (:name layer))
(curl/put (:href layer) (merge auth {:headers {"Content-Type" "application/json"} :body style-json}))))
(pmap update-style! (get-layers))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment