2016-08-03 08:03:55 +03:00
|
|
|
#' @name AzureSM: AzureDeployTemplate
|
|
|
|
#' @title Submit a Azure Resource ManagerTemplate
|
|
|
|
#' @param AzureActiveContext Azure Context Object
|
|
|
|
#' @param DeplName DeplName
|
|
|
|
#' @param TemplateURL TemplateURL
|
|
|
|
#' @param ParamURL ParamURL
|
|
|
|
#' @param ResourceGroup ResourceGroup Object (or use AzureActiveContext)
|
|
|
|
#' @param Token Token Object (or use AzureActiveContext)
|
|
|
|
#' @param SubscriptionID SubscriptionID Object (or use AzureActiveContext)
|
2016-08-03 19:24:11 +03:00
|
|
|
#' @param verbose Print Tracing information (Default False)
|
2016-08-03 08:03:55 +03:00
|
|
|
#' @rdname AzureDeployTemplate
|
|
|
|
#' @export
|
2016-09-16 01:48:52 +03:00
|
|
|
AzureDeployTemplate <- function(AzureActiveContext,DeplName,TemplateURL,ParamURL,TemplateJSON,ParamJSON,Mode="Sync",ResourceGroup,SubscriptionID,AzToken, verbose = FALSE) {
|
2016-09-14 15:09:36 +03:00
|
|
|
AzureCheckToken(AzureActiveContext)
|
|
|
|
|
2016-08-03 08:03:55 +03:00
|
|
|
if(missing(AzToken)) {AT <- AzureActiveContext$Token} else (AT = AzToken)
|
|
|
|
if(missing(SubscriptionID)) {SUBIDI <- AzureActiveContext$SubscriptionID} else (SUBIDI = SubscriptionID)
|
|
|
|
if(missing(ResourceGroup)) {RGI <- AzureActiveContext$ResourceGroup} else (RGI = ResourceGroup)
|
|
|
|
|
|
|
|
if (!length(RGI)) {stop("Error: No ResourceGroup provided: Use ResourceGroup argument or set in AzureContext")}
|
|
|
|
if (!length(SUBIDI)) {stop("Error: No SubscriptionID provided: Use SUBID argument or set in AzureContext")}
|
|
|
|
if (!length(AT)) {stop("Error: No Token / Not currently Authenticated")}
|
|
|
|
if (!length(DeplName)) {stop("No DeplName provided")}
|
2016-09-16 01:48:52 +03:00
|
|
|
|
|
|
|
if (missing(TemplateURL) && missing(TemplateJSON))
|
|
|
|
{stop("No TemplateURL or TemplateJSON provided")}
|
|
|
|
|
|
|
|
verbosity <- if(verbose) httr::verbose(TRUE) else NULL
|
2016-08-03 08:03:55 +03:00
|
|
|
|
|
|
|
URL <- paste("https://management.azure.com/subscriptions/",SUBIDI,"/resourceGroups/",RGI,"/providers/microsoft.resources/deployments/",DeplName,"?api-version=2016-06-01",sep="")
|
|
|
|
# print(URL)
|
|
|
|
|
2016-09-16 01:48:52 +03:00
|
|
|
if (missing(TemplateJSON))
|
2016-08-03 08:03:55 +03:00
|
|
|
{
|
2016-09-16 01:48:52 +03:00
|
|
|
if (missing(ParamURL))
|
2016-08-03 08:03:55 +03:00
|
|
|
{
|
2016-09-16 01:48:52 +03:00
|
|
|
if (missing(ParamJSON))
|
|
|
|
bodyI <- paste("{\"properties\": {\"templateLink\": { \"uri\": \"",TemplateURL,"\",\"contentVersion\": \"1.0.0.0\"},\"mode\": \"Incremental\",\"debugSetting\": {\"detailLevel\": \"requestContent, responseContent\"}}}",sep="")
|
|
|
|
else
|
|
|
|
bodyI <- paste("{\"properties\": {",ParamJSON,",\"templateLink\": { \"uri\": \"",TemplateURL,"\",\"contentVersion\": \"1.0.0.0\"},\"mode\": \"Incremental\",\"debugSetting\": {\"detailLevel\": \"requestContent, responseContent\"}}}",sep="")
|
2016-08-03 08:03:55 +03:00
|
|
|
}
|
|
|
|
else
|
2016-09-16 01:48:52 +03:00
|
|
|
bodyI <- paste("{\"properties\": {\"templateLink\": { \"uri\": \"",TemplateURL,"\",\"contentVersion\": \"1.0.0.0\"}, \"mode\": \"Incremental\", \"parametersLink\": {\"uri\": \"",ParamURL,"\",\"contentVersion\": \"1.0.0.0\"},\"debugSetting\": {\"detailLevel\": \"requestContent, responseContent\"}}}",sep="")
|
2016-08-03 08:03:55 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-09-16 01:48:52 +03:00
|
|
|
if (missing(ParamURL))
|
|
|
|
{
|
|
|
|
if (missing(ParamJSON))
|
|
|
|
bodyI <- paste("{\"properties\": {\"template\": ",TemplateJSON,",\"mode\": \"Incremental\",\"debugSetting\": {\"detailLevel\": \"requestContent, responseContent\"}}}",sep="")
|
|
|
|
else
|
|
|
|
bodyI <- paste("{\"properties\": {",ParamJSON,",\"template\": ",TemplateJSON,",\"mode\": \"Incremental\",\"debugSetting\": {\"detailLevel\": \"requestContent, responseContent\"}}}",sep="")
|
|
|
|
}
|
|
|
|
else
|
|
|
|
bodyI <- paste("{\"properties\": {\"template\": ",TemplateJSON,", \"mode\": \"Incremental\", \"parametersLink\": {\"uri\": \"",ParamURL,"\",\"contentVersion\": \"1.0.0.0\"},\"debugSetting\": {\"detailLevel\": \"requestContent, responseContent\"}}}",sep="")
|
|
|
|
}
|
|
|
|
|
2016-08-03 08:03:55 +03:00
|
|
|
r <- PUT(URL,add_headers(.headers = c("Host" = "management.azure.com" ,"Authorization" = AT, "Content-Type" = "application/json")),body=bodyI,verbosity)
|
|
|
|
# print(paste(DeplName,"Submitted"))
|
2016-09-27 10:48:22 +03:00
|
|
|
if (status_code(r) != 200 && status_code(r) != 201 && status_code(r) != 202 ) {stop(paste("Error: Return code",status_code(r) ))}
|
2016-08-03 08:03:55 +03:00
|
|
|
rl <- content(r,"text",encoding="UTF-8")
|
|
|
|
# print (rl)
|
|
|
|
df <- fromJSON(rl)
|
|
|
|
if(toupper(Mode) == "SYNC")
|
|
|
|
{
|
|
|
|
rc="running"
|
|
|
|
writeLines(paste("AzureDeployTemplate: Request Submitted: ",Sys.time()))
|
|
|
|
writeLines("Running(R), Succeeded(S)")
|
|
|
|
a=1
|
|
|
|
while (a>0)
|
|
|
|
{
|
2016-08-05 14:20:21 +03:00
|
|
|
rc <- AzureDeployStatus(AzureActiveContext,DeplName=DeplName,ResourceGroup=RGI)
|
2016-08-03 08:03:55 +03:00
|
|
|
# cat(paste(rc," "))
|
|
|
|
if (grepl("Succeeded",rc)){
|
|
|
|
writeLines("")
|
|
|
|
writeLines(paste("Finished Deploying Sucessfully: ",Sys.time()))
|
|
|
|
break()
|
|
|
|
}
|
|
|
|
if (grepl("Error",rc)){
|
|
|
|
writeLines("")
|
|
|
|
writeLines(paste("Error Deploying: ",Sys.time()))
|
|
|
|
break()
|
|
|
|
}
|
|
|
|
|
|
|
|
a=a+1
|
|
|
|
if (grepl("Succeeded",rc)) {rc<-"S"}
|
|
|
|
else if (grepl("Running",rc)) {rc<-"R"}
|
|
|
|
else if (grepl("updating",rc)) {rc<-"U"}
|
|
|
|
else if (grepl("Starting",rc)) {rc<-"S"}
|
|
|
|
else if (grepl("Accepted",rc)) {rc<-"A"}
|
|
|
|
|
|
|
|
cat(rc)
|
|
|
|
|
|
|
|
if( a > 500) break()
|
|
|
|
Sys.sleep(5)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeLines(paste("Deployment",DeplName,"Submitted: ",Sys.time()))
|
|
|
|
return("OK")
|
|
|
|
}
|
|
|
|
|
|
|
|
#' @name AzureSM: AzureDeployStatus
|
|
|
|
#' @title Check Template DeployStatus
|
|
|
|
#' @param AzureActiveContext Azure Context Object
|
2016-08-05 14:20:21 +03:00
|
|
|
#' @param DeplName DeplName
|
|
|
|
#' @param ResourceGroup ResourceGroup Object (or use AzureActiveContext)
|
2016-08-03 08:03:55 +03:00
|
|
|
#' @param Token Token Object (or use AzureActiveContext)
|
|
|
|
#' @param SubscriptionID SubscriptionID Object (or use AzureActiveContext)
|
|
|
|
#' @param Verbose Print Tracing information (Default False)
|
|
|
|
#' @export
|
2016-08-05 14:20:21 +03:00
|
|
|
AzureDeployStatus <- function(AzureActiveContext,DeplName,ResourceGroup, SubscriptionID,AzToken,verbose = FALSE) {
|
2016-09-14 15:09:36 +03:00
|
|
|
AzureCheckToken(AzureActiveContext)
|
2016-08-03 08:03:55 +03:00
|
|
|
if(missing(AzToken)) {AT <- AzureActiveContext$Token} else (AT = AzToken)
|
|
|
|
if(missing(SubscriptionID)) {SUBIDI <- AzureActiveContext$SubscriptionID} else (SUBIDI = SubscriptionID)
|
|
|
|
if(missing(ResourceGroup)) {RGI <- AzureActiveContext$ResourceGroup} else (RGI = ResourceGroup)
|
|
|
|
verbosity <- if(verbose) httr::verbose(TRUE) else NULL
|
|
|
|
|
|
|
|
if (!length(RGI)) {stop("Error: No ResourceGroup provided: Use ResourceGroup argument or set in AzureContext")}
|
|
|
|
if (!length(SUBIDI)) {stop("Error: No SubscriptionID provided: Use SUBID argument or set in AzureContext")}
|
|
|
|
if (!length(AT)) {stop("Error: No Token / Not currently Authenticated")}
|
|
|
|
if (!length(DeplName)) {stop("No DeplName provided")}
|
|
|
|
|
|
|
|
URL <- paste("https://management.azure.com/subscriptions/",SUBIDI,"/resourceGroups/",RGI,"/providers/microsoft.resources/deployments/",DeplName,"?api-version=2016-06-01",sep="")
|
|
|
|
# print(URL)
|
|
|
|
|
|
|
|
r <- GET(URL,add_headers(.headers = c("Host" = "management.azure.com" ,"Authorization" = AT, "Content-Type" = "application/json")),verbosity)
|
|
|
|
rl <- content(r,"text",encoding="UTF-8")
|
|
|
|
|
|
|
|
df <- fromJSON(rl)
|
|
|
|
#print(df)
|
|
|
|
return(df$properties$provisioningState)
|
|
|
|
}
|
|
|
|
|
|
|
|
#' @name AzureSM: AzureDeleteDeploy
|
|
|
|
#' @title Delete Template Deployment
|
|
|
|
#' @param AzureActiveContext Azure Context Object
|
2016-08-05 14:20:21 +03:00
|
|
|
#' @param DeplName DeplName
|
|
|
|
#' @param ResourceGroup ResourceGroup Object (or use AzureActiveContext)
|
2016-08-03 08:03:55 +03:00
|
|
|
#' @param Token Token Object (or use AzureActiveContext)
|
|
|
|
#' @param SubscriptionID SubscriptionID Object (or use AzureActiveContext)
|
|
|
|
#' @param Verbose Print Tracing information (Default False)
|
|
|
|
#' @export
|
2016-08-05 14:20:21 +03:00
|
|
|
AzureDeleteDeploy <- function(AzureActiveContext,DeplName,ResourceGroup,SubscriptionID,AzToken,verbose = FALSE) {
|
2016-09-14 15:09:36 +03:00
|
|
|
AzureCheckToken(AzureActiveContext)
|
|
|
|
|
2016-08-03 08:03:55 +03:00
|
|
|
if(missing(AzToken)) {AT <- AzureActiveContext$Token} else (AT = AzToken)
|
|
|
|
if(missing(SubscriptionID)) {SUBIDI <- AzureActiveContext$SubscriptionID} else (SUBIDI = SubscriptionID)
|
|
|
|
if(missing(ResourceGroup)) {RGI <- AzureActiveContext$ResourceGroup} else (RGI = ResourceGroup)
|
|
|
|
verbosity <- if(verbose) httr::verbose(TRUE) else NULL
|
|
|
|
|
|
|
|
if (!length(RGI)) {stop("Error: No ResourceGroup provided: Use ResourceGroup argument or set in AzureContext")}
|
|
|
|
if (!length(SUBIDI)) {stop("Error: No SubscriptionID provided: Use SUBID argument or set in AzureContext")}
|
|
|
|
if (!length(AT)) {stop("Error: No Token / Not currently Authenticated")}
|
|
|
|
if (!length(DeplName)) {stop("No DeplName provided")}
|
|
|
|
|
|
|
|
URL <- paste("https://management.azure.com/subscriptions/",SUBIDI,"/resourceGroups/",RGI,"/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" = AT, "Content-Type" = "application/json")))
|
|
|
|
print(http_status(r))
|
|
|
|
rl <- content(r,"text",encoding="UTF-8")
|
|
|
|
print(rl)
|
|
|
|
df <- fromJSON(rl)
|
|
|
|
print(df)
|
|
|
|
return("OK")
|
|
|
|
}
|
|
|
|
|
2016-08-05 14:20:21 +03:00
|
|
|
AzureCancelDeploy <- function(AzureActiveContext,DeplName,ResourceGroup, SubscriptionID,AzToken,verbose = FALSE) {
|
2016-09-14 15:09:36 +03:00
|
|
|
|
|
|
|
AzureCheckToken(AzureActiveContext)
|
|
|
|
|
2016-08-03 08:03:55 +03:00
|
|
|
if(missing(AzToken)) {AT <- AzureActiveContext$Token} else (AT = AzToken)
|
|
|
|
if(missing(SubscriptionID)) {SUBIDI <- AzureActiveContext$SubscriptionID} else (SUBIDI = SubscriptionID)
|
|
|
|
if(missing(ResourceGroup)) {RGI <- AzureActiveContext$ResourceGroup} else (RGI = ResourceGroup)
|
|
|
|
verbosity <- if(verbose) httr::verbose(TRUE) else NULL
|
|
|
|
|
|
|
|
if (!length(RGI)) {stop("Error: No ResourceGroup provided: Use ResourceGroup argument or set in AzureContext")}
|
|
|
|
if (!length(SUBIDI)) {stop("Error: No SubscriptionID provided: Use SUBID argument or set in AzureContext")}
|
|
|
|
if (!length(AT)) {stop("Error: No Token / Not currently Authenticated")}
|
|
|
|
if (!length(DeplName)) {stop("No DeplName provided")}
|
|
|
|
|
|
|
|
URL <- paste("https://management.azure.com/subscriptions/",SUBIDI,"/resourceGroups/",RGI,"/providers/microsoft.resources/deployments/",DeplName,"/cancel?api-version=2016-06-01",sep="")
|
|
|
|
# print(URL)
|
|
|
|
|
|
|
|
r <- POST(URL,add_headers(.headers = c("Host" = "management.azure.com" ,"Authorization" = AT, "Content-Type" = "application/json")),verbosity)
|
|
|
|
rl <- content(r,"text",encoding="UTF-8")
|
|
|
|
df <- fromJSON(rl)
|
|
|
|
return(df$category)
|
|
|
|
}
|