Refactor template deployment code #62

This commit is contained in:
Andrie de Vries 2017-05-24 12:58:08 +01:00
Родитель c8f38fd333
Коммит d8d5e043f0
11 изменённых файлов: 102 добавлений и 173 удалений

Просмотреть файл

@ -6,10 +6,10 @@
#' @inheritParams azureAuthenticate
#' @inheritParams azureResizeHDI
#'
#' @param deplname deplname
#' @param templateURL templateURL
#' @param paramURL paramURL
#' @param templateJSON templateJSON
#' @param deplname Deployment name
#' @param templateURL URL that contains the ARM template to deploy. You must specify either `templateURL` OR `templateJSON`
#' @param paramURL URL that contains the template parameters. You must specify either `paramULR` OR `paramJSON`
#' @param templateJSON character vector that contains the ARM template to deploy. You must specify either `templateJSON` OR `templateURL`
#' @param paramJSON paramJSON
#'
#' @family Template functions
@ -18,82 +18,50 @@ azureDeployTemplate <- function(azureActiveContext, deplname, templateURL,
paramURL, templateJSON, paramJSON, mode = "Sync",
resourceGroup, 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
}
if (missing(subscriptionID)) subscriptionID <- azureActiveContext$subscriptionID
if (missing(resourceGroup)) resourceGroup <- azureActiveContext$resourceGroup
if (!length(resourceGroup)) {
stop("Error: No resourceGroup provided: Use resourceGroup argument or set in AzureContext")
}
if (!length(subscriptionID)) {
stop("Error: No subscriptionID provided: Use SUBID argument or set in AzureContext")
}
if (!length(azToken)) {
stop("Error: No Token / Not currently Authenticated")
}
if (!length(deplname)) {
stop("No deplname provided")
}
assert_that(is_resource_group(resourceGroup))
assert_that(is_subscription_id(subscriptionID))
assert_that(is_deployment_name(deplname))
if (missing(templateURL) && missing(templateJSON)) {
stop("No templateURL or templateJSON provided")
}
verbosity <- set_verbosity(verbose)
URL <- paste("https://management.azure.com/subscriptions/", subscriptionID,
URL <- paste0("https://management.azure.com/subscriptions/", subscriptionID,
"/resourceGroups/", resourceGroup, "/providers/microsoft.resources/deployments/",
deplname, "?api-version=2016-06-01", sep = "")
deplname, "?api-version=2016-06-01")
bodyI <- if (missing(templateJSON)) {
if (missing(paramURL)) {
if (missing(paramJSON)) {
paste0('{"properties": ',
'{"templateLink": { "uri": "', templateURL, '","contentversion": "1.0.0.0"},',
'"mode": "Incremental","debugSetting": {"detailLevel": "requestContent, responseContent"}}}'
)
} else {
paste0('{"properties": {', paramJSON,
',"templateLink": { "uri": "', templateURL, '","contentversion": "1.0.0.0"},',
'"mode": "Incremental","debugSetting": {"detailLevel": "requestContent, responseContent"}}}'
)
}
} else {
paste0('{"properties": {"templateLink": { "uri": "', templateURL, '","contentversion": "1.0.0.0"},',
'"mode": "Incremental", "parametersLink": {"uri": "', paramURL, '","contentversion": "1.0.0.0"},',
'"debugSetting": {"detailLevel": "requestContent, responseContent"}}}'
)
}
} else {
if (missing(paramURL)) {
if (missing(paramJSON)) {
paste0('{"properties": {"template": ', templateJSON,
',"mode": "Incremental","debugSetting": {"detailLevel": "requestContent, responseContent"}}}'
)
} else {
paste0('{"properties": {', paramJSON,
',"template": ', templateJSON,
',"mode": "Incremental","debugSetting": {"detailLevel": "requestContent, responseContent"}}}'
)
}
} else {
paste0('{"properties": {"template": ', templateJSON,
', "mode": "Incremental", "parametersLink": {"uri": "', paramURL,
'","contentversion": "1.0.0.0"},"debugSetting": {"detailLevel": "requestContent, responseContent"}}}')
}
}
r <- PUT(URL, add_headers(.headers = c(Host = "management.azure.com",
Authorization = azToken, `Content-type` = "application/json")), body = bodyI,
verbosity)
combination <- paste0(if(!missing(templateURL)) "tu" else "tj" ,
if (!missing(paramURL)) "pu" else if (!missing(paramJSON)) "pj" else "")
bodyI <- switch(
combination,
tu = paste0('{"properties": ',
'{"templateLink": { "uri": "', templateURL, '","contentversion": "1.0.0.0"},',
'"mode": "Incremental","debugSetting": {"detailLevel": "requestContent, responseContent"}}}'),
tupj = paste0('{"properties": {', paramJSON,
',"templateLink": { "uri": "', templateURL, '","contentversion": "1.0.0.0"},',
'"mode": "Incremental","debugSetting": {"detailLevel": "requestContent, responseContent"}}}'),
tupu = paste0('{"properties": {"templateLink": { "uri": "', templateURL, '","contentversion": "1.0.0.0"},',
'"mode": "Incremental", "parametersLink": {"uri": "', paramURL, '","contentversion": "1.0.0.0"},',
'"debugSetting": {"detailLevel": "requestContent, responseContent"}}}'),
tj = paste0('{"properties": {"template": ', templateJSON,
',"mode": "Incremental","debugSetting": {"detailLevel": "requestContent, responseContent"}}}'),
tjpj = paste0('{"properties": {', paramJSON,
',"template": ', templateJSON,
',"mode": "Incremental","debugSetting": {"detailLevel": "requestContent, responseContent"}}}'),
tjpu = paste0('{"properties": {"template": ', templateJSON,
', "mode": "Incremental", "parametersLink": {"uri": "', paramURL,
'","contentversion": "1.0.0.0"},"debugSetting": {"detailLevel": "requestContent, responseContent"}}}')
)
r <- PUT(URL, azureApiHeaders(azToken), body = bodyI, verbosity)
stopWithAzureError(r)
if (toupper(mode) == "SYNC") {
@ -106,9 +74,7 @@ azureDeployTemplate <- function(azureActiveContext, deplname, templateURL,
}
#' Check Template Deployment Status.
#' Check template deployment Status.
#'
#' @inheritParams setAzureContext
#' @inheritParams azureAuthenticate
@ -118,100 +84,67 @@ azureDeployTemplate <- function(azureActiveContext, deplname, templateURL,
#' @export
azureDeployStatus <- function(azureActiveContext, deplname, resourceGroup,
subscriptionID, verbose = FALSE) {
assert_that(is.azureActiveContext(azureActiveContext))
azureCheckToken(azureActiveContext)
azToken <- azureActiveContext$Token
if (missing(subscriptionID)) {
subscriptionID <- azureActiveContext$subscriptionID
} else (subscriptionID <- subscriptionID)
if (missing(resourceGroup)) {
resourceGroup <- azureActiveContext$resourceGroup
} else (resourceGroup <- resourceGroup)
if (missing(subscriptionID)) subscriptionID <- azureActiveContext$subscriptionID
if (missing(resourceGroup)) resourceGroup <- azureActiveContext$resourceGroup
verbosity <- set_verbosity(verbose)
if (!length(resourceGroup)) {
stop("Error: No resourceGroup provided: Use resourceGroup argument or set in AzureContext")
}
if (!length(subscriptionID)) {
stop("Error: No subscriptionID provided: Use SUBID argument or set in AzureContext")
}
if (!length(azToken)) {
stop("Error: No Token / Not currently Authenticated")
}
if (!length(deplname)) {
stop("No deplname provided")
}
assert_that(is_resource_group(resourceGroup))
assert_that(is_subscription_id(subscriptionID))
assert_that(is_deployment_name(deplname))
URL <- paste("https://management.azure.com/subscriptions/", subscriptionID,
URL <- paste0("https://management.azure.com/subscriptions/", subscriptionID,
"/resourceGroups/", resourceGroup, "/providers/microsoft.resources/deployments/",
deplname, "?api-version=2016-06-01", sep = "")
# print(URL)
deplname, "?api-version=2016-06-01")
r <- GET(URL, azureApiHeaders(azToken), verbosity)
stopWithAzureError(r)
r <- GET(URL, add_headers(.headers = c(Host = "management.azure.com",
Authorization = azToken, `Content-type` = "application/json")), verbosity)
rl <- content(r, "text", encoding = "UTF-8")
df <- fromJSON(rl)
# print(df)
return(df)
}
azureDeployStatusSummary <- function(x) x$properties$provisioningState
#' Delete Template Deployment.
#' Delete template deployment.
#'
#' @inheritParams setAzureContext
#' @inheritParams azureAuthenticate
#' @inheritParams azureDeployTemplate
# @param azureActiveContext Azure Context Object @param deplname
# deplname
#'
#' @family Template functions
#' @export
azureDeleteDeploy <- function(azureActiveContext, deplname, resourceGroup,
subscriptionID, verbose = FALSE) {
assert_that(is.azureActiveContext(azureActiveContext))
azureCheckToken(azureActiveContext)
azToken <- azureActiveContext$Token
if (missing(subscriptionID)) {
subscriptionID <- azureActiveContext$subscriptionID
} else (subscriptionID <- subscriptionID)
if (missing(resourceGroup)) {
resourceGroup <- azureActiveContext$resourceGroup
} else (resourceGroup <- resourceGroup)
if (missing(subscriptionID)) subscriptionID <- azureActiveContext$subscriptionID
if (missing(resourceGroup)) resourceGroup <- azureActiveContext$resourceGroup
verbosity <- set_verbosity(verbose)
if (!length(resourceGroup)) {
stop("Error: No resourceGroup provided: Use resourceGroup argument or set in AzureContext")
}
if (!length(subscriptionID)) {
stop("Error: No subscriptionID provided: Use SUBID argument or set in AzureContext")
}
if (!length(azToken)) {
stop("Error: No Token / Not currently Authenticated")
}
if (!length(deplname)) {
stop("No deplname provided")
}
assert_that(is_resource_group(resourceGroup))
assert_that(is_subscription_id(subscriptionID))
assert_that(is_deployment_name(deplname))
URL <- paste("https://management.azure.com/subscriptions/", subscriptionID,
"/resourceGroups/", resourceGroup, "/providers/microsoft.resources/deployments/",
deplname, "?api-version=2016-06-01", sep = "")
# print(URL)
r <- DELETE(URL, add_headers(.headers = c(Host = "management.azure.com",
Authorization = azToken, `Content-type` = "application/json")))
print(http_status(r))
r <- DELETE(URL, azureApiHeaders(azToken), verbosity)
rl <- content(r, "text", encoding = "UTF-8")
print(rl)
stopWithAzureError(r)
df <- fromJSON(rl)
print(df)
#print(df)
return(TRUE)
}
#' Cancel Template Deployment.
#' Cancel template deployment.
#'
#' @inheritParams setAzureContext
#' @inheritParams azureAuthenticate
@ -222,38 +155,24 @@ azureDeleteDeploy <- function(azureActiveContext, deplname, resourceGroup,
azureCancelDeploy <- function(azureActiveContext, deplname, resourceGroup,
subscriptionID, verbose = FALSE) {
assert_that(is.azureActiveContext(azureActiveContext))
azureCheckToken(azureActiveContext)
azToken <- azureActiveContext$Token
if (missing(subscriptionID)) {
subscriptionID <- azureActiveContext$subscriptionID
} else (subscriptionID <- subscriptionID)
if (missing(resourceGroup)) {
resourceGroup <- azureActiveContext$resourceGroup
} else (resourceGroup <- resourceGroup)
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_subscription_id(subscriptionID))
assert_that(is_deployment_name(deplname))
if (!length(resourceGroup)) {
stop("Error: No resourceGroup provided: Use resourceGroup argument or set in AzureContext")
}
if (!length(subscriptionID)) {
stop("Error: No subscriptionID provided: Use SUBID argument or set in AzureContext")
}
if (!length(azToken)) {
stop("Error: No Token / Not currently Authenticated")
}
if (!length(deplname)) {
stop("No deplname provided")
}
URL <- paste("https://management.azure.com/subscriptions/", subscriptionID,
URL <- paste0("https://management.azure.com/subscriptions/", subscriptionID,
"/resourceGroups/", resourceGroup, "/providers/microsoft.resources/deployments/",
deplname, "/cancel?api-version=2016-06-01", sep = "")
# print(URL)
deplname, "/cancel?api-version=2016-06-01")
r <- POST(URL, azureApiHeaders(azToken), verbosity)
stopWithAzureError(r)
r <- POST(URL, add_headers(.headers = c(Host = "management.azure.com",
Authorization = azToken, `Content-type` = "application/json")), verbosity)
rl <- content(r, "text", encoding = "UTF-8")
df <- fromJSON(rl)
return(df$category)

Просмотреть файл

@ -151,3 +151,13 @@ is_blob <- function(x) {
on_failure(is_blob) <- function(call, env) {
paste0(deparse(call$x), "Provide a valid blob, or set using createAzureContext()")
}
# --- deployment name
is_deployment_name <- function(x) {
is.character(x) && length(x) == 1 && nchar(x) > 0
}
on_failure(is_deployment_name) <- function(call, env) {
paste0(deparse(call$x), "Provide a deplname")
}

Просмотреть файл

@ -2,7 +2,7 @@
% Please edit documentation in R/AzureBlob.R
\name{azureBlobFind}
\alias{azureBlobFind}
\title{Find file in a Storage account directory.}
\title{Find file in a storage account directory.}
\usage{
azureBlobFind(azureActiveContext, file, storageAccount, storageKey, container,
resourceGroup, verbose = FALSE)
@ -25,7 +25,7 @@ azureBlobFind(azureActiveContext, file, storageAccount, storageKey, container,
\item{verbose}{Print Tracing information (Default False)}
}
\description{
Find file in a Storage account directory.
Find file in a storage account directory.
}
\seealso{
Other Blob store functions: \code{\link{azureBlobCD}},

Просмотреть файл

@ -2,7 +2,7 @@
% Please edit documentation in R/AzureBlob.R
\name{azureBlobLS}
\alias{azureBlobLS}
\title{List blob files in a Storage account directory.}
\title{List blob files in a storage account directory.}
\usage{
azureBlobLS(azureActiveContext, directory, recursive = FALSE, storageAccount,
storageKey, container, resourceGroup, verbose = FALSE)
@ -25,7 +25,7 @@ azureBlobLS(azureActiveContext, directory, recursive = FALSE, storageAccount,
\item{verbose}{Print Tracing information (Default False)}
}
\description{
List blob files in a Storage account directory.
List blob files in a storage account directory.
}
\seealso{
Other Blob store functions: \code{\link{azureBlobCD}},

Просмотреть файл

@ -2,7 +2,7 @@
% Please edit documentation in R/AzureTemplate.R
\name{azureCancelDeploy}
\alias{azureCancelDeploy}
\title{Cancel Template Deployment.}
\title{Cancel template deployment.}
\usage{
azureCancelDeploy(azureActiveContext, deplname, resourceGroup, subscriptionID,
verbose = FALSE)
@ -10,7 +10,7 @@ azureCancelDeploy(azureActiveContext, deplname, resourceGroup, subscriptionID,
\arguments{
\item{azureActiveContext}{A container used for caching variables used by \code{AzureSMR}}
\item{deplname}{deplname}
\item{deplname}{Deployment name}
\item{resourceGroup}{Name of the resource group}
@ -19,7 +19,7 @@ azureCancelDeploy(azureActiveContext, deplname, resourceGroup, subscriptionID,
\item{verbose}{Print Tracing information (Default False)}
}
\description{
Cancel Template Deployment.
Cancel template deployment.
}
\seealso{
Other Template functions: \code{\link{azureDeleteDeploy}},

Просмотреть файл

@ -2,7 +2,7 @@
% Please edit documentation in R/AzureBlob.R
\name{azureDeleteBlob}
\alias{azureDeleteBlob}
\title{Delete a specifed Storage blob.}
\title{Delete a specifed storage blob.}
\usage{
azureDeleteBlob(azureActiveContext, blob, directory, storageAccount, storageKey,
container, resourceGroup, verbose = FALSE)
@ -25,7 +25,7 @@ azureDeleteBlob(azureActiveContext, blob, directory, storageAccount, storageKey,
\item{verbose}{Print Tracing information (Default False)}
}
\description{
Delete a specifed Storage blob.
Delete a specifed storage blob.
}
\seealso{
Other Blob store functions: \code{\link{azureBlobCD}},

Просмотреть файл

@ -2,7 +2,7 @@
% Please edit documentation in R/AzureTemplate.R
\name{azureDeleteDeploy}
\alias{azureDeleteDeploy}
\title{Delete Template Deployment.}
\title{Delete template deployment.}
\usage{
azureDeleteDeploy(azureActiveContext, deplname, resourceGroup, subscriptionID,
verbose = FALSE)
@ -10,7 +10,7 @@ azureDeleteDeploy(azureActiveContext, deplname, resourceGroup, subscriptionID,
\arguments{
\item{azureActiveContext}{A container used for caching variables used by \code{AzureSMR}}
\item{deplname}{deplname}
\item{deplname}{Deployment name}
\item{resourceGroup}{Name of the resource group}
@ -19,7 +19,7 @@ azureDeleteDeploy(azureActiveContext, deplname, resourceGroup, subscriptionID,
\item{verbose}{Print Tracing information (Default False)}
}
\description{
Delete Template Deployment.
Delete template deployment.
}
\seealso{
Other Template functions: \code{\link{azureCancelDeploy}},

Просмотреть файл

@ -2,7 +2,7 @@
% Please edit documentation in R/AzureTemplate.R
\name{azureDeployStatus}
\alias{azureDeployStatus}
\title{Check Template Deployment Status.}
\title{Check template deployment Status.}
\usage{
azureDeployStatus(azureActiveContext, deplname, resourceGroup, subscriptionID,
verbose = FALSE)
@ -10,7 +10,7 @@ azureDeployStatus(azureActiveContext, deplname, resourceGroup, subscriptionID,
\arguments{
\item{azureActiveContext}{A container used for caching variables used by \code{AzureSMR}}
\item{deplname}{deplname}
\item{deplname}{Deployment name}
\item{resourceGroup}{Name of the resource group}
@ -19,7 +19,7 @@ azureDeployStatus(azureActiveContext, deplname, resourceGroup, subscriptionID,
\item{verbose}{Print Tracing information (Default False)}
}
\description{
Check Template Deployment Status.
Check template deployment Status.
}
\seealso{
Other Template functions: \code{\link{azureCancelDeploy}},

Просмотреть файл

@ -11,13 +11,13 @@ azureDeployTemplate(azureActiveContext, deplname, templateURL, paramURL,
\arguments{
\item{azureActiveContext}{A container used for caching variables used by \code{AzureSMR}}
\item{deplname}{deplname}
\item{deplname}{Deployment name}
\item{templateURL}{templateURL}
\item{templateURL}{URL that contains the ARM template to deploy. You must specify either \code{templateURL} OR \code{templateJSON}}
\item{paramURL}{paramURL}
\item{paramURL}{URL that contains the template parameters. You must specify either \code{paramULR} OR \code{paramJSON}}
\item{templateJSON}{templateJSON}
\item{templateJSON}{character vector that contains the ARM template to deploy. You must specify either \code{templateJSON} OR \code{templateURL}}
\item{paramJSON}{paramJSON}

Просмотреть файл

@ -2,7 +2,7 @@
% Please edit documentation in R/AzureBlob.R
\name{azureGetBlob}
\alias{azureGetBlob}
\title{Get contents from a specifed Storage blob.}
\title{Get contents from a specifed storage blob.}
\usage{
azureGetBlob(azureActiveContext, blob, directory, type = "text",
storageAccount, storageKey, container, resourceGroup, verbose = FALSE)
@ -27,7 +27,7 @@ azureGetBlob(azureActiveContext, blob, directory, type = "text",
\item{verbose}{Print Tracing information (Default False)}
}
\description{
Get contents from a specifed Storage blob.
Get contents from a specifed storage blob.
}
\seealso{
Other Blob store functions: \code{\link{azureBlobCD}},

Просмотреть файл

@ -2,7 +2,7 @@
% Please edit documentation in R/AzureBlob.R
\name{azurePutBlob}
\alias{azurePutBlob}
\title{Write contents to a specifed Storage blob.}
\title{Write contents to a specifed storage blob.}
\usage{
azurePutBlob(azureActiveContext, blob, contents = "", file = "", directory,
storageAccount, storageKey, container, resourceGroup, verbose = FALSE)
@ -33,7 +33,7 @@ azurePutBlob(azureActiveContext, blob, contents = "", file = "", directory,
\item{verbose}{Print Tracing information (Default False)}
}
\description{
Write contents to a specifed Storage blob.
Write contents to a specifed storage blob.
}
\seealso{
Other Blob store functions: \code{\link{azureBlobCD}},