Exemplo n.º 1
0
        public void Test_GetAllWebRoles_Should_Return_AlistOfWebRoles()
        {
            ComputeRoles roles = sutR.GetAllWebRoles();

            foreach (var role in testData._ComputeRoles._ComputeRoles)
            {
                var expectedServiceName  = role.ServiceName;
                var expectedInstanceName = role.InstanceName;

                Should.Equals(roles.Contains(expectedServiceName, expectedInstanceName), true);
            }
        }
        /// <summary>
        /// Returns a list of Compute Roles for all cloud services in a given subscription. The Compute Role list will include Web and Worker Roles.
        /// </summary>
        /// <returns>A list of Compute Roles</returns>
        public ComputeRoles GetAllWebRoles()
        {
            ComputeManagementClient client = new ComputeManagementClient(MyCloudCredentials);
            ComputeRoles            roles  = null;

            try
            {
                roles = new ComputeRoles(new List <ComputeRole>());
                var hostedServices = client.HostedServices.List();
                foreach (var service in hostedServices)
                {
                    var deployments = client.Deployments.GetBySlot(service.ServiceName, DeploymentSlot.Production);
                    if (deployments != null)
                    {
                        var osVersion = string.Empty;
                        var _roles    = deployments.Roles;
                        if (_roles != null && _roles.Count > 0)
                        {
                            foreach (var role in _roles)
                            {
                                osVersion = role.OSVersion;
                            }
                        }

                        var instances = deployments.RoleInstances;
                        if (instances.Count > 0)
                        {
                            ComputeRole role = null;
                            foreach (RoleInstance instance in instances)
                            {
                                var rate = Configuration.GetAzureRates().GetMyRate(instance.InstanceSize);
                                role = new ComputeRole(service.ServiceName, instance.InstanceName, instance.RoleName, instance.InstanceSize, instance.InstanceStatus, osVersion, rate);
                                roles.Add(role);
                            }
                        }
                    }
                }
            }
            catch (CloudException ce)
            {
                Logger.Warn(ce, String.Format("Exception during retrieval of Web Roles Exception: {0}", ce));
            }
            return(roles);
        }