fix resource issue with no stable api version

This commit is contained in:
Hong Ooi 2021-02-08 11:50:20 +11:00
Родитель 3b5523c2bc
Коммит 418a394b2d
4 изменённых файлов: 34 добавлений и 19 удалений

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

@ -1,6 +1,7 @@
# AzureRMR 2.4.0.9000
- Fix the `set_tags` method to work when called inside a function (#18).
- `get_resource(*, api_version=NULL)` when there are no stable API versions will now warn and use the latest preview version, rather than throw an error (#17).
# AzureRMR 2.4.0

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

@ -206,20 +206,12 @@ public=list(
provider <- substr(self$type, 1, slash - 1)
path <- substr(self$type, slash + 1, nchar(self$type))
op <- construct_path("providers", provider)
apis <- named_list(call_azure_rm(self$token, self$subscription, op)$resourceTypes, "resourceType")
names(apis) <- tolower(names(apis))
apis <- unlist(apis[[tolower(path)]]$apiVersions)
if(stable_only)
apis <- grep("preview", apis, value=TRUE, invert=TRUE)
if(is_empty(apis))
stop("No API versions found (try setting stable_only=FALSE)", call.=FALSE)
private$api_version <- apis[1]
if(is_empty(private$api_version))
stop("Unable to retrieve API version for resource '", self$type, "'.", call.=FALSE)
temp_sub <- az_subscription$new(self$token, self$subscription, list(NULL))
ver <- temp_sub$get_provider_api_version(provider, path, stable_only=stable_only)
if(ver == "")
stop("No API versions found (try setting stable_only=FALSE")
private$api_version <- ver
invisible(private$api_version)
},
@ -461,7 +453,16 @@ private=list(
{
# make sure we have an API to call
if(is.null(private$api_version))
self$set_api_version()
{
res <- try(self$set_api_version(), silent=TRUE)
if(inherits(res, "try-error"))
{
warning("No stable API versions found, falling back to the latest preview version", call.=FALSE)
res <- try(self$set_api_version(stable_only=FALSE), silent=TRUE)
}
if(inherits(res, "try-error"))
stop("No API versions found", call.=FALSE)
}
op <- construct_path("resourcegroups", self$resource_group, "providers", self$type, self$name, op)
call_azure_rm(self$token, self$subscription, op, ..., api_version=api_version)

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

@ -17,13 +17,13 @@ rg <- az_rm$
test_that("Resource methods work",
{
expect_false(rg$resource_exists(type="foo/bar", name="randomname"))
expect_false(rg$resource_exists(type=restype, name=resname))
expect_false(rg$resource_exists(type="Microsoft.Storage/storageAccounts", name="randomname"))
# public key resource (no wait required)
restype <- "Microsoft.Compute/sshPublicKeys"
resname <- paste(sample(letters, 20, replace=TRUE), collapse="")
expect_false(rg$resource_exists(type=restype, name=resname))
res <- rg$create_resource(type=restype, name=resname)
expect_true(rg$resource_exists(type=restype, name=resname))
@ -134,6 +134,13 @@ test_that("Tag creation inside a function works",
expect_true("test_tag" %in% names(tags) && "test value" == tags$test_tag)
})
test_that("Getting a resource with only preview API version works",
{
type <- "Microsoft.sqlvirtualmachine/sqlvirtualmachines"
name <- "nonexistent"
expect_error(expect_warning(rg$get_resource(type=type, name=name), "No stable API versions found"))
})
test_that("Resource deletion works",
{
reslst <- rg$list_resources()

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

@ -42,7 +42,7 @@ test_that("Resource group RBAC works",
{
expect_false(sub$resource_group_exists(rgname))
rg <- sub$create_resource_group(rgname, location="westus")
rg <- sub$create_resource_group(rgname, location="australiaeast")
defs <- rg$list_role_definitions()
expect_is(defs, "data.frame")
@ -71,8 +71,14 @@ test_that("Resource RBAC works",
rg <- sub$get_resource_group(rgname)
res <- rg$create_resource(type=restype, name=resname,
kind="Storage",
sku=list(name="Standard_LRS", tier="Standard"))
kind="StorageV2",
sku=list(name="Standard_LRS", tier="Standard"),
properties=list(
accessTier="hot",
supportsHttpsTrafficOnly=TRUE,
isHnsEnabled=FALSE
),
wait=TRUE)
defs <- res$list_role_definitions()
expect_is(defs, "data.frame")