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

Commit

Permalink
Merge branch 'dev' of github.com:Microsoft/AzureSMR into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Alanwe committed Jun 15, 2017
2 parents 74906b4 + 19c0e96 commit 823f0fd
Show file tree
Hide file tree
Showing 18 changed files with 1,015 additions and 44 deletions.
15 changes: 8 additions & 7 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ Package: AzureSMR
Title: Manage and Interact with Azure Resources
Description: Helps users to manage Azure Services and objects from within an
R Session. This includes Azure Storage (e.g. containers and blobs), Virtual
Machines and HDInsight (Spark, Hive). To use the package, you must configure
an Azure Active Directory application and service principal in the Azure portal.
Machines and HDInsight (Spark, Hive). To use the package, you must configure an
Azure Active Directory application and service principal in the Azure portal.
Type: Package
Version: 0.2.5
Date: 2017-06-06
Expand All @@ -18,16 +18,17 @@ URL: https://github.com/Microsoft/AzureSMR
BugReports: https://github.com/Microsoft/AzureSMR/issues
NeedsCompilation: no
Imports:
assertthat,
assertthat,
httr,
jsonlite,
XML,
base64enc,
digest,
shiny (>= 0.13),
miniUI (>= 0.1.1),
rstudioapi (>= 0.5),
DT
shiny (>= 0.13),
miniUI (>= 0.1.1),
rstudioapi (>= 0.5),
DT,
lubridate,
Depends:
R(>= 3.0.0)
Suggests:
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ S3method(summary,azureScriptActionHistory)
export(AzureListRG)
export(as.azureActiveContext)
export(azureAuthenticate)
export(azureBatchGetKey)
export(azureBlobCD)
export(azureBlobFind)
export(azureBlobLS)
export(azureCancelDeploy)
export(azureCheckToken)
export(azureCreateBatchAccount)
export(azureCreateHDI)
export(azureCreateResourceGroup)
export(azureCreateStorageAccount)
export(azureCreateStorageContainer)
export(azureDeleteBatchAccount)
export(azureDeleteBlob)
export(azureDeleteDeploy)
export(azureDeleteHDI)
Expand All @@ -30,6 +33,7 @@ export(azureHDIConf)
export(azureHiveSQL)
export(azureHiveStatus)
export(azureListAllResources)
export(azureListBatchAccounts)
export(azureListHDI)
export(azureListRG)
export(azureListSA)
Expand Down
160 changes: 160 additions & 0 deletions R/AzureBatch.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
#' List batch accounts.
#'
#' @inheritParams setAzureContext
#' @inheritParams azureAuthenticate
#' @inheritParams azureBatchGetKey

#' @family Batch account functions
#' @export
azureListBatchAccounts <- function(azureActiveContext, resourceGroup, subscriptionID,
verbose = FALSE) {
assert_that(is.azureActiveContext(azureActiveContext))

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

type_batch <- "Microsoft.Batch/batchAccounts"

z <- if(missing(resourceGroup)) {
azureListAllResources(azureActiveContext, type = type_batch)
} else {
azureListAllResources(azureActiveContext, type = type_batch, resourceGroup = resourceGroup, subscriptionID = subscriptionID)
}
rownames(z) <- NULL
z$batchAccount <- extractStorageAccount(z$id)
z
}


#' Create an azure batch account.
#'
#' @inheritParams setAzureContext
#' @inheritParams azureAuthenticate
#' @inheritParams azureBatchGetKey
#' @param location A string for the location to create batch account
#' @param asynchronous If TRUE, submits asynchronous request to Azure. Otherwise waits until batch account is created.
#' @family Batch account functions
#' @export
azureCreateBatchAccount <- function(azureActiveContext, batchAccount,
location = "northeurope",
resourceGroup, subscriptionID,
asynchronous = FALSE, verbose = FALSE) {
assert_that(is.azureActiveContext(azureActiveContext))

if (missing(subscriptionID)) subscriptionID <- azureActiveContext$subscriptionID
if (missing(resourceGroup)) resourceGroup <- azureActiveContext$resourceGroup
assert_that(is_resource_group(resourceGroup))
assert_that(is_subscription_id(subscriptionID))
assert_that(is_storage_account(batchAccount))

body <- paste0('{
"location":"', location, '",
}'
)

