Search code examples
azurecloudazure-web-roles

List all instance endpoints in Azure Cloud


How do I list all the endpoints in the Azure Cloud and not just the endpoints on the current role instance?

I've tried the following code but it only lists the endpoints on the current Azure instance and not on any other Azure instances:

void ListCloudInstances()
{
    var instances = new StringBuilder("");

    foreach (var role in RoleEnvironment.Roles)
    {
        instances.AppendFormat("<h2>Role <em>{0}</em></h2><br/>", role.Value.Name);
        if (role.Value.Instances.Count == 0)
            instances.AppendFormat("<small>Role <em>{0}</em> has no role instances</small><br/><br/>", role.Value.Name);

        foreach (var roleInstance in role.Value.Instances)
        {
            var currentRoleMarker = RoleEnvironment.CurrentRoleInstance.Id == roleInstance.Id ? " *" : String.Empty;
            instances.AppendFormat("<h3>Role <em>{2}</em> instance <em>{0}</em>{1}</h3><br/>", 
                roleInstance.Id, currentRoleMarker, roleInstance.Role.Name);

            // List some metadata about the role instance
            instances.AppendFormat("<p>Role instance fault domain: {0}</p>", roleInstance.FaultDomain);
            instances.AppendFormat("<p>Role for the instance: {0}</p>", roleInstance.Role.Name);
            instances.AppendFormat("<p>Role instance update domain: {0}</p><br/>", roleInstance.UpdateDomain);

            // List the endpoints
            instances.AppendFormat("<h4>Role <em>{1}</em> instance <em>{0}</em> endpoints</h4><br/>", roleInstance.Id,
                roleInstance.Role.Name);

            foreach (RoleInstanceEndpoint instanceEndpoint in roleInstance.InstanceEndpoints.Values)
            {
                if (roleInstance.Role.Name == "Www")
                    instances.AppendFormat("<p><a href=\"{0}://{1}/Admin/Settings.aspx\" target=\"_blank\">{1}</a></p>", 
                        instanceEndpoint.Protocol, instanceEndpoint.IPEndpoint);
                else instances.AppendFormat("<p>Instance endpoint IP address, port, and protocol : {0} {1}</p>",
                    instanceEndpoint.IPEndpoint, instanceEndpoint.Protocol);
            }
        }
    }

    instances.AppendFormat("<small>* Current role instance is {0}</small>", RoleEnvironment.CurrentRoleInstance.Id);

    CloudHtml.Text = instances.ToString();
}

Output from running on a multiple role project where each role has multiple instances:

Role MultiThreadedWorkerRole

Role MultiThreadedWorkerRole has no role instances

Role Www

Role Www instance deployment22(203).CloudService1.Www_IN_1 *

Role instance fault domain: 1

Role for the instance: Www

Role instance update domain: 1

Role Www instance deployment22(203).CloudService1.Www_IN_1 endpoints

127.255.0.3:82

127.255.0.3:444

  • Current role instance is deployment22(203).CloudService1.Www_IN_1

If I re-run the above query I randomly get the output from the role instances deployment22(203).CloudService1.Www_IN_0 to deployment22(203).CloudService1.Www_IN_4 as decided by which instance that processes the ASP.NET web form that runs the above code snippet. Example:

Role MultiThreadedWorkerRole

Role MultiThreadedWorkerRole has no role instances

Role Www

Role Www instance deployment22(203).CloudService1.Www_IN_0 *

Role instance fault domain: 0

Role for the instance: Www

Role instance update domain: 0

Role Www instance deployment22(203).CloudService1.Www_IN_0 endpoints

127.255.0.2:82

127.255.0.2:444

  • Current role instance is deployment22(203).CloudService1.Www_IN_0

Solution

  • I believe the key is in the following note made on the documentation of the RoleEnvironment's Roles property -

    At least one internal endpoint must be defined for a role to enable instances to be known at runtime

    I've just tried it by creating a cloud project with two roles and iterating on the roles using RoleEnvironment.Roles, without any internal endpoints only the current role's instances were available.

    After adding an internal endpoint to the other role its instances were loaded for me. However - only the internal endpoints were listed in the role instance's InstanceEndpoints collection. I could not see the external endpoints they've exposed.