|
9 | 9 | #' NOTE: there can be problems with usernames that are not unique. Please open
|
10 | 10 | #' an issue if you run into any problems.
|
11 | 11 | #'
|
12 |
| -#' @param connect A R6 Connect object |
13 |
| -#' @param prefix character. The prefix of the user name to search for |
14 |
| -#' @param expect number. Optional. The number of responses to expect for this search |
15 |
| -#' @param check boolean. Optional. Whether to check for local existence first |
| 12 | +#' @param connect An R6 Connect object. |
| 13 | +#' @param prefix character. The prefix of the user name to search for. |
| 14 | +#' @param expect number. Optional. The number of responses to expect for this search. |
| 15 | +#' @param check boolean. Optional. Whether to check for local existence first. |
16 | 16 | #' @param exact boolean. Optional. Whether to only create users whose username
|
17 | 17 | #' exactly matches the provided `prefix`.
|
18 | 18 | #'
|
19 |
| -#' @return The results of creating the users |
| 19 | +#' @return The results of creating the users. |
20 | 20 | #'
|
21 | 21 | #' @export
|
22 | 22 | users_create_remote <- function(connect, prefix, expect = 1, check = TRUE, exact = FALSE) {
|
@@ -63,39 +63,50 @@ users_create_remote <- function(connect, prefix, expect = 1, check = TRUE, exact
|
63 | 63 |
|
64 | 64 | #' Create a Remote Group
|
65 | 65 | #'
|
66 |
| -#' @param connect A R6 Connect object |
67 |
| -#' @param prefix character. The prefix of the group name to search for |
68 |
| -#' @param expect number. The number of responses to expect for this search |
69 |
| -#' @param check boolean. Whether to check for local existence first |
| 66 | +#' @param connect An R6 Connect object. |
| 67 | +#' @param prefix character. The prefix of the user name to search for. |
| 68 | +#' @param expect number. Optional. The number of responses to expect for this search. |
| 69 | +#' @param check boolean. Optional. Whether to check for local existence first. |
| 70 | +#' @param exact boolean. Optional. Whether to only create groups whose name |
| 71 | +#' exactly matches the provided `prefix`. |
70 | 72 | #'
|
71 |
| -#' @return The results of creating the groups |
| 73 | +#' @return The results of creating the groups. |
72 | 74 | #'
|
73 | 75 | #' @export
|
74 |
| -groups_create_remote <- function(connect, prefix, expect = 1, check = TRUE) { |
| 76 | +groups_create_remote <- function(connect, prefix, expect = 1, check = TRUE, exact = FALSE) { |
75 | 77 | expect <- as.integer(expect)
|
76 |
| - if (check && expect > 1) { |
77 |
| - stop(glue::glue("expect > 1 is not tested. Please set expect = 1, and specify a more narrow 'prefix'. You provided: expect={expect}")) |
78 |
| - } |
79 | 78 | if (check) {
|
80 | 79 | # TODO: limit = 1 due to a paging bug in Posit Connect
|
81 |
| - local_groups <- get_groups(connect, prefix = prefix, limit = 1) |
| 80 | + local_groups <- get_groups(connect, page_size = 500, prefix = prefix, limit = 1) |
| 81 | + if (exact) { |
| 82 | + local_groups <- local_groups[local_groups["name"] == prefix, ] |
| 83 | + } |
82 | 84 | if (nrow(local_groups) > 0) {
|
83 |
| - message(glue::glue("At least one group with name prefix '{prefix}' already exists")) |
| 85 | + if (!exact) { |
| 86 | + message(glue::glue("At least one group with name prefix '{prefix}' already exists")) |
| 87 | + } else { |
| 88 | + message(glue::glue("A group with the name '{prefix}' already exists")) |
| 89 | + |
| 90 | + } |
84 | 91 | return(local_groups)
|
85 | 92 | }
|
86 | 93 | }
|
87 | 94 |
|
88 | 95 | remote_groups <- connect$groups_remote(prefix = prefix)
|
89 |
| - if (remote_groups$total != expect) { |
90 |
| - message(glue::glue("Found {remote_groups$total} remote groups. Expected {expect}")) |
91 |
| - if (remote_groups$total > 0) { |
92 |
| - group_str <- toString(purrr::map_chr(remote_groups$results, ~ .x[["name"]])) |
93 |
| - message(glue::glue("Groups found: {group_str}")) |
| 96 | + remote_groups_res <- remote_groups[["results"]] |
| 97 | + if (exact) { |
| 98 | + remote_groups_res <- purrr::keep(remote_groups_res, ~ .x[["name"]] == prefix) |
| 99 | + } |
| 100 | + if (length(remote_groups_res) != expect) { |
| 101 | + message(glue::glue("Found {length(remote_groups_res)} remote groups. Expected {expect}")) |
| 102 | + if (length(remote_groups_res) > 0) { |
| 103 | + groups_found <- glue::glue_collapse(purrr::map_chr(remote_groups_res, ~ .x[["name"]]), sep = ", ") |
| 104 | + message(glue::glue("Groups found: {groups_found}")) |
94 | 105 | }
|
95 | 106 | stop("The expected group(s) were not found. Please specify a more accurate 'prefix'")
|
96 | 107 | }
|
97 | 108 | group_creation <- purrr::map(
|
98 |
| - remote_groups$results, |
| 109 | + remote_groups_res, |
99 | 110 | function(.x, src) {
|
100 | 111 | message(glue::glue("Creating remote group: {.x[['name']]}"))
|
101 | 112 | src$groups_create_remote(temp_ticket = .x[["temp_ticket"]])
|
|
0 commit comments