Delete VM(s)
Recycle bin
Move to recycle bin
- 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_delete_vms = vm_api.move_vm_to_recycle_bin({
    "where": {
        "id_in": ["vm_id_1", "vm_id_2"]
    }
})
tasks = [with_task_delete_vm.task_id for with_task_delete_vm in with_task_delete_vms]
ids = [with_task_vm.data.id for with_task_vm in with_task_vms]
wait_tasks(tasks, api_client)
vm_moved_to_recycle_bin = 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().id("cl2k0njfl04480822fxjq5nns");
    List<Vm> vms = moveVmToRecycleBin(client, where);
    System.out.print(vms);
  }
  public static List<Vm> moveVmToRecycleBin(ApiClient client, VmWhereInput where)
      throws ApiException {
    VmApi vmApi = new VmApi(client);
    List<WithTaskDeleteVm> withTaskVms = vmApi
        .moveVmToRecycleBin(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")
    vmInRecycleBin, err := moveVmToRecycleBin(client, "vmId")
    if err != nil {
        panic(err.Error())
    }
    // fmt.Print(vms)
    print(vmInRecycleBin)
}
func moveVmToRecycleBin(
    client *apiclient.Cloudtower,
    vmId string) (*models.VM, error) {
    moveParams := vm.NewMoveVMToRecycleBinParams()
    moveParams.RequestBody = &models.VMOperateParams{
        Where: &models.VMWhereInput{
            ID: pointy.String(vmId),
        },
    }
    moveRes, err := client.VM.MoveVMToRecycleBin(moveParams)
    if err != nil {
        return nil, err
    }
    withTaskVm := moveRes.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
}
Recover from recycle bin
- 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_delete_vms = vm_api.recover_vm_from_recycle_bin({
    "where": {
        "id_in": ["vm_id_1", "vm_id_2"]
    }
})
tasks = [with_task_delete_vm.task_id for with_task_delete_vm in with_task_delete_vms]
ids = [with_task_vm.data.id for with_task_vm in with_task_vms]
wait_tasks(tasks, api_client)
recovered_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().id("cl2k0njfl04480822fxjq5nns");
    List<Vm> vms = moveVmToRecycleBin(client, where);
    System.out.print(vms);
  }
  public static List<Vm> moveVmToRecycleBin(ApiClient client, VmWhereInput where)
      throws ApiException {
    VmApi vmApi = new VmApi(client);
    List<WithTaskDeleteVm> withTaskVms = vmApi
        .recoverVmFromRecycleBin(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")
    vmInRecycleBin, err := recoverVmFromRecycleBin(client, "vmId")
    if err != nil {
        panic(err.Error())
    }
    // fmt.Print(vms)
    print(vmInRecycleBin)
}
func recoverVmFromRecycleBin(
    client *apiclient.Cloudtower,
    vmId string) (*models.VM, error) {
    recoverParams := vm.NewRecoverVMFromRecycleBinParams()
    recoverParams.RequestBody = &models.VMOperateParams{
        Where: &models.VMWhereInput{
            ID: pointy.String(vmId),
        },
    }
    recoverRes, err := client.VM.RecoverVMFromRecycleBin(recoverParams)
    if err != nil {
        return nil, err
    }
    withTaskVm := recoverRes.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
}
Delete permanently
- 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_delete_vms = vm_api.delete_vm({
    "where": {
        "id_in": ["vm_id_1", "vm_id_2"]
    }
})
tasks = [with_task_delete_vm.task_id for with_task_delete_vm in with_task_delete_vms]
wait_tasks(tasks, 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");
    deleteVm(client, where);
  }
  public static void deleteVm(ApiClient client, VmWhereInput where)
      throws ApiException {
    VmApi vmApi = new VmApi(client);
    List<WithTaskDeleteVm> withTaskVms = vmApi
        .deleteVm(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);
  }
}
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")
    err := deleteVm(client, "vmId")
    if err != nil {
        panic(err.Error())
    }
}
func deleteVm(
    client *apiclient.Cloudtower,
    vmId string) error {
    deleteParams := vm.NewDeleteVMParams()
    deleteParams.RequestBody = &models.VMOperateParams{
        Where: &models.VMWhereInput{
            ID: pointy.String(vmId),
        },
    }
    deleteRes, err := client.VM.DeleteVM(deleteParams)
    if err != nil {
        return err
    }
    withTaskVm := deleteRes.Payload[0]
    err = utils.WaitTask(client, withTaskVm.TaskID)
    if err != nil {
        return err
    }
    return nil
}