Azure-cli: Python + azurecli + sql whitespace issue

Created on 20 Nov 2019  路  3Comments  路  Source: Azure/azure-cli

This is autogenerated. Please review and update as needed.

Describe the bug

While trying to run the azure cli within python 3.7 environment I'm running into an issue where i cannot get the information about encryption from a specific ID as it contains what i believe single quotes.
While running the code outside of the python env so in cmd:
az sql db tde show --ids "/subscriptions/xxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/xxxxx/providers/Microsoft.Sql/servers/xxxxxx/databases/First Name of_db"
It works perfectly
But when trying to reproduce that in python:
a = az_cli("sql db tde show --ids %s" % (databaseID)
It fails,
I have tried the following and it ALWAYS fails:

json.dump && json.dumps on the databaseID variable.
databaseID.replace("'","\"") , `databaseID.replace(" ","&"), and many more along with \, %20 and many many many more.

Command Name
az sql db tde show

Errors:

invalid resource ID: '/subscriptions/xxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/zzzzzzzzzzz/providers/Microsoft.Sql/servers/xxxxxx/databases/First

To Reproduce:

def az_cli(args_str):
        args = args_str.split()
        cli = get_default_cli()
        cli.invoke(args)
        if cli.result.result:
            return cli.result.result
        elif cli.result.error:
            raise cli.result.error
        return True```
This is the code used to invoke the azure cli within the python env.

az sql db tde show --ids '/subscriptions/xxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/xxxxxxx/providers/Microsoft.Sql/servers/xxxxxx/databases/First Name of_db'

## Expected Behavior
Working code. (processes the databases even with spaces)

## Environment Summary

Windows-10-10.0.18362-SP0
Python 3.6.6
Shell: cmd.exe

azure-cli 2.0.74 *

```


Core

Most helpful comment

str.split splits at all whitespaces, regardless of the quotes. This tears the SQL ID apart.

def az_cli(args_str):
        args = args_str.split()  <<<

A simple solution is to use shlex.split which takes quotes into consideration.

For example:

>>> import shlex
>>> shlex.split('sql db tde show --ids "/subscriptions/xxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/xxxxx/providers/Microsoft.Sql/servers/xxxxxx/databases/First Name of_db"')
['sql', 'db', 'tde', 'show', '--ids', '/subscriptions/xxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/xxxxx/providers/Microsoft.Sql/servers/xxxxxx/databases/First Name of_db']

All 3 comments

@jiasli can you take a look at this issue? It's caused by white sapce in database name. What is the correct way to pass the name in Python?

We're still stuck on this so if anyone has any input on a workaround or even an ETA on a fix I'd really really appreciate this.

str.split splits at all whitespaces, regardless of the quotes. This tears the SQL ID apart.

def az_cli(args_str):
        args = args_str.split()  <<<

A simple solution is to use shlex.split which takes quotes into consideration.

For example:

>>> import shlex
>>> shlex.split('sql db tde show --ids "/subscriptions/xxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/xxxxx/providers/Microsoft.Sql/servers/xxxxxx/databases/First Name of_db"')
['sql', 'db', 'tde', 'show', '--ids', '/subscriptions/xxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/xxxxx/providers/Microsoft.Sql/servers/xxxxxx/databases/First Name of_db']
Was this page helpful?
0 / 5 - 0 ratings

Related issues

ambakshi picture ambakshi  路  3Comments

amarzavery picture amarzavery  路  3Comments

oakeyc picture oakeyc  路  3Comments

cicorias picture cicorias  路  3Comments

dhermans picture dhermans  路  3Comments