internal static SimulatorClusterContainer GetClusterInternal(string clusterName)
 {
     SimulatorClusterContainer simulationCluster =
         Clusters.First(cluster => string.Equals(cluster.Cluster.Name, clusterName, StringComparison.OrdinalIgnoreCase));
     simulationCluster.Cluster.SubscriptionId = IntegrationTestBase.TestCredentials.SubscriptionId;
     return simulationCluster;
 }
        public async Task<IHttpResponseMessageAbstraction> CreateContainer(string dnsName, string location, string clusterPayload, int schemaVersion = 2)
        {
            this.LogMessage("Creating cluster '{0}' in location {1}", dnsName, location);
            this.ValidateConnection();

            var registrationClient = ServiceLocator.Instance.Locate<ISubscriptionRegistrationClientFactory>().Create(this.credentials, this.context, false);
            if (!await registrationClient.ValidateSubscriptionLocation(location))
            {
                var resolver = ServiceLocator.Instance.Locate<ICloudServiceNameResolver>();
                string regionCloudServicename = resolver.GetCloudServiceName(
                    this.credentials.SubscriptionId, this.credentials.DeploymentNamespace, location);

                throw new HttpLayerException(
                    HttpStatusCode.NotFound, string.Format("The cloud service with name {0} was not found.", regionCloudServicename));
            }

            lock (Clusters)
            {
                var existingCluster = Clusters.FirstOrDefault(c => c.Cluster.Name == dnsName && c.Cluster.Location == location);
                if (existingCluster != null)
                    throw new HttpLayerException(HttpStatusCode.BadRequest, "<!DOCTYPE html><html>" + HDInsightClient.ClusterAlreadyExistsError + "</html>");

                HDInsight.ClusterCreateParametersV2 cluster = null;
                AzureHDInsightClusterConfiguration azureClusterConfig = null;
                ClusterDetails createCluster = null;
                if (schemaVersion == 2)
                {
                    var resource = ServerSerializer.DeserializeClusterCreateRequestIntoResource(clusterPayload);
                    if (resource.SchemaVersion != "2.0")
                    {
                        throw new HttpLayerException(HttpStatusCode.BadRequest, "SchemaVersion needs to be at 2.0");
                    }
                    cluster = ServerSerializer.DeserializeClusterCreateRequest(clusterPayload);
                    var storageAccounts = GetStorageAccounts(cluster).ToList();
                    createCluster = new ClusterDetails(cluster.Name, HDInsight.ClusterState.ReadyForDeployment.ToString())
                    {
                        ConnectionUrl = string.Format(@"https://{0}.azurehdinsight.net", cluster.Name),
                        Location = cluster.Location,
                        Error = this.ValidateClusterCreation(cluster),
                        HttpUserName = cluster.UserName,
                        HttpPassword = cluster.Password,
                        Version = this.GetVersion(cluster.Version),
                        ClusterSizeInNodes = cluster.ClusterSizeInNodes,
                        DefaultStorageAccount = storageAccounts.FirstOrDefault(),
                        AdditionalStorageAccounts = storageAccounts.Skip(1),
                        ClusterType = cluster.ClusterType
                    };
                    var clusterCreateDetails = ServerSerializer.DeserializeClusterCreateRequestToInternal(clusterPayload);
                    azureClusterConfig = GetAzureHDInsightClusterConfiguration(createCluster, clusterCreateDetails);
                }
                else if (schemaVersion == 3)
                {
                    var resource = ServerSerializer.DeserializeClusterCreateRequestIntoResource(clusterPayload);
                    if (resource.SchemaVersion != "3.0")
                    {
                        throw new HttpLayerException(HttpStatusCode.BadRequest, "SchemaVersion needs to be at 3.0");
                    }

                    cluster = ServerSerializer.DeserializeClusterCreateRequestV3(clusterPayload);
                    var storageAccounts = GetStorageAccounts(cluster).ToList();
                    createCluster = new ClusterDetails(cluster.Name, HDInsight.ClusterState.ReadyForDeployment.ToString())
                    {
                        ConnectionUrl = string.Format(@"https://{0}.azurehdinsight.net", cluster.Name),
                        Location = cluster.Location,
                        Error = this.ValidateClusterCreation(cluster),
                        HttpUserName = cluster.UserName,
                        HttpPassword = cluster.Password,
                        Version = this.GetVersion(cluster.Version),
                        ClusterSizeInNodes = cluster.ClusterSizeInNodes,
                        DefaultStorageAccount = storageAccounts.FirstOrDefault(),
                        AdditionalStorageAccounts = storageAccounts.Skip(1),
                        ClusterType = cluster.ClusterType
                    };
                    var clusterCreateDetails = ServerSerializer.DeserializeClusterCreateRequestToInternalV3(clusterPayload);
                    azureClusterConfig = GetAzureHDInsightClusterConfigurationV3(createCluster, clusterCreateDetails);
                }
                else
                {
                    throw new HttpLayerException(HttpStatusCode.BadRequest, "Invalid SchemaVersion");
                }

                var simCluster = new SimulatorClusterContainer() { Cluster = createCluster, Configuration = azureClusterConfig };
                Clusters.Add(simCluster);
                if (createCluster.Name.Contains("NetworkExceptionButCreateSucceeds"))
                {
                    throw new HttpRequestException("An error occurred while sending the request.");
                }
                if (createCluster.Name.Contains("HttpErrorButCreateSucceeds"))
                {
                    return new HttpResponseMessageAbstraction(HttpStatusCode.BadGateway, null, "BadGateway");
                }
            }
            return new HttpResponseMessageAbstraction(HttpStatusCode.Accepted, null, string.Empty);
        }