编辑虚拟机
编辑基本信息
- Python
- Java
- Go
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;
}
}
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")
updateParams := vm.NewUpdateVMParams()
updateParams.RequestBody = &models.VMUpdateParams{
Where: &models.VMWhereInput{
ID: pointy.String("vmId"),
},
Data: &models.VMUpdateParamsData{
Name: pointy.String("updated_name"),
Description: pointy.String("updated description"),
Ha: pointy.Bool(true),
CPUCores: pointy.Int32(2),
CPUSockets: pointy.Int32(8),
Vcpu: pointy.Int32(2 * 8),
Memory: pointy.Float64(16 * 1024 * 1024 * 1024),
},
}
updatedVm, err := updateVm(client, updateParams)
if err != nil {
panic(err.Error())
}
// handle updated vm
}
func updateVm(
client *apiclient.Cloudtower,
updateParams *vm.UpdateVMParams) (*models.VM, error) {
updateRes, err := client.VM.UpdateVM(updateParams)
if err != nil {
return nil, err
}
withTaskVm := updateRes.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
}
编辑高级信息
- Java
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;
}
}
CD-ROM 编辑
添加 CD-ROM
- Python
- Java
- Go
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
}
})
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");
VmAddCdRomParamsData data = new VmAddCdRomParamsData()
.addVmCdRomsItem(new VmCdRomParams()
.boot(0)
.index(0)
.elfImageId("cl2k1yswo0csh0822299yalwn"));
List<Vm> vms = addCdRom(client, where, data);
}
public static List<Vm> addCdRom(ApiClient client, VmWhereInput where, VmAddCdRomParamsData data)
throws ApiException {
VmApi vmApi = new VmApi(client);
List<WithTaskVm> withTaskVms = vmApi
.addVmCdRom(new VmAddCdRomParams().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;
}
}
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")
addCdRomParams := vm.NewAddVMCdRomParams()
addCdRomParams.RequestBody = &models.VMAddCdRomParams{
Where: &models.VMWhereInput{
ID: pointy.String("vmId"),
},
Data: &models.VMAddCdRomParamsData{
VMCdRoms: []*models.VMCdRomParams{
{
Index: pointy.Int32(0),
ElfImageID: pointy.String("elfImageId"),
Boot: pointy.Int32(0),
},
},
},
}
updatedVm, err := addVmCdRom(client, addCdRomParams)
if err != nil {
panic(err.Error())
}
// handle updated vm
}
func addVmCdRom(
client *apiclient.Cloudtower,
addCdRomParams *vm.AddVMCdRomParams) (*models.VM, error) {
updateRes, err := client.VM.AddVMCdRom(addCdRomParams)
if err != nil {
return nil, err
}
withTaskVm := updateRes.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
- Python
- Java
- Go
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
}
})
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");
VmRemoveCdRomParamsData data = new VmRemoveCdRomParamsData().addCdRomIdsItem("cl2k2v1fv0jvo0822dr73hd1n");
List<Vm> vms = deleteCdRom(client, where, data);
}
public static List<Vm> deleteCdRom(ApiClient client, VmWhereInput where, VmRemoveCdRomParamsData data)
throws ApiException {
VmApi vmApi = new VmApi(client);
List<WithTaskVm> withTaskVms = vmApi
.removeVmCdRom(new VmRemoveCdRomParams().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;
}
}
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")
updateParams := vm.NewRemoveVMCdRomParams()
updateParams.RequestBody = &models.VMRemoveCdRomParams{
Where: &models.VMWhereInput{
ID: pointy.String("vmId"),
},
Data: &models.VMRemoveCdRomParamsData{
CdRomIds: []string{"cdRomId1", "cdRomId2"},
},
}
updatedVm, err := removeVmCdRom(client, updateParams)
if err != nil {
panic(err.Error())
}
// handle updated vm
}
func removeVmCdRom(
client *apiclient.Cloudtower,
removeCdRomParams *vm.RemoveVMCdRomParams) (*models.VM, error) {
updateRes, err := client.VM.RemoveVMCdRom(removeCdRomParams)
if err != nil {
return nil, err
}
withTaskVm := updateRes.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, 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
}
})
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");
VmAddDiskParamsData data = new VmAddDiskParamsData().vmDisks(new VmAddDiskParamsDataVmDisks()
.addMountNewCreateDisksItem(new MountNewCreateDisksParams()
.boot(0)
.index(0)
.bus(Bus.VIRTIO)
.vmVolume(
new MountNewCreateDisksParamsVmVolume()
.elfStoragePolicy(VmVolumeElfStoragePolicyType._2_THIN_PROVISION)
.name("new_mount_disk")
.size(10L * 1024 * 1024 * 1024))));
List<Vm> vms = addDisk(client, where, data);
}
public static List<Vm> addDisk(ApiClient client, VmWhereInput where, VmAddDiskParamsData data)
throws ApiException {
VmApi vmApi = new VmApi(client);
List<WithTaskVm> withTaskVms = vmApi
.addVmDisk(new VmAddDiskParams().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;
}
}
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")
updateParams := vm.NewAddVMDiskParams()
updateParams.RequestBody = &models.VMAddDiskParams{
Where: &models.VMWhereInput{
ID: pointy.String("vmId"),
},
Data: &models.VMAddDiskParamsData{
VMDisks: &models.VMAddDiskParamsDataVMDisks{
MountNewCreateDisks: []*models.MountNewCreateDisksParams{
{
VMVolume: &models.MountNewCreateDisksParamsVMVolume{
ElfStoragePolicy: models.VMVolumeElfStoragePolicyTypeREPLICA2THINPROVISION.Pointer(),
Size: pointy.Float64(10 * 1024 * 1024 * 1024),
Name: pointy.String("new_disk_name"),
},
Boot: pointy.Int32(1),
Bus: models.BusVIRTIO.Pointer(),
},
},
},
},
}
updatedVm, err := addVmDisk(client, updateParams)
if err != nil {
panic(err.Error())
}
// handle created vm
}
func addVmDisk(
client *apiclient.Cloudtower,
addVMDiskParams *vm.AddVMDiskParams) (*models.VM, error) {
updateRes, err := client.VM.AddVMDisk(addVMDiskParams)
if err != nil {
return nil, err
}
withTaskVm := updateRes.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, 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
}
})
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");
VmAddDiskParamsData data = new VmAddDiskParamsData().vmDisks(new VmAddDiskParamsDataVmDisks()
.addMountDisksItem(new MountDisksParams()
.boot(0)
.index(0)
.bus(Bus.VIRTIO)
.vmVolumeId("cl2k37rex0maa0822ivcy2s9l")));
List<Vm> vms = mountDisk(client, where, data);
}
public static List<Vm> mountDisk(ApiClient client, VmWhereInput where, VmAddDiskParamsData data)
throws ApiException {
VmApi vmApi = new VmApi(client);
List<WithTaskVm> withTaskVms = vmApi
.addVmDisk(new VmAddDiskParams().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;
}
}
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")
updateParams := vm.NewAddVMDiskParams()
updateParams.RequestBody = &models.VMAddDiskParams{
Where: &models.VMWhereInput{
ID: pointy.String("vmId"),
},
Data: &models.VMAddDiskParamsData{
VMDisks: &models.VMAddDiskParamsDataVMDisks{
MountDisks: []*models.MountDisksParams{
{
Index: pointy.Int32(0),
VMVolumeID: pointy.String("vmVolumeId"),
Boot: pointy.Int32(0),
Bus: models.BusVIRTIO.Pointer(),
},
},
},
},
}
updatedVm, err := addVmDisk(client, updateParams)
if err != nil {
panic(err.Error())
}
// handle updated vm
}
func addVmDisk(
client *apiclient.Cloudtower,
addVMDiskParams *vm.AddVMDiskParams) (*models.VM, error) {
updateRes, err := client.VM.AddVMDisk(addVMDiskParams)
if err != nil {
return nil, err
}
withTaskVm := updateRes.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, 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
}
})
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");
VmRemoveDiskParamsData data = new VmRemoveDiskParamsData().addDiskIdsItem("cl2k38qv70mna082283l646jl");
List<Vm> vms = removeDisk(client, where, data);
}
public static List<Vm> removeDisk(ApiClient client, VmWhereInput where, VmRemoveDiskParamsData data)
throws ApiException {
VmApi vmApi = new VmApi(client);
List<WithTaskVm> withTaskVms = vmApi
.removeVmDisk(new VmRemoveDiskParams().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;
}
}
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")
updateParams := vm.NewRemoveVMDiskParams()
updateParams.RequestBody = &models.VMRemoveDiskParams{
Where: &models.VMWhereInput{
ID: pointy.String("vmId"),
},
Data: &models.VMRemoveDiskParamsData{
DiskIds: []string{"diskId1", "diskId2"},
},
}
updatedVm, err := removeVmDisk(client, updateParams)
if err != nil {
panic(err.Error())
}
// handle updated vm
}
func removeVmDisk(
client *apiclient.Cloudtower,
removeVmDiskParams *vm.RemoveVMDiskParams) (*models.VM, error) {
updateRes, err := client.VM.RemoveVMDisk(removeVmDiskParams)
if err != nil {
return nil, err
}
withTaskVm := updateRes.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, 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
}
})
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");
VmAddNicParamsData data = new VmAddNicParamsData()
.addVmNicsItem(new VmNicParams().connectVlanId("cl2k1ohoq09si0822q648n9v8"));
List<Vm> vms = addVmNic(client, where, data);
}
public static List<Vm> addVmNic(ApiClient client, VmWhereInput where, VmAddNicParamsData data)
throws ApiException {
VmApi vmApi = new VmApi(client);
List<WithTaskVm> withTaskVms = vmApi
.addVmNic(new VmAddNicParams().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;
}
}
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")
updateParams := vm.NewAddVMNicParams()
updateParams.RequestBody = &models.VMAddNicParams{
Where: &models.VMWhereInput{
ID: pointy.String("vmId"),
},
Data: &models.VMAddNicParamsData{
VMNics: []*models.VMNicParams{
{
ConnectVlanID: pointy.String("vlanId2"),
Enabled: pointy.Bool(true),
Model: models.VMNicModelVIRTIO.Pointer(),
},
},
},
}
updatedVm, err := addVmNic(client, updateParams)
if err != nil {
panic(err.Error())
}
// handle updated vm
}
func addVmNic(
client *apiclient.Cloudtower,
addVMNicParams *vm.AddVMNicParams) (*models.VM, error) {
updateRes, err := client.VM.AddVMNic(addVMNicParams)
if err != nil {
return nil, err
}
withTaskVm := updateRes.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
}
编辑网卡基本信息
- Java
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;
}
}
编辑网卡高级信息
- Java
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;
}
}
移除网卡
- Python
- Java
- Go
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
}
})
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");
VmRemoveNicParamsData data = new VmRemoveNicParamsData()
.addNicIndexItem(1);
List<Vm> vms = removeVmNic(client, where, data);
}
public static List<Vm> removeVmNic(ApiClient client, VmWhereInput where, VmRemoveNicParamsData data)
throws ApiException {
VmApi vmApi = new VmApi(client);
List<WithTaskVm> withTaskVms = vmApi
.removeVmNic(new VmRemoveNicParams().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;
}
}
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")
updateParams := vm.NewRemoveVMNicParams()
updateParams.RequestBody = &models.VMRemoveNicParams{
Where: &models.VMWhereInput{
ID: pointy.String("vmId"),
},
Data: &models.VMRemoveNicParamsData{
NicIndex: []int32{0, 1},
},
}
updatedVm, err := removeVmNic(client, updateParams)
if err != nil {
panic(err.Error())
}
// handle updated vm
}
func removeVmNic(
client *apiclient.Cloudtower,
removeVmDiskParams *vm.RemoveVMNicParams) (*models.VM, error) {
updateRes, err := client.VM.RemoveVMNic(removeVmDiskParams)
if err != nil {
return nil, err
}
withTaskVm := updateRes.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
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)
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");
VmStartParamsData data = new VmStartParamsData().hostId("cl2k0mq69027u0822q69zct7z");
List<Vm> vms = migrateVm(client, where, data);
}
public static List<Vm> migrateVm(ApiClient client, VmWhereInput where, VmStartParamsDatadata)
throws ApiException {
VmApi vmApi = new VmApi(client);
List<WithTaskVm> withTaskVms = vmApi
.migRateVm(new VmMigrateParams().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;
}
}
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")
migratedVm, err := migrateVmToHost(client, "vmId", "hostId")
if err != nil {
panic(err.Error())
}
// handle migrated vm
}
func migrateVmToHost(
client *apiclient.Cloudtower,
vmId string,
hostId string) (*models.VM, error) {
migrateParams := vm.NewMigRateVMParams()
migrateParams.RequestBody = &models.VMMigrateParams{
Where: &models.VMWhereInput{
ID: pointy.String(vmId),
},
Data: &models.VMMigrateParamsData{
HostID: pointy.String(hostId),
},
}
migrateRes, err := client.VM.MigRateVM(migrateParams)
if err != nil {
return nil, err
}
withTaskVm := migrateRes.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
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)
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");
VmStartParamsData data = null;
List<Vm> vms = migrateVm(client, where, data);
}
public static List<Vm> migrateVm(ApiClient client, VmWhereInput where, VmStartParamsDatadata)
throws ApiException {
VmApi vmApi = new VmApi(client);
List<WithTaskVm> withTaskVms = vmApi
.migRateVm(new VmMigrateParams().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;
}
}
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")
migratedVm, err := migrateVmAutoSchedule(client, "vmId")
if err != nil {
panic(err.Error())
}
// handle migrated vm
}
func migrateVmAutoSchedule(
client *apiclient.Cloudtower,
vmId string) (*models.VM, error) {
migrateParams := vm.NewMigRateVMParams()
migrateParams.RequestBody = &models.VMMigrateParams{
Where: &models.VMWhereInput{
ID: pointy.String(vmId),
},
}
migrateRes, err := client.VM.MigRateVM(migrateParams)
if err != nil {
return nil, err
}
withTaskVm := migrateRes.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
}