Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template udf support #14

Merged
merged 4 commits into from
Oct 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Package: AzureRMR
Title: Interface to 'Azure Resource Manager'
Version: 2.3.5.9000
Authors@R: c(
person("Hong", "Ooi", , "hongooi@microsoft.com", role = c("aut", "cre")),
person("Hong", "Ooi", , "hongooi73@gmail.com", role = c("aut", "cre")),
person("Microsoft", role="cph")
)
Description: A lightweight but powerful R interface to the 'Azure Resource Manager' REST API. The package exposes a comprehensive class framework and related tools for creating, updating and deleting 'Azure' resource groups, resources and templates. While 'AzureRMR' can be used to manage any 'Azure' service, it can also be extended by other packages to provide extra functionality for specific services. Part of the 'AzureR' family of packages.
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# AzureRMR 2.3.5.9000

- Add ability to specify user-defined functions in `build_template_definition`. Also allow changing the schema, content version and api profile.
- Change maintainer email address.

# AzureRMR 2.3.5

- Fix a bug in printing the error message when a template deployment fails.
Expand Down
63 changes: 51 additions & 12 deletions R/build_tpl_json.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
#' @param ... For `build_template_parameters`, named arguments giving the values of each template parameter. For `build_template_definition`, further arguments passed to class methods.
#' @param parameters For `build_template_definition`, the parameter names and types for the template. See 'Details' below.
#' @param variables Internal variables used by the template.
#' @param functions User-defined functions used by the template.
#' @param resources List of resources that the template should deploy.
#' @param outputs The template outputs.
#' @param schema,content_version,api_profile Less common arguments that can be used to customise the template. See the guide to template syntax on Microsoft Docs, linked below.
#'
#' @details
#' `build_template_definition` is used to generate a template from its components. The arguments can be specified in various ways:
#' `build_template_definition` is used to generate a template from its components. The main arguments are `parameters`, `variables`, `functions`, `resources` and `outputs`. Each of these can be specified in various ways:
#' - As character strings containing unparsed JSON text.
#' - As an R list of (nested) objects, which will be converted to JSON via `jsonlite::toJSON`.
#' - A connection pointing to a JSON file or object.
Expand All @@ -22,6 +24,8 @@
#'
#' @seealso
#' [az_template], [jsonlite::toJSON]
#'
#' [Guide to template syntax](https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/template-syntax)
#' @examples
#' # dummy example
#' # note that 'resources' arg should be a _list_ of resources
Expand All @@ -35,6 +39,27 @@
#' build_template_definition(parameters=c(par1="string"),
#' resources=list(list(name="resource here")))
#'
#' # a user-defined function
#' build_template_definition(
#' parameters=c(name="string"),
#' functions=list(
#' list(
#' namespace="mynamespace",
#' members=list(
#' prefixedName=list(
#' parameters=list(
#' list(name="name", type="string")
#' ),
#' output=list(
#' type="string",
#' value="[concat('AzureR', parameters('name'))]"
#' )
#' )
#' )
#' )
#' )
#' )
#'
#' # realistic example: storage account
#' build_template_definition(
#' parameters=c(
Expand Down Expand Up @@ -116,32 +141,46 @@ build_template_definition <- function(...)

#' @rdname build_template
#' @export
build_template_definition.default <- function(parameters=NULL, variables=NULL, resources=NULL, outputs=NULL, ...)
build_template_definition.default <- function(
parameters=named_list(), variables=named_list(), functions=list(), resources=list(), outputs=named_list(),
schema="https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
content_version="1.0.0.0",
api_profile=NULL,
...)
{
# special treatment for parameters arg: convert 'c(name="type")' to 'list(name=list(type="type"))'
if(is.character(parameters))
parameters <- sapply(parameters, function(type) list(type=type), simplify=FALSE)

parts <- lapply(
list(parameters=parameters, variables=variables, resources=resources, outputs=outputs, ...),
list(
`$schema`=schema,
contentVersion=content_version,
apiProfile=api_profile,
parameters=parameters,
variables=variables,
functions=functions,
resources=resources,
outputs=outputs,
...
),
function(x)
{
if(inherits(x, "connection"))
{
on.exit(close(x))
readLines(x)
}
else generate_json(if(is.null(x)) named_list() else x)
else generate_json(x)
}
)
json <- generate_json(list(
`$schema`="https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
contentVersion="1.0.0.0"
))
for(i in seq_along(parts))
json <- do.call(append_json, c(json, parts[i]))

jsonlite::prettify(json)
parts <- parts[parts != "null"]
# json <- "{}"
# for(i in seq_along(parts))
# if(parts[i] != "null")
# json <- do.call(append_json, c(json, parts[i]))

jsonlite::prettify(do.call(append_json, c(list("{}"), parts)))
}


