1.0.1

VMware, Inc. 17
Chapter 2 Understanding the Workflow
<Hierarchy id="38">
<Entities>
<Entity id="171">
<VCenterServers>
<VCenterServer id="145">
<Entities>
<!-- Either id or MoId needs to be there. id takes
precedance over MoId-->
<Entity id="630">
<!-- <MoId>host-2460</MoId> -->
</Entity>
</Entities>
</VCenterServer>
</VCenterServers>
</Entity>
</Entities>
</Hierarchy>
</Hierarchies>
</Request>
ThefollowingisasampleprogramthatcallstheAddanEntityAPI.Makesurethatyou:
UpdatetherequestXMLforLoginAPIwithrequiredvaluesandrunthelogin.javasamplecode.
RunAddVCenterServer.javaandreadthevcIdfromtheresponse
ReadthevcEntityIdwhichyouneedtoaddundervCenterChargeBackhierarchyentity,fromthe
responseofsampleGetVcHierarchyMethod()
AddavCenterChargeBackHierarchyandfromtheresponse,readthehierarchyIdandtheCBEntity
IdunderwhichthevcEntityistobeadded
UpdatetheHierarchyid,cbEntityid,VCenterServeridandvcEntityidintherequestXMLfile.
/**
* This method is to add a new vCenter-Server entity under
* vCenter-Chargeback Hierarchy entity
*
* @param requestFilePath
* @param baseURL
* @param hierachyId
* @param CBEntityId
* @param clientVersion
* @throws IOException
* @throws JDOMException
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
* @throws HttpException
*/
@SuppressWarnings("deprecation")
public static void sampleAddNewVCenterServerEntity(String requestFilePath, String
baseURL, int hierachyId, int CBEntityId,String clientVersion) throws
IOException,
JDOMException, NoSuchAlgorithmException, KeyManagementException, HttpException {
PostMethod post = null;
NameValuePair[] parameters = {new NameValuePair("version", clientVersion)};
Document requestDocument = CommonUtil.getXMLDocument(requestFilePath);
String bodyString = CommonUtil.xmlAsString(requestDocument);
Protocol.registerProtocol("https", new Protocol("https", (ProtocolSocketFactory) new
FakeSSLCertificateSocketFactory(), 443));
String uri = "https://" + baseURL + "/vCenter-CB/api/hierarchy/" + hierachyId +
"/entity/" + CBEntityId;
try {
post = new PostMethod(uri);
post.setQueryString(parameters);
post.setRequestBody(bodyString);
client.executeMethod(post);
System.out.println(post.getResponseBodyAsString());