private static LokadCloudHostedService MapHostedService(HostedServiceInfo hostedService)
        {
            var lokadCloudDeployments = hostedService.Deployments.Select(deployment =>
                {
                    var workerRole = deployment.Roles.Single(r => r.RoleName == "Lokad.Cloud.WorkerRole");
                    var storageAccount = CloudStorageAccount.Parse(workerRole.Settings["DataConnectionString"]);
                    var credentials = storageAccount.Credentials;

                    return new LokadCloudDeployment
                        {
                            DeploymentName = deployment.DeploymentName,
                            DeploymentLabel = deployment.DeploymentLabel,
                            Status = deployment.Status.ToString(),
                            Slot = deployment.Slot.ToString(),
                            InstanceCount = workerRole.ActualInstanceCount,
                            IsRunning = deployment.Status == DeploymentStatus.Running,
                            IsTransitioning = deployment.Status != DeploymentStatus.Running && deployment.Status != DeploymentStatus.Suspended,
                            StorageAccount = storageAccount,
                            StorageAccountName = storageAccount.Credentials.AccountName,
                            StorageAccountKeyPrefix = credentials != null ? credentials.ExportBase64EncodedKey().Substring(0, 4) : null,
                        };
                }).ToList();

            return new LokadCloudHostedService
                {
                    ServiceName = hostedService.ServiceName,
                    ServiceLabel = hostedService.ServiceLabel,
                    Description = hostedService.Description,
                    Deployments = lokadCloudDeployments,
                    StorageAccount = lokadCloudDeployments[0].StorageAccount,
                    StorageAccountName = lokadCloudDeployments[0].StorageAccountName,
                    StorageAccountKeyPrefix = lokadCloudDeployments[0].StorageAccountKeyPrefix,
                };
        }
        Task<HostedServiceInfo> DoDiscoverHostedService(HttpClient client, string serviceName, CancellationToken cancellationToken)
        {
            return client.GetXmlAsync<HostedServiceInfo>(
                string.Format("services/hostedservices/{0}?embed-detail=true", serviceName),
                cancellationToken, _policies.RetryOnTransientErrors,
                (xml, tcs) =>
                {
                    var xmlService = xml.AzureElement("HostedService");
                    var xmlProperties = xmlService.AzureElement("HostedServiceProperties");

                    var info = new HostedServiceInfo
                    {
                        ServiceName = xmlService.AzureValue("ServiceName"),
                        Description = xmlProperties.AzureValue("Description"),
                        ServiceLabel = xmlProperties.AzureEncodedValue("Label"),

                        Deployments = xmlService.AzureElements("Deployments", "Deployment").Select(d =>
                        {
                            var config = d.AzureConfiguration();
                            var instanceCountPerRole = d.AzureElements("RoleInstanceList", "RoleInstance")
                                .GroupBy(ri => ri.AzureValue("RoleName"))
                                .ToDictionary(g => g.Key, g => g.Count());

                            return new DeploymentInfo
                            {
                                DeploymentName = d.AzureValue("Name"),
                                DeploymentLabel = d.AzureEncodedValue("Label"),
                                Slot = (DeploymentSlot)Enum.Parse(typeof(DeploymentSlot), d.AzureValue("DeploymentSlot")),
                                PrivateId = d.AzureValue("PrivateID"),
                                Status = (DeploymentStatus)Enum.Parse(typeof(DeploymentStatus), d.AzureValue("Status")),
                                Roles = d.AzureElements("RoleList", "Role").Select(r =>
                                {
                                    var roleName = r.AzureValue("RoleName");
                                    var roleConfig = config.ServiceConfigElements("ServiceConfiguration", "Role")
                                        .Single(role => role.AttributeValue("name") == roleName);

                                    int instanceCount;
                                    if (!instanceCountPerRole.TryGetValue(roleName, out instanceCount))
                                    {
                                        instanceCount = 0;
                                    }

                                    return new RoleInfo
                                    {
                                        RoleName = roleName,
                                        ActualInstanceCount = instanceCount,
                                        ConfiguredInstanceCount = Int32.Parse(roleConfig.ServiceConfigElement("Instances").AttributeValue("count")),
                                        Settings = roleConfig.ServiceConfigElements("ConfigurationSettings", "Setting").ToDictionary(
                                            x => x.AttributeValue("name"), x => x.AttributeValue("value"))
                                    };
                                }).ToList()
                            };
                        }).ToList()
                    };

                    tcs.TrySetResult(info);
                });
        }