コード例 #1
0
        static void Main(string[] args)
        {
            var task = Task.Run(async() =>
            {
                var vcenterUrl      = "https://vcenter01.domain.local";
                var vcenterUser     = "******";
                var vcenterPassword = "";

                var client = new VsphereClient(vcenterUrl);
                await client.LoginAsync(vcenterUser, vcenterPassword);

                var enviormentConfig = new EnviormentConfig
                {
                    DatastoreName = "MyDatastoreName",
                    HostName      = "MyEsxHost",
                    NetworkName   = "NetworkName"
                };

                var virtualMachineConfig = new SimpleVirtualMachineConfig
                {
                    Name         = "MyUbuntuServer1",
                    GuestOs      = GuestOs.UBUNTU_64,
                    Cpus         = 2,
                    DiskSizeGB   = 40,
                    MemorySizeGB = 4,
                    IsoFile      = "[MyDatastoreName] ISO/ubuntu-20.04.1-live-server-amd64.iso",
                };

                await client.CreateVirtualMachineAsync(enviormentConfig, virtualMachineConfig);

                await client.LogoutAsync();
            });

            task.Wait();
        }
コード例 #2
0
        public async Task <CreateVirtualMachineResponse> CreateVirtualMachineAsync(
            EnviormentConfig enviormentConfig,
            SimpleVirtualMachineConfig simpleVirtualMachineConfig,
            CancellationToken cancellationToken = default)
        {
            var folder = await this.GetFolderAsync(cancellationToken);

            if (folder == null)
            {
                return(null);
            }

            var esxDatastore = await this.GetDatastoreAsync(enviormentConfig.DatastoreName, cancellationToken);

            var esxHost = await this.GetHostAsync(enviormentConfig.HostName, cancellationToken);

            var esxNetwork = await this.GetNetworkAsync(enviormentConfig.NetworkName, cancellationToken);

            var virtualMachineConfig = new VirtualMachineConfig
            {
                Spec = new Spec
                {
                    Name    = simpleVirtualMachineConfig.Name,
                    GuestOs = simpleVirtualMachineConfig.GuestOs,
                    Cpu     = new Cpu
                    {
                        Count          = simpleVirtualMachineConfig.Cpus,
                        CoresPerSocket = 1,
                        HotAddEnabled  = true
                    },
                    Disks = new Disk[]
                    {
                        new Disk
                        {
                            Scsi = new Scsi
                            {
                                Bus  = 0,
                                Unit = 0,
                            },
                            Type    = "SCSI",
                            NewVmdk = new NewVmdk
                            {
                                Capacity = simpleVirtualMachineConfig.DiskSizeGB * 1024L * 1024L * 1024L
                            }
                        }
                    },
                    Cdroms = new Cdrom[]
                    {
                        new Cdrom
                        {
                            StartConnected    = true,
                            AllowGuestControl = true,
                            Type = "SATA",
                            Sata = new Sata
                            {
                                Bus  = 0,
                                Unit = 0
                            },
                            Backing = new CdromBacking
                            {
                                Type    = "ISO_FILE",
                                IsoFile = simpleVirtualMachineConfig.IsoFile
                            }
                        }
                    },
                    Memory = new Memory
                    {
                        SizeMiB = simpleVirtualMachineConfig.MemorySizeGB * 1024
                    },
                    Nics = new Nic[]
                    {
                        new Nic
                        {
                            Type              = EthernetAdapterEmulationType.VMXNET3,
                            MacType           = MacAddressType.GENERATED,
                            StartConnected    = true,
                            AllowGuestControl = true,
                            Backing           = new NicBacking
                            {
                                Network = esxNetwork.Network,
                                Type    = esxNetwork.Type
                            }
                        }
                    },
                    Placement = new Placement
                    {
                        Datastore = esxDatastore,
                        Folder    = folder,
                        Host      = esxHost
                    }
                }
            };

            var json = JsonConvert.SerializeObject(virtualMachineConfig, this._jsonSerializerSettings);

            var responseMessage = await this._httpClient.PostAsync($"vcenter/vm", new StringContent(json, Encoding.UTF8, "application/json"), cancellationToken);

            json = await responseMessage.Content.ReadAsStringAsync();

            if (!responseMessage.IsSuccessStatusCode)
            {
                Console.WriteLine(json);
                return(null);
            }

            var item = JsonConvert.DeserializeObject <CreateVirtualMachineResponse>(json, this._jsonSerializerSettings);

            return(item);
        }