Skip to content

VM Power Operations and Deletion.

Nick Kasprzak edited this page Apr 1, 2019 · 2 revisions

Introduction

In this example app we will show how to power on and off a VM as well as how to delete a vApp and a VM.

To start either git clone the project and cd into the power-off-vm example or follow the steps from the Home page of the Wiki and build a project from scratch and follow along from here.

VM power operations

Powering on and off a VM is as easy as a few lines of code.

To do this we need the VM uuid of the VM we want to power on/off.

So now that we have the vApp we created we can find the uuid of it's VM.

How do you find the uuid of a VM? Well if you know the uuid of the vApp that it's in then it is fairly simple. You can just use the VappResource endpoint getVmsForVapp and look through the list of VMs you get back until you find the one you need.

This brings up another question though, how do you find the vApp uuid of the vApp that has your VM inside it?

Let's say you have the name of the vApp. Since vApp names are unique we can look through all the vApps and get the uuid of the one we want by checking its name.

Here's how to do that:

    String vappName = "Name of vApp to get";
    String vappUuid = "";
    UserInventoryResponse userInventory =
            userResource.getInventory(USERNAME, null);
    for (UserCompanyInventoryResponse companyInventory : userInventory
            .getInventory()) {
      if (companyInventory.getEntities() != null
              && !companyInventory.getEntities().isEmpty()) {
        List<UserInventoryEntityResponse> vApps =
                companyInventory.getEntities().get(IamEntityType.IAAS_VAPP);
        for (UserInventoryEntityResponse vApp : vApps) {
          if (vApp.getName().equals(vappName)) {
            vappUuid = vApp.getUuid();
          }
          System.out.println(String.format("vApp name: %s, UUID: %s",
                  vApp.getName(), vApp.getUuid()));
        }
      }
    }

What is exactly happening in the above code? Well we are getting the user's inventory and then searching through all the vApps and finding the uuid of the one whose name we put in the vappName variable.

After finding the uuid of the vApp we want then we can use that to get the uuid of the VM we need by using the function getVmsForVapp.

We can do that we the following code:

VappResource vappResource = apiClient.getVappResource();
VmListResponse vms = vappResource.getVmsForVapp(vappUuid);
VmResponse vm = vms.getData().get(0);

Now that we have the VmResponse object of the scratch VM we just made we can now do power operations on it.

Power operations with iland's Java SDK are as easy as this:

TaskResponse startVm = vmResource.powerOnVm(vm.getUuid(), false);
TaskResponse stopVm = vmResource.powerOffVm(vm.getUuid());

Remember to declare a TaskResponse when calling powerOn and powerOff so you can track when the task is synced and thus the power state of your VM.

Deleting VMs and vApps

Deleting a vApp or a VM is easy. All you need is the UUID of the resource you want to delete and the permissions to delete it.

Here is an example of how to delete a vApp/VM.

vmResource.deleteVm(vmUuid);
vappResource.deleteVapp(vappUuid);