AzureRMR/R/az_resgroup.R

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

2018-05-01 18:40:47 +03:00
#' @export
az_resource_group <- R6::R6Class("az_resource_group",
public=list(
subscription=NULL,
id=NULL,
name=NULL,
location=NULL,
managed_by=NULL,
properties=NULL,
tags=NULL,
2018-05-01 23:24:41 +03:00
token=NULL,
2018-05-01 18:40:47 +03:00
2018-05-04 04:43:14 +03:00
# constructor: can refer to an existing RG, or create a new RG
initialize=function(token, subscription, name=NULL, ..., parms=list(...), create=FALSE)
2018-05-01 18:40:47 +03:00
{
2018-05-04 04:43:14 +03:00
if(is_empty(name) && is_empty(parms))
stop("Must supply either resource group name, or parameter list")
2018-05-01 23:24:41 +03:00
self$token <- token
2018-05-01 18:40:47 +03:00
self$subscription <- subscription
2018-05-04 04:43:14 +03:00
parms <- if(create)
private$init_and_create(name, parms)
else private$init(name, parms)
self$id <- parms$id
self$location <- parms$location
self$managed_by <- parms$managedBy
self$properties <- parms$properties
self$tags <- parms$tags
private$is_valid <- TRUE
2018-05-01 18:40:47 +03:00
NULL
},
2018-05-04 04:43:14 +03:00
delete=function()
{
2018-05-05 19:26:20 +03:00
# TODO: allow wait until complete
2018-05-04 16:55:06 +03:00
private$rg_op(http_verb="DELETE")
2018-05-04 04:43:14 +03:00
message("Resource group '", self$name, "' deleted")
private$is_valid <- FALSE
invisible(NULL)
},
check=function()
{
2018-05-04 16:55:06 +03:00
res <- private$rg_op(http_verb="HEAD", http_status_handler="pass")
2018-05-04 04:43:14 +03:00
private$is_valid <- httr::status_code(res) < 300
private$is_valid
},
2018-05-04 16:55:06 +03:00
list_templates=function()
{
2018-05-05 19:26:20 +03:00
# TODO: handle paging
2018-05-04 16:55:06 +03:00
res <- private$rg_op("providers/Microsoft.Resources/deployments")$value
2018-05-05 19:53:10 +03:00
lst <- lapply(res, function(parms) az_template$new(self$token, self$subscription, self$name,
2018-05-04 21:37:58 +03:00
deployed_properties=parms))
2018-05-05 19:53:10 +03:00
named_list(lst)
2018-05-04 16:55:06 +03:00
},
2018-05-04 21:37:58 +03:00
deploy_template=function(template_name, template, parameters, ...)
2018-05-04 16:55:06 +03:00
{
2018-05-04 21:37:58 +03:00
az_template$new(self$token, self$subscription, self$name, template_name,
2018-05-05 19:53:10 +03:00
template, parameters, ...)
2018-05-04 16:55:06 +03:00
},
get_template=function(template_name)
{
az_template$new(self$token, self$subscription, self$name, template_name)
},
delete_template=function(template_name, free_resources=FALSE)
{
self$get_template(template_name)$delete(free_resources=free_resources)
},
list_resources=function()
{
2018-05-05 19:26:20 +03:00
# TODO: handle paging
2018-05-04 16:55:06 +03:00
res <- private$rg_op("resources")$value
2018-05-06 15:32:09 +03:00
lst <- lapply(res, function(parms) az_resource$new(self$token, self$subscription, deployed_properties=parms))
2018-05-05 19:26:20 +03:00
named_list(lst)
2018-05-04 16:55:06 +03:00
},
2018-05-05 19:26:20 +03:00
get_resource=function(provider, path, type, name, id)
{
2018-05-05 19:53:10 +03:00
az_resource$new(self$token, self$subscription,
resource_group=self$name, provider=provider, path=path, type=type, name=name, id=id)
2018-05-05 19:26:20 +03:00
},
delete_resource=function(...)
{
self$get_resource(...)$delete()
},
2018-05-05 19:53:10 +03:00
create_resource=function(provider, path, type, name, id, ...)
{
az_resource$new(self$token, self$subscription,
resource_group=self$name, provider=provider, path=path, type=type, name=name, id=id, ...)
}
2018-05-01 18:40:47 +03:00
),
private=list(
2018-05-04 04:43:14 +03:00
is_valid=FALSE,
2018-05-01 23:24:41 +03:00
2018-05-04 04:43:14 +03:00
init=function(name, parms)
2018-05-01 18:40:47 +03:00
{
2018-05-04 04:43:14 +03:00
if(is_empty(parms))
{
2018-05-04 16:55:06 +03:00
self$name <- name
parms <- private$rg_op()
}
else
2018-05-05 19:53:10 +03:00
{
2018-05-04 16:55:06 +03:00
private$validate_parms(parms)
self$name <- name
2018-05-04 04:43:14 +03:00
}
parms
},
init_and_create=function(name, parms)
{
parms <- c(name=name, parms)
private$validate_parms(parms)
2018-05-04 16:55:06 +03:00
self$name <- name
private$rg_op(body=parms, encode="json", http_verb="PUT")
2018-05-04 04:43:14 +03:00
},
validate_parms=function(parms)
{
required_names <- c("location", "name")
optional_names <- c("id", "managedBy", "tags", "properties")
validate_object_names(names(parms), required_names, optional_names)
2018-05-04 16:55:06 +03:00
},
rg_op=function(op="", ...)
{
op <- file.path("resourcegroups", self$name, op)
call_azure_rm(self$token, self$subscription, op, ...)
2018-05-01 18:40:47 +03:00
}
))
2018-05-05 19:53:10 +03:00