Expand Down
36 changes: 33 additions & 3 deletions man/build_template.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions tests/resources/functions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[
{
"namespace": "mynamespace",
"members": {
"prefixedName": {
"parameters": [
{
"name": "name",
"type": "string"
}
],
"output": {
"type": "string",
"value": "[concat('azurer', parameters('name'))]"
}
},
"prefixedTagValue": {
"parameters": [
{
"name": "name",
"type": "string"
}
],
"output": {
"type": "string",
"value": "[concat('Azr_', parameters('name'))]"
}
}
}
}
]
3 changes: 3 additions & 0 deletions tests/resources/parameters.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
},
"name": {
"type": "string"
},
"tagvalue": {
"type": "string"
}
}
7 changes: 5 additions & 2 deletions tests/resources/resources.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"name": "[parameters('name')]",
"name": "[mynamespace.prefixedName(parameters('name'))]",
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2018-07-01",
"location": "[parameters('location')]",
Expand All @@ -11,6 +11,9 @@
"sku": {
"name": "Standard_LRS"
},
"kind": "Storage"
"kind": "Storage",
"tags": {
"tag1": "[mynamespace.prefixedTagValue(parameters('tagvalue'))]"
}
}
]
43 changes: 40 additions & 3 deletions tests/resources/template.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,54 @@
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"type": "string"
},
"name": {
"type": "string"
},
"tagvalue": {
"type": "string"
}
},
"variables": {

},
"functions": [
{
"namespace": "mynamespace",
"members": {
"prefixedName": {
"parameters": [
{
"name": "name",
"type": "string"
}
],
"output": {
"type": "string",
"value": "[concat('azurer', parameters('name'))]"
}
},
"prefixedTagValue": {
"parameters": [
{
"name": "name",
"type": "string"
}
],
"output": {
"type": "string",
"value": "[concat('Azr_', parameters('name'))]"
}
}
}
}
],
"resources": [
{
"name": "[parameters('name')]",
"name": "[mynamespace.prefixedName(parameters('name'))]",
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2018-07-01",
"location": "[parameters('location')]",
Expand All @@ -27,7 +61,10 @@
"sku": {
"name": "Standard_LRS"
},
"kind": "Storage"
"kind": "Storage",
"tags": {
"tag1": "[mynamespace.prefixedTagValue(parameters('tagvalue'))]"
}
}
],
"outputs": {
Expand Down
19 changes: 19 additions & 0 deletions tests/resources/template_null.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {

},
"variables": {

},
"functions": [

],
"resources": [

],
"outputs": {

}
}
13 changes: 11 additions & 2 deletions tests/testthat/test05a_template_builders.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ context("Template builders")

test_that("Template definition builder works",
{
expect_silent(build_template_definition())
expect_silent(tpl0 <- build_template_definition())
# make sure test doesn't fail on extraneous newlines at end
expect_identical(
sub("\n+$", "", unclass(tpl0)),
sub("\n+$", "", paste0(readLines("../resources/template_null.json"), collapse="\n"))
)

expect_silent(tpl1 <- build_template_definition(parameters=c(parm1="string", parm2="string")))

Expand Down Expand Up @@ -32,9 +37,13 @@ test_that("Template definition builder works",

expect_silent(tpl6 <- build_template_definition(
parameters=file("../resources/parameters.json"),
functions=file("../resources/functions.json"),
resources=file("../resources/resources.json")
))
expect_identical(unclass(tpl6), paste0(readLines("../resources/template.json"), collapse="\n"))
expect_identical(
sub("\n+$", "", unclass(tpl6)),
sub("\n+$", "", paste0(readLines("../resources/template.json"), collapse="\n"))
)
})


Expand Down
Loading