虚拟机电源操作
虚拟机开机
指定虚拟机开机,自动调度到合适的虚拟机
- 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.start_vm({
"where": {
"id": "vm_id"
}
})[0]
wait_tasks([with_task_vm.task_id], api_client)
opened_vm = vm_api.get_vms({"where": {"id": with_task_vm.data.id}})[0]
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 = startVm(client, where, data);
}
public static List<Vm> startVm(ApiClient client, VmWhereInput where, VmStartParamsData data)
throws ApiException {
VmApi vmApi = new VmApi(client);
List<WithTaskVm> withTaskVms = vmApi
.startVm(new VmStartParams().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")
startedVm, err := startVm(client, "vmId")
if err != nil {
panic(err.Error())
}
// handle started vm
}
func startVm(
client *apiclient.Cloudtower,
vmId string) (*models.VM, error) {
startParams := vm.NewStartVMParams()
startParams.RequestBody = &models.VMStartParams{
Where: &models.VMWhereInput{
ID: pointy.String(vmId),
},
}
startRes, err := client.VM.StartVM(startParams)
if err != nil {
return nil, err
}
withTaskVm := startRes.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
api_client = ApiClient(Configuration(host="http://192.168.96.133/v2/api"))
vm_api = VmApi(api_client)
with_task_vms = vm_api.restart_vm({
"where": {
"id_in": ["vm_id_1", "vm_id_2"]
}
})
tasks = [with_task_vm.task_id for with_task_vm in with_task_vms]
ids = [with_task_vm.data.id for with_task_vm in with_task_vms]
wait_tasks(tasks, api_client)
restarted_vms = vm_api.get_vms({"where": {"id_in": ids}})
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().nameStartsWith("prefix");
List<Vm> vms = restartVm(client, where);
}
public static List<Vm> restartVm(ApiClient client, VmWhereInput where)
throws ApiException {
VmApi vmApi = new VmApi(client);
List<WithTaskVm> withTaskVms = vmApi
.restartVm(new VmOperateParams().where(where));
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 (
"fmt"
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"
"github.com/thoas/go-funk"
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")
restartedVms, err := restartVmsByQuery(client, &models.VMWhereInput{
IDIn: []string{"vmId"},
})
if err != nil {
panic(err.Error())
}
// handle restarted vms
}
func restartVmsByQuery(client *apiclient.Cloudtower,
where *models.VMWhereInput) ([]*models.VM, error) {
restartParams := vm.NewRestartVMParams()
restartParams.RequestBody = &models.VMOperateParams{
Where: where,
}
restartRes, err := client.VM.RestartVM(restartParams)
if err != nil {
return nil, err
}
withTaskVms := restartRes.Payload
taskIds, valid := funk.Map(withTaskVms, func(vm *models.WithTaskVM) string {
return *vm.TaskID
}).([]string)
if !valid {
return nil, fmt.Errorf("failed to parse task ids")
}
vmIds, valid := funk.Map(withTaskVms, func(vm *models.WithTaskVM) string {
return *vm.Data.ID
}).([]string)
if !valid {
return nil, fmt.Errorf("failed to parse vm ids")
}
err = utils.WaitTasks(client, taskIds)
if err != nil {
return nil, err
}
getVmParams := vm.NewGetVmsParams()
getVmParams.RequestBody = &models.GetVmsRequestBody{
Where: &models.VMWhereInput{
IDIn: vmIds,
},
}
queryRes, err := client.VM.GetVms(getVmParams)
if err != nil {
return nil, err
}
return queryRes.Payload, 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.start_vm({
"where": {
"id": "vm_id"
},
"data": {
"host_id": "host_id"
}
})[0]
wait_tasks([with_task_vm.task_id], api_client)
opened_vm = vm_api.get_vms({"where": {"id": with_task_vm.data.id}})[0]
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 = startVm(client, where, data);
}
public static List<Vm> startVm(ApiClient client, VmWhereInput where, VmStartParamsData data)
throws ApiException {
VmApi vmApi = new VmApi(client);
List<WithTaskVm> withTaskVms = vmApi
.startVm(new VmStartParams().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")
startedVm, err := startVmOnHost(client, "vmId", "hostId2")
if err != nil {
panic(err.Error())
}
// handle started vm
}
func startVmOnHost(
client *apiclient.Cloudtower,
vmId string,
hostId string) (*models.VM, error) {
startParams := vm.NewStartVMParams()
startParams.RequestBody = &models.VMStartParams{
Where: &models.VMWhereInput{
ID: pointy.String(vmId),
},
Data: &models.VMStartParamsData{
HostID: pointy.String(hostId),
},
}
startRes, err := client.VM.StartVM(startParams)
if err != nil {
return nil, err
}
withTaskVm := startRes.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.shut_down_vm({
"where": {
"id": "vm_id"
}
})[0]
wait_tasks([with_task_vm.task_id], api_client)
closed_vm = vm_api.get_vms({"where": {"id": with_task_vm.data.id}})[0]
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");
List<Vm> vms = shutdownVm(client, where);
}
public static List<Vm> shutdownVm(ApiClient client, VmWhereInput where)
throws ApiException {
VmApi vmApi = new VmApi(client);
List<WithTaskVm> withTaskVms = vmApi
.shutDownVm(new VmOperateParams().where(where));
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")
shutdownVm, err := shutdownVm(client, "vmId")
if err != nil {
panic(err.Error())
}
// handle shutdown vm
}
func shutdownVm(
client *apiclient.Cloudtower,
vmId string) (*models.VM, error) {
shutdownParams := vm.NewShutDownVMParams()
shutdownParams.RequestBody = &models.VMOperateParams{
Where: &models.VMWhereInput{
ID: pointy.String(vmId),
},
}
shutdownRes, err := client.VM.ShutDownVM(shutdownParams)
if err != nil {
return nil, err
}
withTaskVm := shutdownRes.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_vms = vm_api.shut_down_vm({
"where": {
"id_in": ["vm_id_1", "vm_id_2"]
}
})
tasks = [with_task_vm.task_id for with_task_vm in with_task_vms]
ids = [with_task_vm.data.id for with_task_vm in with_task_vms]
wait_tasks(tasks, api_client)
closed_vms = vm_api.get_vms({"where": {"id_in": ids}})
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().nameStartsWith("prefix");
List<Vm> vms = shutdownVm(client, where);
}
public static List<Vm> shutdownVm(ApiClient client, VmWhereInput where)
throws ApiException {
VmApi vmApi = new VmApi(client);
List<WithTaskVm> withTaskVms = vmApi
.shutDownVm(new VmOperateParams().where(where));
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 (
"fmt"
"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"
"github.com/thoas/go-funk"
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")
shutdownVms, err := shutdownVmsByQuery(client, &models.VMWhereInput{
IDIn: []string{"vmId"},
})
if err != nil {
panic(err.Error())
}
// handle shutdown vms
}
func shutdownVmsByQuery(client *apiclient.Cloudtower,
where *models.VMWhereInput) ([]*models.VM, error) {
shutdownParams := vm.NewShutDownVMParams()
shutdownParams.RequestBody = &models.VMOperateParams{
Where: where,
}
shutdownRes, err := client.VM.ShutDownVM(shutdownParams)
if err != nil {
return nil, err
}
withTaskVms := shutdownRes.Payload
taskIds, valid := funk.Map(withTaskVms, func(vm *models.WithTaskVM) string {
return *vm.TaskID
}).([]string)
if !valid {
return nil, fmt.Errorf("failed to parse task ids")
}
vmIds, valid := funk.Map(withTaskVms, func(vm *models.WithTaskVM) string {
return *vm.Data.ID
}).([]string)
if !valid {
return nil, fmt.Errorf("failed to parse vm ids")
}
err = utils.WaitTasks(client, taskIds)
if err != nil {
return nil, err
}
getVmParams := vm.NewGetVmsParams()
getVmParams.RequestBody = &models.GetVmsRequestBody{
Where: &models.VMWhereInput{
IDIn: vmIds,
},
}
queryRes, err := client.VM.GetVms(getVmParams)
if err != nil {
return nil, err
}
return queryRes.Payload, 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.force_shut_down_vm({
"where": {
"id": "vm_id"
}
})[0]
wait_tasks([with_task_vm.task_id], api_client)
closed_vm = vm_api.get_vms({"where": {"id": with_task_vm.data.id}})[0]
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");
List<Vm> vms = powerOffVm(client, where);
}
public static List<Vm> powerOffVm(ApiClient client, VmWhereInput where)
throws ApiException {
VmApi vmApi = new VmApi(client);
List<WithTaskVm> withTaskVms = vmApi
.poweroffVm(new VmOperateParams().where(where));
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")
shutdownVm, err := forceShutdownVm(client, "vmId")
if err != nil {
panic(err.Error())
}
// handle shutdown vm
}
func forceShutdownVm(
client *apiclient.Cloudtower,
vmId string) (*models.VM, error) {
shutdownParams := vm.NewForceShutDownVMParams()
shutdownParams.RequestBody = &models.VMOperateParams{
Where: &models.VMWhereInput{
ID: pointy.String(vmId),
},
}
shutdownRes, err := client.VM.ForceShutDownVM(shutdownParams)
if err != nil {
return nil, err
}
withTaskVm := shutdownRes.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_vms = vm_api.force_shut_down_vm({
"where": {
"id_in": ["vm_id_1", "vm_id_2"]
}
})
tasks = [with_task_vm.task_id for with_task_vm in with_task_vms]
ids = [with_task_vm.data.id for with_task_vm in with_task_vms]
wait_tasks(tasks, api_client)
closed_vms = vm_api.get_vms({"where": {"id_in": ids}})
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().nameStartsWith("prefix");
List<Vm> vms = powerOffVm(client, where);
}
public static List<Vm> powerOffVm(ApiClient client, VmWhereInput where)
throws ApiException {
VmApi vmApi = new VmApi(client);
List<WithTaskVm> withTaskVms = vmApi
.poweroffVm(new VmOperateParams().where(where));
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 (
"fmt"
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"
"github.com/thoas/go-funk"
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")
shutdownVms, err := forceshutdownVmsByQuery(client, &models.VMWhereInput{
IDIn: []string{"vmId"},
})
if err != nil {
panic(err.Error())
}
// handle shutdown vms
}
func forceshutdownVmsByQuery(client *apiclient.Cloudtower,
where *models.VMWhereInput) ([]*models.VM, error) {
shutdownParams := vm.NewForceShutDownVMParams()
shutdownParams.RequestBody = &models.VMOperateParams{
Where: where,
}
shutdownRes, err := client.VM.ForceShutDownVM(shutdownParams)
if err != nil {
return nil, err
}
withTaskVms := shutdownRes.Payload
taskIds, valid := funk.Map(withTaskVms, func(vm *models.WithTaskVM) string {
return *vm.TaskID
}).([]string)
if !valid {
return nil, fmt.Errorf("failed to parse task ids")
}
vmIds, valid := funk.Map(withTaskVms, func(vm *models.WithTaskVM) string {
return *vm.Data.ID
}).([]string)
if !valid {
return nil, fmt.Errorf("failed to parse vm ids")
}
err = utils.WaitTasks(client, taskIds)
if err != nil {
return nil, err
}
getVmParams := vm.NewGetVmsParams()
getVmParams.RequestBody = &models.GetVmsRequestBody{
Where: &models.VMWhereInput{
IDIn: vmIds,
},
}
queryRes, err := client.VM.GetVms(getVmParams)
if err != nil {
return nil, err
}
return queryRes.Payload, nil
}
虚拟机重启
重启指定虚拟机
- Python
- Java
- Go
from cloudtower import ApiClient, Configuration, VmApi
from cloudtower.utils import wait_tasks
api_client = ApiClient(Configuration(host="http://192.168.96.133/v2/api"))
vm_api = VmApi(api_client)
with_task_vm = vm_api.restart_vm({
"where": {
"id": "vm_id"
}
})[0]
wait_tasks([with_task_vm.task_id], api_client)
restarted_vm = vm_api.get_vms({"where": {"id": with_task_vm.data.id}})[0]
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");
List<Vm> vms = restartVm(client, where);
}
public static List<Vm> restartVm(ApiClient client, VmWhereInput where)
throws ApiException {
VmApi vmApi = new VmApi(client);
List<WithTaskVm> withTaskVms = vmApi
.restartVm(new VmOperateParams().where(where));
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")
restartedVm, err := restartVm(client, "vmId")
if err != nil {
panic(err.Error())
}
// handle restarted vm
}
func restartVm(
client *apiclient.Cloudtower,
vmId string) (*models.VM, error) {
restartParams := vm.NewRestartVMParams()
restartParams.RequestBody = &models.VMOperateParams{
Where: &models.VMWhereInput{
ID: pointy.String(vmId),
},
}
restartRes, err := client.VM.RestartVM(restartParams)
if err != nil {
return nil, err
}
withTaskVm := restartRes.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
api_client = ApiClient(Configuration(host="http://192.168.96.133/v2/api"))
vm_api = VmApi(api_client)
with_task_vms = vm_api.restart_vm({
"where": {
"id_in": ["vm_id_1", "vm_id_2"]
}
})
tasks = [with_task_vm.task_id for with_task_vm in with_task_vms]
ids = [with_task_vm.data.id for with_task_vm in with_task_vms]
wait_tasks(tasks, api_client)
restarted_vms = vm_api.get_vms({"where": {"id_in": ids}})
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().nameStartsWith("prefix");
List<Vm> vms = restartVm(client, where);
}
public static List<Vm> restartVm(ApiClient client, VmWhereInput where)
throws ApiException {
VmApi vmApi = new VmApi(client);
List<WithTaskVm> withTaskVms = vmApi
.restartVm(new VmOperateParams().where(where));
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 (
"fmt"
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"
"github.com/thoas/go-funk"
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")
restartedVms, err := restartVmsByQuery(client, &models.VMWhereInput{
IDIn: []string{"vmId"},
})
if err != nil {
panic(err.Error())
}
// handle restarted vms
}
func restartVmsByQuery(client *apiclient.Cloudtower,
where *models.VMWhereInput) ([]*models.VM, error) {
restartParams := vm.NewRestartVMParams()
restartParams.RequestBody = &models.VMOperateParams{
Where: where,
}
restartRes, err := client.VM.RestartVM(restartParams)
if err != nil {
return nil, err
}
withTaskVms := restartRes.Payload
taskIds, valid := funk.Map(withTaskVms, func(vm *models.WithTaskVM) string {
return *vm.TaskID
}).([]string)
if !valid {
return nil, fmt.Errorf("failed to parse task ids")
}
vmIds, valid := funk.Map(withTaskVms, func(vm *models.WithTaskVM) string {
return *vm.Data.ID
}).([]string)
if !valid {
return nil, fmt.Errorf("failed to parse vm ids")
}
err = utils.WaitTasks(client, taskIds)
if err != nil {
return nil, err
}
getVmParams := vm.NewGetVmsParams()
getVmParams.RequestBody = &models.GetVmsRequestBody{
Where: &models.VMWhereInput{
IDIn: vmIds,
},
}
queryRes, err := client.VM.GetVms(getVmParams)
if err != nil {
return nil, err
}
return queryRes.Payload, nil
}
强制重启指定虚拟机
- Python
- Java
- Go
from cloudtower import ApiClient, Configuration, VmApi
from cloudtower.utils import wait_tasks
api_client = ApiClient(Configuration(host="http://192.168.96.133/v2/api"))
vm_api = VmApi(api_client)
with_task_vm = vm_api.force_restart_vm({
"where": {
"id": "vm_id"
}
})[0]
wait_tasks([with_task_vm.task_id], api_client)
restarted_vm = vm_api.get_vms({"where": {"id": with_task_vm.data.id}})[0]
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");
List<Vm> vms = forceRestartVm(client, where);
}
public static List<Vm> forceRestartVm(ApiClient client, VmWhereInput where)
throws ApiException {
VmApi vmApi = new VmApi(client);
List<WithTaskVm> withTaskVms = vmApi
.forceRestartVm(new VmOperateParams().where(where));
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")
restartedVm, err := forceRestartVm(client, "vmId")
if err != nil {
panic(err.Error())
}
// handle restarted vm
}
func forceRestartVm(
client *apiclient.Cloudtower,
vmId string) (*models.VM, error) {
restartParams := vm.NewForceRestartVMParams()
restartParams.RequestBody = &models.VMOperateParams{
Where: &models.VMWhereInput{
ID: pointy.String(vmId),
},
}
shutdownRes, err := client.VM.ForceRestartVM(restartParams)
if err != nil {
return nil, err
}
withTaskVm := shutdownRes.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
api_client = ApiClient(Configuration(host="http://192.168.96.133/v2/api"))
vm_api = VmApi(api_client)
with_task_vms = vm_api.suspend_vm({
"where": {
"id_in": ["vm_id_1", "vm_id_2"]
}
})
tasks = [with_task_vm.task_id for with_task_vm in with_task_vms]
ids = [with_task_vm.data.id for with_task_vm in with_task_vms]
wait_tasks(tasks, api_client)
suspended_vms = vm_api.get_vms({"where": {"id_in": ids}})
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().nameStartsWith("prefix");
List<Vm> vms = suspendVm(client, where);
}
public static List<Vm> suspendVm(ApiClient client, VmWhereInput where)
throws ApiException {
VmApi vmApi = new VmApi(client);
List<WithTaskVm> withTaskVms = vmApi
.suspendVm(new VmOperateParams().where(where));
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 (
"fmt"
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"
"github.com/thoas/go-funk"
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")
suspendedVms, err := suspendVmsByQuery(client, &models.VMWhereInput{
IDIn: []string{"vmId"},
})
if err != nil {
panic(err.Error())
}
// handle restarted vms
print(suspendedVms)
}
func suspendVmsByQuery(client *apiclient.Cloudtower,
where *models.VMWhereInput) ([]*models.VM, error) {
suspendParams := vm.NewSuspendVMParams()
suspendParams.RequestBody = &models.VMOperateParams{
Where: where,
}
suspendRes, err := client.VM.SuspendVM(suspendParams)
if err != nil {
return nil, err
}
withTaskVms := suspendRes.Payload
taskIds, valid := funk.Map(withTaskVms, func(vm *models.WithTaskVM) string {
return *vm.TaskID
}).([]string)
if !valid {
return nil, fmt.Errorf("failed to parse task ids")
}
vmIds, valid := funk.Map(withTaskVms, func(vm *models.WithTaskVM) string {
return *vm.Data.ID
}).([]string)
if !valid {
return nil, fmt.Errorf("failed to parse vm ids")
}
err = utils.WaitTasks(client, taskIds)
if err != nil {
return nil, err
}
getVmParams := vm.NewGetVmsParams()
getVmParams.RequestBody = &models.GetVmsRequestBody{
Where: &models.VMWhereInput{
IDIn: vmIds,
},
}
queryRes, err := client.VM.GetVms(getVmParams)
if err != nil {
return nil, err
}
return queryRes.Payload, nil
}
虚拟机暂停
暂停指定虚拟机
- Python
- Java
- Go
from cloudtower import ApiClient, Configuration, VmApi
from cloudtower.utils import wait_tasks
api_client = ApiClient(Configuration(host="http://192.168.96.133/v2/api"))
vm_api = VmApi(api_client)
with_task_vm = vm_api.suspend_vm({
"where": {
"id": "vm_id"
}
})[0]
wait_tasks([with_task_vm.task_id], api_client)
suspended_vm = vm_api.get_vms({"where": {"id": with_task_vm.data.id}})[0]
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");
List<Vm> vms = suspendVm(client, where);
}
public static List<Vm> suspendVm(ApiClient client, VmWhereInput where)
throws ApiException {
VmApi vmApi = new VmApi(client);
List<WithTaskVm> withTaskVms = vmApi
.suspendVm(new VmOperateParams().where(where));
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")
suspendedVm, err := suspendVm(client, "vmId")
if err != nil {
panic(err.Error())
}
// handle suspended vm
}
func suspendVm(
client *apiclient.Cloudtower,
vmId string) (*models.VM, error) {
suspendParams := vm.NewSuspendVMParams()
suspendParams.RequestBody = &models.VMOperateParams{
Where: &models.VMWhereInput{
ID: pointy.String(vmId),
},
}
shutdownRes, err := client.VM.SuspendVM(suspendParams)
if err != nil {
return nil, err
}
withTaskVm := shutdownRes.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
- Python
虚拟机恢复
恢复指定虚拟机
- Python
- Java
- Go
from cloudtower import ApiClient, Configuration, VmApi
from cloudtower.utils import wait_tasks
api_client = ApiClient(Configuration(host="http://192.168.96.133/v2/api"))
vm_api = VmApi(api_client)
with_task_vm = vm_api.resume_vm({
"where": {
"id": "vm_id"
}
})[0]
wait_tasks([with_task_vm.task_id], api_client)
resumed_vm = vm_api.get_vms({"where": {"id": with_task_vm.data.id}})[0]
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");
List<Vm> vms = resumeVm(client, where);
}
public static List<Vm> resumeVm(ApiClient client, VmWhereInput where)
throws ApiException {
VmApi vmApi = new VmApi(client);
List<WithTaskVm> withTaskVms = vmApi
.resumeVm(new VmOperateParams().where(where));
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")
resumedVm, err := resumeVm(client, "vmId")
if err != nil {
panic(err.Error())
}
// handle resumed vm
}
func resumeVm(
client *apiclient.Cloudtower,
vmId string) (*models.VM, error) {
resumeVmParams := vm.NewResumeVMParams()
resumeVmParams.RequestBody = &models.VMOperateParams{
Where: &models.VMWhereInput{
ID: pointy.String(vmId),
},
}
resumeRes, err := client.VM.ResumeVM(resumeVmParams)
if err != nil {
return nil, err
}
withTaskVm := resumeRes.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
api_client = ApiClient(Configuration(host="http://192.168.96.133/v2/api"))
vm_api = VmApi(api_client)
with_task_vms = vm_api.resume_vm({
"where": {
"id_in": ["vm_id_1", "vm_id_2"]
}
})
tasks = [with_task_vm.task_id for with_task_vm in with_task_vms]
ids = [with_task_vm.data.id for with_task_vm in with_task_vms]
wait_tasks(tasks, api_client)
resumed_vms = vm_api.get_vms({"where": {"id_in": ids}})
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().nameStartsWith("prefix");
List<Vm> vms = resumeVm(client, where);
}
public static List<Vm> resumeVm(ApiClient client, VmWhereInput where)
throws ApiException {
VmApi vmApi = new VmApi(client);
List<WithTaskVm> withTaskVms = vmApi
.resumeVm(new VmOperateParams().where(where));
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 (
"fmt"
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"
"github.com/thoas/go-funk"
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")
resumedVms, err := resumeVmsByQuery(client, &models.VMWhereInput{
IDIn: []string{"vmId"},
})
if err != nil {
panic(err.Error())
}
// handle resumed vms
}
func resumeVmsByQuery(client *apiclient.Cloudtower,
where *models.VMWhereInput) ([]*models.VM, error) {
resumeParams := vm.NewResumeVMParams()
resumeParams.RequestBody = &models.VMOperateParams{
Where: where,
}
resumeRes, err := client.VM.ResumeVM(resumeParams)
if err != nil {
return nil, err
}
withTaskVms := resumeRes.Payload
taskIds, valid := funk.Map(withTaskVms, func(vm *models.WithTaskVM) string {
return *vm.TaskID
}).([]string)
if !valid {
return nil, fmt.Errorf("failed to parse task ids")
}
vmIds, valid := funk.Map(withTaskVms, func(vm *models.WithTaskVM) string {
return *vm.Data.ID
}).([]string)
if !valid {
return nil, fmt.Errorf("failed to parse vm ids")
}
err = utils.WaitTasks(client, taskIds)
if err != nil {
return nil, err
}
getVmParams := vm.NewGetVmsParams()
getVmParams.RequestBody = &models.GetVmsRequestBody{
Where: &models.VMWhereInput{
IDIn: vmIds,
},
}
queryRes, err := client.VM.GetVms(getVmParams)
if err != nil {
return nil, err
}
return queryRes.Payload, nil
}