Groups

Create an RStudio Connect Group from LDAP

The following snippets search for a group in LDAP and then create an RStudio Connect group for that LDAP group.

There are two steps:

  1. Search for the group via the GET /groups/remote endpoint. A group that does not exist on Connect will lack a guid. Note the temp_ticket for the desired group.
  2. Use the PUT /groups endpoint with the temp_ticket to create a corresponding group on RStudio Connect.

Search for a Group

# set up environment
library(httr)
# Note that the connectServer string must have a trailing slash
connectServer <- Sys.getenv("CONNECT_SERVER")
apiKey <- Sys.getenv("CONNECT_API_KEY")

# set the search parameter
prefix <- "accounting"

# make the query request
authHeader <- paste("Key", apiKey)
response <- GET(
    paste0(connectServer, "__api__/v1/groups/remote"),
    add_headers(Authorization = authHeader),
    query = list(prefix = prefix)
)

results <- content(response)$results

# print the results of the API call
formatGuid <- function(guid) {
    if (is.null(guid))
        "NULL"
    else
        guid
}
cat(sprintf("NAME\tGUID\n"))
for (group in results) {
    cat(
        sprintf(
            "%s\t%s\n",
            group$name,
            formatGuid(group$guid)
        )
    )
}

The output looks like the following:

NAME          GUID
accounting1   15f5f51d-08ff-4e5b-beba-4ccf24e248dd
accounting2   NULL

In this particular case, there are two groups matching the search for the prefix accounting. The group accounting1 has a GUID value, which means that this group already exists in RStudio Connect. The group accounting2 does not yet have a corresponding group in RStudio Connect.

Included in the API response for each group is a temp_ticket value that can be used to create the group in RStudio Connect. In the example above, the second group, accounting2, does not exist in RStudio Connect, so you will need the temp_ticket for this group:

tempTicket <- results[[2]]$temp_ticket

You can use this tempTicket value in the next section to create the group.

Create an RStudio Connect Group

Using the tempTicket value from the previous section, you can create an RStudio Connect group with an HTTP PUT request:

# use a tempTicket value from searching /groups/remote

response <- PUT(
    paste0(connectServer, "__api__/v1/groups"),
    add_headers(Authorization = authHeader),
    body = list(temp_ticket = tempTicket),
    encode = "json"
)

print(content(response))

When the call succeeds, the response will contain a non-NULL guid value, which is a unique identifier for the group.

If the group already exists in Connect, the response will contain an error:

$error
[1] "The requested group name is already in use."