Saturday, July 11, 2015

Jclouds - How to terminate an instance without node-id

If you don't have the node-id of the instance , you can terminate an instance using its instance-id. Following code sample will destroy the instance using node-id, if node-id is available. If node-id is not available, it will use instance-id to destroy the node. Similarly you can terminate an instance using private IP, public IP and so on.

// if node-id is available
if (nodeId != null && !nodeId.isEmpty()) {
       jcloudComputeService.destroyNode(nodeId);
} else if (instanceId != null && !instanceId.isEmpty()) {
 // if instance-id is available
 jcloudComputeService.destroyNodesMatching(new Predicate<NodeMetadata>() {
  @Override
  public boolean apply(NodeMetadata input) {
   // if node id contains instance id
   if (input.getId() != null && !input.getId().isEmpty() 
     && instanceId != null && !instanceId.isEmpty() 
     && input.getId().contains(instanceId)) {
    }
    return true;
   }
   return false;
  }
 });
} else {
   // use other attributes like private IP or public IP to terminate
}

No comments:

Post a Comment