public void TestVMScenarioWithPlan()
        {
            using (var context = UndoContext.Current)
            {
                context.Start();
                EnsureClientsInitialized();

                string imgRefId = GetPlatformOSImage(useWindowsImage: true);
                var rgName = TestUtilities.GenerateName(TestPrefix);
                string storageAccountName = TestUtilities.GenerateName(TestPrefix);
                string asName = TestUtilities.GenerateName("as");
                VirtualMachine inputVM;
                try
                {
                    var storageAccountOutput = CreateStorageAccount(rgName, storageAccountName);

                    var imageRef = new ImageReference 
                            { 
                               Publisher = "datastax",
                               Offer = "datastax-enterprise-non-production-use-only",
                               Sku = "sandbox_single-node",
                               Version = "latest",
                            };

                    var plan = new Microsoft.Azure.Management.Compute.Models.Plan
                    {
                        Publisher = imageRef.Publisher,
                        Product = imageRef.Offer,
                        Name = imageRef.Sku,
                    };
                    var vm1 = CreateVM(rgName, asName, storageAccountOutput, imgRefId, out inputVM, (vm) =>
                        {
                            vm.StorageProfile.SourceImage = null;
                            vm.StorageProfile.ImageReference = imageRef;
                            vm.Plan = plan; 
                        }                        
                        );

                    var getVMResponse = m_CrpClient.VirtualMachines.Get(rgName, inputVM.Name);
                    Assert.True(getVMResponse.StatusCode == HttpStatusCode.OK);
                    ValidateVM(inputVM, getVMResponse.VirtualMachine, Helpers.GetVMReferenceId(m_subId, rgName, inputVM.Name));

                    var listResponse = m_CrpClient.VirtualMachines.List(rgName);
                    Assert.True(listResponse.StatusCode == HttpStatusCode.OK);
                    ValidateVM(inputVM, listResponse.VirtualMachines.FirstOrDefault(x => x.Name == inputVM.Name),
                        Helpers.GetVMReferenceId(m_subId, rgName, inputVM.Name));

                }
                finally
                {
                    var deleteResourceGroupResponse = m_ResourcesClient.ResourceGroups.Delete(rgName);
                    Assert.True(deleteResourceGroupResponse.StatusCode == HttpStatusCode.OK);
                }
            }
        }
        protected VirtualMachine CreateDefaultVMInput(string rgName, string storageAccountName, ImageReference imageRef, string asetId, string nicId)
        {
            // Generate Container name to hold disk VHds
            string containerName = TestUtilities.GenerateName(TestPrefix);
            var vhdContainer = string.Format(Constants.StorageAccountBlobUriTemplate, storageAccountName) + containerName;
            var vhduri = vhdContainer + string.Format("/{0}.vhd", TestUtilities.GenerateName(TestPrefix));
            var osVhduri = vhdContainer + string.Format("/os{0}.vhd", TestUtilities.GenerateName(TestPrefix));

            return new VirtualMachine
            {
                Name = TestUtilities.GenerateName("vm"),
                Location = m_location,
                Tags = new Dictionary<string, string>() { { "RG", "rg" }, { "testTag", "1" } },
                Type = "Microsoft.Compute/virtualMachines",
                AvailabilitySetReference = new AvailabilitySetReference { ReferenceUri = asetId },
                HardwareProfile = new HardwareProfile
                {
                    VirtualMachineSize = VirtualMachineSizeTypes.StandardA0
                },
                StorageProfile = new StorageProfile
                {
                    ImageReference = imageRef,
                    OSDisk = new OSDisk
                    {
                        Caching = CachingTypes.None,
                        CreateOption = DiskCreateOptionTypes.FromImage,
                        Name = "test",
                        VirtualHardDisk = new VirtualHardDisk
                        {
                            Uri = osVhduri
                        }
                    },
                    DataDisks = null,
                },
                NetworkProfile = new NetworkProfile
                {
                    NetworkInterfaces = new List<NetworkInterfaceReference>
                        {
                            new NetworkInterfaceReference
                            {
                                ReferenceUri = nicId
                            }
                        }
                },
                OSProfile = new OSProfile
                {
                    AdminUsername = "******",
                    AdminPassword = "******" + rgName,
                    ComputerName = "test"
                }
            };
        }
        protected ImageReference GetPlatformVMImage(bool useWindowsImage)
        {
            if (useWindowsImage)
            {
                if (m_windowsImageReference == null)
                {
                    Trace.TraceInformation("Querying available Windows Server image from PIR...");
                    m_windowsImageReference = FindVMImage("MicrosoftWindowsServer", "WindowsServer", "2012-R2-Datacenter");
                }
                return m_windowsImageReference;
            }

            if (m_linuxImageReference == null)
            {
                Trace.TraceInformation("Querying available Ubuntu image from PIR...");
                // If this sku disappears, query latest with 
                // GET https://management.azure.com/subscriptions/<subId>/providers/Microsoft.Compute/locations/SoutheastAsia/publishers/Canonical/artifacttypes/vmimage/offers/UbuntuServer/skus?api-version=2015-06-15
                m_linuxImageReference = FindVMImage("Canonical", "UbuntuServer", "15.04");
            }
            return m_linuxImageReference;
        }
        protected VirtualMachine CreateVM_NoAsyncTracking(
            string rgName, string asName, StorageAccount storageAccount, ImageReference imageRef, 
            out VirtualMachine inputVM,
            Action<VirtualMachine> vmCustomizer = null,
            bool createWithPublicIpAddress = false)
        {
            try
            {
                // Create the resource Group, it might have been already created during StorageAccount creation.
                var resourceGroup = m_ResourcesClient.ResourceGroups.CreateOrUpdate(
                    rgName,
                    new ResourceGroup
                    {
                        Location = m_location
                    });

                PublicIpAddressGetResponse getPublicIpAddressResponse = createWithPublicIpAddress ? null : CreatePublicIP(rgName);
                
                SubnetGetResponse subnetResponse = CreateVNET(rgName);

                NetworkInterfaceGetResponse nicResponse = CreateNIC(
                    rgName, 
                    subnetResponse.Subnet, 
                    getPublicIpAddressResponse != null ? getPublicIpAddressResponse.PublicIpAddress : null);

                string asetId = CreateAvailabilitySet(rgName, asName);

                inputVM = CreateDefaultVMInput(rgName, storageAccount.Name, imageRef, asetId, nicResponse.NetworkInterface.Id);
                if (vmCustomizer != null)
                {
                    vmCustomizer(inputVM);
                }

                string expectedVMReferenceId = Helpers.GetVMReferenceId(m_subId, rgName, inputVM.Name);
                var createOrUpdateResponse = m_CrpClient.VirtualMachines.BeginCreatingOrUpdating(
                    rgName, inputVM);

                Assert.True(createOrUpdateResponse.StatusCode == HttpStatusCode.Created);

                Assert.True(createOrUpdateResponse.VirtualMachine.Name == inputVM.Name);
                Assert.True(createOrUpdateResponse.VirtualMachine.Location == inputVM.Location.ToLower().Replace(" ", "") ||
                            createOrUpdateResponse.VirtualMachine.Location.ToLower() == inputVM.Location.ToLower());

                Assert.True(
                    createOrUpdateResponse.VirtualMachine.AvailabilitySetReference.ReferenceUri
                        .ToLowerInvariant() == asetId.ToLowerInvariant());
                ValidateVM(inputVM, createOrUpdateResponse.VirtualMachine, expectedVMReferenceId);

                var operationUri = new Uri(createOrUpdateResponse.AzureAsyncOperation);
                string operationId = operationUri.Segments.LastOrDefault();
                var lroResponse =
                    m_CrpClient.GetLongRunningOperationStatus(createOrUpdateResponse.AzureAsyncOperation.ToString());
                ValidateLROResponse(lroResponse, operationId);

                // CONSIDER dropping this Get and ValidateVM call. Nothing changes in the VM model after it's accepted.
                // There might have been intent to track the async operation to completion and then check the VM is
                // still this and okay, but that's not what the code above does and still doesn't make much sense.
                var getResponse = m_CrpClient.VirtualMachines.Get(rgName, inputVM.Name);
                Assert.True(getResponse.StatusCode == HttpStatusCode.OK);
                ValidateVM(inputVM, getResponse.VirtualMachine, expectedVMReferenceId);
                return getResponse.VirtualMachine;
            }
            catch
            {
                var deleteRg1Response = m_ResourcesClient.ResourceGroups.Delete(rgName);
                Assert.True(deleteRg1Response.StatusCode == HttpStatusCode.OK);
                throw;
            }
        }