diff --git a/DESCRIPTION b/DESCRIPTION index 042b92c..e5007ba 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,12 +1,12 @@ Package: aws.iam Type: Package Title: AWS IAM Client Package -Version: 0.1.6 -Date: 2017-06-30 +Version: 0.1.7 +Date: 2017-07-01 Author: Thomas J. Leeper Maintainer: Thomas J. Leeper -Description: A simple client package for the Amazon Web Services (AWS) Identity - and Access Management (IAM) API. +Description: A simple client for the Amazon Web Services ('AWS') Identity + and Access Management ('IAM') 'API' . License: GPL (>= 2) Imports: utils, diff --git a/NAMESPACE b/NAMESPACE index 9413ca4..1ba7016 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,12 @@ # Generated by roxygen2: do not edit by hand +S3method(print,iam_alias) +S3method(print,iam_group) +S3method(print,iam_instance_profile) +S3method(print,iam_key) +S3method(print,iam_policy) +S3method(print,iam_role) +S3method(print,iam_user) export(add_policy) export(add_profile_role) export(add_user) diff --git a/NEWS.md b/NEWS.md index b9d350a..8cdb8be 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +# CHANGES TO aws.iam 0.1.6 + +* Expanded documentation. + # CHANGES TO aws.iam 0.1.5 * Bump **aws.signature** dependency to 0.3.4. diff --git a/R/accesskey.R b/R/accesskey.R index 290005d..26d4b28 100644 --- a/R/accesskey.R +++ b/R/accesskey.R @@ -8,6 +8,24 @@ #' @template marker #' @template dots #' @return \code{create_user} and \code{get_user} return objects of class \dQuote{iam_user}. \code{update_user} and \code{delete_user} return a logical \code{TRUE} (if successful) or an error. \code{list_users} returns a list of IAM user objects. +#' @examples +#' \dontrun{ +#' # list access keys +#' list_keys() +#' +#' # create a user key +#' u <- create_user("example-user") +#' str(k <- create_key(u)) +#' +#' # toggle key status to inactive +#' update_key(k, u, "Inactive") +#' list_keys(u) +#' +#' # cleanup +#' delete_key(k) +#' delete_user(u) +#' } +#' @seealso \code{\link{create_user}} #' @export create_key <- function(user, ...) { query <- list(Action = "CreateAccessKey") diff --git a/R/get_account.R b/R/get_account.R index 9654e70..b153ced 100644 --- a/R/get_account.R +++ b/R/get_account.R @@ -4,6 +4,14 @@ #' @template dots #' @return A list containing various account details. #' @details \code{get_account} returns a list of account details. \code{credential_report} generates and/or retrieves a credential report. \code{auth_details} returns a list of group, user, role, and policy details. +#' @examples +#' \dontrun{ +#' # account details +#' get_aaccount() +#' +#' # big list of authorizations +#' auth_details() +#' } #' @export get_account <- function(...) { query <- list(Action = "GetAccountSummary") diff --git a/R/groups.R b/R/groups.R index 6efee6c..d726ea8 100644 --- a/R/groups.R +++ b/R/groups.R @@ -9,6 +9,29 @@ #' @template marker #' @template dots #' @return \code{create_group} and \code{get_group} return objects of class \dQuote{iam_group}. \code{update_group} and \code{delete_group}, \code{add_user}, and \code{remove_user} return a logical \code{TRUE} (if successful) or an error. \code{list_groups} returns a list of IAM group objects. \code{get_group_users} returns a list of objects of class \dQuote{iam_user}, with a \dQuote{iam_group} attribute. +#' @examples +#' \dontrun{ +#' list_groups() +#' +#' # create group +#' (g <- create_group("example")) +#' # rename +#' update_group(g, "example2") +#' list_groups() +#' +#' # create example user +#' u <- create_user("example-user") +#' # add user to group +#' add_user(u, "example2") +#' +#' get_group_users("example2") +#' +#' # cleanup +#' remove_user(u, "example2") +#' delete_user(u) +#' delete_group("example2") +#' } +#' @seealso \code{\link{create_user}}, \code{\link{create_role}}, #' @export create_group <- function(group, path, ...){ query <- list(Action = "CreateGroup") @@ -107,6 +130,7 @@ get_group_users <- function(group, n, marker, ...) { #' @export list_groups <- function(user, n, marker, path, ...) { if (!missing(user)) { + user <- get_username(user) query <- list(Action = "ListGroupsForUsers", UserName = user) } else { user <- NULL diff --git a/R/http.R b/R/http.R index 78c41a1..54ffd01 100644 --- a/R/http.R +++ b/R/http.R @@ -56,7 +56,7 @@ iamHTTP <- function(query, } else if (verb == "POST") { r <- POST(paste0("https://iam.amazonaws.com"), H, query = query, body = body, ...) } - if (http_status(r)$category == "Client error") { + if (http_error(r)) { x <- try(as_list(read_xml(content(r, "text", encoding = "UTF-8"))), silent = TRUE) if (inherits(x, "try-error")) { x <- try(fromJSON(content(r, "text", encoding = "UTF-8"))$Error, silent = TRUE) diff --git a/R/print.R b/R/print.R new file mode 100644 index 0000000..a082514 --- /dev/null +++ b/R/print.R @@ -0,0 +1,119 @@ +#' @export +print.iam_alias <- function(x, ...) { + if (!is.null(x[["AliasName"]])) { + cat("AliasName:", x[["AliasName"]], "\n") + } + invisible(x) +} + +#' @export +print.iam_group <- function(x, ...) { + if (!is.null(x[["GroupId"]])) { + cat("GroupId: ", x[["GroupId"]], "\n") + } + if (!is.null(x[["GroupName"]])) { + cat("GroupName: ", paste0(x[["Path"]], x[["GroupName"]]), "\n") + } + if (!is.null(x[["Arn"]])) { + cat("Arn: ", x[["Arn"]], "\n") + } + if (!is.null(x[["CreateDate"]])) { + cat("CreateDate:", x[["CreateDate"]], "\n") + } + invisible(x) +} + +#' @export +print.iam_key <- function(x, ...) { + if (!is.null(x[["AccessKeyId"]])) { + cat("AccessKeyId:", x[["AccessKeyId"]], "\n") + } + if (!is.null(x[["CreateDate"]])) { + cat("CreateDate: ", x[["CreateDate"]], "\n") + } + if (!is.null(x[["Status"]])) { + cat("Status: ", x[["Status"]], "\n") + } + if (!is.null(x[["UserName"]])) { + cat("UserName: ", x[["UserName"]], "\n") + } + invisible(x) +} + +#' @export +print.iam_policy <- function(x, ...) { + if (!is.null(x[["PolicyName"]])) { + cat("PolicyName:", x[["PolicyName"]], "\n") + } + if (!is.null(x[["RoleName"]])) { + cat("RoleName: ", x[["RoleName"]], "\n") + } + if (!is.null(x[["UserName"]])) { + cat("UserName: ", x[["UserName"]], "\n") + } + if (!is.null(x[["GroupName"]])) { + cat("GroupName: ", x[["GroupName"]], "\n") + } + if (!is.null(x[["PolicyDocument"]])) { + cat("policy: ", x[["PolicyDocument"]], "\n") + } + invisible(x) +} + +#' @export +print.iam_instance_profile <- function(x, ...) { + if (!is.null(x[["InstanceProfileName"]])) { + cat("InstanceProfileName: ", paste0(x[["Path"]], x[["InstanceProfileName"]]), "\n") + } + if (!is.null(x[["InstanceProfileId"]])) { + cat("InstanceProfileId: ", x[["InstanceProfileId"]], "\n") + } + if (!is.null(x[["Arn"]])) { + cat("Arn: ", x[["Arn"]], "\n") + } + if (!is.null(x[["CreateDate"]])) { + cat("CreateDate: ", x[["CreateDate"]], "\n") + } + if (!is.null(x[["Roles"]])) { + cat("Roles:\n") + print(x[["Roles"]]) + } + invisible(x) +} + +#' @export +print.iam_role <- function(x, ...) { + if (!is.null(x[["RoleName"]])) { + cat("RoleName: ", paste0(x[["Path"]], x[["RoleName"]]), "\n") + } + if (!is.null(x[["RoleId"]])) { + cat("RoleId: ", x[["RoleId"]], "\n") + } + if (!is.null(x[["Arn"]])) { + cat("Arn: ", x[["Arn"]], "\n") + } + if (!is.null(x[["CreateDate"]])) { + cat("CreateDate:", x[["CreateDate"]], "\n") + } + if (!is.null(x[["AssumeRolePolicyDocument"]])) { + cat("policy: ", x[["AssumeRolePolicyDocument"]], "\n") + } + invisible(x) +} + +#' @export +print.iam_user <- function(x, ...) { + if (!is.null(x[["UserName"]])) { + cat("UserName: ", paste0(x[["Path"]], x[["UserName"]]), "\n") + } + if (!is.null(x[["UserId"]])) { + cat("UserId: ", x[["UserId"]], "\n") + } + if (!is.null(x[["Arn"]])) { + cat("Arn: ", x[["Arn"]], "\n") + } + if (!is.null(x[["CreateDate"]])) { + cat("CreateDate:", x[["CreateDate"]], "\n") + } + invisible(x) +} diff --git a/R/roles.R b/R/roles.R index 9f21de8..da444cc 100644 --- a/R/roles.R +++ b/R/roles.R @@ -9,6 +9,7 @@ #' @template marker #' @template dots #' @return \code{create_role} and \code{get_role} return objects of class \dQuote{iam_role}. \code{update_role} and \code{delete_role} return a logical \code{TRUE} (if successful) or an error. \code{list_roles} returns a list of IAM role objects. +#' @seealso \code{\link{create_user}}, \code{\link{create_group}}, #' @export create_role <- function(role, policy, path, ...){ query <- list(Action = "CreateRole") diff --git a/R/user.R b/R/user.R index b587e20..3a31f72 100644 --- a/R/user.R +++ b/R/user.R @@ -8,6 +8,16 @@ #' @template marker #' @template dots #' @return \code{create_user} and \code{get_user} return objects of class \dQuote{iam_user}. \code{update_user} and \code{delete_user} return a logical \code{TRUE} (if successful) or an error. \code{list_users} returns a list of IAM user objects. +#' @examples +#' \dontrun{ +#' list_users() +#' +#' # create example user +#' u <- create_user("example-user") +#' +#' # cleanup +#' delete_user(u) +#' } #' @export create_user <- function(user, path, ...) { query <- list(Action = "CreateUser") @@ -79,6 +89,7 @@ get_user <- function(user, ...) { #' @export delete_user <- function(user, ...) { query <- list(Action = "DeleteUser") + user <- get_username(user) if (!missing(user)) { if (nchar(user) < 1 | nchar(user) > 128) { stop("'user' must be between 1 and 128 characters") diff --git a/R/utils.R b/R/utils.R index bef5aa7..f254496 100644 --- a/R/utils.R +++ b/R/utils.R @@ -67,116 +67,3 @@ get_rolename <- function(x) { x } } - -print.iam_alias <- function(x, ...) { - if (!is.null(x[["AliasName"]])) { - cat("AliasName:", x[["AliasName"]], "\n") - } - invisible(x) -} - -print.iam_group <- function(x, ...) { - if (!is.null(x[["GroupId"]])) { - cat("GroupId: ", x[["GroupId"]], "\n") - } - if (!is.null(x[["GroupName"]])) { - cat("GroupName: ", paste0(x[["Path"]], x[["GroupName"]]), "\n") - } - if (!is.null(x[["Arn"]])) { - cat("Arn: ", x[["Arn"]], "\n") - } - if (!is.null(x[["CreateDate"]])) { - cat("CreateDate:", x[["CreateDate"]], "\n") - } - invisible(x) -} - -print.iam_key <- function(x, ...) { - if (!is.null(x[["AccessKeyId"]])) { - cat("AccessKeyId:", x[["AccessKeyId"]], "\n") - } - if (!is.null(x[["CreateDate"]])) { - cat("CreateDate: ", x[["CreateDate"]], "\n") - } - if (!is.null(x[["Status"]])) { - cat("Status: ", x[["Status"]], "\n") - } - if (!is.null(x[["UserName"]])) { - cat("UserName: ", x[["UserName"]], "\n") - } - invisible(x) -} - -print.iam_policy <- function(x, ...) { - if (!is.null(x[["PolicyName"]])) { - cat("PolicyName:", x[["PolicyName"]], "\n") - } - if (!is.null(x[["RoleName"]])) { - cat("RoleName: ", x[["RoleName"]], "\n") - } - if (!is.null(x[["UserName"]])) { - cat("UserName: ", x[["UserName"]], "\n") - } - if (!is.null(x[["GroupName"]])) { - cat("GroupName: ", x[["GroupName"]], "\n") - } - if (!is.null(x[["PolicyDocument"]])) { - cat("policy: ", x[["PolicyDocument"]], "\n") - } - invisible(x) -} - -print.iam_instance_profile <- function(x, ...) { - if (!is.null(x[["InstanceProfileName"]])) { - cat("InstanceProfileName: ", paste0(x[["Path"]], x[["InstanceProfileName"]]), "\n") - } - if (!is.null(x[["InstanceProfileId"]])) { - cat("InstanceProfileId: ", x[["InstanceProfileId"]], "\n") - } - if (!is.null(x[["Arn"]])) { - cat("Arn: ", x[["Arn"]], "\n") - } - if (!is.null(x[["CreateDate"]])) { - cat("CreateDate: ", x[["CreateDate"]], "\n") - } - if (!is.null(x[["Roles"]])) { - cat("Roles:\n") - print(x[["Roles"]]) - } - invisible(x) -} - -print.iam_role <- function(x, ...) { - if (!is.null(x[["RoleName"]])) { - cat("RoleName: ", paste0(x[["Path"]], x[["RoleName"]]), "\n") - } - if (!is.null(x[["RoleId"]])) { - cat("RoleId: ", x[["RoleId"]], "\n") - } - if (!is.null(x[["Arn"]])) { - cat("Arn: ", x[["Arn"]], "\n") - } - if (!is.null(x[["CreateDate"]])) { - cat("CreateDate:", x[["CreateDate"]], "\n") - } - if (!is.null(x[["AssumeRolePolicyDocument"]])) { - cat("policy: ", x[["AssumeRolePolicyDocument"]], "\n") - } - invisible(x) -} - -print.iam_user <- function(x, ...) { - if (!is.null(x[["UserName"]])) { - cat("UserName: ", paste0(x[["Path"]], x[["UserName"]]), "\n") - } - if (!is.null(x[["UserId"]])) { - cat("UserId: ", x[["UserId"]], "\n") - } - if (!is.null(x[["Arn"]])) { - cat("Arn: ", x[["Arn"]], "\n") - } - if (!is.null(x[["CreateDate"]])) { - cat("CreateDate:", x[["CreateDate"]], "\n") - } - invisible(x) -} diff --git a/README.Rmd b/README.Rmd index d0e866d..a98728b 100644 --- a/README.Rmd +++ b/README.Rmd @@ -34,7 +34,35 @@ Temporary session tokens are stored in environment variable `AWS_SESSION_TOKEN` ## Code Examples -Coming soon... +The package gives basically fully access to IAM functionality to create and manage groups and users, including creating and managing AWS credentials on-the-fly. + +```{r} +library("aws.iam") + +# create user +u <- create_user("example-user") + +# create group +(g <- create_group("example")) +# rename group +update_group(g, "example2") + +# add user to group +add_user(u, "example2") +get_group_users("example2") + +# create AWS credentials for user +k <- create_key(u) +# update key to inactive +update_key(k, u, "Inactive") +list_keys(u) + +# cleanup +delete_key(k) +remove_user(u, "example2") +delete_user(u) +delete_group("example2") +``` ## Installation diff --git a/README.md b/README.md index d0e866d..216609d 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,117 @@ Temporary session tokens are stored in environment variable `AWS_SESSION_TOKEN` ## Code Examples -Coming soon... +The package gives basically fully access to IAM functionality to create and manage groups and users, including creating and managing AWS credentials on-the-fly. + + +```r +library("aws.iam") + +# create user +u <- create_user("example-user") + +# create group +(g <- create_group("example")) +``` + +``` +## GroupId: AGPAIZHQL3T5B4GGXXVGU +## GroupName: /example +## Arn: arn:aws:iam::920667304251:group/example +## CreateDate: 1498901165 +``` + +```r +# rename group +update_group(g, "example2") +``` + +``` +## [1] TRUE +``` + +```r +# add user to group +add_user(u, "example2") +``` + +``` +## [1] TRUE +``` + +```r +get_group_users("example2") +``` + +``` +## [[1]] +## UserName: /example-user +## UserId: AIDAI54ZINZ2F3NUVS4XW +## Arn: arn:aws:iam::920667304251:user/example-user +## CreateDate: 1498901164 +## +## attr(,"group") +## GroupId: AGPAIZHQL3T5B4GGXXVGU +## GroupName: /example2 +## Arn: arn:aws:iam::920667304251:group/example2 +## CreateDate: 1498901165 +``` + +```r +# create AWS credentials for user +k <- create_key(u) +# update key to inactive +update_key(k, u, "Inactive") +``` + +``` +## [1] TRUE +``` + +```r +list_keys(u) +``` + +``` +## [[1]] +## AccessKeyId: AKIAIUSX3NZJVKNGNTRA +## CreateDate: 1498901167 +## Status: Inactive +## UserName: example-user +``` + +```r +# cleanup +delete_key(k) +``` + +``` +## [1] TRUE +``` + +```r +remove_user(u, "example2") +``` + +``` +## [1] TRUE +``` + +```r +delete_user(u) +``` + +``` +## [1] TRUE +``` + +```r +delete_group("example2") +``` + +``` +## [1] TRUE +``` ## Installation diff --git a/man/get_account.Rd b/man/get_account.Rd index a22af47..8172dc6 100644 --- a/man/get_account.Rd +++ b/man/get_account.Rd @@ -30,3 +30,12 @@ Retrieve IAM Account Details. This is useful as a \dQuote{hello world!} test. \details{ \code{get_account} returns a list of account details. \code{credential_report} generates and/or retrieves a credential report. \code{auth_details} returns a list of group, user, role, and policy details. } +\examples{ +\dontrun{ +# account details +get_aaccount() + +# big list of authorizations +auth_details() +} +} diff --git a/man/groups.Rd b/man/groups.Rd index 7deb8f2..b32cf57 100644 --- a/man/groups.Rd +++ b/man/groups.Rd @@ -45,3 +45,29 @@ remove_user(user, group, ...) \description{ Retrieve, create, update, and delete IAM user groups } +\examples{ +\dontrun{ + list_groups() + +# create group +(g <- create_group("example")) +# rename +update_group(g, "example2") +list_groups() + +# create example user +u <- create_user("example-user") +# add user to group +add_user(u, "example2") + +get_group_users("example2") + +# cleanup +remove_user(u, "example2") +delete_user(u) +delete_group("example2") +} +} +\seealso{ +\code{\link{create_user}}, \code{\link{create_role}}, +} diff --git a/man/keys.Rd b/man/keys.Rd index dbadb34..7782434 100644 --- a/man/keys.Rd +++ b/man/keys.Rd @@ -34,3 +34,24 @@ list_keys(user, n, marker, ...) \description{ Retrieve, create, update, and delete IAM access keys } +\examples{ +\dontrun{ +# list access keys +list_keys() + +# create a user key +u <- create_user("example-user") +str(k <- create_key(u)) + +# toggle key status to inactive +update_key(k, u, "Inactive") +list_keys(u) + +# cleanup +delete_key(k) +delete_user(u) +} +} +\seealso{ +\code{\link{create_user}} +} diff --git a/man/roles.Rd b/man/roles.Rd index 052e1b8..42e20e6 100644 --- a/man/roles.Rd +++ b/man/roles.Rd @@ -39,3 +39,6 @@ list_roles(n, marker, path, ...) \description{ Retrieve, create, update, and delete IAM Roles } +\seealso{ +\code{\link{create_user}}, \code{\link{create_group}}, +} diff --git a/man/users.Rd b/man/users.Rd index 52cc5a8..fa858c6 100644 --- a/man/users.Rd +++ b/man/users.Rd @@ -37,3 +37,14 @@ list_users(n, marker, path, ...) \description{ Retrieve, create, update, and delete IAM Users } +\examples{ +\dontrun{ +list_users() + +# create example user +u <- create_user("example-user") + +# cleanup +delete_user(u) +} +}