Skip to content

Commit

Permalink
require confirmation before delete
Browse files Browse the repository at this point in the history
  • Loading branch information
hongooi73 committed May 8, 2018
1 parent fa89b8b commit 8273937
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 15 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
\.sln$
\.Rproj$
\.Rxproj$
^\.Rproj\.user$
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,4 @@ __pycache__/

.RHistory
misc/
.Rproj.user
6 changes: 5 additions & 1 deletion AzureSMRbase.Rproj
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 8 additions & 1 deletion R/az_resgroup.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 28 additions & 12 deletions R/az_resource.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
),

Expand Down
13 changes: 12 additions & 1 deletion R/az_template.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit 8273937

Please sign in to comment.