Handle extended resource fields (#9)

closes #8
This commit is contained in:
Hong Ooi 2020-05-13 20:07:48 +10:00 коммит произвёл GitHub
Родитель 412f097db8
Коммит 875c71cb3a
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
7 изменённых файлов: 96 добавлений и 56 удалений

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

@ -1,6 +1,6 @@
Package: AzureRMR
Title: Interface to 'Azure Resource Manager'
Version: 2.3.2
Version: 2.3.2.9000
Authors@R: c(
person("Hong", "Ooi", , "hongooi@microsoft.com", role = c("aut", "cre")),
person("Microsoft", role="cph")

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

@ -1,3 +1,7 @@
# AzureRMR 2.3.2.9000
- 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.
# AzureRMR 2.3.2
- Add `do_operation` method for the Resource Manager login client, allowing arbitrary operations at the top-level scope.

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

@ -302,7 +302,7 @@ private=list(
}
else
{
private$validate_parms(parms)
# private$validate_parms(parms)
self$name <- parms$name
}
parms
@ -311,17 +311,17 @@ private=list(
init_and_create=function(name, ...)
{
parms <- modifyList(list(...), list(name=name))
private$validate_parms(parms)
# private$validate_parms(parms)
self$name <- name
private$rg_op(body=parms, encode="json", http_verb="PUT")
},
validate_parms=function(parms)
{
required_names <- c("location", "name")
optional_names <- c("id", "managedBy", "tags", "properties", "type")
validate_object_names(names(parms), required_names, optional_names)
},
# validate_parms=function(parms)
# {
# required_names <- c("location", "name")
# optional_names <- c("id", "managedBy", "tags", "properties", "type")
# validate_object_names(names(parms), required_names, optional_names)
# },
rg_op=function(op="", ...)
{

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

@ -130,6 +130,7 @@ public=list(
tags=NULL,
token=NULL,
etag=NULL,
ext=list(),
# constructor overloads:
# 1. deploy resource: resgroup, {provider, path}|type, name, ...
@ -163,6 +164,7 @@ public=list(
self$sku <- parms$sku
self$tags <- parms$tags
self$etag <- parms$etag
self$ext <- get_extended_resource_fields(parms)
NULL
},
@ -240,9 +242,9 @@ public=list(
update=function(..., options=list())
{
parms <- list(...)
private$validate_update_parms(names(parms))
# private$validate_update_parms(names(parms))
private$res_op(
body=jsonlite::toJSON(parms, auto_unbox=TRUE, digits=22),
body=jsonlite::toJSON(parms, auto_unbox=TRUE, digits=22, null="null"),
options=options,
encode="raw",
http_verb="PATCH"
@ -326,8 +328,8 @@ private=list(
init_from_parms=function(parms)
{
# allow list(NULL) as special case for creating an empty object
if(!identical(parms, list(NULL)))
private$validate_response_parms(parms)
# if(!identical(parms, list(NULL)))
# private$validate_response_parms(parms)
parms
},
@ -344,7 +346,7 @@ private=list(
if(length(properties) == 1 && is.character(properties[[1]]) && jsonlite::validate(properties[[1]]))
properties <- jsonlite::fromJSON(properties[[1]], simplifyVector=FALSE)
private$validate_deploy_parms(properties)
# private$validate_deploy_parms(properties)
private$res_op(body=properties, encode="json", http_verb="PUT")
# do we wait until resource has finished provisioning?
@ -379,29 +381,29 @@ private=list(
}
},
validate_deploy_parms=function(parms)
{
required_names <- character(0)
optional_names <-
c("identity", "kind", "location", "managedBy", "plan", "properties", "sku", "tags", "etag")
validate_object_names(names(parms), required_names, optional_names)
},
# validate_deploy_parms=function(parms)
# {
# required_names <- character(0)
# optional_names <-
# c("identity", "kind", "location", "managedBy", "plan", "properties", "sku", "tags", "etag")
# validate_object_names(names(parms), required_names, optional_names)
# },
validate_response_parms=function(parms)
{
required_names <- c("id", "name", "type")
optional_names <-
c("identity", "kind", "location", "managedBy", "plan", "properties", "sku", "tags", "etag")
validate_object_names(names(parms), required_names, optional_names)
},
# validate_response_parms=function(parms)
# {
# required_names <- c("id", "name", "type")
# optional_names <-
# c("identity", "kind", "location", "managedBy", "plan", "properties", "sku", "tags", "etag")
# validate_object_names(names(parms), required_names, optional_names)
# },
validate_update_parms=function(parms)
{
required_names <- character(0)
optional_names <-
c("identity", "kind", "location", "managedBy", "plan", "properties", "sku", "tags", "etag")
validate_object_names(names(parms), required_names, optional_names)
},
# validate_update_parms=function(parms)
# {
# required_names <- character(0)
# optional_names <-
# c("identity", "kind", "location", "managedBy", "plan", "properties", "sku", "tags", "etag")
# validate_object_names(names(parms), required_names, optional_names)
# },
res_op=function(op="", ..., api_version=private$api_version)
{
@ -413,3 +415,12 @@ private=list(
call_azure_rm(self$token, self$subscription, op, ..., api_version=api_version)
}
))
get_extended_resource_fields <- function(res_fields)
{
known_fields <- c("id", "name", "type", "identity", "kind", "location", "managedBy",
"plan", "properties", "sku", "tags", "etag")
nms <- names(res_fields)
res_fields[!(nms %in% known_fields)]
}

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

@ -178,7 +178,7 @@ private=list(
init_from_parms=function(parms)
{
private$validate_response_parms(parms)
# private$validate_response_parms(parms)
self$name <- parms$name
parms
},
@ -193,7 +193,7 @@ private=list(
mode="Incremental"
)
properties <- modifyList(default_properties, list(...))
private$validate_deploy_parms(properties)
# private$validate_deploy_parms(properties)
# rather than working with R objects, convert to JSON and do text munging
# this allows adding template/params that are already JSON text without conversion roundtrip
@ -260,19 +260,19 @@ private=list(
parms
},
validate_response_parms=function(parms)
{
required_names <- c("name")
optional_names <- c("id", "properties")
validate_object_names(names(parms), required_names, optional_names)
},
# validate_response_parms=function(parms)
# {
# required_names <- c("name")
# optional_names <- c("id", "properties")
# validate_object_names(names(parms), required_names, optional_names)
# },
validate_deploy_parms=function(parms)
{
required_names <- c("debugSetting", "mode")
optional_names <- c("onErrorDeployment")
validate_object_names(names(parms), required_names, optional_names)
},
# validate_deploy_parms=function(parms)
# {
# required_names <- c("debugSetting", "mode")
# optional_names <- c("onErrorDeployment")
# validate_object_names(names(parms), required_names, optional_names)
# },
# delete resources that were created (which may not be the same as resources that are required)
free_resources=function()

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

@ -78,12 +78,12 @@ get_paged_list <- function(lst, token, next_link_name="nextLink", value_name="va
# check that 1) all required names are present; 2) optional names may be present; 3) no other names are present
validate_object_names <- function(x, required, optional=character(0))
{
valid <- all(required %in% x) && all(x %in% c(required, optional))
if(!valid)
stop("Invalid object names")
}
# validate_object_names <- function(x, required, optional=character(0))
# {
# valid <- all(required %in% x) && all(x %in% c(required, optional))
# if(!valid)
# stop("Invalid object names")
# }
# handle different behaviour of file_path on Windows/Linux wrt trailing /

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

@ -17,8 +17,8 @@ rg <- az_rm$
test_that("Resource methods work",
{
restype <- "Microsoft.Storage/storageAccounts"
# storage account resource
restype <- "Microsoft.Storage/storageAccounts"
resname <- paste(sample(letters, 20, replace=TRUE), collapse="")
expect_false(rg$resource_exists(type="foo/bar", name="randomname"))
@ -78,4 +78,29 @@ test_that("Resource methods work",
expect_true(is(res2, "az_resource") && !is_empty(res2$properties))
})
test_that("Extended resource fields works",
{
# managed disk resource
restype <- "Microsoft.Compute/disks"
resname <- paste(sample(letters, 20, replace=TRUE), collapse="")
res <- rg$create_resource(type=restype, name=resname,
properties=list(
creationData=list(createOption="empty"),
diskSizeGB=500,
osType=""
),
sku=list(name="Standard_LRS"),
zones=list(1)
)
expect_true(rg$resource_exists(type=restype, name=resname))
expect_is(res, "az_resource")
expect_false(is_empty(res$ext))
reslst <- rg$list_resources()
expect_true(is.list(reslst) && all(sapply(reslst, is_resource)))
})
rg$delete(confirm=FALSE)