创建空白虚拟机
简单创建
- Python
- Java
- Go
from cloudtower import (
ApiClient,
Configuration,
VmApi,
VmStatus,
VmFirmware,
Bus
)
from cloudtower.utils import wait_tasks
conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)
vm_api = VmApi(api_client)
with_task_vm = vm_api.create_vm([
{
"cluster_id": "cluster_id",
"name": "vm_name",
"ha": True,
"cpu_cores": 4,
"cpu_sockets": 4,
"memory": 4*1024*1024*1024,
"vcpu": 16,
"status": VmStatus.STOPPED,
"firmware": VmFirmware.BIOS,
"vm_nics": [
{
"connect_vlan_id": "vlan_id",
}
],
"vm_disks": {
"mount_cd_roms": [{
"boot": 0,
"index": 0
}],
}
}
])[0]
wait_tasks([with_task_vm.task_id], api_client)
created_vm = vm_api.get_vms({
"where": {
"id": with_task_vm.data.id
}
})
public class App {
public static void main(String[] args) throws ApiException {
ApiClient client = new ApiClient();
client.setBasePath("http://192.168.96.133/v2/api");
client.setApiKey("token");
VmCreationParams param = new VmCreationParams()
.clusterId("cl2k0mpoy026d0822xq6ctsim")
.name("vm_name_2")
.ha(true)
.cpuCores(4)
.cpuSockets(4)
.memory(4L * 1024 * 1024 * 1024)
.vcpu(16)
.status(VmStatus.STOPPED)
.firmware(VmFirmware.BIOS)
.addVmNicsItem(new VmNicParams().connectVlanId("cl2k0msiz02wc08220d6m3bz5"))
.vmDisks(new VmDiskParams().addMountCdRomsItem(new VmCdRomParams().boot(0).index(0)));
List<Vm> vms = createVm(client, param);
}
public static List<Vm> createVm(ApiClient client, VmCreationParams param)
throws ApiException {
VmApi vmApi = new VmApi(client);
List<VmCreationParams> params = new ArrayList<VmCreationParams>(1);
params.add(param);
List<WithTaskVm> withTaskVms = vmApi.createVm(params);
List<String> tasks = withTaskVms.stream().map(vms -> vms.getTaskId()).collect(Collectors.toList());
List<String> ids = withTaskVms.stream().map(vms -> vms.getData().getId()).collect(Collectors.toList());
TaskUtil.WaitTasks(tasks, client);
List<Vm> vms = vmApi
.getVms(
new GetVmsRequestBody()
.where(new VmWhereInput()
.idIn(ids)));
return vms;
}
}
package main
import (
"github.com/openlyinc/pointy"
apiclient "github.com/smartxworks/cloudtower-go-sdk/client"
"github.com/smartxworks/cloudtower-go-sdk/client/vm"
"github.com/smartxworks/cloudtower-go-sdk/models"
"github.com/smartxworks/cloudtower-go-sdk/utils"
httptransport "github.com/go-openapi/runtime/client"
"github.com/go-openapi/strfmt"
)
func main() {
transport := httptransport.New("192.168.36.133", "/v2/api", []string{"http"})
client := apiclient.New(transport, strfmt.Default)
transport.DefaultAuthentication = httptransport.APIKeyAuth("Authorization", "header", "token")
createParams := vm.NewCreateVMParams()
createParams.RequestBody = []*models.VMCreationParams{
{
ClusterID: pointy.String("clusterId"),
Name: pointy.String("test_vm_name"),
Ha: pointy.Bool(true),
CPUCores: pointy.Int32(4),
CPUSockets: pointy.Int32(2),
Memory: pointy.Float64(8 * 1024 * 1024 * 1024),
Vcpu: pointy.Int32(4 * 2),
Status: models.VMStatusSTOPPED.Pointer(),
Firmware: models.VMFirmwareBIOS.Pointer(),
VMNics: []*models.VMNicParams{
{ConnectVlanID: pointy.String("vlanId1")},
},
VMDisks: &models.VMDiskParams{
MountCdRoms: []*models.VMCdRomParams{
{
Boot: pointy.Int32(0),
Index: pointy.Int32(0),
},
},
},
},
}
createdVm, err := createVm(client, createParams)
if err != nil {
panic(err.Error())
}
// handle created vm
}
func createVm(
client *apiclient.Cloudtower,
createParams *vm.CreateVMParams) (*models.VM, error) {
createRes, err := client.VM.CreateVM(createParams)
if err != nil {
return nil, err
}
withTaskVm := createRes.Payload[0]
err = utils.WaitTask(client, withTaskVm.TaskID)
if err != nil {
return nil, err
}
getVmParams := vm.NewGetVmsParams()
getVmParams.RequestBody = &models.GetVmsRequestBody{
Where: &models.VMWhereInput{
ID: withTaskVm.Data.ID,
},
}
queryRes, err := client.VM.GetVms(getVmParams)
if err != nil {
return nil, err
}
return queryRes.Payload[0], nil
}
创建时配置虚拟盘
CD-ROM 加载 ISO
- Python
- Java
- Go
from cloudtower import (
ApiClient,
Configuration,
VmApi,
VmStatus,
VmFirmware,
Bus
)
from cloudtower.utils import wait_tasks
conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)
vm_api = VmApi(api_client)
with_task_vm = vm_api.create_vm([
{
"cluster_id": "cluster_id",
"name": "vm_name",
"ha": True,
"cpu_cores": 4,
"cpu_sockets": 4,
"memory": 4*1024*1024*1024,
"vcpu": 16,
"status": VmStatus.STOPPED,
"firmware": VmFirmware.BIOS,
"vm_nics": [
{
"connect_vlan_id": "vlan_id",
}
],
"vm_disks": {
"mount_cd_roms": [{
"index": 0,
"boot": 0,
"elf_image_id": "elf_image_id"
}],
}
}
])[0]
wait_tasks([with_task_vm.task_id], api_client)
created_vm = vm_api.get_vms({
"where": {
"id": with_task_vm.data.id
}
})
public class App {
public static void main(String[] args) throws ApiException {
ApiClient client = new ApiClient();
client.setBasePath("http://192.168.96.133/v2/api");
client.setApiKey("token");
VmCreationParams param = new VmCreationParams()
.clusterId("cl2k0mpoy026d0822xq6ctsim")
.name("vm_name")
.ha(true)
.cpuCores(4)
.cpuSockets(4)
.memory(4L * 1024 * 1024 * 1024)
.vcpu(16)
.status(VmStatus.STOPPED)
.firmware(VmFirmware.BIOS)
.addVmNicsItem(new VmNicParams().connectVlanId("cl2k0msiz02wc08220d6m3bz5"))
.vmDisks(new VmDiskParams()
.addMountCdRomsItem(new VmCdRomParams()
.boot(0)
.index(0)
.elfImageId("cl2k1yswo0csh0822299yalwn")));
List<Vm> vms = createVm(client, param);
}
public static List<Vm> createVm(ApiClient client, VmCreationParams param)
throws ApiException {
VmApi vmApi = new VmApi(client);
List<VmCreationParams> params = new ArrayList<VmCreationParams>(1);
params.add(param);
List<WithTaskVm> withTaskVms = vmApi.createVm(params);
List<String> tasks = withTaskVms.stream().map(vms -> vms.getTaskId()).collect(Collectors.toList());
List<String> ids = withTaskVms.stream().map(vms -> vms.getData().getId()).collect(Collectors.toList());
TaskUtil.WaitTasks(tasks, client);
List<Vm> vms = vmApi
.getVms(
new GetVmsRequestBody()
.where(new VmWhereInput()
.idIn(ids)));
return vms;
}
}
package main
import (
"github.com/openlyinc/pointy"
apiclient "github.com/smartxworks/cloudtower-go-sdk/client"
"github.com/smartxworks/cloudtower-go-sdk/client/vm"
"github.com/smartxworks/cloudtower-go-sdk/models"
"github.com/smartxworks/cloudtower-go-sdk/utils"
httptransport "github.com/go-openapi/runtime/client"
"github.com/go-openapi/strfmt"
)
func main() {
transport := httptransport.New("192.168.36.133", "/v2/api", []string{"http"})
client := apiclient.New(transport, strfmt.Default)
transport.DefaultAuthentication = httptransport.APIKeyAuth("Authorization", "header", "token")
createParams := vm.NewCreateVMParams()
createParams.RequestBody = []*models.VMCreationParams{
{
ClusterID: pointy.String("clusterId"),
Name: pointy.String("test_vm_name"),
Ha: pointy.Bool(true),
CPUCores: pointy.Int32(4),
CPUSockets: pointy.Int32(2),
Memory: pointy.Float64(8 * 1024 * 1024 * 1024),
Vcpu: pointy.Int32(4 * 2),
Status: models.VMStatusSTOPPED.Pointer(),
Firmware: models.VMFirmwareBIOS.Pointer(),
VMNics: []*models.VMNicParams{
{ConnectVlanID: pointy.String("vlanId1")},
},
VMDisks: &models.VMDiskParams{
MountCdRoms: []*models.VMCdRomParams{
{
Index: pointy.Int32(0),
Boot: pointy.Int32(0),
ElfImageID: pointy.String("elfImageId"),
},
},
},
},
}
createdVm, err := createVm(client, createParams)
if err != nil {
panic(err.Error())
}
// handle created vm
}
func createVm(
client *apiclient.Cloudtower,
createParams *vm.CreateVMParams) (*models.VM, error) {
createRes, err := client.VM.CreateVM(createParams)
if err != nil {
return nil, err
}
withTaskVm := createRes.Payload[0]
err = utils.WaitTask(client, withTaskVm.TaskID)
if err != nil {
return nil, err
}
getVmParams := vm.NewGetVmsParams()
getVmParams.RequestBody = &models.GetVmsRequestBody{
Where: &models.VMWhereInput{
ID: withTaskVm.Data.ID,
},
}
queryRes, err := client.VM.GetVms(getVmParams)
if err != nil {
return nil, err
}
return queryRes.Payload[0], nil
}
挂载虚拟卷为虚拟盘
- Python
- Java
- Go
from cloudtower import (
ApiClient,
Configuration,
VmApi,
VmStatus,
VmFirmware,
Bus
)
from cloudtower.utils import wait_tasks
conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)
vm_api = VmApi(api_client)
with_task_vm = vm_api.create_vm([
{
"cluster_id": "cluster_id",
"name": "vm_name",
"ha": True,
"cpu_cores": 4,
"cpu_sockets": 4,
"memory": 4*1024*1024*1024,
"vcpu": 16,
"status": VmStatus.STOPPED,
"firmware": VmFirmware.BIOS,
"vm_nics": [
{
"connect_vlan_id": "vlan_id",
}
],
"vm_disks": {
"mount_disks": [{
"index": 0,
"boot": 0,
"bus": Bus.VIRTIO,
"vm_volume_id": "vm_volume_id",
"index": 0,
}],
}
}
])[0]
wait_tasks([with_task_vm.task_id], api_client)
created_vm = vm_api.get_vms({
"where": {
"id": with_task_vm.data.id
}
})
public class App {
public static void main(String[] args) throws ApiException {
ApiClient client = new ApiClient();
client.setBasePath("http://192.168.96.133/v2/api");
client.setApiKey("token");
VmCreationParams param = new VmCreationParams()
.clusterId("cl2k0mpoy026d0822xq6ctsim")
.name("vm_name")
.ha(true)
.cpuCores(4)
.cpuSockets(4)
.memory(4L * 1024 * 1024 * 1024)
.vcpu(16)
.status(VmStatus.STOPPED)
.firmware(VmFirmware.BIOS)
.addVmNicsItem(new VmNicParams().connectVlanId("cl2k0msiz02wc08220d6m3bz5"))
.vmDisks(new VmDiskParams()
.addMountDisksItem(new MountDisksParams()
.boot(0)
.index(0)
.bus(Bus.SCSI)
.vmVolumeId("cl2k1kohp08up08225yjgfpdz")));
List<Vm> vms = createVm(client, param);
}
public static List<Vm> createVm(ApiClient client, VmCreationParams param)
throws ApiException {
VmApi vmApi = new VmApi(client);
List<VmCreationParams> params = new ArrayList<VmCreationParams>(1);
params.add(param);
List<WithTaskVm> withTaskVms = vmApi.createVm(params);
List<String> tasks = withTaskVms.stream().map(vms -> vms.getTaskId()).collect(Collectors.toList());
List<String> ids = withTaskVms.stream().map(vms -> vms.getData().getId()).collect(Collectors.toList());
TaskUtil.WaitTasks(tasks, client);
List<Vm> vms = vmApi
.getVms(
new GetVmsRequestBody()
.where(new VmWhereInput()
.idIn(ids)));
return vms;
}
}
package main
import (
"github.com/openlyinc/pointy"
apiclient "github.com/smartxworks/cloudtower-go-sdk/client"
"github.com/smartxworks/cloudtower-go-sdk/client/vm"
"github.com/smartxworks/cloudtower-go-sdk/models"
"github.com/smartxworks/cloudtower-go-sdk/utils"
httptransport "github.com/go-openapi/runtime/client"
"github.com/go-openapi/strfmt"
)
func main() {
transport := httptransport.New("192.168.36.133", "/v2/api", []string{"http"})
client := apiclient.New(transport, strfmt.Default)
transport.DefaultAuthentication = httptransport.APIKeyAuth("Authorization", "header", "token")
createParams := vm.NewCreateVMParams()
createParams.RequestBody = []*models.VMCreationParams{
{
ClusterID: pointy.String("clusterId"),
Name: pointy.String("test_vm_name"),
Ha: pointy.Bool(true),
CPUCores: pointy.Int32(4),
CPUSockets: pointy.Int32(2),
Memory: pointy.Float64(8 * 1024 * 1024 * 1024),
Vcpu: pointy.Int32(4 * 2),
Status: models.VMStatusSTOPPED.Pointer(),
Firmware: models.VMFirmwareBIOS.Pointer(),
VMNics: []*models.VMNicParams{
{ConnectVlanID: pointy.String("vlanId1")},
},
VMDisks: &models.VMDiskParams{
MountDisks: []*models.MountDisksParams{
{
Boot: pointy.Int32(0),
Bus: models.BusVIRTIO.Pointer(),
VMVolumeID: pointy.String("vmVolumeId1"),
Index: pointy.Int32(0),
},
},
},
},
}
createdVm, err := createVm(client, createParams)
if err != nil {
panic(err.Error())
}
// handle created vm
}
func createVm(
client *apiclient.Cloudtower,
createParams *vm.CreateVMParams) (*models.VM, error) {
createRes, err := client.VM.CreateVM(createParams)
if err != nil {
return nil, err
}
withTaskVm := createRes.Payload[0]
err = utils.WaitTask(client, withTaskVm.TaskID)
if err != nil {
return nil, err
}
getVmParams := vm.NewGetVmsParams()
getVmParams.RequestBody = &models.GetVmsRequestBody{
Where: &models.VMWhereInput{
ID: withTaskVm.Data.ID,
},
}
queryRes, err := client.VM.GetVms(getVmParams)
if err != nil {
return nil, err
}
return queryRes.Payload[0], nil
}
挂载新增虚拟盘
- Python
- Java
- Go
from cloudtower import (
ApiClient,
Configuration,
VmApi,
VmStatus,
VmFirmware,
Bus,
VmVolumeElfStoragePolicyType
)
from cloudtower.utils import wait_tasks
conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)
vm_api = VmApi(api_client)
with_task_vm = vm_api.create_vm([
{
"cluster_id": "cluster_id",
"name": "vm_name",
"ha": True,
"cpu_cores": 4,
"cpu_sockets": 4,
"memory": 4 * 1024*1024*1024,
"vcpu": 16,
"status": VmStatus.STOPPED,
"firmware": VmFirmware.BIOS,
"vm_nics": [
{
"connect_vlan_id": "vlan_id",
}
],
"vm_disks": {
"mount_new_create_disks": [{
"boot": 0,
"bus": Bus.VIRTIO,
"vm_volume": {
"elf_storage_policy": VmVolumeElfStoragePolicyType._2_THIN_PROVISION,
"size": 10 * 1024 * 1024 * 1024,
"name": "new_volume_name"
}
}],
}
}
])[0]
wait_tasks([with_task_vm.task_id], api_client)
created_vm = vm_api.get_vms({
"where": {
"id": with_task_vm.data.id
}
})
public class App {
public static void main(String[] args) throws ApiException {
ApiClient client = new ApiClient();
client.setBasePath("http://192.168.96.133/v2/api");
client.setApiKey("token");
VmCreationParams param = new VmCreationParams()
.clusterId("cl2k0mpoy026d0822xq6ctsim")
.name("vm_name")
.ha(true)
.cpuCores(4)
.cpuSockets(4)
.memory(4L * 1024 * 1024 * 1024)
.vcpu(16)
.status(VmStatus.STOPPED)
.firmware(VmFirmware.BIOS)
.addVmNicsItem(new VmNicParams().connectVlanId("cl2k0msiz02wc08220d6m3bz5"))
.vmDisks(new VmDiskParams()
.addMountNewCreateDisksItem(new MountNewCreateDisksParams()
.index(0)
.boot(0)
.bus(Bus.VIRTIO)
.vmVolume(new MountNewCreateDisksParamsVmVolume()
.elfStoragePolicy(VmVolumeElfStoragePolicyType._2_THIN_PROVISION)
.name("new_disk")
.size(4L * 1024 * 1024 * 1024))));
List<Vm> vms = createVm(client, param);
}
public static List<Vm> createVm(ApiClient client, VmCreationParams param)
throws ApiException {
VmApi vmApi = new VmApi(client);
List<VmCreationParams> params = new ArrayList<VmCreationParams>(1);
params.add(param);
List<WithTaskVm> withTaskVms = vmApi.createVm(params);
List<String> tasks = withTaskVms.stream().map(vms -> vms.getTaskId()).collect(Collectors.toList());
List<String> ids = withTaskVms.stream().map(vms -> vms.getData().getId()).collect(Collectors.toList());
TaskUtil.WaitTasks(tasks, client);
List<Vm> vms = vmApi
.getVms(
new GetVmsRequestBody()
.where(new VmWhereInput()
.idIn(ids)));
return vms;
}
}
package main
import (
"github.com/openlyinc/pointy"
apiclient "github.com/smartxworks/cloudtower-go-sdk/client"
"github.com/smartxworks/cloudtower-go-sdk/client/vm"
"github.com/smartxworks/cloudtower-go-sdk/models"
"github.com/smartxworks/cloudtower-go-sdk/utils"
httptransport "github.com/go-openapi/runtime/client"
"github.com/go-openapi/strfmt"
)
func main() {
transport := httptransport.New("192.168.36.133", "/v2/api", []string{"http"})
client := apiclient.New(transport, strfmt.Default)
transport.DefaultAuthentication = httptransport.APIKeyAuth("Authorization", "header", "token")
createParams := vm.NewCreateVMParams()
createParams.RequestBody = []*models.VMCreationParams{
{
ClusterID: pointy.String("clusterId"),
Name: pointy.String("test_vm_name"),
Ha: pointy.Bool(true),
CPUCores: pointy.Int32(4),
CPUSockets: pointy.Int32(2),
Memory: pointy.Float64(8 * 1024 * 1024 * 1024),
Vcpu: pointy.Int32(4 * 2),
Status: models.VMStatusSTOPPED.Pointer(),
Firmware: models.VMFirmwareBIOS.Pointer(),
VMNics: []*models.VMNicParams{
{ConnectVlanID: pointy.String("vlanId1")},
},
VMDisks: &models.VMDiskParams{
MountNewCreateDisks: []*models.MountNewCreateDisksParams{
{
Boot: pointy.Int32(0),
Bus: models.BusVIRTIO.Pointer(),
VMVolume: &models.MountNewCreateDisksParamsVMVolume{
Size: pointy.Float64(10 * 1024 * 1024 * 1024),
ElfStoragePolicy: models.VMVolumeElfStoragePolicyTypeREPLICA2THINPROVISION.Pointer(),
Name: pointy.String("new_vm_disk_name"),
},
},
},
},
},
}
createdVm, err := createVm(client, createParams)
if err != nil {
panic(err.Error())
}
// handle created vm
}
func createVm(
client *apiclient.Cloudtower,
createParams *vm.CreateVMParams) (*models.VM, error) {
createRes, err := client.VM.CreateVM(createParams)
if err != nil {
return nil, err
}
withTaskVm := createRes.Payload[0]
err = utils.WaitTask(client, withTaskVm.TaskID)
if err != nil {
return nil, err
}
getVmParams := vm.NewGetVmsParams()
getVmParams.RequestBody = &models.GetVmsRequestBody{
Where: &models.VMWhereInput{
ID: withTaskVm.Data.ID,
},
}
queryRes, err := client.VM.GetVms(getVmParams)
if err != nil {
return nil, err
}
return queryRes.Payload[0], nil
}
创建时配置虚拟网卡
- Python
- Java
- Go
from cloudtower import (
ApiClient,
Configuration,
VmApi,
VmStatus,
VmFirmware,
Bus,
VmVolumeElfStoragePolicyType
)
from cloudtower.utils import wait_tasks
conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)
vm_api = VmApi(api_client)
with_task_vm = vm_api.create_vm([
{
"cluster_id": "cluster_id",
"name": "vm_name",
"ha": True,
"cpu_cores": 4,
"cpu_sockets": 4,
"memory": 4 * 1024*1024*1024,
"vcpu": 16,
"status": VmStatus.STOPPED,
"firmware": VmFirmware.BIOS,
"vm_nics": [
{
"connect_vlan_id": "vlan_id",
}
],
"vm_disks": {
"mount_new_create_disks": [{
"boot": 0,
"bus": Bus.VIRTIO,
"vm_volume": {
"elf_storage_policy": VmVolumeElfStoragePolicyType._2_THIN_PROVISION,
"size": 10 * 1024 * 1024 * 1024,
"name": "new_volume_name"
}
}],
}
}
])[0]
wait_tasks([with_task_vm.task_id], api_client)
created_vm = vm_api.get_vms({
"where": {
"id": with_task_vm.data.id
}
})
public class App {
public static void main(String[] args) throws ApiException {
ApiClient client = new ApiClient();
client.setBasePath("http://192.168.96.133/v2/api");
client.setApiKey("token");
VmCreationParams param = new VmCreationParams()
.clusterId("cl2k0mpoy026d0822xq6ctsim")
.name("vm_name")
.ha(true)
.cpuCores(4)
.cpuSockets(4)
.memory(4L * 1024 * 1024 * 1024)
.vcpu(16)
.status(VmStatus.STOPPED)
.firmware(VmFirmware.BIOS)
.addVmNicsItem(new VmNicParams().connectVlanId("cl2k0msiz02wc08220d6m3bz5"))
.vmDisks(new VmDiskParams()
.addMountNewCreateDisksItem(new MountNewCreateDisksParams()
.index(0)
.boot(0)
.bus(Bus.VIRTIO)
.vmVolume(new MountNewCreateDisksParamsVmVolume()
.elfStoragePolicy(VmVolumeElfStoragePolicyType._2_THIN_PROVISION)
.name("new_disk")
.size(4L * 1024 * 1024 * 1024))));
List<Vm> vms = createVm(client, param);
}
public static List<Vm> createVm(ApiClient client, VmCreationParams param)
throws ApiException {
VmApi vmApi = new VmApi(client);
List<VmCreationParams> params = new ArrayList<VmCreationParams>(1);
params.add(param);
List<WithTaskVm> withTaskVms = vmApi.createVm(params);
List<String> tasks = withTaskVms.stream().map(vms -> vms.getTaskId()).collect(Collectors.toList());
List<String> ids = withTaskVms.stream().map(vms -> vms.getData().getId()).collect(Collectors.toList());
TaskUtil.WaitTasks(tasks, client);
List<Vm> vms = vmApi
.getVms(
new GetVmsRequestBody()
.where(new VmWhereInput()
.idIn(ids)));
return vms;
}
}
package main
import (
"github.com/openlyinc/pointy"
apiclient "github.com/smartxworks/cloudtower-go-sdk/client"
"github.com/smartxworks/cloudtower-go-sdk/client/vm"
"github.com/smartxworks/cloudtower-go-sdk/models"
"github.com/smartxworks/cloudtower-go-sdk/utils"
httptransport "github.com/go-openapi/runtime/client"
"github.com/go-openapi/strfmt"
)
func main() {
transport := httptransport.New("192.168.36.133", "/v2/api", []string{"http"})
client := apiclient.New(transport, strfmt.Default)
transport.DefaultAuthentication = httptransport.APIKeyAuth("Authorization", "header", "token")
createParams := vm.NewCreateVMParams()
createParams.RequestBody = []*models.VMCreationParams{
{
ClusterID: pointy.String("clusterId"),
Name: pointy.String("test_vm_name"),
Ha: pointy.Bool(true),
CPUCores: pointy.Int32(4),
CPUSockets: pointy.Int32(2),
Memory: pointy.Float64(8 * 1024 * 1024 * 1024),
Vcpu: pointy.Int32(4 * 2),
Status: models.VMStatusSTOPPED.Pointer(),
Firmware: models.VMFirmwareBIOS.Pointer(),
VMNics: []*models.VMNicParams{
{ConnectVlanID: pointy.String("vlanId1")},
},
VMDisks: &models.VMDiskParams{
MountNewCreateDisks: []*models.MountNewCreateDisksParams{
{
Boot: pointy.Int32(0),
Bus: models.BusVIRTIO.Pointer(),
VMVolume: &models.MountNewCreateDisksParamsVMVolume{
Size: pointy.Float64(10 * 1024 * 1024 * 1024),
ElfStoragePolicy: models.VMVolumeElfStoragePolicyTypeREPLICA2THINPROVISION.Pointer(),
Name: pointy.String("new_vm_disk_name"),
},
},
},
},
},
}
createdVm, err := createVm(client, createParams)
if err != nil {
panic(err.Error())
}
// handle created vm
}
func createVm(
client *apiclient.Cloudtower,
createParams *vm.CreateVMParams) (*models.VM, error) {
createRes, err := client.VM.CreateVM(createParams)
if err != nil {
return nil, err
}
withTaskVm := createRes.Payload[0]
err = utils.WaitTask(client, withTaskVm.TaskID)
if err != nil {
return nil, err
}
getVmParams := vm.NewGetVmsParams()
getVmParams.RequestBody = &models.GetVmsRequestBody{
Where: &models.VMWhereInput{
ID: withTaskVm.Data.ID,
},
}
queryRes, err := client.VM.GetVms(getVmParams)
if err != nil {
return nil, err
}
return queryRes.Payload[0], nil
}