private async Task <VirtualMachine> CreateCRPImage(string rgName, string imageName)
        {
            string         storageAccountName = Recording.GenerateAssetName("saforgallery");
            string         asName             = Recording.GenerateAssetName("asforgallery");
            ImageReference imageRef           = await GetPlatformVMImage(useWindowsImage : true);

            StorageAccount storageAccountOutput = await CreateStorageAccount(rgName, storageAccountName); // resource group is also created in this method.

            VirtualMachine inputVM     = null;
            var            returnTwoVM = await CreateVM(rgName, asName, storageAccountOutput, imageRef);

            VirtualMachine createdVM = returnTwoVM.Item1;

            inputVM = returnTwoVM.Item2;
            Image imageInput = new Image(m_location)
            {
                Tags =
                {
                    { "RG",      "rg" },
                    { "testTag", "1"  },
                },
                StorageProfile = new ImageStorageProfile()
                {
                    OsDisk = new ImageOSDisk(OperatingSystemTypes.Windows, OperatingSystemStateTypes.Generalized)
                    {
                        BlobUri = createdVM.StorageProfile.OsDisk.Vhd.Uri,
                    },
                    ZoneResilient = true
                },
                HyperVGeneration = HyperVGenerationTypes.V1
            };

            await WaitForCompletionAsync(await ImagesOperations.StartCreateOrUpdateAsync(rgName, imageName, imageInput));

            Image getImage = await ImagesOperations.GetAsync(rgName, imageName);

            sourceImageId = getImage.Id;
            return(createdVM);
        }
        private async Task CreateImageTestHelper(string originalTestLocation, string diskEncryptionSetId)
        {
            // Create resource group
            var rgName    = Recording.GenerateAssetName(TestPrefix);
            var imageName = Recording.GenerateAssetName("imageTest");
            // Create a VM, so we can use its OS disk for creating the image
            string         storageAccountName = Recording.GenerateAssetName(TestPrefix);
            string         asName             = Recording.GenerateAssetName("as");
            ImageReference imageRef           = await GetPlatformVMImage(useWindowsImage : true);

            // Create Storage Account
            var storageAccountOutput = await CreateStorageAccount(rgName, storageAccountName);

            // Add data disk to the VM.
            Action <VirtualMachine> addDataDiskToVM = vm =>
            {
                string containerName = Recording.GenerateAssetName("testimageoperations", TestPrefix);
                var    vhdContainer  = "https://" + storageAccountName + ".blob.core.windows.net/" + containerName;
                var    vhduri        = vhdContainer + string.Format("/{0}.vhd", Recording.GenerateAssetName("testimageoperations", TestPrefix));

                vm.HardwareProfile.VmSize = VirtualMachineSizeTypes.StandardA4;
                foreach (int index in new int[] { 1, 2 })
                {
                    var diskName = "dataDisk" + index;
                    var ddUri    = vhdContainer + string.Format("/{0}{1}.vhd", diskName, Recording.GenerateAssetName("testimageoperations", TestPrefix));
                    var dd       = new DataDisk(1 + index, DiskCreateOptionTypes.Empty)
                    {
                        Caching    = CachingTypes.None,
                        Image      = null,
                        DiskSizeGB = 10,
                        Name       = diskName,
                        Vhd        = new VirtualHardDisk
                        {
                            Uri = ddUri
                        }
                    };
                    vm.StorageProfile.DataDisks.Add(dd);
                }
                var testStatus = new InstanceViewStatus
                {
                    Code    = "test",
                    Message = "test"
                };
                var testStatusList = new List <InstanceViewStatus> {
                    testStatus
                };
            };
            // Create the VM, whose OS disk will be used in creating the image
            var returnTwoVM = await CreateVM(rgName, asName, storageAccountOutput, imageRef, addDataDiskToVM);

            var createdVM = returnTwoVM.Item1;
            int expectedDiskLunWithDiskEncryptionSet = createdVM.StorageProfile.DataDisks[0].Lun;
            // Create the Image
            var imageInput = new Image(m_location)
            {
                Tags =
                {
                    { "RG",      "rg" },
                    { "testTag", "1"  },
                },
                StorageProfile = new ImageStorageProfile()
                {
                    OsDisk = new ImageOSDisk(OperatingSystemTypes.Windows, OperatingSystemStateTypes.Generalized)
                    {
                        BlobUri           = createdVM.StorageProfile.OsDisk.Vhd.Uri,
                        DiskEncryptionSet = diskEncryptionSetId == null ? null : new DiskEncryptionSetParameters()
                        {
                            Id = diskEncryptionSetId
                        },
                    },
                    DataDisks =
                    {
                        new ImageDataDisk(expectedDiskLunWithDiskEncryptionSet)
                        {
                            BlobUri           = createdVM.StorageProfile.DataDisks[0].Vhd.Uri,
                            DiskEncryptionSet = diskEncryptionSetId == null ? null: new DiskEncryptionSetParameters()
                            {
                                Id = diskEncryptionSetId
                            },
                        }
                    }
                },
                HyperVGeneration = HyperVGenerationTypes.V1
            };
            var image = await WaitForCompletionAsync(await ImagesOperations.StartCreateOrUpdateAsync(rgName, imageName, imageInput));

            var getImage = (await ImagesOperations.GetAsync(rgName, imageName)).Value;

            ValidateImage(imageInput, getImage);
            if (diskEncryptionSetId != null)
            {
                Assert.True(getImage.StorageProfile.OsDisk.DiskEncryptionSet != null, "OsDisk.DiskEncryptionSet is null");
                Assert.True(string.Equals(diskEncryptionSetId, getImage.StorageProfile.OsDisk.DiskEncryptionSet.Id, StringComparison.OrdinalIgnoreCase),
                            "getImage.StorageProfile.OsDisk.DiskEncryptionSet is not matching with expected DiskEncryptionSet resource");
                Assert.AreEqual(1, getImage.StorageProfile.DataDisks.Count);
                Assert.True(getImage.StorageProfile.DataDisks[0].DiskEncryptionSet != null, ".DataDisks.DiskEncryptionSet is null");
                Assert.True(string.Equals(diskEncryptionSetId, getImage.StorageProfile.DataDisks[0].DiskEncryptionSet.Id, StringComparison.OrdinalIgnoreCase),
                            "DataDisks.DiskEncryptionSet.Id is not matching with expected DiskEncryptionSet resource");
            }

            ImageUpdate updateParams = new ImageUpdate();

            updateParams.Tags.InitializeFrom(getImage.Tags);

            string tagKey = "UpdateTag";

            updateParams.Tags.Add(tagKey, "TagValue");
            await WaitForCompletionAsync(await ImagesOperations.StartUpdateAsync(rgName, imageName, updateParams));

            getImage = (await ImagesOperations.GetAsync(rgName, imageName)).Value;
            Assert.True(getImage.Tags.ContainsKey(tagKey));
            var listResponse = await(ImagesOperations.ListByResourceGroupAsync(rgName)).ToEnumerableAsync();

            Assert.IsTrue(listResponse.Count() == 1);
            await WaitForCompletionAsync(await ImagesOperations.StartDeleteAsync(rgName, image.Value.Name));
        }