Skip to main content

Edit VM(s)

Edit basic information

Edit vm advance options

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");

VmWhereInput where = new VmWhereInput().id("cl2k0njfl04480822fxjq5nns");
VmUpdateAdvancedOptionsParamsData data = new VmUpdateAdvancedOptionsParamsData()
.clockOffset(VmClockOffset.LOCALTIME)
.cpuModel("Skylake-Server-IBRS")
.videoType(VmVideoType.VGA)
.windowsOptimize(true);
List<Vm> vms = updateVm(client, where, data);
}

public static List<Vm> updateVm(ApiClient client, VmWhereInput where, VmUpdateAdvancedOptionsParamsData data)
throws ApiException {
VmApi vmApi = new VmApi(client);
List<WithTaskVm> withTaskVms = vmApi
.updateVmAdvancedOptions(new VmUpdateAdvancedOptionsParams().where(where).data(data));
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;
}
}

Edit a CD-ROM

Add a CD-ROM

from cloudtower import ApiClient, Configuration, VmApi
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.add_vm_cd_rom({
"where": {
"id": "vm_id"
},
"data": {
"vm_cd_roms": [
{
"elf_image_id": "elf_image_id",
"boot": 0,
"index": 0
}
]
}
})[0]
wait_tasks([with_task_vm.task_id], api_client)

updated_vm = vm_api.get_vms({
"where": {
"id": with_task_vm.data.id
}
})

Delete a CD-ROM

from cloudtower import ApiClient, Configuration, VmApi
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.remove_vm_cd_rom({
"where": {
"id": "vm_id"
},
"data": {
"cd_rom_ids": ["cd_rom_id_1", "cd_rom_id_2"]
}
})[0]

wait_tasks([with_task_vm.task_id], api_client)

updated_vm = vm_api.get_vms({
"where": {
"id": with_task_vm.data.id
}
})

Virtual volume operations

Add a new virtual volume

from cloudtower import ApiClient, Configuration, Bus, VmVolumeElfStoragePolicyType, VmApi
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.add_vm_disk({
"where": {
"id": "vm_id"
},
"data": {
"vm_disks": {
"mount_new_create_disks": [
{
"vm_volume": {
"elf_storage_policy": VmVolumeElfStoragePolicyType._2_THIN_PROVISION,
"size": 5*1024*1024*1024,
"name": "new_volume_name"
},
"boot": 1,
"bus": Bus.VIRTIO,
}
]
}
}
})[0]

wait_tasks([with_task_vm.task_id], api_client)

updated_vm = vm_api.get_vms({
"where": {
"id": with_task_vm.data.id
}
})

Mount an existing virtual volume as a virtual disk

from cloudtower import ApiClient, Configuration, Bus, VmApi
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.add_vm_disk({
"where": {
"id": "vm_id"
},
"data": {
"vm_disks": {
"mount_disks": [
{
"index": 0,
"vm_volume_id": "vm_volume_id",
"boot": 1,
"bus": Bus.VIRTIO,
}
]
}
}
})[0]

wait_tasks([with_task_vm.task_id], api_client)

updated_vm = vm_api.get_vms({
"where": {
"id": with_task_vm.data.id
}
})

Unmount a virtual disk

from cloudtower import ApiClient, Configuration, VmVolumeElfStoragePolicyType, Bus, VmApi
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.remove_vm_disk({
"where": {
"id": "vm_id"
},
"data": {
"disk_ids": ["vm_disk_id_1", "vm_disk_id_2"]
}
})[0]

wait_tasks([with_task_vm.task_id], api_client)

updated_vm = vm_api.get_vms({
"where": {
"id": with_task_vm.data.id
}
})

NIC operations

Add a NIC

from cloudtower import ApiClient, Configuration, VmApi, VmNicModel
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.add_vm_nic({
"where": {
"id": "vm_id"
},
"data": {
"vm_nics": [
{
"connect_vlan_id": "vlan_id",
"enabled": False,
"model": VmNicModel.VIRTIO,
},
{
"connect_vlan_id": "vlan_id_2",
"enabled": True,
"mirror": True,
"model": VmNicModel.VIRTIO,
}
]
}
})[0]

wait_tasks([with_task_vm.task_id], api_client)
updated_vm = vm_api.get_vms({
"where": {
"id": with_task_vm.data.id
}
})

Edit basic information of a nic

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");

VmWhereInput where = new VmWhereInput().id("cl2k0njfl04480822fxjq5nns");
VmUpdateParamsData data = new VmUpdateParamsData()
.cpuSockets(2)
.cpuCores(2)
.vcpu(4)
.memory(8L * 1024 * 1024 * 1024)
.name("new_name")
.description("new_description");
List<Vm> vms = updateVm(client, where, data);
}

public static List<Vm> updateVm(ApiClient client, VmWhereInput where, VmUpdateParamsData data)
throws ApiException {
VmApi vmApi = new VmApi(client);
List<WithTaskVm> withTaskVms = vmApi.updateVm(new VmUpdateParams().where(where).data(data));
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;
}
}

Edit advance information of a nic

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");

VmWhereInput where = new VmWhereInput().id("cl2k0njfl04480822fxjq5nns");
VmUpdateAdvancedOptionsParamsData data = new VmUpdateAdvancedOptionsParamsData()
.clockOffset(VmClockOffset.LOCALTIME)
.cpuModel("Skylake-Server-IBRS")
.videoType(VmVideoType.VGA)
.windowsOptimize(true);
List<Vm> vms = updateVm(client, where, data);
}

public static List<Vm> updateVm(ApiClient client, VmWhereInput where, VmUpdateAdvancedOptionsParamsData data)
throws ApiException {
VmApi vmApi = new VmApi(client);
List<WithTaskVm> withTaskVms = vmApi
.updateVmAdvancedOptions(new VmUpdateAdvancedOptionsParams().where(where).data(data));
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;
}
}

Delete a NIC

from cloudtower import ApiClient, Configuration, VmApi
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.remove_vm_nic({
"where": {
"id": "vm_id"
},
"data": {
"nic_index": [0, 1]
}
})[0]

wait_tasks([with_task_vm.task_id], api_client)
updated_vm = vm_api.get_vms({
"where": {
"id": with_task_vm.data.id
}
})

Virtual machine migration

Migrate to a specified host

from cloudtower import ApiClient, Configuration, VmApi
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.mig_rate_vm({
"where": {
"id": "vm_id"
},
"data": {
"host_id": "host_id"
}
})[0]

wait_tasks([with_task_vm.task_id], api_client)

Schedule to an appropriate host automatically

from cloudtower import ApiClient, Configuration, VmApi
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.mig_rate_vm({
"where": {
"id": "vm_id"
}
})[0]

wait_tasks([with_task_vm.task_id], api_client)