这个包提供了代理类,用于直接从Python调用azure命令。
AzurePyProx的Python项目详细描述
az pyproxy公司
这个Python包提供了一个代理类,用于直接从Python调用az命令。在
要求
- azure cli
- Python 3.x版
安装
通过PIP
pip3 install azpyproxy
或者
^{pr2}$源代码构建
wget https://github.com/tomasvotava/az-pyproxy/archive/master.zipunzip master.zipcd az-pyproxy-masterpython setup.py buildpython setup.py install
使用
fromazure_pyproxyimportAzureaz=Azure()# command: az vm start virtualaz.vm_start("virtual")# additional flags and parameters# next method call expands like this:# az resource list --resource-group="YOUR_RESOURCE_GROUP"az.resource_list(resource_group="YOUR_RESOURCE_GROUP")# positional arguments are simply concatenated after the command call, so commands can be also passed as argumentsaz.vm("deallocate",name="virtual",resource_group="YOUR_RESOURCE_GROUP")# expands as:# az vm deallocate --name="virtual" --resource_group="YOUR_RESOURCE_GROUP"# by default, underscores in flag names are replaced with dashesaz.vm_deallocate(name="virtual",resource_group="YOUR_RESOURCE_GROUP")# az vm deallocate --name="virtual" --resource_group="YOUR_RESOURCE_GROUP"# however, this can be overriden by specifying replace_underscore=Falseaz.vm_deallocate(name="virtual",resource_group="YOUR_RESOURCE_GROUP",replace_underscore=False)# az vm deallocate --name="virtual" --resource_group="YOUR_RESOURCE_GROUP"# the command returns json output from the commands, if you would like to redirect stdout/stderr/stdin instead, just pass keep_output=Trueaz.vm_deallocate(name="virtual",resource_group="YOUR_RESOURCE_GROUP",keep_stdout=True)# Returns True/False based on errorcode of the command, prints all output
默认订阅和资源组
如果您不想在每次调用中指定subscription/resource_group,并且没有使用az命令或azurewebui设置默认值,则可以使用为您设计的类方法来处理它。在
fromazure_pyproxyimportAzureaz=Azure()# Shows your default subscriptionaz.account_show()az.account_show(subscription="another_subscription")# Shows your "another_subscription"Azure.set_subscription("another_subscription")az.account_show()# Shows your "another_subscription"# If you have set your subscription using Azure.set_subscription# and you need to use few calls using another one,# you can always specify your subscription as a parameter (this overrides default setting)Azure.set_subscription("another_subscription")az.account_show(subscription="yet_another_one")# Shows info for "yet_another_one"# In case you want to switch back to your az default, you can reset your subscription:Azure.reset_subscription()
上述所有操作也适用于资源组:
fromazure_pyproxyimportAzureaz=Azure()Azure.set_resource_group("resource-group")az.vm_start(name="virtual")
上面的代码启动资源组resource-group
中名为virtual
的VM。以下代码为您重置设置:
Azure.reset_resource_group()
- 项目
标签: