AzureKeyVault/R/storage_accounts.R

156 строки
5.4 KiB
R
Исходник Обычный вид История

2019-04-17 15:17:59 +03:00
#' Storage accounts in Key Vault
#'
#' This class represents the collection of storage accounts managed by a vault. It provides methods for adding and removing accounts, and doing backups and restores. For operations with a specific account, see [storage].
#'
#' @docType class
#'
#' @section Methods:
#' This class provides the following methods:
#' ```
#' add(name, storage_account, key_name, regen_key=TRUE, regen_period=30,
#' attributes=vault_object_attrs(), ...)
#' get(name)
#' remove(name, confirm=TRUE)
#' list()
#' backup(name)
#' restore(backup)
#' ```
#' @section Arguments:
#' - `name`: A name by which to refer to the storage account.
#' - `storage_account`: The Azure resource ID of the account. This can also be an object of class `az_resource` or `az_storage`, as provided by the AzureRMR or AzureStor packages respectively; in this case, the resource ID is obtained from the object.
#' - `key_name`: The name of the storage access key that Key Vault will manage.
#' - `regen_key`: Whether to automatically regenerate the access key at periodic intervals.
#' - `regen_period`: How often to regenerate the access key. This can be a number, which will be interpreted as days; or as an ISO-8601 string denoting a duration, eg "P30D" (30 days).
#' - `attributes`: Optional attributes for the secret. A convenient way to provide this is via the [vault_object_attrs] helper function.
#' - `...`: For `create` and `import`, other named arguments which will be treated as tags.
#' - `confirm`: For `remove`, whether to ask for confirmation before removing the account.
#' - `backup`: For `restore`, a string representing the backup blob for a key.
#' - `email`: For `set_contacts`, the email addresses of the contacts.
#'
#' @section Value:
#' For `get` and `add`, an object of class `stored_account`, representing the storage account itself.
#'
#' For `list`, a vector of account names.
#'
#' For `backup`, a string representing the backup blob for a storage account. If the account has multiple versions, the blob will contain all versions.
#'
#' @seealso
2019-04-19 15:32:19 +03:00
#' [storage_account], [vault_object_attrs]
2019-04-17 15:17:59 +03:00
#'
#' [Azure Key Vault documentation](https://docs.microsoft.com/en-us/azure/key-vault/),
#' [Azure Key Vault API reference](https://docs.microsoft.com/en-us/rest/api/keyvault)
#'
#' @examples
#' \dontrun{
#'
2019-04-27 21:24:32 +03:00
#' vault <- key_vault("mykeyvault")
2019-04-17 15:17:59 +03:00
#'
#' # get the storage account details
#' library(AzureStor)
#' stor <- AzureRMR::get_azure_login()$
#' get_subscription("sub_id")$
#' get_resource_group("rgname")$
#' get_storage_account("mystorageacct")
#' vault$storage$create("mystor", stor, "key1")
#'
#' vault$storage$list()
#' vault$storage$get("mystor")
#'
#' # specifying a regeneration period of 6 months
#' vault$storage$create("mystor", regen_period="P6M")
#'
#' # setting management tags
#' vault$storage$create("mystor", tag1="a value", othertag="another value")
#'
#' # backup and restore an account
#' bak <- vault$storage$backup("mystor")
#' vault$storage$delete("mystor", confirm=FALSE)
#' vault$storage$restore(bak)
2019-07-16 21:12:13 +03:00
#'
2019-04-17 15:17:59 +03:00
#' }
2019-04-19 15:32:19 +03:00
#' @name storage_accounts
#' @aliases storage_accounts storage
#' @rdname storage_accounts
2019-04-17 15:17:59 +03:00
NULL
2019-07-16 21:12:13 +03:00
vault_storage_accounts <- R6::R6Class("vault_storage_accounts",
2019-03-29 16:06:30 +03:00
public=list(
token=NULL,
url=NULL,
initialize=function(token, url)
{
self$token <- token
self$url <- url
},
add=function(name, storage_account, key_name, regen_key=TRUE, regen_period=30,
2019-04-12 10:20:51 +03:00
attributes=vault_object_attrs(), ...)
2019-03-29 16:06:30 +03:00
{
if(is_resource(storage_account))
storage_account <- storage_account$id
2019-04-03 21:00:51 +03:00
if(is.numeric(regen_period))
regen_period <- sprintf("P%sD", regen_period)
2019-04-12 10:20:51 +03:00
# some attributes not used for storage accounts
attributes$nbf <- attributes$exp <- NULL
2019-07-16 21:12:13 +03:00
2019-04-03 20:26:08 +03:00
body <- list(resourceId=storage_account, activeKeyName=key_name,
2019-03-29 16:06:30 +03:00
autoRegenerateKey=regen_key, regenerationPeriod=regen_period,
2019-04-12 10:20:51 +03:00
attributes=attributes, tags=list(...))
2019-03-29 16:06:30 +03:00
self$do_operation(name, body=body, encode="json", http_verb="PUT")
2019-04-11 12:54:44 +03:00
self$get(name)
2019-03-29 16:06:30 +03:00
},
2019-04-11 12:54:44 +03:00
get=function(name, version=NULL)
2019-03-29 16:06:30 +03:00
{
op <- construct_path(name, version)
2019-04-11 12:35:40 +03:00
stored_account$new(self$token, self$url, name, version, self$do_operation(op))
2019-03-29 16:06:30 +03:00
},
remove=function(name, confirm=TRUE)
{
2019-04-03 22:56:30 +03:00
if(delete_confirmed(confirm, name, "storage account"))
2019-04-27 21:41:25 +03:00
invisible(self$do_operation(name, http_verb="DELETE"))
2019-03-29 16:06:30 +03:00
},
2019-04-14 14:47:01 +03:00
list=function()
2019-03-29 16:06:30 +03:00
{
2019-04-14 14:47:01 +03:00
sapply(get_vault_paged_list(self$do_operation(), self$token),
function(props) basename(props$id))
2019-03-29 16:06:30 +03:00
},
backup=function(name)
{
self$do_operation(construct_path(name, "backup"), http_verb="POST")$value
},
restore=function(name, backup)
{
stopifnot(is.character(backup))
2019-07-16 21:12:13 +03:00
self$do_operation("restore", body=list(value=backup), encode="json", http_verb="POST")
2019-03-29 16:06:30 +03:00
},
do_operation=function(op="", ..., options=list(),
api_version=getOption("azure_keyvault_api_version"))
{
url <- self$url
url$path <- construct_path("storage", op)
url$query <- utils::modifyList(list(`api-version`=api_version), options)
call_vault_url(self$token, url, ...)
2019-04-25 12:56:45 +03:00
},
print=function(...)
{
url <- self$url
url$path <- "storage"
cat("<key vault endpoint '", httr::build_url(url), "'>\n", sep="")
invisible(self)
2019-03-29 16:06:30 +03:00
}
))