コード例 #1
0
        private IVirtualMachineCustomImage PrepareCustomImage(string rgName, Region region, IAzure azure)
        {
            string vmName   = TestUtilities.GenerateName("muldvm");
            string uname    = "javauser";
            string password = "******";
            KnownLinuxVirtualMachineImage linuxImage = KnownLinuxVirtualMachineImage.UbuntuServer16_04_Lts;
            string          publicIpDnsLabel         = TestUtilities.GenerateName("pip");
            string          storageName = TestUtilities.GenerateName("stg");
            IVirtualMachine linuxVM     = azure.VirtualMachines
                                          .Define(vmName)
                                          .WithRegion(region)
                                          .WithExistingResourceGroup(rgName)
                                          .WithNewPrimaryNetwork("10.0.0.0/28")
                                          .WithPrimaryPrivateIPAddressDynamic()
                                          .WithNewPrimaryPublicIPAddress(publicIpDnsLabel)
                                          .WithPopularLinuxImage(linuxImage)
                                          .WithRootUsername(uname)
                                          .WithRootPassword(password)
                                          .WithUnmanagedDisks()
                                          .DefineUnmanagedDataDisk("disk-1")
                                          .WithNewVhd(30)
                                          .WithCaching(CachingTypes.ReadWrite)
                                          .Attach()
                                          .DefineUnmanagedDataDisk("disk-2")
                                          .WithNewVhd(60)
                                          .WithCaching(CachingTypes.ReadOnly)
                                          .Attach()
                                          .WithSize(VirtualMachineSizeTypes.Parse("Standard_D2a_v4"))
                                          .WithNewStorageAccount(storageName)
                                          .WithOSDiskCaching(CachingTypes.ReadWrite)
                                          .Create();

            //
            TestHelper.DeprovisionAgentInLinuxVM(linuxVM.GetPrimaryPublicIPAddress().Fqdn, 22, uname, password);
            //
            linuxVM.Deallocate();
            linuxVM.Generalize();
            //
            string vhdBasedImageName = TestUtilities.GenerateName("img");
            //
            var creatableDisk = azure
                                .VirtualMachineCustomImages
                                .Define(vhdBasedImageName)
                                .WithRegion(region)
                                .WithExistingResourceGroup(rgName)
                                .WithLinuxFromVhd(linuxVM.OSUnmanagedDiskVhdUri, OperatingSystemStateTypes.Generalized)
                                .WithOSDiskCaching(linuxVM.OSDiskCachingType);

            foreach (IVirtualMachineUnmanagedDataDisk disk in linuxVM.UnmanagedDataDisks.Values)
            {
                creatableDisk.DefineDataDiskImage()
                .WithLun(disk.Lun)
                .FromVhd(disk.VhdUri)
                .WithDiskCaching(disk.CachingType)
                .WithDiskSizeInGB(disk.Size + 10)         // Resize each data disk image by +10GB
                .Attach();
            }
            //
            IVirtualMachineCustomImage customImage = creatableDisk.Create();

            return(customImage);
        }