Skip to content

Commit b140544

Browse files
Add function to get_runtimes() from Connect server (#320)
Co-authored-by: Barret Schloerke <[email protected]>
1 parent b79b311 commit b140544

File tree

9 files changed

+215
-4
lines changed

9 files changed

+215
-4
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export(get_my_permission)
8585
export(get_oauth_credentials)
8686
export(get_procs)
8787
export(get_runtime_caches)
88+
export(get_runtimes)
8889
export(get_tag_data)
8990
export(get_tags)
9091
export(get_thumbnail)

NEWS.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
remote groups and checking for existing local groups. (#216)
1313
- New functions to let you view and delete runtime caches on a Connect server:
1414
`get_runtime_caches()`, `delete_runtime_cache()` (#312)
15-
15+
- New `get_runtimes()` lets you view available runtimes and versions on a
16+
Connect server. (#311)
1617

1718
## Lifecycle changes
1819

@@ -23,6 +24,8 @@
2324
`set_thumbnail()`, which works both with local file paths and remote URLs to
2425
images. Likewise, `has_image()` and `delete_image()` have been deprecated in
2526
favor of `has_thumbnail()` and `delete_thumbnail()`. (#294, #304)
27+
- `Connect$server_settings_r()` has been deprecated in favor of
28+
`get_runtimes(client, "r")`. (#311)
2629

2730
## Minor improvements and fixes
2831

R/connect.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,7 @@ Connect <- R6::R6Class(
824824

825825
#' @description Get R installations.
826826
server_settings_r = function() {
827+
lifecycle::deprecate_soft("0.3.1", "Connect$server_settings_r()", "get_runtimes()")
827828
self$GET(v1_url("server_settings", "r"))
828829
},
829830

R/get.R

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,3 +702,59 @@ get_oauth_credentials = function(connect, user_session_token) {
702702
body = body
703703
)
704704
}
705+
706+
#' Get available runtimes on server
707+
#'
708+
#' Get a table showing available versions of R, Python, Quarto, and Tensorflow
709+
#' on the Connect server.
710+
#'
711+
#' @param client A `Connect` object.
712+
#' @param runtimes Optional. A character vector of runtimes to include. Must be
713+
#' some combination of `"r"`, `"python"`, `"quarto"`, and `"tensorflow"`. Quarto
714+
#' is only supported on Connect >= 2021.08.0, and Tensorflow is only supported
715+
#' on Connect >= 2024.03.0.
716+
717+
#' @return A tibble with columns for `runtime`, `version`, and `cluster_name`
718+
#' and `image_name`. Cluster name and image name are only meaningful on Connect
719+
#' instances running off-host execution.
720+
#'
721+
#' @examples
722+
#' \dontrun{
723+
#' library(connectapi)
724+
#' client <- connect()
725+
#' get_runtimes(client, runtimes = c("r", "python", "tensorflow"))
726+
#' }
727+
#'
728+
#' @export
729+
get_runtimes <- function(client, runtimes = NULL) {
730+
validate_R6_class(client, "Connect")
731+
732+
# Construct valid runtimes for the Connect version.
733+
supported <- c(
734+
"r",
735+
"python",
736+
if (compare_connect_version(client$version, "2021.08.0") >= 0) {
737+
"quarto"
738+
},
739+
if (compare_connect_version(client$version, "2024.05.0") >= 0) {
740+
"tensorflow"
741+
}
742+
)
743+
if (is.null(runtimes)) {
744+
runtimes <- supported
745+
} else {
746+
if (any(!runtimes %in% supported)) {
747+
stop(glue::glue(
748+
"`runtimes` must be one of ",
749+
"{paste(paste0('\"', supported, '\"'), collapse = ', ')}; ",
750+
"received: {paste(paste0('\"', runtimes, '\"'), collapse = ', ')}."
751+
))
752+
}
753+
}
754+
755+
purrr::map_dfr(runtimes, function(runtime) {
756+
res <- client$GET(paste0("v1/server_settings/", runtime))
757+
res_df <- purrr::map_dfr(res$installations, ~tibble::as_tibble(.))
758+
tibble::add_column(res_df, runtime = runtime, .before = 1)
759+
})
760+
}

man/get_groups.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/get_runtimes.Rd

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/setup.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ MockConnect <- R6Class(
7979
# same URL.
8080
mock_response = function(method, path, content, status_code = 200L, headers = c("Content-Type" = "application/json; charset=utf-8")) {
8181
url <- self$api_url(path)
82-
82+
route <- paste(method, url)
83+
8384
res <- new_mock_response(url, content, status_code, headers)
8485

8586
route <- paste(method, url)

tests/testthat/test-get.R

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
test_that("get_runtimes() gets all runtimes if none specified", {
2+
client <- MockConnect$new("2024.10.0")
3+
4+
client$mock_response(
5+
"GET", "v1/server_settings/r",
6+
content = list(
7+
installations = list(
8+
list(version = "4.3.1", cluster_name = "Local", image_name = "Local"),
9+
list(version = "4.4.0", cluster_name = "Local", image_name = "Local")
10+
)
11+
)
12+
)
13+
client$mock_response(
14+
"GET", "v1/server_settings/python",
15+
content = list(
16+
installations = list(
17+
list(version = "3.11.3", cluster_name = "Local", image_name = "Local"),
18+
list(version = "3.12.4", cluster_name = "Local", image_name = "Local")
19+
)
20+
)
21+
)
22+
client$mock_response(
23+
"GET", "v1/server_settings/quarto",
24+
content = list(
25+
installations = list(
26+
list(version = "1.4.557", cluster_name = "Local", image_name = "Local"),
27+
list(version = "1.5.55", cluster_name = "Local", image_name = "Local")
28+
)
29+
)
30+
)
31+
client$mock_response(
32+
"GET", "v1/server_settings/tensorflow",
33+
content = list(
34+
installations = list(
35+
list(version = "2.16.1", cluster_name = "Local", image_name = "Local")
36+
)
37+
)
38+
)
39+
40+
expected <- tibble::as_tibble(list(
41+
runtime = c(
42+
"r", "r", "python", "python", "quarto",
43+
"quarto", "tensorflow"
44+
), version = c(
45+
"4.3.1", "4.4.0", "3.11.3",
46+
"3.12.4", "1.4.557", "1.5.55", "2.16.1"
47+
), cluster_name = c(
48+
"Local",
49+
"Local", "Local", "Local", "Local", "Local", "Local"
50+
), image_name = c(
51+
"Local",
52+
"Local", "Local", "Local", "Local", "Local", "Local"
53+
)
54+
))
55+
expect_identical(get_runtimes(client), expected)
56+
expect_identical(client$call_log, c(
57+
"GET https://connect.example/__api__/v1/server_settings/r",
58+
"GET https://connect.example/__api__/v1/server_settings/python",
59+
"GET https://connect.example/__api__/v1/server_settings/quarto",
60+
"GET https://connect.example/__api__/v1/server_settings/tensorflow"
61+
))
62+
})
63+
64+
test_that("get_runtimes() only specified runtimes", {
65+
client <- MockConnect$new("2024.10.0")
66+
67+
client$mock_response(
68+
"GET", "v1/server_settings/python",
69+
content = list(
70+
installations = list(
71+
list(version = "3.11.3", cluster_name = "Local", image_name = "Local"),
72+
list(version = "3.12.4", cluster_name = "Local", image_name = "Local")
73+
)
74+
)
75+
)
76+
client$mock_response(
77+
"GET", "v1/server_settings/tensorflow",
78+
content = list(
79+
installations = list(
80+
list(version = "2.16.1", cluster_name = "Local", image_name = "Local")
81+
)
82+
)
83+
)
84+
85+
expected <- tibble::as_tibble(list(
86+
runtime = c("python", "python", "tensorflow"),
87+
version = c("3.11.3", "3.12.4", "2.16.1"),
88+
cluster_name = c("Local", "Local", "Local"),
89+
image_name = c("Local", "Local", "Local")
90+
))
91+
expect_identical(get_runtimes(client, c("python", "tensorflow")), expected)
92+
expect_identical(client$call_log, c(
93+
"GET https://connect.example/__api__/v1/server_settings/python",
94+
"GET https://connect.example/__api__/v1/server_settings/tensorflow"
95+
))
96+
})
97+
98+
test_that("get_runtimes() restricts available runtimes based on Connect version.", {
99+
client <- MockConnect$new("2024.10.0")
100+
expect_error(
101+
get_runtimes(client, c("r", "python", "foofy")),
102+
'`runtimes` must be one of "r", "python", "quarto", "tensorflow"; received: "r", "python", "foofy".'
103+
)
104+
105+
client <- MockConnect$new("2024.02.0")
106+
expect_error(
107+
get_runtimes(client, "tensorflow"),
108+
'`runtimes` must be one of "r", "python", "quarto"; received: "tensorflow".'
109+
)
110+
111+
client <- MockConnect$new("1.8.3")
112+
expect_error(
113+
get_runtimes(client, c("r", "quarto")),
114+
'`runtimes` must be one of "r", "python"; received: "r", "quarto".'
115+
)
116+
})
117+

tests/testthat/test-schedule.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
test_that("get_timezones() gets timeszones from v1 url when available", {
2-
# With version available
32
client <- MockConnect$new()
43

54
client$mock_response(

0 commit comments

Comments
 (0)