使用python Azure SDK在Traffic Manager中添加终结点

问题描述

我在azure中有一个流量管理器配置文件,我想使用python脚本将端点添加到流量管理器中。我提到了python sdk,要求使用Azure添加端点。

我们可以使用此功能添加或更新端点

tm_client.endpoints.create_or_update()

但是我似乎不太了解此函数中需要传递的参数。如果有人可以发布示例代码或帮助我。

解决方法

如果要将端点添加到Azure流量管理器,则需要定义Endpoint 对象profileNamegroupNameendpointType。此外,请注意,我们必须根据端点类型和流量路由方法定义不同的Endpoint对象。有关更多详细信息,请参阅here

例如。我的流量路由方法是Priority,端点类型是AzureEndpoints

例如

  1. 创建服务主体并将Contributor角色分配给sp
az login
az ad sp create-for-rbac -n "MyApp" --role contributor \
    --scopes /subscriptions/{SubID} --sdk-auth true
  1. 代码
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.trafficmanager import TrafficManagerManagementClient
from azure.mgmt.trafficmanager.models import EndpointStatus,Endpoint

client_id = "<sp appId>"
secret = "<sp password>"
tenant = "<sp tenant>"
subscription_ID = "<>"

tm_client = TrafficManagerManagementClient(creds,subscription_ID)
# define endpoint
param = Endpoint(
    # my target resource is app service
    target_resource_id='/subscriptions/<>/resourceGroups/<>/providers/Microsoft.Web/sites/<>',endpoint_status=EndpointStatus.enabled,priority=1
)
result = tm_client.endpoints.create_or_update(
    resource_group_name='testsignlar',profile_name='test05',endpoint_type='AzureEndpoints',endpoint_name='mypoint',parameters=param)

print(result.id)

enter image description here enter image description here