Skip to content
This repository has been archived by the owner on Oct 31, 2019. It is now read-only.

Commit

Permalink
Refactor calls to Azure SM REST API #73
Browse files Browse the repository at this point in the history
  • Loading branch information
andrie committed Jun 7, 2017
1 parent 0c6100c commit 2dd1cd8
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 158 deletions.
2 changes: 0 additions & 2 deletions R/AzureBlob.R
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ azureBlobLS <- function(azureActiveContext, directory, recursive = FALSE,
if(missing(directory)) directory <- "/"
if(missing(container)) container <- ""
}
#browser()

if(is.null(directory) || directory == "") directory <- "/"

Expand Down Expand Up @@ -297,7 +296,6 @@ azurePutBlob <- function(azureActiveContext, blob, contents = "", file = "",

verbosity <- set_verbosity(verbose)

#browser()
if (!grepl("^/", directory)) directory <- paste0("/", directory)
directory <- gsub("//", "/", directory)

Expand Down
71 changes: 24 additions & 47 deletions R/AzureHDI.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ azureListHDI <- function(azureActiveContext, resourceGroup, clustername = "*",
subscriptionID, name, type, location, verbose = FALSE) {

assert_that(is.azureActiveContext(azureActiveContext))
azureCheckToken(azureActiveContext)
azToken <- azureActiveContext$Token
if (missing(subscriptionID)) subscriptionID <- azureActiveContext$subscriptionID
if (missing(resourceGroup)) resourceGroup <- azureActiveContext$resourceGroup
verbosity <- set_verbosity(verbose)

assert_that(is_subscription_id(subscriptionID))
if (clustername != "*") {
Expand All @@ -28,11 +25,11 @@ azureListHDI <- function(azureActiveContext, resourceGroup, clustername = "*",
rg <- if (clustername == "*") "" else paste0("/resourceGroups/", resourceGroup)
cn <- if (clustername == "*") "" else clustername

URL <- paste0("https://management.azure.com/subscriptions/", subscriptionID, rg,
uri <- paste0("https://management.azure.com/subscriptions/", subscriptionID, rg,
"/providers/Microsoft.HDInsight/clusters/", cn,
"?api-version=2015-03-01-preview")

r <- GET(URL, azureApiHeaders(azToken), verbosity)
r <- call_azure_sm(azureActiveContext, uri = uri, verbose = verbose)
stopWithAzureError(r)
rc <- content(r)
extract_one <- function(x) {
Expand Down Expand Up @@ -69,25 +66,20 @@ azureListHDI <- function(azureActiveContext, resourceGroup, clustername = "*",
azureHDIConf <- function(azureActiveContext, clustername, resourceGroup,
subscriptionID, name, type, location, verbose = FALSE) {
assert_that(is.azureActiveContext(azureActiveContext))
azureCheckToken(azureActiveContext)
azToken <- azureActiveContext$Token
if (missing(subscriptionID)) subscriptionID <- azureActiveContext$subscriptionID
if (missing(resourceGroup)) resourceGroup <- azureActiveContext$resourceGroup
if (missing(clustername)) clustername <- azureActiveContext$clustername

verbosity <- set_verbosity(verbose)

assert_that(is_subscription_id(subscriptionID))
assert_that(is_resource_group(resourceGroup))
assert_that(is_clustername(clustername))

URL <- paste0("https://management.azure.com/subscriptions/", subscriptionID,
uri <- paste0("https://management.azure.com/subscriptions/", subscriptionID,
"/resourceGroups/", resourceGroup,
"/providers/Microsoft.HDInsight/clusters/", clustername,
"?api-version=2015-03-01-preview")

r <- GET(URL, azureApiHeaders(azToken), verbosity)
browser()
r <- call_azure_sm(azureActiveContext, uri = uri, verbose = verbose)
rc <- content(r)

if (length(rc) == 0) {
Expand Down Expand Up @@ -160,16 +152,13 @@ azureCreateHDI <- function(azureActiveContext, resourceGroup, location,
subscriptionID, mode = c("Sync", "Async"),
verbose = FALSE, debug = FALSE) {
assert_that(is.azureActiveContext(azureActiveContext))
azureCheckToken(azureActiveContext)
azToken <- azureActiveContext$Token

kind <- match.arg(kind)
mode <- match.arg(mode)

if (missing(subscriptionID)) subscriptionID <- azureActiveContext$subscriptionID
if (missing(resourceGroup)) resourceGroup <- azureActiveContext$resourceGroup
if (missing(storageAccount)) storageAccount <- azureActiveContext$storageAccount
verbosity <- set_verbosity(verbose)

assert_that(is_resource_group(resourceGroup))
if (missing(location)) {
Expand Down Expand Up @@ -218,7 +207,7 @@ azureCreateHDI <- function(azureActiveContext, resourceGroup, location,
hiveUser = hiveUser, hivePassword = hivePassword)
}

bodyI <- hdi_json(subscriptionID = subscriptionID, clustername = clustername,
body <- hdi_json(subscriptionID = subscriptionID, clustername = clustername,
location = location, storageAccount = storageAccount, storageKey = storageKey,
version = version,
kind = kind, vmSize = vmSize,
Expand All @@ -229,16 +218,17 @@ azureCreateHDI <- function(azureActiveContext, resourceGroup, location,
workers = workers)

if (debug) {
z <- fromJSON(bodyI)
z <- fromJSON(body)
return(z)
}

URL <- paste0("https://management.azure.com/subscriptions/", subscriptionID,
uri <- paste0("https://management.azure.com/subscriptions/", subscriptionID,
"/resourceGroups/", resourceGroup,
"/providers/Microsoft.HDInsight/clusters/", clustername,
"?api-version=2015-03-01-preview")

r <- PUT(URL, azureApiHeaders(azToken), body = bodyI, encode = "json", verbosity)
r <- call_azure_sm(azureActiveContext, uri = uri, body = body,
verb = "PUT", verbose = verbose)
stopWithAzureError(r)

azureActiveContext$resourceGroup <- resourceGroup
Expand Down Expand Up @@ -271,31 +261,26 @@ azureResizeHDI <- function(azureActiveContext, clustername,
role = c("worker", "head", "edge"),
size = 2, mode = c("Sync", "Async"), subscriptionID,
resourceGroup, verbose = FALSE) {
azureCheckToken(azureActiveContext)
azToken <- azureActiveContext$Token

if (missing(resourceGroup)) resourceGroup <- azureActiveContext$resourceGroup
if (missing(subscriptionID)) subscriptionID <- azureActiveContext$subscriptionID
verbosity <- set_verbosity(verbose)

assert_that(is_resource_group(resourceGroup))
assert_that(is_clustername(clustername))
assert_that(is.integer(size))

role <- match.arg(role)
mode <- match.arg(mode)
verbosity <- set_verbosity(verbose)

URL <- paste0("https://management.azure.com/subscriptions/", subscriptionID,
uri <- paste0("https://management.azure.com/subscriptions/", subscriptionID,
"/resourceGroups/", resourceGroup,
"/providers/Microsoft.HDInsight/clusters/", clustername,
"/roles/", role, "/resize?api-version=2015-03-01-preview")

bodyI <- list(targetInstanceCount = size)

r <- POST(URL, azureApiHeaders(azToken),
body = bodyI,
encode = "json", verbosity)
body <- list(targetInstanceCount = size)
r <- call_azure_sm(azureActiveContext, uri = uri, body = body,
verb = "POST", verbose = verbose)
stopWithAzureError(r)

rl <- content(r, "text", encoding = "UTF-8")
if (status_code(r) != 202) {
Expand Down Expand Up @@ -364,23 +349,21 @@ azureDeleteHDI <- function(azureActiveContext, clustername, subscriptionID,
resourceGroup, verbose = FALSE) {

assert_that(is.azureActiveContext(azureActiveContext))
azureCheckToken(azureActiveContext)
azToken <- azureActiveContext$Token

assert_that(is_clustername(clustername))
if (missing(subscriptionID)) subscriptionID <- azureActiveContext$subscriptionID
if (missing(resourceGroup)) resourceGroup <- azureActiveContext$resourceGroup
verbosity <- set_verbosity(verbose)

assert_that(is_resource_group(resourceGroup))
assert_that(is_clustername(clustername))

URL <- paste0("https://management.azure.com/subscriptions/", subscriptionID,
uri <- paste0("https://management.azure.com/subscriptions/", subscriptionID,
"/resourceGroups/", resourceGroup,
"/providers/Microsoft.HDInsight/clusters/", clustername,
"?api-version=2015-03-01-preview")

r <- DELETE(URL, azureApiHeaders(azToken), verbosity)
r <- call_azure_sm(azureActiveContext, uri = uri,
verb = "DELETE", verbose = verbose)
stopWithAzureError(r)

message("Delete request accepted")
Expand Down Expand Up @@ -412,13 +395,10 @@ azureRunScriptAction <- function(azureActiveContext, scriptname, scriptURL,
parameters = "", subscriptionID,
wait = TRUE, verbose = FALSE) {
assert_that(is.azureActiveContext(azureActiveContext))
azureCheckToken(azureActiveContext)
azToken <- azureActiveContext$Token

if (missing(subscriptionID)) subscriptionID <- azureActiveContext$subscriptionID
if (missing(resourceGroup)) resourceGroup <- azureActiveContext$resourceGroup
if (missing(clustername)) clustername <- azureActiveContext$clustername
verbosity <- set_verbosity(verbose)

assert_that(is_resource_group(resourceGroup))
assert_that(is_subscription_id(subscriptionID))
Expand All @@ -435,7 +415,7 @@ azureRunScriptAction <- function(azureActiveContext, scriptname, scriptURL,
edgeNode = '"edgenode"')
RL <- paste(roles[c(headNode, workerNode, edgeNode)], sep = ", ")

bodyI <- paste0('
body <- paste0('
{
"scriptActions": [{
"name":"', scriptname, '",
Expand All @@ -446,14 +426,13 @@ azureRunScriptAction <- function(azureActiveContext, scriptname, scriptURL,
"persistOnSuccess": true
}')

URL <- paste0("https://management.azure.com/subscriptions/", subscriptionID,
uri <- paste0("https://management.azure.com/subscriptions/", subscriptionID,
"/resourceGroups/", resourceGroup,
"/providers/Microsoft.HDInsight/clusters/", clustername,
"/executeScriptActions?api-version=2015-03-01-preview")

r <- POST(URL, azureApiHeaders(azToken),
body = bodyI,
encode = "json", verbosity)
r <- call_azure_sm(azureActiveContext, uri = uri, body = body,
verb = "POST", verbose = verbose)
stopWithAzureError(r)

azureActiveContext$clustername <- clustername
Expand All @@ -480,24 +459,22 @@ azureScriptActionHistory <- function(azureActiveContext, resourceGroup,
clustername = "*", subscriptionID,
name, type, verbose = FALSE) {
assert_that(is.azureActiveContext(azureActiveContext))
azureCheckToken(azureActiveContext)
azToken <- azureActiveContext$Token

if (missing(resourceGroup)) resourceGroup <- azureActiveContext$resourceGroup
if (missing(subscriptionID)) subscriptionID <- azureActiveContext$subscriptionID
if (missing(clustername)) clustername <- azureActiveContext$clustername
verbosity <- set_verbosity(verbose)

assert_that(is_resource_group(resourceGroup))
assert_that(is_subscription_id(subscriptionID))
assert_that(is_clustername(clustername))

URL <- paste0("https://management.azure.com/subscriptions/", subscriptionID,
uri <- paste0("https://management.azure.com/subscriptions/", subscriptionID,
"/resourceGroups/", resourceGroup,
"/providers/Microsoft.HDInsight/clusters/", clustername,
"/scriptExecutionHistory/?api-version=2015-03-01-preview")

r <- GET(URL, azureApiHeaders(azToken), verbosity)
r <- call_azure_sm(azureActiveContext, uri = uri,
verb = "GET", verbose = verbose)
stopWithAzureError(r)

rc <- content(r, bigint_as_char = TRUE)$value
Expand Down
6 changes: 3 additions & 3 deletions R/AzureHive.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#' @inheritParams setAzureContext
#' @inheritParams azureAuthenticate
#'
#' @importFrom httr authenticate
#'
#' @family Hive functions
#' @export
azureHiveStatus <- function(azureActiveContext, clustername, hdiAdmin,
Expand All @@ -17,7 +19,6 @@ azureHiveStatus <- function(azureActiveContext, clustername, hdiAdmin,
if (missing(hdiPassword)) {
HP <- azureActiveContext$hdiPassword
} else (HP = hdiPassword)
verbosity <- set_verbosity(verbose)


if (!length(CN)) {
Expand All @@ -34,8 +35,7 @@ azureHiveStatus <- function(azureActiveContext, clustername, hdiAdmin,
azureActiveContext$hdiPassword <- HP
azureActiveContext$clustername <- CN

URL <- paste("https://", CN, ".azurehdinsight.net/templeton/v1/status",
sep = "")
uri <- paste0("https://", CN, ".azurehdinsight.net/templeton/v1/status")

r <- GET(URL, add_headers(.headers = c(`Content-type` = "application/json")),
authenticate(HA, HP), verbosity)
Expand Down
48 changes: 18 additions & 30 deletions R/AzureResources.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
#' @export
azureListSubscriptions <- function(azureActiveContext, verbose = FALSE) {
assert_that(is.azureActiveContext(azureActiveContext))
azureCheckToken(azureActiveContext)
azToken <- azureActiveContext$Token
verbosity <- set_verbosity(verbose)

URLGS <- "https://management.azure.com/subscriptions?api-version=2015-01-01"
r <- GET(URLGS, azureApiHeaders(azToken), verbosity)
uri <- "https://management.azure.com/subscriptions?api-version=2015-01-01"
r <- call_azure_sm(azureActiveContext, uri = uri,
verb = "GET", verbose = verbose)
stopWithAzureError(r)

dfs <- lapply(content(r), data.frame, stringsAsFactors = FALSE)
Expand All @@ -34,17 +32,14 @@ azureListSubscriptions <- function(azureActiveContext, verbose = FALSE) {
#' @export
azureListRG <- function(azureActiveContext, subscriptionID, verbose = FALSE) {
assert_that(is.azureActiveContext(azureActiveContext))
azureCheckToken(azureActiveContext)
azToken <- azureActiveContext$Token
verbosity <- set_verbosity(verbose)

if (missing(subscriptionID)) subscriptionID <- azureActiveContext$subscriptionID
assert_that(is_subscription_id(subscriptionID))

URLRG <- paste0("https://management.azure.com/subscriptions/", subscriptionID,
uri <- paste0("https://management.azure.com/subscriptions/", subscriptionID,
"/resourcegroups?api-version=2015-01-01")

r <- GET(URLRG, azureApiHeaders(azToken), verbosity)
r <- call_azure_sm(azureActiveContext, uri = uri,
verb = "GET", verbose = verbose)
stopWithAzureError(r)

rl <- content(r, "text", encoding = "UTF-8")
Expand Down Expand Up @@ -77,19 +72,17 @@ getResourceGroupLocation <- function(azureActiveContext, resourceGroup) {
#' @return Returns Dataframe of Resources
#' @family Resource group functions
#' @export
azureListAllResources <- function(azureActiveContext, resourceGroup, subscriptionID,
azureListAllResources <- function(azureActiveContext,
resourceGroup, subscriptionID,
name, type, location, verbose = FALSE) {

assert_that(is.azureActiveContext(azureActiveContext))
azureCheckToken(azureActiveContext)
azToken <- azureActiveContext$Token
if (missing(subscriptionID)) subscriptionID <- azureActiveContext$subscriptionID
verbosity <- set_verbosity(verbose)


url <- paste0("https://management.azure.com/subscriptions/", subscriptionID,
uri <- paste0("https://management.azure.com/subscriptions/", subscriptionID,
"/resources?api-version=2015-01-01")
r <- GET(url, azureApiHeaders(azToken), verbosity)
r <- call_azure_sm(azureActiveContext, uri = uri,
verb = "GET", verbose = verbose)
stopWithAzureError(r)

rl <- content(r, "text", encoding = "UTF-8")
Expand Down Expand Up @@ -123,8 +116,6 @@ azureListAllResources <- function(azureActiveContext, resourceGroup, subscriptio
azureCreateResourceGroup <- function(azureActiveContext, resourceGroup,
location, subscriptionID, verbose = FALSE) {
assert_that(is.azureActiveContext(azureActiveContext))
azureCheckToken(azureActiveContext)
azToken <- azureActiveContext$Token
if (missing(subscriptionID)) subscriptionID <- azureActiveContext$subscriptionID
if (missing(resourceGroup)) resourceGroup <- azureActiveContext$resourceGroup
verbosity <- set_verbosity(verbose)
Expand All @@ -133,13 +124,12 @@ azureCreateResourceGroup <- function(azureActiveContext, resourceGroup,
assert_that(is_location(location))

azureActiveContext$resourceGroup <- resourceGroup
verbosity <- set_verbosity(verbose)

bodyI <- list(location = location)

url <- paste0("https://management.azure.com/subscriptions/", subscriptionID,
body <- list(location = location)
uri <- paste0("https://management.azure.com/subscriptions/", subscriptionID,
"/resourcegroups/", resourceGroup, "?api-version=2015-01-01")
r <- httr::PUT(url, azureApiHeaders(azToken), body = bodyI, encode = "json", verbosity)
r <- call_azure_sm(azureActiveContext, uri = uri, body = body,
verb = "PUT", verbose = verbose)
stopWithAzureError(r)

rl <- content(r, "text", encoding = "UTF-8")
Expand All @@ -164,18 +154,16 @@ azureCreateResourceGroup <- function(azureActiveContext, resourceGroup,
azureDeleteResourceGroup <- function(azureActiveContext, resourceGroup,
subscriptionID, type, verbose = FALSE) {
assert_that(is.azureActiveContext(azureActiveContext))
azureCheckToken(azureActiveContext)
azToken <- azureActiveContext$Token

if (missing(subscriptionID)) subscriptionID <- azureActiveContext$subscriptionID
verbosity <- set_verbosity(verbose)

assert_that(is_resource_group(resourceGroup))
assert_that(is_subscription_id(subscriptionID))

url <- paste0("https://management.azure.com/subscriptions/", subscriptionID,
uri <- paste0("https://management.azure.com/subscriptions/", subscriptionID,
"/resourcegroups/", resourceGroup, "?api-version=2015-01-01")
r <- DELETE(url, azureApiHeaders(azToken), verbosity)
r <- call_azure_sm(azureActiveContext, uri = uri,
verb = "DELETE", verbose = verbose)
if (status_code(r) == 404) {
stop(paste0("Error: Resource Group Not Found(", status_code(r), ")"))
}
Expand Down
Loading

0 comments on commit 2dd1cd8

Please sign in to comment.