Skip to content

Instantly share code, notes, and snippets.

@leifericf

leifericf/bb.edn Secret

Last active December 19, 2023 14:05
Show Gist options
  • Save leifericf/2574d715a938b9fb0e98418ba4d77f36 to your computer and use it in GitHub Desktop.
Save leifericf/2574d715a938b9fb0e98418ba4d77f36 to your computer and use it in GitHub Desktop.
Fetch Pulumi Stack data for an organization
{:paths ["src"]
:deps {http-kit/http-kit {:mvn/version "2.8.0-beta3"}
version-clj/version-clj {:mvn/version "2.0.2"}}}
; Docs: https://www.pulumi.com/docs/pulumi-cloud/cloud-rest-api/#list-stacks
(ns pulumi
(:require [cheshire.core :as json]
[org.httpkit.client :as http-kit]))
(def pulumi-uri "https://api.pulumi.com")
; These environment variables must be set in your .bashrc or .zshrc
(def pulumi-access-token (System/getenv "PULUMI_ACCESS_TOKEN"))
(def pulumi-org (System/getenv "PULUMI_ORG"))
(def base-headers
{"Accept" "application/vnd.pulumi+8"
"Content-Type" "application/json"
"Authorization" (str "token " pulumi-access-token)})
(defn fetch-stacks [request callback]
(http-kit/request (assoc request
:url (str pulumi-uri "/api/user/stacks")
:method :get)
callback))
(def base-request
{:headers base-headers
:query-params {:organization pulumi-org}})
(defn response-body->json [response]
(-> (:body response)
json/parse-string))
(defn get-stack-names [stacks]
(for [stacks (get @stacks "stacks")]
(get-in stacks ["stackName"])))
(comment
; Fetch all Stacks from Pulumi's API.
(def stacks (fetch-stacks base-request response-body->json))
; Check if the response contains a continuation token.
(get @stacks "continuationToken")
; Which stack names did we get?
(-> stacks
get-stack-names
sort)
; How many stack names did we get?
(-> stacks
get-stack-names
count)
)
@leifericf
Copy link
Author

leifericf commented Dec 19, 2023

Like this?

(ns pulumi
  (:require [cheshire.core :as json]
            [org.httpkit.client :as http-kit]))

(def pulumi-uri "https://api.pulumi.com")

; Set these environment variables in your .bashrc or .zshrc
(def pulumi-access-token (System/getenv "PULUMI_ACCESS_TOKEN"))
(def pulumi-org (System/getenv "PULUMI_ORG"))

(def headers
  {"Accept" "application/vnd.pulumi+8"
   "Content-Type" "application/json"
   "Authorization" (str "token " pulumi-access-token)})

(def stack-request
  {:headers headers
   :query-params {:organization pulumi-org}
   :url (str pulumi-uri "/api/user/stacks")
   :method :get})

(defn response-body->json [response]
  (json/parse-string (:body response)))

(comment
  ; Docs: https://www.pulumi.com/docs/pulumi-cloud/cloud-rest-api/#list-stacks

  ; Fetch first batch of Stacks from Pulumi's API.
  (def stacks @(http-kit/request stack-request response-body->json))

  ; Check if the response contains a continuation token.
  (get stacks "continuationToken")

  ; Which stack names did we get?
  (def stack-names
    (for [stack (get stacks "stacks")]
      (get stack "stackName")))

  ; How many stack names did we get?
  (count stack-names))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment