public static ClusterDetails CreateClusterDetailsFromGetClustersResult(Cluster clusterDetailsFromServer)
        {
            if (clusterDetailsFromServer == null)
            {
                throw new ArgumentNullException("clusterDetailsFromServer");
            }

            ClusterDetails clusterDetails = new ClusterDetails();
            clusterDetails.CreatedDate = clusterDetailsFromServer.CreatedTime;
            clusterDetails.Location = clusterDetailsFromServer.Location;
            clusterDetails.Name = clusterDetailsFromServer.DnsName;
            clusterDetails.Version = clusterDetailsFromServer.Version;
            clusterDetails.StateString = clusterDetailsFromServer.State.ToString();
            clusterDetails.DeploymentId = clusterDetailsFromServer.DeploymentId ?? string.Empty;

            if (!string.IsNullOrEmpty(clusterDetails.Version))
            {
                clusterDetails.VersionNumber = new PayloadConverter().ConvertStringToVersion(clusterDetails.Version);
                clusterDetails.VersionStatus = VersionFinderClient.GetVersionStatus(clusterDetails.Version);
            }
            else
            {
                clusterDetails.VersionNumber = new Version(0, 0);
            }

            string componentListCommaSeperated = clusterDetailsFromServer.Components != null
                                                     ? string.Join(",", clusterDetailsFromServer.Components.Select(c => c.GetType().Name))
                                                     : string.Empty;

            clusterDetails.ClusterType = !string.IsNullOrEmpty(componentListCommaSeperated) 
                                        ? GetClusterTypeFromComponentList(componentListCommaSeperated) 
                                        : ClusterType.Unknown;

            // This code will only execute for PaaS clusters which only support Windows
            clusterDetails.OSType = OSType.Windows;

            if (clusterDetailsFromServer.Error != null)
            {
                //Populate error details with the most recent one. These occur if the deployment workflow errors out
                clusterDetails.Error = new ClusterErrorStatus(
                    (int)clusterDetailsFromServer.Error.StatusCode, clusterDetailsFromServer.Error.ErrorMessage ?? string.Empty, string.Empty);
            }

            //Populate Uri and HttpCreds from the gateway. This should not throw
            //even if the gateway component is null
            PopulateClusterUriAndHttpCredsFromGateway(clusterDetails, clusterDetailsFromServer.Components.OfType<GatewayComponent>().SingleOrDefault());

            //Look for Yarn for 3X clusters
            var yarn = clusterDetailsFromServer.Components.OfType<YarnComponent>().SingleOrDefault();
            //Look for MR for 2X clusters
            var mr = clusterDetailsFromServer.Components.OfType<MapReduceComponent>().SingleOrDefault();

            if (yarn != null)
            {
                clusterDetails.ClusterSizeInNodes = yarn.NodeManagerRole.InstanceCount;
                var mapReduceApplication = yarn.Applications.OfType<MapReduceApplication>().SingleOrDefault();
                if (mapReduceApplication != null)
                {
                    //ToWasbConfiguration returns null if DefaultStorageAccountAndContainer is null
                    clusterDetails.DefaultStorageAccount = mapReduceApplication.DefaultStorageAccountAndContainer.ToWabStorageAccountConfiguration();
                    if (mapReduceApplication.AdditionalStorageContainers != null)
                    {
                        clusterDetails.AdditionalStorageAccounts =
                            mapReduceApplication.AdditionalStorageContainers.Select(s => s.ToWabStorageAccountConfiguration()).ToList();
                    }
                }
            }
            else if (mr != null)
            {
                clusterDetails.ClusterSizeInNodes = mr.WorkerNodeRole.InstanceCount;
                clusterDetails.DefaultStorageAccount = mr.DefaultStorageAccountAndContainer.ToWabStorageAccountConfiguration();
                if (mr.AdditionalStorageAccounts != null)
                {
                    clusterDetails.AdditionalStorageAccounts = mr.AdditionalStorageAccounts.Select(s => s.ToWabStorageAccountConfiguration()).ToList();
                }
            }

            //populate RDP user name. All roles will have the same RDP properties so we pick the first one
            if (clusterDetailsFromServer.ClusterRoleCollection != null &&
                clusterDetailsFromServer.ClusterRoleCollection.Any() &&
                clusterDetailsFromServer.ClusterRoleCollection.First().RemoteDesktopSettings.IsEnabled)
            {
                clusterDetails.RdpUserName =
                    clusterDetailsFromServer.ClusterRoleCollection.First().RemoteDesktopSettings.AuthenticationCredential.Username;
            }

            //populate virtual network info
            VirtualNetworkConfiguration vnetConfigFromServer = clusterDetailsFromServer.VirtualNetworkConfiguration;
            if (vnetConfigFromServer != null && !string.IsNullOrEmpty(vnetConfigFromServer.VirtualNetworkSite))
            {
                clusterDetails.VirtualNetworkId = vnetConfigFromServer.VirtualNetworkSite;
                //Populate the subnet name
                if (vnetConfigFromServer.AddressAssignments != null 
                    && vnetConfigFromServer.AddressAssignments.Any()
                    && vnetConfigFromServer.AddressAssignments.First().Subnets != null
                    && vnetConfigFromServer.AddressAssignments.First().Subnets.First() != null)
                {
                    if (vnetConfigFromServer.AddressAssignments.Any())
                    {
                        clusterDetails.SubnetName = vnetConfigFromServer.AddressAssignments.First().Subnets.First().Name;
                    }
                }
            }
           
            return clusterDetails;
        }
 private string GetClusterComponents(Cluster cluster)
 {
     return string.Join(",", cluster.Components.Select(c => c.GetType().Name));
 }
        public async Task<HttpResponseMessage> CreateCluster(string subscriptionId, string cloudServiceName, string resourceNamespace, string dnsName)
        {
            var requestMessage = this.Request;
            var rdfeResource = await requestMessage.Content.ReadAsAsync<RDFEResource>();
            XmlNode node = rdfeResource.IntrinsicSettings[0];

            MemoryStream stm = new MemoryStream();
            StreamWriter stw = new StreamWriter(stm);
            stw.Write(node.OuterXml);
            stw.Flush();
            stm.Position = 0;
            DataContractSerializer ser = new DataContractSerializer(typeof(ClusterCreateParameters));
            ClusterCreateParameters clusterCreateParams = (ClusterCreateParameters)ser.ReadObject(stm);

            // Spark cluster creation in introduced after schema version 3.0
            if (clusterCreateParams.Components.Any(c => c.GetType() == typeof(SparkComponent)))
            {
                if (!requestMessage.Headers.GetValues("SchemaVersion").Any(v => v.Equals("3.0")))
                {
                    throw new NotSupportedException(ClustersTestConstants.NotSupportedBySubscriptionException);
                }
            }

            var testCluster = new Cluster
            {
                ClusterRoleCollection = clusterCreateParams.ClusterRoleCollection,
                CreatedTime = DateTime.UtcNow,
                Error = null,
                FullyQualifiedDnsName = clusterCreateParams.DnsName,
                State = ClusterState.Running,
                UpdatedTime = DateTime.UtcNow,
                DnsName = clusterCreateParams.DnsName,
                Components = clusterCreateParams.Components,
                ExtensionData = clusterCreateParams.ExtensionData,
                Location = clusterCreateParams.Location,
                Version = ClusterVersionUtils.TryGetVersionNumber(clusterCreateParams.Version),
                VirtualNetworkConfiguration = clusterCreateParams.VirtualNetworkConfiguration
            };

            List<Cluster> clusters;
            bool subExists = _clustersAvailable.TryGetValue(subscriptionId, out clusters);
            if (subExists)
            {
                clusters.Add(testCluster);
                _clustersAvailable[subscriptionId] = clusters;
            }
            else
            {
                _clustersAvailable.Add(
                    new KeyValuePair<string, List<Cluster>>(subscriptionId, new List<Cluster> { testCluster }));
            }

            return this.Request.CreateResponse(HttpStatusCode.Created);
        }
        private OutputItemList GetOutputItems(Cluster cluster)
        {
            var oi = new OutputItemList();

            var version = new OutputItem();
            version.Key = "Version";
            version.Value = cluster.Version;
            oi.Add(version);

            var components = new OutputItem();
            components.Key = "ClusterComponents";
            components.Value = this.GetClusterComponents(cluster);
            oi.Add(components);

            return oi;
        }
 private static Cluster CreateClusterFromCreateParameters(HDInsight.ClusterCreateParametersV2 clusterCreateParameters)
 {
     var clusterCreateParams = HDInsightClusterRequestGenerator.Create3XClusterFromMapReduceTemplate(clusterCreateParameters);
     var cluster = new Cluster
     {
         ClusterRoleCollection = clusterCreateParams.ClusterRoleCollection,
         CreatedTime = DateTime.UtcNow,
         Error = null,
         FullyQualifiedDnsName = clusterCreateParams.DnsName,
         State = ClusterState.Running,
         UpdatedTime = DateTime.UtcNow,
         DnsName = clusterCreateParams.DnsName,
         Components = clusterCreateParams.Components,
         ExtensionData = clusterCreateParams.ExtensionData,
         Location = clusterCreateParams.Location,
         Version = clusterCreateParams.Version,
         VirtualNetworkConfiguration = clusterCreateParams.VirtualNetworkConfiguration
     };
     return cluster;
 }