AzureRMR/R/az_resgroup.R

163 строки
4.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
2018-05-06 18:09:30 +03:00
initialize=function(token, subscription, name=NULL, ..., parms=list())
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-06 18:09:30 +03:00
parms <- if(!is_empty(list(...)))
private$init_and_create(name, ...)
2018-05-04 04:43:14 +03:00
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-08 05:40:22 +03:00
delete=function(confirm=TRUE)
2018-05-04 04:43:14 +03:00
{
2018-05-08 05:40:22 +03:00
if(confirm && interactive())
{
yn <- readline(paste0("Do you really want to delete resource group '", self$name, "'? (y/N) "))
if(tolower(substr(yn, 1, 1)) != "y")
return(invisible(NULL))
}
2018-05-04 16:55:06 +03:00
private$rg_op(http_verb="DELETE")
message("Deleting resource group '", self$name, "'. This operation may take some time to complete.")
2018-05-04 04:43:14 +03:00
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-06 20:00:52 +03:00
cont <- private$rg_op("providers/Microsoft.Resources/deployments")
lst <- lapply(cont$value,
function(parms) az_template$new(self$token, self$subscription, self$name, deployed_properties=parms))
# keep going until paging is complete
while(!is_empty(cont$nextLink))
{
cont <- call_azure_url(self$token, cont$nextLink)
lst <- c(lst, lapply(cont$value,
function(parms) az_template$new(self$token, self$subscription, self$name, 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-07 00:51:15 +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)
},
2018-05-08 05:50:50 +03:00
delete_template=function(template_name, confirm=TRUE, free_resources=FALSE)
2018-05-04 16:55:06 +03:00
{
2018-05-08 05:50:50 +03:00
self$get_template(template_name)$delete(confirm=confirm, free_resources=free_resources)
2018-05-04 16:55:06 +03:00
},
list_resources=function()
{
2018-05-06 20:00:52 +03:00
cont <- private$rg_op("resources")
lst <- lapply(cont$value, function(parms) az_resource$new(self$token, self$subscription, deployed_properties=parms))
# keep going until paging is complete
while(!is_empty(cont$nextLink))
{
cont <- call_azure_url(self$token, cont$nextLink)
lst <- c(lst, lapply(cont$value,
function(parms) az_resource$new(self$token, self$subscription, deployed_properties=parms)))
}
named_list(lst, c("type", "name"))
2018-05-04 16:55:06 +03:00
},
2018-05-06 20:58:34 +03:00
get_resource=function(provider, path, type, name, id, api_version=NULL)
2018-05-05 19:26:20 +03:00
{
2018-05-05 19:53:10 +03:00
az_resource$new(self$token, self$subscription,
2018-05-06 20:58:34 +03:00
resource_group=self$name, provider=provider, path=path, type=type, name=name, id=id,
api_version=api_version)
2018-05-05 19:26:20 +03:00
},
2018-05-08 05:50:50 +03:00
delete_resource=function(..., confirm=TRUE, wait=FALSE)
2018-05-05 19:26:20 +03:00
{
2018-05-08 05:50:50 +03:00
self$get_resource(...)$delete(confirm=confirm, wait=wait)
2018-05-05 19:26:20 +03:00
},
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-06 20:00:52 +03:00
{
2018-05-04 16:55:06 +03:00
private$validate_parms(parms)
2018-05-06 20:00:52 +03:00
self$name <- parms$name
2018-05-04 04:43:14 +03:00
}
parms
},
2018-05-06 18:09:30 +03:00
init_and_create=function(name, ...)
2018-05-04 04:43:14 +03:00
{
2018-05-06 18:09:30 +03:00
parms <- modifyList(list(...), list(name=name))
2018-05-04 04:43:14 +03:00
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