如何正确调用Azure Resource Manager模板语言中定义的函数resourceId()?
Context
/// - Azure
/// - Azure Resource Management
/// https://msdn.microsoft.com/en-us/library/azure/dn578292.aspx
///
/// - Azure Resource Manager Template Language
/// https://msdn.microsoft.com/en-us/library/azure/dn835138.aspx
///
/// - Azure Resource Manager Template Language functions and expressions
/// - Azure Resource Manager Template Language function:
/// resourceId('resourceNamespace/resourceType','resourceName')
///
/// - Powershell
/// - Azure PowerShell
/// - Azure PowerShell Resource Manager Mode (Switch-AzureMode AzureResourceManager)
/// - Azure PowerShell CmdLet: New-AzureResourceGroup
///
This line in the template (see full template below)"sourceDatabaseId": "[resourceId('Microsoft.Sql/servers/databases', 'TestDB')]"
Gives this error when running the PowerShell New-AzureResourceGroup CmdLet:
PS c:\AzureDeployment> New-AzureResourceGroup -Location "North Europe" -Name "psResourceGroup" -DeploymentName "psDeployment" -TemplateFile .\Template.json -TemplateParameterFile .\Parameters.json -Verbose
cmdlet New-AzureResourceGroup at command pipeline position 1
Supply values for the following parameters:
(Type !? for Help.)
VERBOSE: Performing the operation "Replacing resource group ..." on target "psDeployment".
VERBOSE: 16:22:07 - Created resource group 'psResourceGroup' in location 'northeurope'
New-AzureResourceGroup : 16:22:08 - Resource Microsoft.Sql/servers/databases
'xxx-sql-server-name-xxx/psDatabaseName' failed with message
'Unable to process template language expressions for resource
'/subscriptions/xxxxxxxx/resourceGroups/psResourceGroup/providers/Microsoft.Sql/servers/xxx-sql-server-name-xxx/databases/psDatabaseName'
at line _ and column _.
'Unable to evaluate template language function 'resource Id': the type 'Microsoft.Sql/servers/databases' requires '2' resource name argument(s).''
At line:1 char:1
+ New-AzureResourceGroup -Location "North Europe" -Name "psResourceGroup" -Templat ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [ New-AzureResourceGroup ], Exception
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.Resources.NewAzureResourceGroupCommand
根据documentation,函数 resourceId()
有2个参数,我用两个常量字符串调用它,我认为是正确的:resourceId('Microsoft.Sql/servers/databases', 'TestDB')
仍然会产生一条错误消息,指出参数的数量是错误的:'Unable to evaluate template language function 'resource Id': the type 'Microsoft.Sql/servers/databases' requires '2' resource name argument(s).'
根据错误消息使用的资源是: '/subscriptions/xxxxxxxx/resourceGroups/psResourceGroup/providers/Microsoft.Sql/servers/xxx-sql-server-name-xxx/databases/psDatabaseName'
那么,为数据库调用resourceId()的正确方法是什么?
此外,如果我从模板中删除createMode和sourceDatabaseId一切正常 .
This is the template used above
{
"$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"type": "string",
"defaultValue": "North Europe",
"allowedValues": [
"East Asia",
"South East Asia",
"East US",
"West US",
"North Central US",
"South Central US",
"Central US",
"North Europe",
"West Europe"
]
},
"sqlServerName": { "type": "string" },
"sqlAdminUserName": { "type": "string" },
"sqlAdminUserPassword": { "type": "securestring" },
"databaseName": { "type": "string" }
},
"resources": [
{
"type": "Microsoft.Sql/servers",
"apiVersion": "2.0",
"location": "[parameters('location')]",
"name": "[parameters('sqlServerName')]",
"properties": { "administratorLogin": "[parameters('sqlAdminUserName')]", "administratorLoginPassword": "[parameters('sqlAdminUserPassword')]" },
"resources": [
{
"type": "databases",
"apiVersion": "2.0",
"location": "[parameters('location')]",
"name": "[parameters('databaseName')]",
"dependsOn": [ "[concat('Microsoft.Sql/servers/', parameters('sqlServerName'))]" ],
"properties": {
"edition": "Standard",
"collation": "SQL_Latin1_General_CP1_CI_AS",
"maxSizeBytes": "10737418240",
"requestedServiceObjectiveId": "f1173c43-91bd-4aaa-973c-54e79e15235b",
"createMode": "Copy",
====> "sourceDatabaseId": "[resourceId('Microsoft.Sql/servers/databases', 'TestDB')]"
}
}
]
}
]
}
2 回答
我在一篇完全不相关的文章中偶然发现了解决方案,但你应该过去了
[resourceId('Microsoft.SQL/servers/databases', parameters('sqlServerName'), 'TestDB')]
我注意到你的apiVersion值格式不正确 . Azure使用API的日期值 .
尝试使用示例文件中的值: