public IList<DirectoryEntry> GetVmAdStaleEntries(IList<RemoteAppVm> vmList, ActiveDirectoryConfig adConfig, PSCredential credential)
        {
            
            Dictionary<string, string> vmPrefixes = new Dictionary<string, string>();
            List<DirectoryEntry> staleVmEntries = null;

            foreach (RemoteAppVm vm in vmList)
            {
                string vmNamePrefix = vm.VirtualMachineName.Substring(0, 8);

                // for each VM group with the same 8-character prefix, find the "minimum" VM name.
                // i.g, for VMs "abcdefgh0004", "abcdefgh0002", "abcdefgh0005", "abcdefgh0003" the minimum will be "abcdefgh0002"
                // this will be used later to determine which AD entries are stale
                // because each staleVMName < MIN(existingVMName) using regular case-insensitive alphanumerical comparison
                if (vmPrefixes.ContainsKey(vmNamePrefix))
                {
                    if (String.Compare(vm.VirtualMachineName, vmPrefixes[vmNamePrefix], StringComparison.OrdinalIgnoreCase) < 0)
                    {
                        vmPrefixes[vmNamePrefix] = vm.VirtualMachineName;
                    }
                }
                else
                {
                    vmPrefixes.Add(vmNamePrefix, vm.VirtualMachineName);
                }
            }

            staleVmEntries = new List<DirectoryEntry>();

            foreach (string vmNamePrefix in vmPrefixes.Keys)
            {
                IList<DirectoryEntry> adEntries = ActiveDirectoryHelper.GetVmAdEntries(
                    adConfig.DomainName,
                    adConfig.OrganizationalUnit,
                    vmNamePrefix,
                    credential);

                string maxName = vmPrefixes[vmNamePrefix];

                foreach (DirectoryEntry adEntry in adEntries)
                {
                    string name = ActiveDirectoryHelper.GetCN(adEntry);

                    if ((name.Length == AdHelper.VMNameLength) && (String.Compare(name, maxName, StringComparison.OrdinalIgnoreCase) < 0))
                    {
                        staleVmEntries.Add(adEntry);
                    }
                }
            }

            return staleVmEntries;
        }
        private void CanCreateNonPopulateOnlyAndDeleteRemoteAppService()
        {
            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();

                var client = GetRemoteAppManagementClient();

                string name = TestUtilities.GenerateName("ghut");
                string activeDirectoryName = TestUtilities.GenerateName("ghut");
                string billingPlanName = "Standard";
                string templateName = TestUtilities.GenerateName("ghut");    // BUGBUG this must be a real template
                HttpStatusCode[] statusSuccess = { HttpStatusCode.OK, HttpStatusCode.Accepted };

                ActiveDirectoryConfig adDetails = new ActiveDirectoryConfig()
                {
                    DomainName = activeDirectoryName,
                    UserName = "******",
                    Password = "******"
                };

                CollectionCreationDetails collectionDetails = new CollectionCreationDetails()
                {
                    Name = name,
                    AdInfo = adDetails,
                    PlanName = billingPlanName,
                    TemplateImageName = templateName,
                    Description = "OneSDK test created.",
                    Mode = CollectionMode.Apps,
                    ReadyForPublishing = true,
                    VNetName = "SomeVnet"
                };

                CollectionResult queriedCollection = null;

                OperationResultWithTrackingId result = client.Collections.Create(false, collectionDetails);
                Assert.NotNull(result);
                Assert.Contains(result.StatusCode, statusSuccess);

                if (result.StatusCode == HttpStatusCode.Accepted)
                {
                    //                    RemoteAppManagementClient.WaitForLongRunningOperation(result.TrackingId, timeoutMs, client);
                    Assert.True(result.StatusCode == HttpStatusCode.OK, "Failed to create collection.");
                }

                queriedCollection = client.Collections.Get(collectionDetails.Name);
                Assert.Equal(queriedCollection.Collection.AdInfo.DomainName, collectionDetails.AdInfo.DomainName);
                Assert.Equal(queriedCollection.Collection.PlanName, collectionDetails.PlanName);
                Assert.Equal(queriedCollection.Collection.TemplateImageName, collectionDetails.TemplateImageName);
                Assert.Equal(queriedCollection.Collection.Mode, collectionDetails.Mode);
                Assert.Equal(queriedCollection.Collection.VNetName, collectionDetails.VNetName);
                Assert.Equal(queriedCollection.Collection.Description, collectionDetails.Description);
                Assert.Equal(queriedCollection.Collection.ReadyForPublishing, collectionDetails.ReadyForPublishing);

                OperationResultWithTrackingId response = client.Collections.Delete(queriedCollection.Collection.Name);
                Assert.True(result.StatusCode == HttpStatusCode.OK, "Delete collection did not return OK.");
            }
        }
 private void ValidateAD(RemoteAppManagementClient client, ActiveDirectoryConfig adConfig)
 {
     AssertNotNullOrEmpty(adConfig.DomainName);
     AssertNotNullOrEmpty(adConfig.UserName);
 }
        private Collection CreateNewServiceWithPopulateOnlyTrue(RemoteAppManagementClient client)
        {
            string name = "hsut2861";
            string activeDirectoryName = "ghutad";
            string billingPlanName = "Standard";
            string templateName = "bluerefresh.2014.08.21.vhd"; //GetReadyTemplateImageName(client);

            ActiveDirectoryConfig adDetails = new ActiveDirectoryConfig()
            {
                DomainName = activeDirectoryName,
                UserName = "******",
                Password = "******"
            };

            CollectionCreationDetails collectionDetails = new CollectionCreationDetails()
            {
                Name = name,
                AdInfo = adDetails,
                PlanName = billingPlanName,
                TemplateImageName = templateName,
                Description = "OneSDK test created.",
                Mode = CollectionMode.Apps,
                ReadyForPublishing = true,
                VNetName = "SomeVnet"
            };

            OperationResultWithTrackingId result = null;

            Assert.DoesNotThrow(() =>
            {
                result = client.Collections.Create(true, collectionDetails);
            });

            Assert.NotNull(result);

            // if OK is returned then the tracking id is the name of the newly created collection
            Assert.Equal(HttpStatusCode.OK, result.StatusCode);

            Assert.NotNull(result.TrackingId);

            // now check if the object is actually created at the backend
            CollectionResult queriedService = client.Collections.Get(collectionDetails.Name);

            Assert.Equal(HttpStatusCode.OK, queriedService.StatusCode);
            Assert.Equal(queriedService.Collection.Name, name);
            Assert.Equal(queriedService.Collection.PlanName, collectionDetails.PlanName);
            Assert.Equal(queriedService.Collection.TemplateImageName, collectionDetails.TemplateImageName);
            Assert.Equal(queriedService.Collection.Status, "Creating");

            return queriedService.Collection;
        }