Command Name
az rest
Errors:
Bad Request({"error":{"details":[{"code":"InvalidResourceIdSegment","message":"","target":"parameters.properties.weeklyRetention"}],"code":"InvalidResourceIdSegment","message":"The 'parameters.properties.weeklyRetention' segment in the url is invalid."}})
Steps to reproduce the behavior. Note that argument values have been redacted, as they may contain sensitive information.
# Language : PowerShell
$server = "YOUR_AZURE_SQL_SERVER"
$database = "YOUR_AZURE_SQL_DATABASE"
$weeklyRetention = "P2W"
$policyName = "default"
$apiVersion = "2019-06-01-preview"
$srvs = az sql server list --query "[?name=='$server']" | ConvertFrom-Json
$srv = $srvs[0]
$dbs = az sql db list --resource-group $srv.resourceGroup --server $srv.name --query "[?name=='$database']" | ConvertFrom-Json
$db = $dbs[0]
# This works 馃憤
az rest --method "GET" --uri "$($srv.id)/databases/$($db.name)/backupLongTermRetentionPolicies/$($policyName)?api-version=$apiVersion" | ConvertFrom-Json
# This Fails 馃憥
az rest --method "PUT" --uri "$($srv.id)/databases/$($db.name)/backupLongTermRetentionPolicies/$($policyName)?api-version=$apiVersion" --header "Content-Type=application/json" --body "{ ""properties"": { ""weeklyRetention"": ""$weeklyRetention"" } }" --debug
Should update with 202 response:
{
"operation": "UpsertDatabaseBackupArchivalPolicyV2",
"startTime": "2020-05-05T15:04:16.397Z"
}
Windows-10-10.0.14393-SP0
Python 3.6.6
azure-cli 2.2.0
Also note that setting backupShortTermRetentionPolicies on the same resource works without a problem:
az rest --method "PUT" --uri "$($srv.id)/databases/$($db.name)/backupShortTermRetentionPolicies/$($policyName)?api-version=$apiVersion" --header "Content-Type=application/json" --body "{ ""properties"": { ""retentionDays"": 35 } }"
So I know it's not an issue with the headers or how I pass the JSON body.
PostMan behavior is working as per documentation

But the behavior with Azure CLI is not working as expected.
While debugging the code below:
https://github.com/Azure/azure-cli/blob/4e1ed61cb44933fd59e4d44f729b09763c372895/src/azure-cli-core/azure/cli/core/util.py#L598

I noticed that the json body being sent here is as below python string:
{ properties: { weeklyRetention: P10Y } }
Which throws an exception at:
https://github.com/Azure/azure-cli/blob/4e1ed61cb44933fd59e4d44f729b09763c372895/src/azure-cli-core/azure/cli/core/util.py#L297

Double-Escaping quotes around json property values seems like the only way this works at the moment.
For example:
# Language : PowerShell
az rest --body '{ "properties": { "weeklyRetention": "\"P10Y\"" } }' --method "PUT" --uri "$($srv.id)/databases/$($db.name)/backupLongTermRetentionPolicies/$($policyName)?api-version=$apiVersion" --header "Content-Type=application/json"

As this is no longer a blocker, it is not a high priority.
But it would be very nice to fix as it is causing issues with upstream json serializers when double-escaping like this.
hi @jiasli could you have a look?
add to S170
Typical PowerShell issue https://github.com/PowerShell/PowerShell/issues/1995. 馃槙
This command should fail as well because the quote quotes are lost:
az "{ ""properties"": { ""retentionDays"": 35 } }" --debug
Command arguments: ['{ properties: { retentionDays: 35 } }', '--debug']
Please see my latest PR https://github.com/Azure/azure-cli/pull/13419 where I am trying to make this clear and providing some workarounds.