From 1682a1e5f73a361375e0d93210e9d28d922efc76 Mon Sep 17 00:00:00 2001 From: Andrie de Vries Date: Mon, 13 Feb 2017 16:41:11 +0000 Subject: [PATCH] Replace cat() with message() #49 --- R/AzureHDI.R | 10 +-- R/AzureHive.R | 14 ++-- R/AzureSpark.R | 11 ++- R/AzureStorageAccount.R | 166 +++++++++++++++------------------------- R/AzureTemplate.R | 4 +- R/AzureVM.R | 16 ++-- 6 files changed, 87 insertions(+), 134 deletions(-) diff --git a/R/AzureHDI.R b/R/AzureHDI.R index d5c74a7..727eed9 100644 --- a/R/AzureHDI.R +++ b/R/AzureHDI.R @@ -272,7 +272,7 @@ azureResizeHDI <- function(azureActiveContext, clustername, role = "worker", rc <- azureListHDI(azureActiveContext, clustername = clustername) rc1 <- rc[9, 1] if (rc1 == "Running") { - cat("R") + message("R") writeLines("") writeLines(paste("Finished Resizing Sucessfully: ", Sys.time())) (break)() @@ -297,7 +297,7 @@ azureResizeHDI <- function(azureActiveContext, clustername, role = "worker", if (rc1 == "HdInsightConfiguration") { rc1 <- "R" } - cat(rc1) + message(rc1) if (a > 500) (break)() @@ -436,7 +436,7 @@ azureCreateHDI <- function(azureActiveContext, clustername, location, kind = "sp stop("Error: No resourceGroup provided: Use resourceGroup argument or set in AzureContext") } - cat("Fetching Storage Key..") + message("Fetching Storage Key..") storageKey <- azureSAGetKey(azureActiveContext, resourceGroup = RGI, storageAccount = storageAccount) HIVE <- FALSE @@ -620,7 +620,7 @@ azureCreateHDI <- function(azureActiveContext, clustername, location, kind = "sp rc <- azureListHDI(azureActiveContext, clustername = clustername) rc1 <- rc[8, 1] if (rc1 == "Succeeded") { - cat("S") + message("S") writeLines("") writeLines(paste("Finished Creating Sucessfully: ", Sys.time())) (break)() @@ -634,7 +634,7 @@ azureCreateHDI <- function(azureActiveContext, clustername, location, kind = "sp if (rc1 == "InProgress") { rc1 <- "R" } - cat(rc1) + message(rc1) if (a > 500) (break)() Sys.sleep(5) diff --git a/R/AzureHive.R b/R/AzureHive.R index 6e223aa..c0558a5 100644 --- a/R/AzureHive.R +++ b/R/AzureHive.R @@ -130,9 +130,9 @@ azureHiveSQL <- function(azureActiveContext, CMD, clustername, hdiAdmin, if (DUR < 5) DUR <- DUR + 1 if (df$status$state == "PREP") - cat("P") + message("P") if (df$status$state == "RUNNING") - cat("R") + message("R") # print(df$status$state) r <- GET(URL, add_headers(.headers = c(`Content-type` = "application/json")), @@ -142,13 +142,11 @@ azureHiveSQL <- function(azureActiveContext, CMD, clustername, hdiAdmin, df <- fromJSON(rl) } if (df$status$state == "SUCCEEDED") - cat("S") + message("S") if (df$status$state == "FAILED") - cat("F") + message("F") STATE <- df$status$state - writeLines("") - writeLines(paste("Finished Running statement: ", Sys.time())) - # print(df) - return("Done") + message("Finished Running statement: ", Sys.time()) + return(TRUE) } diff --git a/R/AzureSpark.R b/R/AzureSpark.R index 42fc832..9c6da29 100644 --- a/R/AzureSpark.R +++ b/R/AzureSpark.R @@ -282,7 +282,7 @@ azureSparkCMD <- function(azureActiveContext, CMD, clustername, hdiAdmin, Sys.sleep(DUR) if (DUR < 5) DUR <- DUR + 1 - cat("R") + message("R") r <- GET(URL, add_headers(.headers = c(`Content-type` = "application/json")), authenticate(HA, HP)) rl <- content(r, "text", encoding = "UTF-8") @@ -290,9 +290,8 @@ azureSparkCMD <- function(azureActiveContext, CMD, clustername, hdiAdmin, df <- fromJSON(rl) } - cat("C") - writeLines("") - writeLines(paste("Finished Running statement: ", Sys.time())) + message("C") + message("Finished Running statement: ", Sys.time()) RET <- df$output$data[1] # rownames(RET) <- 'Return Value' return(toString(RET)) @@ -382,7 +381,7 @@ azureSparkJob <- function(azureActiveContext, FILE, clustername, hdiAdmin, Sys.sleep(DUR) if (DUR < 5) DUR <- DUR + 1 - cat("R") + message("R") r <- GET(URL, add_headers(.headers = c(`Content-type` = "application/json")), authenticate(HA, HP), verbosity) rl <- content(r, "text", encoding = "UTF-8") @@ -391,7 +390,7 @@ azureSparkJob <- function(azureActiveContext, FILE, clustername, hdiAdmin, if (length(df$appInfo$driverlogUrl)) LOGURL2 <- df$appInfo$driverlogUrl } - cat("C") + message("C") STATE <- df$state writeLines("") writeLines(paste("Finished Running statement: ", Sys.time())) diff --git a/R/AzureStorageAccount.R b/R/AzureStorageAccount.R index 51d34e3..8b13483 100644 --- a/R/AzureStorageAccount.R +++ b/R/AzureStorageAccount.R @@ -8,27 +8,15 @@ #' @family Storage account functions #' @export azureListSA <- function(azureActiveContext, resourceGroup, subscriptionID, - azToken, verbose = FALSE) { + verbose = FALSE) { if (missing(resourceGroup)) { - RGI <- azureActiveContext$resourceGroup - } else { - RGI = resourceGroup - } - if (missing(azToken)) { - ATI <- azureActiveContext$Token - } else { - ATI = azToken - } + resourceGroup <- azureActiveContext$resourceGroup + } if (missing(subscriptionID)) { - SUBIDI <- azureActiveContext$subscriptionID - } else { - SUBIDI = subscriptionID - } - if (!length(ATI)) { - stop("Error: No Token / Not currently Authenticated.") - } - if (!length(SUBIDI)) { + subscriptionID <- azureActiveContext$subscriptionID + } + if (!length(subscriptionID)) { stop("Error: No subscriptionID provided: Use SUBID argument or set in AzureContext") } verbosity <- if (verbose) httr::verbose(TRUE) else NULL @@ -39,13 +27,13 @@ azureListSA <- function(azureActiveContext, resourceGroup, subscriptionID, } else { azureListAllResources(azureActiveContext, type = "Microsoft.Storage/storageAccounts", - resourceGroup = RGI) + resourceGroup = resourceGroup) } rownames(SA) <- NULL SA$storageAccount <- extractStorageAccount(SA$id) - return(SA) + SA } @@ -54,38 +42,28 @@ azureListSA <- function(azureActiveContext, resourceGroup, subscriptionID, #' @inheritParams setAzureContext #' @inheritParams azureAuthenticate #' @param storageAccount storageAccount -#' @param azToken Token Object (or use azureActiveContext) #' #' @family Storage account functions #' @export -azureSAGetKey <- function(azureActiveContext, storageAccount, azToken, +azureSAGetKey <- function(azureActiveContext, storageAccount, resourceGroup, subscriptionID, verbose = FALSE) { azureCheckToken(azureActiveContext) if (missing(resourceGroup)) { - RGI <- azureActiveContext$resourceGroup - } else { - RGI = resourceGroup - } - if (missing(azToken)) { - ATI <- azureActiveContext$Token - } else { - ATI = azToken - } + resourceGroup <- azureActiveContext$resourceGroup + } if (missing(subscriptionID)) { - SUBIDI <- azureActiveContext$subscriptionID - } else (SUBIDI = subscriptionID) + subscriptionID <- azureActiveContext$subscriptionID + } + if (!length(storageAccount)) { stop("Error: No Valid storageAccount provided") } - if (length(RGI) < 1) { + if (length(resourceGroup) < 1) { stop("Error: No resourceGroup provided: Use resourceGroup argument or set in AzureContext") } - if (!length(ATI)) { - stop("Error: No Token / Not currently Authenticated.") - } - if (!length(SUBIDI)) { + if (!length(subscriptionID)) { stop("Error: No subscriptionID provided: Use SUBID argument or set in AzureContext") } verbosity <- if (verbose) @@ -93,12 +71,12 @@ azureSAGetKey <- function(azureActiveContext, storageAccount, azToken, message("Fetching Storage Key..") - URL <- paste("https://management.azure.com/subscriptions/", SUBIDI, - "/resourceGroups/", RGI, "/providers/Microsoft.Storage/storageAccounts/", + URL <- paste("https://management.azure.com/subscriptions/", subscriptionID, + "/resourceGroups/", resourceGroup, "/providers/Microsoft.Storage/storageAccounts/", storageAccount, "/listkeys?api-version=2016-01-01", sep = "") r <- POST(URL, add_headers(.headers = c(Host = "management.azure.com", - Authorization = ATI, + Authorization = azureActiveContext$Token, `Content-type` = "application/json")), verbosity) @@ -109,7 +87,7 @@ azureSAGetKey <- function(azureActiveContext, storageAccount, azToken, azureActiveContext$storageAccountK <- storageAccount azureActiveContext$resourceGroup <- resourceGroup azureActiveContext$storageKey <- df$keys$value[1] - return(azureActiveContext$storageKey) + azureActiveContext$storageKey } @@ -123,35 +101,23 @@ azureSAGetKey <- function(azureActiveContext, storageAccount, azToken, #' @family Storage account functions #' @export azureCreateStorageAccount <- function(azureActiveContext, storageAccount, - azToken, resourceGroup, subscriptionID, verbose = FALSE) { + resourceGroup, subscriptionID, verbose = FALSE) { azureCheckToken(azureActiveContext) if (missing(resourceGroup)) { - RGI <- azureActiveContext$resourceGroup - } else { - RGI <- resourceGroup - } - if (missing(azToken)) { - ATI <- azureActiveContext$Token - } else { - ATI <- azToken - } + resourceGroup <- azureActiveContext$resourceGroup + } if (missing(subscriptionID)) { - SUBIDI <- azureActiveContext$subscriptionID - } else { - SUBIDI <- subscriptionID - } + subscriptionID <- azureActiveContext$subscriptionID + } if (!length(storageAccount)) { stop("Error: No Valid storageAccount provided") } - if (length(RGI) < 1) { + if (length(resourceGroup) < 1) { stop("Error: No resourceGroup provided: Use resourceGroup argument or set in AzureContext") } - if (!length(ATI)) { - stop("Error: No Token / Not currently Authenticated.") - } - if (!length(SUBIDI)) { + if (!length(subscriptionID)) { stop("Error: No subscriptionID provided: Use SUBID argument or set in AzureContext") } verbosity <- if (verbose) @@ -159,20 +125,20 @@ azureCreateStorageAccount <- function(azureActiveContext, storageAccount, # https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupname}/providers/Microsoft.Storage/storageAccounts/{accountname}?api-version={api-version} - bodyI = " - { - \"location\": \"northeurope\", - \"tags\": { - \"key1\": \"value1\" - }, - \"properties\": { - \"accessTier\": \"Hot\" - }, - \"sku\": { - \"name\": \"Standard_LRS\" - }, - \"kind\": \"Storage\" - }" + #bodyI = " + #{ + #\"location\": \"northeurope\", + #\"tags\": { + #\"key1\": \"value1\" + #}, + #\"properties\": { + #\"accessTier\": \"Hot\" + #}, + #\"sku\": { + #\"name\": \"Standard_LRS\" + #}, + #\"kind\": \"Storage\" + #}" bodyI <- "{ \"location\": \"northeurope\", @@ -180,19 +146,19 @@ azureCreateStorageAccount <- function(azureActiveContext, storageAccount, \"name\": \"Standard_LRS\" }}" - URL <- paste("https://management.azure.com/subscriptions/", SUBIDI, - "/resourceGroups/", RGI, "/providers/Microsoft.Storage/storageAccounts/", + URL <- paste("https://management.azure.com/subscriptions/", subscriptionID, + "/resourceGroups/", resourceGroup, "/providers/Microsoft.Storage/storageAccounts/", storageAccount, "?api-version=2016-01-01", sep = "") r <- PUT(URL, add_headers(.headers = c(Host = "management.azure.com", - Authorization = ATI, + Authorization = azureActiveContext$Token, `Content-type` = "application/json")), body = bodyI, encode = "json", verbosity) if (status_code(r) == 409) { warning("409: Conflict : Account already exists with the same name") - return(NULL) + return(FALSE) } @@ -205,7 +171,8 @@ azureCreateStorageAccount <- function(azureActiveContext, storageAccount, rl <- content(r, "text", encoding = "UTF-8") azureActiveContext$storageAccount <- storageAccount azureActiveContext$resourceGroup <- resourceGroup - return("Create request Accepted. It can take a few moments to provision the storage account") + message("Create request Accepted. It can take a few moments to provision the storage account") + TRUE } @@ -218,58 +185,47 @@ azureCreateStorageAccount <- function(azureActiveContext, storageAccount, #' @family Storage account functions #' @export azureDeletestorageAccount <- function(azureActiveContext, storageAccount, - azToken, resourceGroup, subscriptionID, verbose = FALSE) { + resourceGroup, subscriptionID, verbose = FALSE) { azureCheckToken(azureActiveContext) if (missing(resourceGroup)) { - RGI <- azureActiveContext$resourceGroup - } else { - RGI <- resourceGroup - } - if (missing(azToken)) { - ATI <- azureActiveContext$Token - } else (ATI = azToken) + resourceGroup <- azureActiveContext$resourceGroup + } if (missing(subscriptionID)) { - SUBIDI <- azureActiveContext$subscriptionID - } else { - SUBIDI <- subscriptionID - } + subscriptionID <- azureActiveContext$subscriptionID + } if (!length(storageAccount)) { stop("Error: No Valid storageAccount provided") } - if (length(RGI) < 1) { + if (length(resourceGroup) < 1) { stop("Error: No resourceGroup provided: Use resourceGroup argument or set in AzureContext") } - if (!length(ATI)) { - stop("Error: No Token / Not currently Authenticated.") - } - if (!length(SUBIDI)) { + if (!length(subscriptionID)) { stop("Error: No subscriptionID provided: Use SUBID argument or set in AzureContext") } verbosity <- if (verbose) httr::verbose(TRUE) else NULL - URL <- paste("https://management.azure.com/subscriptions/", SUBIDI, - "/resourceGroups/", RGI, "/providers/Microsoft.Storage/storageAccounts/", - storageAccount, "?api-version=2016-01-01", sep = "") + URL <- paste0("https://management.azure.com/subscriptions/", subscriptionID, + "/resourceGroups/", resourceGroup, "/providers/Microsoft.Storage/storageAccounts/", + storageAccount, "?api-version=2016-01-01") r <- DELETE(URL, add_headers(.headers = c(Host = "management.azure.com", - Authorization = ATI, + Authorization = azureActiveContext$Token, `Content-type` = "application/json")), verbosity) if (status_code(r) == 204) { - stop(paste0("Error: Storage Account not found")) + stop("Error: Storage Account not found") } if (status_code(r) == 409) { - stop(paste0("Error: An operation for the storage account is in progress.")) + stop("Error: An operation for the storage account is in progress.") } if (status_code(r) != 200) stopWithAzureError(r) - rl <- content(r, "text", encoding = "UTF-8") azureActiveContext$storageAccount <- storageAccount azureActiveContext$resourceGroup <- resourceGroup - return("Done") + TRUE } diff --git a/R/AzureTemplate.R b/R/AzureTemplate.R index 59cc544..e150022 100644 --- a/R/AzureTemplate.R +++ b/R/AzureTemplate.R @@ -126,14 +126,14 @@ azureDeployTemplate <- function(azureActiveContext, deplname, templateURL, rc <- "A" } - cat(rc) + message(rc) if (a > 500) (break)() Sys.sleep(5) } } - writeLines(paste("Deployment", deplname, "Submitted: ", Sys.time())) + message("Deployment", deplname, "Submitted: ", Sys.time()) return(TRUE) } diff --git a/R/AzureVM.R b/R/AzureVM.R index c2f0e1a..ad1ced5 100644 --- a/R/AzureVM.R +++ b/R/AzureVM.R @@ -160,13 +160,13 @@ azureStartVM <- function(azureActiveContext, resourceGroup, vmName, mode = "Sync if (rc1 == "NA") rc1 <- "*" - cat(rc1) + message(rc1) if (a > 500) (break)() Sys.sleep(5) } - writeLines(paste("Finished: ", Sys.time())) + message("Finished: ", Sys.time()) return("Done") } writeLines(paste("Start request Submitted: ", Sys.time())) @@ -264,17 +264,17 @@ azureStopVM <- function(azureActiveContext, resourceGroup, vmName, mode = "Sync" rc1 <- "-" } - cat(rc1) + message(rc1) if (a > 500) (break)() Sys.sleep(5) } - writeLines(paste("Finished: ", Sys.time())) - return("Done") + message("Finished: ", Sys.time()) + return(TRUE) } - writeLines(paste("Stop request Submitted: ", Sys.time())) - return("") + message("Stop request Submitted: ", Sys.time()) + return(TRUE) } @@ -431,7 +431,7 @@ azureDeleteVM <- function(azureActiveContext, resourceGroup, vmName, subscriptio rc <- "-" } - cat(rc) + message(rc) if (a > 500) (break)()