From 82739372f65db82058979c485560ecb0a6b55500 Mon Sep 17 00:00:00 2001 From: hong-revo Date: Tue, 8 May 2018 12:40:22 +1000 Subject: [PATCH] require confirmation before delete --- .Rbuildignore | 1 + .gitignore | 1 + AzureSMRbase.Rproj | 6 +++++- R/az_resgroup.R | 9 ++++++++- R/az_resource.R | 40 ++++++++++++++++++++++++++++------------ R/az_template.R | 13 ++++++++++++- 6 files changed, 55 insertions(+), 15 deletions(-) diff --git a/.Rbuildignore b/.Rbuildignore index 37bf16b..e5a7544 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -3,3 +3,4 @@ \.sln$ \.Rproj$ \.Rxproj$ +^\.Rproj\.user$ diff --git a/.gitignore b/.gitignore index 636e69d..1bd84a9 100644 --- a/.gitignore +++ b/.gitignore @@ -262,3 +262,4 @@ __pycache__/ .RHistory misc/ +.Rproj.user diff --git a/AzureSMRbase.Rproj b/AzureSMRbase.Rproj index 8e3c2eb..a4dce49 100644 --- a/AzureSMRbase.Rproj +++ b/AzureSMRbase.Rproj @@ -6,8 +6,12 @@ AlwaysSaveHistory: Default EnableCodeIndexing: Yes UseSpacesForTab: Yes -NumSpacesForTab: 2 +NumSpacesForTab: 4 Encoding: UTF-8 RnwWeave: Sweave LaTeX: pdfLaTeX + +BuildType: Package +PackageUseDevtools: Yes +PackageInstallArgs: --no-multiarch --with-keep.source diff --git a/R/az_resgroup.R b/R/az_resgroup.R index 30f560b..91a8da5 100644 --- a/R/az_resgroup.R +++ b/R/az_resgroup.R @@ -34,8 +34,15 @@ public=list( NULL }, - delete=function() + delete=function(confirm=TRUE) { + 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)) + } + private$rg_op(http_verb="DELETE") message("Deleting resource group '", self$name, "'. This operation may take some time to complete.") private$is_valid <- FALSE diff --git a/R/az_resource.R b/R/az_resource.R index 6162e41..a6e4554 100644 --- a/R/az_resource.R +++ b/R/az_resource.R @@ -76,34 +76,50 @@ public=list( sync_fields=function(force=FALSE) { - if(self$is_synced && !force) - return(invisible(NULL)) - self$initialize(self$token, self$subscription, id=self$id) + if(force || !self$is_synced) + self$initialize(self$token, self$subscription, id=self$id) invisible(NULL) }, - delete=function(wait=FALSE) + delete=function(confirm=TRUE, wait=FALSE) { + if(confirm && interactive()) + { + yn <- readline(paste0("Do you really want to delete resource '", self$type, "/", self$name, "'? (y/N) ")) + if(tolower(substr(yn, 1, 1)) != "y") + return(invisible(NULL)) + } + private$res_op(http_verb="DELETE") message("Deleting resource '", file.path(self$type, self$name), "'") - status <- 200 - while(wait && status < 300) + if(wait) { - Sys.sleep(5) - res <- private$res_op(http_status_handler="pass") - status <- httr::status_code(res) + for(i in 1:1000) + { + status <- httr::status_code(private$res_op(http_status_handler="pass")) + if(status >= 300) + break + Sys.sleep(5) + } + if(status < 300) + warning("Attempt to delete resource did not succeed", call.=FALSE) } private$is_valid <- FALSE invisible(NULL) }, + do_operation=function(http_verb="GET", ..., options=list()) + { + private$res_op(..., http_verb=http_verb, options=options) + }, + check=function() { - res <- private$res_op(http_verb="HEAD", http_status_handler="pass") - private$is_valid <- httr::status_code(res) < 300 - private$is_valid + # HEAD seems to be broken; do a GET and test whether it fails + res <- try(private$res_op()) + !inherits(res, "try-error") } ), diff --git a/R/az_template.R b/R/az_template.R index 2e75e84..e7c32e8 100644 --- a/R/az_template.R +++ b/R/az_template.R @@ -47,8 +47,19 @@ public=list( invisible(NULL) }, - delete=function(free_resources=FALSE) + delete=function(confirm=TRUE, free_resources=FALSE) { + if(confirm && interactive()) + { + msg <- paste0("Do you really want to delete template '", self$name, "'") + if(free_resources) + msg <- paste0(msg, " and associated resources") + msg <- paste0(msg, "? (y/N) ") + yn <- readline(msg) + if(tolower(substr(yn, 1, 1)) != "y") + return(invisible(NULL)) + } + message("Deleting template '", self$name, "'") if(free_resources) {