Content Environments

Environments in use

This recipe contains an R Markdown document that presents a table detailing the images used by content deployed to a Posit Connect server.

Use this recipe only when your Connect installation uses the off-host execution model, where content is executed in containers using Kubernetes.

The document uses the GET /v1/content endpoint to obtain information about content. This API call returns every content item visible to your API key.

Use the GET /v1/environments endpoint to determine the environments available on your server.

---
title: "Environments in use"
---

This document analyzes the image names assigned to run content on the
Posit Connect server. The table presents the number of unique content items
currently using each image. Content reported with an `NA` value is either
incomplete or static (needing no runtime).


```{r setup, echo = FALSE, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r libraries, echo = FALSE, message = FALSE}
library(httr)
library(jsonlite)
library(magrittr)
library(dplyr)
library(knitr)

# Ignore summarise telling us about the grouping.
options(dplyr.summarise.inform = FALSE)
```

```{r verification, echo = FALSE}
# Confirm that environment variables are available.
connectServer <- Sys.getenv("CONNECT_SERVER")
if (nchar(connectServer) == 0) {
  stop("Set the CONNECT_SERVER environment variable.")
}
connectAPIKey <- Sys.getenv("CONNECT_API_KEY")
if (nchar(connectAPIKey) == 0) {
  stop("Set the CONNECT_API_KEY environment variable.")
}
contentURL <- paste0(connectServer, "/__api__/v1/content")
```

```{r fetch, echo = FALSE}
# Fetch all content items from Posit Connect.
res <- httr::GET(
  contentURL,
  httr::add_headers(Authorization = paste("Key", connectAPIKey)),
  httr::write_memory()
)
if (httr::http_error(res)) {
  err <- sprintf(
    "%s request failed with %s",
    res$request$url,
    httr::http_status(res)$message
  )
  message(capture.output(str(httr::content(res))))
  stop(err)
}
payload <- httr::content(res, as = "text")
apps <- jsonlite::fromJSON(payload, simplifyDataFrame = TRUE)
```

```{r report, echo = FALSE}
# Report the number of content items with each image name.
RunAs.accounts <- apps %>% 
  select(image_name) %>% 
  group_by(image_name) %>% 
  summarise(N = n()) %>% 
  arrange(desc(image_name)) %>%
  ungroup()

kable(RunAs.accounts)
```