azure-cli icon indicating copy to clipboard operation
azure-cli copied to clipboard

Python + azurecli + sql whitespace issue

Open 0xkam opened this issue 7 years ago • 3 comments

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 *


<!--Please don't remove this:-->
<!--auto-generated-->

0xkam avatar Nov 20 '19 13:11 0xkam

@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?

fengzhou-msft avatar Nov 21 '19 02:11 fengzhou-msft

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.

0xkam avatar Nov 22 '19 10:11 0xkam

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']

jiasli avatar Nov 25 '19 06:11 jiasli