From c813e72b8cce96cf4b64a718bf2827c48b5cf771 Mon Sep 17 00:00:00 2001 From: Hong Ooi Date: Fri, 5 Jun 2020 23:15:50 +1000 Subject: [PATCH 1/6] add createdBy tag to new objects --- R/az_resgroup.R | 1 + R/az_resource.R | 1 + R/utils.R | 7 +++++++ tests/testthat/test03_rg.R | 6 +++--- tests/testthat/test04_resource.R | 6 +++--- 5 files changed, 15 insertions(+), 6 deletions(-) diff --git a/R/az_resgroup.R b/R/az_resgroup.R index fe12f29..5b72e2b 100644 --- a/R/az_resgroup.R +++ b/R/az_resgroup.R @@ -311,6 +311,7 @@ private=list( init_and_create=function(name, ...) { parms <- modifyList(list(...), list(name=name)) + parms$tags <- add_creator_tag(parms$tags) # private$validate_parms(parms) self$name <- name private$rg_op(body=parms, encode="json", http_verb="PUT") diff --git a/R/az_resource.R b/R/az_resource.R index 6d989f9..422b94d 100644 --- a/R/az_resource.R +++ b/R/az_resource.R @@ -347,6 +347,7 @@ private=list( properties <- jsonlite::fromJSON(properties[[1]], simplifyVector=FALSE) # private$validate_deploy_parms(properties) + properties$tags <- add_creator_tag(properties$tags) private$res_op(body=properties, encode="json", http_verb="PUT") # do we wait until resource has finished provisioning? diff --git a/R/utils.R b/R/utils.R index d355f76..f349dae 100644 --- a/R/utils.R +++ b/R/utils.R @@ -113,3 +113,10 @@ delete_confirmed <- function(confirm, name, type, quote_name=TRUE) else utils::askYesNo(msg, FALSE) isTRUE(ok) } + + +# add a tag on objects created by this package +add_creator_tag <- function(tags) +{ + utils::modifyList(tags, list(createdBy="AzureR/AzureRMR")) +} diff --git a/tests/testthat/test03_rg.R b/tests/testthat/test03_rg.R index 5826752..4f02132 100644 --- a/tests/testthat/test03_rg.R +++ b/tests/testthat/test03_rg.R @@ -31,11 +31,11 @@ test_that("Resource group methods work", # tagging rgnew$set_tags(tag1="value1") - expect_identical(rgnew$get_tags(), list(tag1="value1")) + expect_identical(rgnew$get_tags(), list(createdBy="AzureR/AzureRMR", tag1="value1")) rgnew$set_tags(tag2) - expect_identical(rgnew$get_tags(), list(tag1="value1", tag2="")) + expect_identical(rgnew$get_tags(), list(createdBy="AzureR/AzureRMR", tag1="value1", tag2="")) rgnew$set_tags(tag2=NULL) - expect_identical(rgnew$get_tags(), list(tag1="value1")) + expect_identical(rgnew$get_tags(), list(createdBy="AzureR/AzureRMR", tag1="value1")) rgnew$set_tags(keep_existing=FALSE) expect_true(is_empty(rgnew$get_tags())) diff --git a/tests/testthat/test04_resource.R b/tests/testthat/test04_resource.R index dc7f8fa..b5696d2 100644 --- a/tests/testthat/test04_resource.R +++ b/tests/testthat/test04_resource.R @@ -46,11 +46,11 @@ test_that("Resource methods work", # tagging res$set_tags(tag1="value1") - expect_identical(res$get_tags(), list(tag1="value1")) + expect_identical(res$get_tags(), list(createdBy="AzureR/AzureRMR", tag1="value1")) res$set_tags(tag2) - expect_identical(res$get_tags(), list(tag1="value1", tag2="")) + expect_identical(res$get_tags(), list(createdBy="AzureR/AzureRMR", tag1="value1", tag2="")) res$set_tags(tag2=NULL) - expect_identical(res$get_tags(), list(tag1="value1")) + expect_identical(res$get_tags(), list(createdBy="AzureR/AzureRMR", tag1="value1")) res$set_tags(keep_existing=FALSE) expect_true(is_empty(res$get_tags())) From 65783c30c94b38a4854aa6d6ad77dacb170de8a2 Mon Sep 17 00:00:00 2001 From: Hong Ooi Date: Fri, 5 Jun 2020 23:25:05 +1000 Subject: [PATCH 2/6] fix --- R/utils.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/R/utils.R b/R/utils.R index f349dae..ac4ce81 100644 --- a/R/utils.R +++ b/R/utils.R @@ -118,5 +118,7 @@ delete_confirmed <- function(confirm, name, type, quote_name=TRUE) # add a tag on objects created by this package add_creator_tag <- function(tags) { + if(!is.list(tags)) + tags <- list() utils::modifyList(tags, list(createdBy="AzureR/AzureRMR")) } From 92818c45f153888f4029288aa40f8fc6d5ead668 Mon Sep 17 00:00:00 2001 From: Hong Ooi Date: Fri, 5 Jun 2020 23:37:52 +1000 Subject: [PATCH 3/6] also tag templates --- R/az_template.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/az_template.R b/R/az_template.R index 2aeb92c..380fb6b 100644 --- a/R/az_template.R +++ b/R/az_template.R @@ -225,8 +225,9 @@ private=list( else append_json(properties, parameters=parameters) self$name <- name + tags <- jsonlite::toJSON(list(createdBy="AzureR/AzureRMR"), auto_unbox=TRUE) parms <- private$tpl_op( - body=jsonlite::prettify(sprintf('{"properties": %s}', properties)), + body=jsonlite::prettify(sprintf('{"properties": %s, "tags": %s}', properties, tags)), encode="raw", http_verb="PUT" ) From a5ca24c309a137f90602cbb1c5720403446dff21 Mon Sep 17 00:00:00 2001 From: Hong Ooi Date: Fri, 5 Jun 2020 23:42:24 +1000 Subject: [PATCH 4/6] don't overwrite manually assigned tag --- DESCRIPTION | 2 +- NEWS.md | 4 ++++ R/utils.R | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 45cf174..b923766 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: AzureRMR Title: Interface to 'Azure Resource Manager' -Version: 2.3.3 +Version: 2.3.3.9000 Authors@R: c( person("Hong", "Ooi", , "hongooi@microsoft.com", role = c("aut", "cre")), person("Microsoft", role="cph") diff --git a/NEWS.md b/NEWS.md index a0e57ca..49b9b75 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +# AzureRMR 2.3.3.9000 + +- Add `createdBy:AzureR/AzureRMR` tag to Azure objects (resource groups, resources and templates) created by this package. + # AzureRMR 2.3.3 - Allow for extra resource type-specific fields beyond those mentioned in the Resource Manager documentation. In particular, virtual machines and managed disks may have a `zones` field containing the availability zones. diff --git a/R/utils.R b/R/utils.R index ac4ce81..9265be0 100644 --- a/R/utils.R +++ b/R/utils.R @@ -120,5 +120,5 @@ add_creator_tag <- function(tags) { if(!is.list(tags)) tags <- list() - utils::modifyList(tags, list(createdBy="AzureR/AzureRMR")) + utils::modifyList(list(createdBy="AzureR/AzureRMR"), tags) } From dda26775703600ff132dce2edde7e57472adcf4f Mon Sep 17 00:00:00 2001 From: Hong Ooi Date: Tue, 9 Jun 2020 21:40:30 +1000 Subject: [PATCH 5/6] add template get_tags(), update docs --- R/az_resgroup.R | 6 +++--- R/az_subscription.R | 2 +- R/az_template.R | 8 ++++++++ man/az_resource_group.Rd | 6 +++--- man/az_subscription.Rd | 2 +- man/az_template.Rd | 1 + tests/testthat/test05b_template.R | 3 +++ 7 files changed, 20 insertions(+), 8 deletions(-) diff --git a/R/az_resgroup.R b/R/az_resgroup.R index 5b72e2b..1d7bf13 100644 --- a/R/az_resgroup.R +++ b/R/az_resgroup.R @@ -9,16 +9,16 @@ #' - `sync_fields()`: Synchronise the R object with the resource group it represents in Azure. #' - `list_templates()`: List deployed templates in this resource group. #' - `get_template(name)`: Return an object representing an existing template. -#' - `deploy_template(...)`: Deploy a new template. See 'Templates' for more details. +#' - `deploy_template(...)`: Deploy a new template. See 'Templates' for more details. By default, AzureRMR will set the `createdBy` tag on a newly-deployed template to the value `AzureR/AzureRMR`. #' - `delete_template(name, confirm=TRUE, free_resources=FALSE)`: Delete a deployed template, and optionally free any resources that were created. #' - `get_resource(...)`: Return an object representing an existing resource. See 'Resources' for more details. -#' - `create_resource(...)`: Create a new resource. +#' - `create_resource(...)`: Create a new resource. By default, AzureRMR will set the `createdBy` tag on a newly-created resource to the value `AzureR/AzureRMR`. #' - `delete_resource(..., confirm=TRUE, wait=FALSE)`: Delete an existing resource. Optionally wait for the delete to finish. #' - `resource_exists(...)`: Check if a resource exists. #' - `list_resources()`: Return a list of resource group objects for this subscription. #' - `do_operation(...)`: Carry out an operation. See 'Operations' for more details. #' - `set_tags(..., keep_existing=TRUE)`: Set the tags on this resource group. The tags can be either names or name-value pairs. To delete a tag, set it to `NULL`. -#' - `get_tags()`: Get the tags on this resource. +#' - `get_tags()`: Get the tags on this resource group. #' - `create_lock(name, level)`: Create a management lock on this resource group (which will propagate to all resources within it). #' - `get_lock(name)`: Returns a management lock object. #' - `delete_lock(name)`: Deletes a management lock object. diff --git a/R/az_subscription.R b/R/az_subscription.R index 7c2573f..89388f0 100644 --- a/R/az_subscription.R +++ b/R/az_subscription.R @@ -9,7 +9,7 @@ #' - `new(token, id, ...)`: Initialize a subscription object. #' - `list_resource_groups()`: Return a list of resource group objects for this subscription. #' - `get_resource_group(name)`: Return an object representing an existing resource group. -#' - `create_resource_group(name, location)`: Create a new resource group in the specified region/location, and return an object representing it. +#' - `create_resource_group(name, location)`: Create a new resource group in the specified region/location, and return an object representing it. By default, AzureRMR will set the `createdBy` tag on a newly-created resource group to the value `AzureR/AzureRMR`. #' - `delete_resource_group(name, confirm=TRUE)`: Delete a resource group, after asking for confirmation. #' - `resource_group_exists(name)`: Check if a resource group exists. #' - `list_resources()`: List all resources deployed under this subscription. diff --git a/R/az_template.R b/R/az_template.R index 380fb6b..34ffb2d 100644 --- a/R/az_template.R +++ b/R/az_template.R @@ -9,6 +9,7 @@ #' - `cancel(free_resources=FALSE)`: Cancel an in-progress deployment. Optionally free any resources that have already been created. #' - `delete(confirm=TRUE, free_resources=FALSE)`: Delete a deployed template, after a confirmation check. Optionally free any resources that were created. If the template was deployed in Complete mode (its resource group is exclusive to its use), the latter process will delete the entire resource group. Otherwise resources are deleted in the order given by the template's output resources list; in this case, some may be left behind if the ordering is incompatible with dependencies. #' - `list_resources()`: Returns a list of Azure resource objects that were created by the template. This returns top-level resources only, not those that represent functionality provided by another resource. +#' - `get_tags()`: Returns the tags for the deployment template (note: this is not the same as the tags applied to resources that are deployed). #' #' @section Initialization: #' Initializing a new object of this class can either retrieve an existing template, or deploy a new template on the host. Generally, the easiest way to create a template object is via the `get_template`, `deploy_template` or `list_templates` methods of the [az_resource_group] class, which handle the details automatically. @@ -64,6 +65,7 @@ public=list( id=NULL, name=NULL, properties=NULL, + tags=NULL, token=NULL, # constructor overloads: 1) get an existing template from host; 2) from passed-in data; 3) deploy new template @@ -84,6 +86,7 @@ public=list( self$id <- parms$id self$properties <- parms$properties + self$tags <- parms$tags NULL }, @@ -159,6 +162,11 @@ public=list( named_list(outlst[!nulls], c("type", "name")) }, + get_tags=function() + { + self$tags + }, + print=function(...) { cat("\n", sep="") diff --git a/man/az_resource_group.Rd b/man/az_resource_group.Rd index 3d1b7d7..d12c6c3 100644 --- a/man/az_resource_group.Rd +++ b/man/az_resource_group.Rd @@ -18,16 +18,16 @@ Class representing an Azure resource group. \item \code{sync_fields()}: Synchronise the R object with the resource group it represents in Azure. \item \code{list_templates()}: List deployed templates in this resource group. \item \code{get_template(name)}: Return an object representing an existing template. -\item \code{deploy_template(...)}: Deploy a new template. See 'Templates' for more details. +\item \code{deploy_template(...)}: Deploy a new template. See 'Templates' for more details. By default, AzureRMR will set the \code{createdBy} tag on a newly-deployed template to the value \code{AzureR/AzureRMR}. \item \code{delete_template(name, confirm=TRUE, free_resources=FALSE)}: Delete a deployed template, and optionally free any resources that were created. \item \code{get_resource(...)}: Return an object representing an existing resource. See 'Resources' for more details. -\item \code{create_resource(...)}: Create a new resource. +\item \code{create_resource(...)}: Create a new resource. By default, AzureRMR will set the \code{createdBy} tag on a newly-created resource to the value \code{AzureR/AzureRMR}. \item \code{delete_resource(..., confirm=TRUE, wait=FALSE)}: Delete an existing resource. Optionally wait for the delete to finish. \item \code{resource_exists(...)}: Check if a resource exists. \item \code{list_resources()}: Return a list of resource group objects for this subscription. \item \code{do_operation(...)}: Carry out an operation. See 'Operations' for more details. \item \code{set_tags(..., keep_existing=TRUE)}: Set the tags on this resource group. The tags can be either names or name-value pairs. To delete a tag, set it to \code{NULL}. -\item \code{get_tags()}: Get the tags on this resource. +\item \code{get_tags()}: Get the tags on this resource group. \item \code{create_lock(name, level)}: Create a management lock on this resource group (which will propagate to all resources within it). \item \code{get_lock(name)}: Returns a management lock object. \item \code{delete_lock(name)}: Deletes a management lock object. diff --git a/man/az_subscription.Rd b/man/az_subscription.Rd index 1b4d74e..65048ee 100644 --- a/man/az_subscription.Rd +++ b/man/az_subscription.Rd @@ -16,7 +16,7 @@ Class representing an Azure subscription. \item \code{new(token, id, ...)}: Initialize a subscription object. \item \code{list_resource_groups()}: Return a list of resource group objects for this subscription. \item \code{get_resource_group(name)}: Return an object representing an existing resource group. -\item \code{create_resource_group(name, location)}: Create a new resource group in the specified region/location, and return an object representing it. +\item \code{create_resource_group(name, location)}: Create a new resource group in the specified region/location, and return an object representing it. By default, AzureRMR will set the \code{createdBy} tag on a newly-created resource group to the value \code{AzureR/AzureRMR}. \item \code{delete_resource_group(name, confirm=TRUE)}: Delete a resource group, after asking for confirmation. \item \code{resource_group_exists(name)}: Check if a resource group exists. \item \code{list_resources()}: List all resources deployed under this subscription. diff --git a/man/az_template.Rd b/man/az_template.Rd index 7a36a24..f32de34 100644 --- a/man/az_template.Rd +++ b/man/az_template.Rd @@ -18,6 +18,7 @@ Class representing an Azure deployment template. \item \code{cancel(free_resources=FALSE)}: Cancel an in-progress deployment. Optionally free any resources that have already been created. \item \code{delete(confirm=TRUE, free_resources=FALSE)}: Delete a deployed template, after a confirmation check. Optionally free any resources that were created. If the template was deployed in Complete mode (its resource group is exclusive to its use), the latter process will delete the entire resource group. Otherwise resources are deleted in the order given by the template's output resources list; in this case, some may be left behind if the ordering is incompatible with dependencies. \item \code{list_resources()}: Returns a list of Azure resource objects that were created by the template. This returns top-level resources only, not those that represent functionality provided by another resource. +\item \code{get_tags()}: Returns the tags for the deployment template (note: this is not the same as the tags applied to resources that are deployed). } } diff --git a/tests/testthat/test05b_template.R b/tests/testthat/test05b_template.R index 42d206f..3aa83ba 100644 --- a/tests/testthat/test05b_template.R +++ b/tests/testthat/test05b_template.R @@ -72,6 +72,9 @@ test_that("Template methods work", tpl4 <- rg$deploy_template(tplname4, template=tpl_def, parameters=par_def, wait=TRUE) tpl4$check() expect_is(tpl4, "az_template") + + # tagging + expect_identical(tpl4$get_tags(), list(createdBy="AzureR/AzureRMR")) }) rg$delete(confirm=FALSE) From 68cabdea03313282dac42360324745774f6f2b23 Mon Sep 17 00:00:00 2001 From: Hong Ooi Date: Tue, 9 Jun 2020 21:41:29 +1000 Subject: [PATCH 6/6] update news --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index 49b9b75..4198361 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,7 @@ # AzureRMR 2.3.3.9000 - Add `createdBy:AzureR/AzureRMR` tag to Azure objects (resource groups, resources and templates) created by this package. +- Add a `get_tags()` method for templates. # AzureRMR 2.3.3