Skip to content

Commit

Permalink
add fxns for check if user and group exists, add fxn to add user to g…
Browse files Browse the repository at this point in the history
…roup
  • Loading branch information
sckott committed Dec 13, 2023
1 parent 69e92e3 commit b56c390
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 1 deletion.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export(aws_file_upload)
export(aws_group)
export(aws_group_create)
export(aws_group_delete)
export(aws_group_exists)
export(aws_groups)
export(aws_iam_client)
export(aws_policies)
Expand All @@ -34,9 +35,11 @@ export(aws_role_delete)
export(aws_roles)
export(aws_user)
export(aws_user_access_key)
export(aws_user_add_to_group)
export(aws_user_create)
export(aws_user_current)
export(aws_user_delete)
export(aws_user_exists)
export(aws_users)
export(billing)
export(s3_path)
Expand Down
2 changes: 1 addition & 1 deletion R/bucket.R
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,6 @@ aws_bucket_acl_get <- function(bucket) {
#' aws_bucket_acl_get("s3://s64-test-2")
#' }
aws_bucket_acl_modify <- function(bucket, acl, ...) {
bucket <- path_s3_parser(bucket)[[1]]$bucket
bucket <- path_s3_parse(bucket)[[1]]$bucket
env64$s3$put_bucket_acl(ACL = acl, Bucket = bucket, ...)
}
16 changes: 16 additions & 0 deletions R/groups.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ aws_group <- function(name) {
)
}

#' Check if a group exists
#'
#' @export
#' @param name (character) the group name
#' @return a single boolean
#' @details uses `aws_group` internally. see docs
#' <https://www.paws-r-sdk.com/docs/iam_get_group/>
#' @examples \dontrun{
#' aws_group_exists(name="users")
#' aws_group_exists(name="apples")
#' }
aws_group_exists <- function(name) {
check_aws_group <- purrr::safely(aws_group, otherwise = FALSE)
is.null(check_aws_group(name)$error)
}

#' Create a group
#'
#' @export
Expand Down
38 changes: 38 additions & 0 deletions R/users.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ aws_user <- function(username = NULL) {
)
}

#' Check if a user exists
#'
#' @export
#' @param username (character) the user name
#' @return a single boolean
#' @details uses `aws_group` internally. see docs
#' <https://www.paws-r-sdk.com/docs/iam_get_group/>
#' @examples \dontrun{
#' aws_user_exists(aws_user_current())
#' aws_user_exists("blueberry")
#' }
aws_user_exists <- function(username) {
check_aws_user <- purrr::safely(aws_user, otherwise = FALSE)
is.null(check_aws_user(username)$error)
}

#' Get the current logged-in username as a string
#' @export
#' @return username as character
Expand Down Expand Up @@ -131,3 +147,25 @@ aws_user_delete <- function(username) {
aws_user_access_key <- function() {
env64$iam$list_access_keys()$AccessKeyMetadata[[1]] %>% as_tibble()
}

#' Add a user to a group
#'
#' @export
#' @inheritParams aws_user_create
#' @param groupname (character) a group name. required
#' @inherit aws_user return
#' @details See <https://www.paws-r-sdk.com/docs/iam_add_user_to_group/>
#' docs for more details
#' @examples \dontrun{
#' if (!aws_group_exists("testgroup3")) {
#' aws_group_create("testgroup3")
#' }
#' if (!aws_user_exists("testBlueBird3")) {
#' aws_user_create("testBlueBird3")
#' }
#' aws_user_add_to_group(username = "testBlueBird3", groupname = "testgroup3")
#' }
aws_user_add_to_group <- function(username, groupname) {
env64$iam$add_user_to_group(groupname, username)
aws_user(username)
}
27 changes: 27 additions & 0 deletions man/aws_group_exists.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions man/aws_user_add_to_group.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions man/aws_user_exists.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b56c390

Please sign in to comment.