Add endpoints in Traffic Manager using python Azure SDK

301 views Asked by At

I have a traffic manager profile in azure and I want to add endpoints to my traffic manager using my python script. I referred to the python sdk for azure to add the endpoints.

We can add or update endpoint using this function

tm_client.endpoints.create_or_update()

But I don't seem to understand the parameters which needs to be passed in this function. If anyone could post a sample code or help me out with it.

1

There are 1 answers

0
Jim Xu On

If you want to add an endpoint to Azure traffic manager, you need to define Endpoint object, profileName, groupName and endpointType. Besides, please note that we have to define different Endpoint object according to the endpoint type and traffic routing method. For more details, please refer to here.

For example. My traffic routing method is Priority and the endpoint type is AzureEndpoints

For example

  1. Create a service principal and assign Contributor role to the sp
az login
az ad sp create-for-rbac -n "MyApp" --role contributor \
    --scopes /subscriptions/{SubID} --sdk-auth true
  1. Code
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