如何使用python SDK获取Azure FunctionApp列表

prdp8dxp  于 2022-12-24  发布在  Python
关注(0)|答案(2)|浏览(159)

我正在尝试使用python SDK获取函数应用程序的详细信息,并寻找合适的客户端和方法。我可以使用ResourceManagementClient获取它们的基本信息,但无法获取运行时和其他详细信息。我可以运行CLI az functionapp list并获取所有详细信息。我正在寻找使用python SDK执行等效操作

tzdcorbm

tzdcorbm1#

下面的代码将为我们提供功能应用程序列表以及所有详细信息:
在相应的变量中输入您的订阅ID和资源组。

# Import the needed credential and management objects from the libraries.
from types import FunctionType
from azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient
import os

credential = AzureCliCredential()
subscription_id = "SUBSCRIPTION_ID" #Add your subscription ID
resource_group = "RESOURCE_GROUP_ID" #Add your resource group
resource_client = ResourceManagementClient(credential, subscription_id)
resource_list = resource_client.resources.list()

print(resource_list)
column_width =36
print("Resource".ljust(column_width) + "Type".ljust(column_width)+ "Id".ljust(column_width))
print("-" * (column_width * 2))
for resource in list (resource_list) :
    if (resource.type == "Microsoft.Web/sites" ) and ((resource.kind == "functionapp" ) or (resource.kind == "functionapp,linux" )) :
        print(f"{resource.name:<{column_width}}{resource.type:<{column_width}}{resource.id:<{column_width}}")

有关添加额外参数的信息,请查看文档。

测试的输出信息:

bhmjp9jg

bhmjp9jg2#

您可以使用WebSiteManagementClient类查询函数应用程序的更多详细信息。
WebSiteManagementClient示例可以示例化WebAppsOperations类,该类允许您在函数应用上执行许多操作。
请参阅下面的示例代码,我的工作:

from azure.identity import AzureCliCredential
from azure.mgmt.web import WebSiteManagementClient

credential = AzureCliCredential()
subscription_id = "SUBSCRIPTION_ID" #Add your subscription ID
resource_group = "RESOURCE_GROUP_ID" #Add your resource group
function_app_name = "FUNCTION_APP_NAME" #Add your function app name

# Create the web management client
web_mgmt_client = WebSiteManagementClient(credential, subscription_id)

# Get function app details
function_app_details = app_service_client.web_apps.get(resource_group, function_app_name)
print(function_app_details)

# Get function app configuration details
function_app_config = app_service_client.web_apps.get_configuration(resource_group, function_app_name)
print(function_app_config)

# List function app application settings
function_app_settings = app_service_client.web_apps.list_application_settings(resource_group, function_app_name)
print(function_app_settings)

有关需要访问的任何其他详细信息,请参阅WebAppsOperations方法。

相关问题