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

Add tags for created objects #10

Merged
merged 6 commits into from
Jun 9, 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
@@ -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", , "[email protected]", role = c("aut", "cre")),
person("Microsoft", role="cph")
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.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

- 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.
Expand Down
7 changes: 4 additions & 3 deletions R/az_resgroup.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions R/az_resource.R
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
2 changes: 1 addition & 1 deletion R/az_subscription.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 10 additions & 1 deletion R/az_template.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -84,6 +86,7 @@ public=list(

self$id <- parms$id
self$properties <- parms$properties
self$tags <- parms$tags
NULL
},

Expand Down Expand Up @@ -159,6 +162,11 @@ public=list(
named_list(outlst[!nulls], c("type", "name"))
},

get_tags=function()
{
self$tags
},

print=function(...)
{
cat("<Azure template ", self$name, ">\n", sep="")
Expand Down Expand Up @@ -225,8 +233,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"
)
Expand Down
9 changes: 9 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,12 @@ 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)
{
if(!is.list(tags))
tags <- list()
utils::modifyList(list(createdBy="AzureR/AzureRMR"), tags)
}
6 changes: 3 additions & 3 deletions man/az_resource_group.Rd

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

2 changes: 1 addition & 1 deletion man/az_subscription.Rd

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

1 change: 1 addition & 0 deletions man/az_template.Rd

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

6 changes: 3 additions & 3 deletions tests/testthat/test03_rg.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()))

Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test04_resource.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()))

Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/test05b_template.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)