Azure-cli: Azure REST API for SQL not working for CLI but works for Post Man

Created on 5 May 2020  路  5Comments  路  Source: Azure/azure-cli

Describe the bug

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."}})

To Reproduce:

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

Expected Behavior

Should update with 202 response:

{
    "operation": "UpsertDatabaseBackupArchivalPolicyV2",
    "startTime": "2020-05-05T15:04:16.397Z"
}

Environment Summary

Windows-10-10.0.14393-SP0
Python 3.6.6

azure-cli 2.2.0

Additional Context

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.


Shell - PowerShell customer-reported rest

All 5 comments

PostMan behavior is working as per documentation

image

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
image

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
image


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"

image


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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dhermans picture dhermans  路  3Comments

binderjoe picture binderjoe  路  3Comments

williexu picture williexu  路  3Comments

cicorias picture cicorias  路  3Comments

Kannaj picture Kannaj  路  3Comments