uri <- paste0("https://management.azure.com/subscriptions/", subscriptionID,
"/resourceGroups/", resourceGroup, "/providers/Microsoft.Batch/batchAccounts/",
batchAccount, "?api-version=2017-05-01")

r <- call_azure_sm(azureActiveContext, uri = uri, body = body,
verb = "PUT", verbose = verbose)

if (status_code(r) == 409) {
message("409: Conflict : Account already exists with the same name")
return(TRUE)
}

if (status_code(r) == 200) {
message("Account already exists with the same properties")
}
stopWithAzureError(r)

rl <- content(r, "text", encoding = "UTF-8")
azureActiveContext$batchAccount <- batchAccount
azureActiveContext$resourceGroup <- resourceGroup
message("Create request Accepted. It can take a few moments to provision the batch account")

if (!asynchronous) {
wait_for_azure(
batchAccount %in% azureListBatchAccounts(azureActiveContext, subscriptionID = subscriptionID)$name
)
}
TRUE
}


#' Delete an azure batch account.
#'
#' @inheritParams setAzureContext
#' @inheritParams azureAuthenticate
#' @inheritParams azureBatchGetKey

#' @family Batch account functions
#' @export
azureDeleteBatchAccount <- function(azureActiveContext, batchAccount,
resourceGroup, subscriptionID, verbose = FALSE) {
assert_that(is.azureActiveContext(azureActiveContext))

if (missing(resourceGroup)) resourceGroup <- azureActiveContext$resourceGroup
if (missing(subscriptionID)) subscriptionID <- azureActiveContext$subscriptionID

assert_that(is_storage_account(batchAccount))
assert_that(is_resource_group(resourceGroup))
assert_that(is_subscription_id(subscriptionID))

uri <- paste0("https://management.azure.com/subscriptions/", subscriptionID,
"/resourceGroups/", resourceGroup, "/providers/Microsoft.Batch/batchAccounts/",
batchAccount, "?api-version=2017-05-01")

r <- call_azure_sm(azureActiveContext, uri = uri,
verb = "DELETE", verbose = verbose)

if (status_code(r) == 204) {
warning("Batch Account not found")
return(FALSE)
}
if (status_code(r) != 200) stopWithAzureError(r)

azureActiveContext$batchAccount <- batchAccount
azureActiveContext$resourceGroup <- resourceGroup
TRUE
}


#' Get the Batch Keys for Specified Batch Account.
#'
#' @inheritParams setAzureContext
#' @inheritParams azureAuthenticate
#'
#' @family Batch account functions
#' @export
azureBatchGetKey <- function(azureActiveContext, batchAccount,
resourceGroup, subscriptionID, verbose = FALSE) {
assert_that(is.azureActiveContext(azureActiveContext))

if (missing(resourceGroup)) resourceGroup <- azureActiveContext$resourceGroup
if (missing(subscriptionID)) subscriptionID <- azureActiveContext$subscriptionID

assert_that(is_storage_account(batchAccount))
assert_that(is_resource_group(resourceGroup))
assert_that(is_subscription_id(subscriptionID))

message("Fetching Batch Key..")

uri <- paste0("https://management.azure.com/subscriptions/", subscriptionID,
"/resourceGroups/", resourceGroup,
"/providers/Microsoft.Batch/batchAccounts/", batchAccount,
"/listkeys?api-version=2017-05-01")

r <- call_azure_sm(azureActiveContext, uri = uri,
verb = "POST", verbose = verbose)
stopWithAzureError(r)

rl <- content(r, "text", encoding = "UTF-8")
df <- fromJSON(rl)
azureActiveContext$batchAccount <- batchAccount
azureActiveContext$resourceGroup <- resourceGroup
azureActiveContext$batchKey <- df$primary

return(azureActiveContext$batchKey)
}
Loading

0 comments on commit 823f0fd

Please sign in to comment.