«

»

Aug 17 2014

Using Nutanix Java SDK to Create Protection Domains and Add/Remove VMs

In a previous article I demonstrated how to use the Nutanix Java SDK (here) and provided the URL for the Nutanix Code Repository (here). In SampleRestJavaClient I have been implementing and encapsulating methods that make my life easier when automating Nutanix with Java.

I previously included some initial methods such as getVMs, getDisks, getHosts, getContainers, getManagementServers and getProtectionDomains. Today I have upstreamed couple important methods: getAlerts, addProtectionDomainaddVMstoProtectionDomainremoveVmsFromProtectionDomain.

  • addProtectionDomain – add a new protection domain
  • addVMstoProtectionDomain – add virtual machine(s) into an existing protection domain
  • removeVmsFromProtectionDomain – remove virtual machine(s) from a protection domain

 
App developers would consider automate the creation of a protection domain dedicated to the application with strict backup and replication SLA requirements. As part of the workflow addProtectionDomain and addProtectionDomain methods would be required. After adding VMs to the protection domain the application or script need to define corncobs and replication configuration. I’ll explain the last step in a next blog post.

[css lang=”plain” autolinks=”false” classname=”myclass” collapse=”false” firstline=”1″ padlinenumbers=”false” gutter=”true” smarttabs=”true” toolbar=”true” language=”true”]
/**
* Method addProtectionDomain

* @param protectionDomainName
* @param connMgr
* @throws BackupAndDrAdministrationException
*/
public void addProtectionDomain(String protectionDomainName,
ConnectionsManager connMgr)
throws BackupAndDrAdministrationException {
final BackupAndDrAdministration bcdr = connMgr
.getBackupAndDrAdministration();
bcdr.addProtectionDomain(PrimitiveDTO.wrap(protectionDomainName));
}

/**
* Method addVMstoProtectionDomain

* @param vMs
* @param protectionDomain
* @param setAppConsistentSnapshots
* @param setConsistencyGroupName
* @param setIgnoreDupOrMissingVms
* @param connMgr
* @throws BackupAndDrAdministrationException
*/
public void addVMstoProtectionDomain(String vMs[], String protectionDomain,
Boolean setAppConsistentSnapshots, String setConsistencyGroupName,
Boolean setIgnoreDupOrMissingVms, ConnectionsManager connMgr)
throws BackupAndDrAdministrationException {
final List<String> list = Arrays.asList(vMs);
final AddVMsToPdRequestDTO vMsToPd = new AddVMsToPdRequestDTO();
vMsToPd.setNames(list);
vMsToPd.setAppConsistentSnapshots(setAppConsistentSnapshots);
vMsToPd.setConsistencyGroupName(setConsistencyGroupName);
vMsToPd.setIgnoreDupOrMissingVms(setIgnoreDupOrMissingVms);
final com.nutanix.prism.services.dr.BackupAndDrAdministration bcdr = connMgr
.getBackupAndDrAdministration();
bcdr.addVmsByNamesToProtectionDomain(protectionDomain, vMsToPd);
}

/**
* Method removeVmsFromProtectionDomain

* @param vMs
* @param protectionDomain
* @param connMgr
* @throws BackupAndDrAdministrationException
*/
public void removeVmsFromProtectionDomain(String vMs[],
String protectionDomain, ConnectionsManager connMgr)
throws BackupAndDrAdministrationException {
final List<String> list = Arrays.asList(vMs);
final com.nutanix.prism.services.dr.BackupAndDrAdministration bcdr = connMgr
.getBackupAndDrAdministration();
bcdr.removeVmsFromProtectionDomain(protectionDomain, list);
}
[/css]

Please note that the code published in this blog post has also been uploaded to the SampleRestJavaClient repository in GitHub. To execute the methods:

[css lang=”plain” autolinks=”false” classname=”myclass” collapse=”false” firstline=”1″ padlinenumbers=”false” gutter=”true” smarttabs=”true” toolbar=”true” language=”true”]
/*
* Stablish connection with PRISM API
*/
final ConnectionsManager connMgr = ConnectionsManager
.getInstance();
connMgr.setUSERNAME("admin");
connMgr.setPASSWORD("admin");
connMgr.setSERVER("10.20.18.10");

/*
* Add a protection domain
*/
connMgr.addProtectionDomain("ProtectionDomainName", connMgr);

/*
* Add VMs to Protection Domain
*/
String VMs[] = new String[] { "vmName" };
connMgr.addVMstoProtectionDomain(VMs, "protectionDomainName", false, null, false,
connMgr);

/*
* Remove VMs from Protection Domain
*/
String VMs[] = new String[] { "vmName" };
connMgr.removeVmsFromProtectionDomain(VMs, "protectionDomainName", connMgr);
[/css]

This article was first published by Andre Leibovici (@andreleibovici) at myvirtualcloud.net

Leave a Reply