Skip to content

Commit

Permalink
Merge pull request #124 from sumitAgrawal007/master
Browse files Browse the repository at this point in the history
Updating Bindings, Samples, POM and ReadMe for 7.0U2 Release
  • Loading branch information
sumitAgrawal007 authored Mar 18, 2021
2 parents 895be19 + 166e4a8 commit 041232d
Show file tree
Hide file tree
Showing 11 changed files with 361 additions and 19 deletions.
33 changes: 17 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ JDK 1.8.

## Supported OnPrem vCenter Releases:

vCenter 6.0, 6.5, 6.7, 6.7U1, 6.7U2, 6.7U3, 7.0, 7.0U1
vCenter 6.0, 6.5, 6.7, 6.7U1, 6.7U2, 6.7U3, 7.0, 7.0U1, 7.0U2

Please refer to the notes in each sample for detailed compatibility information.

Expand Down Expand Up @@ -88,9 +88,9 @@ When running the samples, parameters can be provided either on the command line,

Use a command like the following to display usage information for a particular sample.
```` bash
$java -ea -cp target/vsphere-samples-7.0.1.0.jar vmware.samples.vcenter.vm.list.ListVMs
$java -ea -cp target/vsphere-samples-7.0.2.0.jar vmware.samples.vcenter.vm.list.ListVMs

java -cp target/vsphere-samples-7.0.1.0.jar vmware.samples.vcenter.vm.list.ListVMs [--config-file <CONFIGURATION FILE>]
java -cp target/vsphere-samples-7.0.2.0.jar vmware.samples.vcenter.vm.list.ListVMs [--config-file <CONFIGURATION FILE>]
--server <SERVER> --username <USERNAME> --password <PASSWORD> --cluster <CLUSTER> [--truststorepath <ABSOLUTE PATH OF JAVA TRUSTSTORE FILE>]
[--truststorepassword <JAVA TRUSTSTORE PASSWORD>] [--cleardata] [--skip-server-verification]

Expand All @@ -114,17 +114,17 @@ Sample Options:

Use a command like the following to run a sample using only command line parameters:
```` bash
$java -ea -cp target/vsphere-samples-7.0.1.0.jar vmware.samples.vcenter.vm.list.ListVMs --server servername --username [email protected] --password password --skip-server-verification
$java -ea -cp target/vsphere-samples-7.0.2.0.jar vmware.samples.vcenter.vm.list.ListVMs --server servername --username [email protected] --password password --skip-server-verification
````

Use a command like the following to run a sample using only a configuration file:
```` bash
$java -ea -cp target/vsphere-samples-7.0.1.0.jar vmware.samples.vcenter.vm.list.ListVMs --config-file sample.properties
$java -ea -cp target/vsphere-samples-7.0.2.0.jar vmware.samples.vcenter.vm.list.ListVMs --config-file sample.properties
````

Use the following command to run the sample using a combination of configuration file and command line parameters:
```` bash
$java -ea -cp target/vsphere-samples-7.0.1.0.jar vmware.samples.vcenter.vm.list.ListVMs --config-file sample.properties --server servername
$java -ea -cp target/vsphere-samples-7.0.2.0.jar vmware.samples.vcenter.vm.list.ListVMs --config-file sample.properties --server servername
````

### Importing the samples to eclipse
Expand Down Expand Up @@ -152,20 +152,21 @@ This will import a basic sample class to your package, which you can then custom
### vSphere API Documentation
* [VMware vSphere REST API Reference documentation](https://developer.vmware.com/docs/vsphere-automation/latest/).

* [VMware vSphere APIs 7.0.1.0 (latest version)](https://vmware.github.io/vsphere-automation-sdk-java/vsphere/7.0.1.0/vsphereautomation-client-sdk/index.html).
* [VMware vSphere JAVA APIs 7.0.2.0 (latest version)](https://vmware.github.io/vsphere-automation-sdk-java/vsphere/7.0.2.0/vsphereautomation-client-sdk/index.html).

* [VMware Cloud on AWS vSphere (latest version)](https://vmware.github.io/vsphere-automation-sdk-java/vsphere/cloud/vsphereautomation-client-sdk/index.html).

* Previous releases: [7.0.0.1](https://vmware.github.io/vsphere-automation-sdk-java/vsphere/7.0.0.1/vsphereautomation-client-sdk/index.html),
[6.7.3](https://vmware.github.io/vsphere-automation-sdk-java/vsphere/6.7.3/vsphereautomation-client-sdk/index.html)
* Previous releases: [7.0.1.0](https://vmware.github.io/vsphere-automation-sdk-java/vsphere/7.0.1.0/vsphereautomation-client-sdk/index.html),
[7.0.0.1](https://vmware.github.io/vsphere-automation-sdk-java/vsphere/7.0.0.1/vsphereautomation-client-sdk/index.html)

### VMware Cloud on AWS API Documentation

* [VMware Cloud on AWS Console APIs](https://vmware.github.io/vsphere-automation-sdk-java/vmc/index.html).
* [VMware Cloud on AWS REST APIs](http://developers.eng.vmware.com/docs/vmc/latest/).

* [VMware Cloud on AWS JAVA APIs](https://vmware.github.io/vsphere-automation-sdk-java/vmc/index.html).

* [VMware Cloud on AWS Disaster Recovery as a Service (DRaaS) APIs](https://vmware.github.io/vsphere-automation-sdk-java/vmc-draas/index.html).
* [VMware Cloud on AWS Disaster Recovery as a Service (DRaaS) JAVA APIs](https://vmware.github.io/vsphere-automation-sdk-java/vmc-draas/index.html).

### NSX API Documentation
* [VMware NSX-T Data Center REST API](https://code.vmware.com/apis/976)

* [VMware NSX-T Manager APIs (for on-prem customers)](https://vmware.github.io/vsphere-automation-sdk-java/nsx/nsx/index.html).

Expand Down Expand Up @@ -238,10 +239,10 @@ Members:
Items added to the repository, including items from the Board members, require 2 votes from the board members before being added to the repository. The approving members will have ideally downloaded and tested the item. When two “Approved for Merge” comments are added from board members, the pull can then be committed to the repository.
## VMware Resources
* [vSphere Automation SDK Overview](http://pubs.vmware.com/vsphere-65/index.jsp#com.vmware.vapi.progguide.doc/GUID-AF73991C-FC1C-47DF-8362-184B6544CFDE.html)
* [VMware Developers Site](http://developers.eng.vmware.com/)
* [vSphere Automation SDK Overview](https://code.vmware.com/web/sdk/7.0/vsphere-automation-java)
* [VMware Code](https://code.vmware.com/home)
* [VMware Developer Community](https://communities.vmware.com/community/vmtn/developer)
* VMware vSphere [Java API Reference documentation](https://vmware.github.io/vsphere-automation-sdk-java/vsphere/7.0.1.0/vsphereautomation-client-sdk/index.html).
* VMware vSphere [Java API Reference documentation](https://vmware.github.io/vsphere-automation-sdk-java/vsphere/7.0.2.0/vsphereautomation-client-sdk/index.html).
* [VMware Java forum](https://code.vmware.com/forums/7508/vsphere-automation-sdk-for-java)
Binary file modified lib/vapi-vmc-sdk-2.19.0.jar
Binary file not shown.
Binary file modified lib/vsphereautomation-client-sdk-3.5.0-javadoc.jar
Binary file not shown.
Binary file modified lib/vsphereautomation-client-sdk-3.5.0.jar
Binary file not shown.
Binary file modified lib/vsphereautomation-lookupservice-1.0.0-javadoc.jar
Binary file not shown.
Binary file modified lib/vsphereautomation-lookupservice-1.0.0.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.vmware</groupId>
<artifactId>vsphere-samples-nodeps</artifactId>
<version>7.0.1.0</version>
<version>7.0.2.0</version>
<packaging>jar</packaging>

<name>samples</name>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/vmware/samples/common/ParametersHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public int compare(Option option1, Option option2) {

});
formatter.printHelp(150,
"\njava -cp target/vsphere-samples-7.0.1.0.jar " + sampleName,
"\njava -cp target/vsphere-samples-7.0.2.0.jar " + sampleName,
"\nSample Options:",
getOptions(new ArrayList<Option>(this.optionMap.keySet())),
"",
Expand Down
42 changes: 41 additions & 1 deletion src/main/java/vmware/samples/vcenter/helpers/VmHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import com.vmware.vapi.bindings.StubFactory;
import com.vmware.vcenter.VM;
import com.vmware.vcenter.VMTypes;
import com.vmware.vcenter.vm.guest.Identity;
import com.vmware.vcenter.vm.guest.IdentityTypes;
import com.vmware.vapi.std.errors.ServiceUnavailable;

public class VmHelper {
/**
Expand Down Expand Up @@ -46,4 +49,41 @@ public static String getVM(

return vmList.get(0).getVm();
}
}

/**
* it will wait till guest vm become powered on and information about the same is available.
* @param identityService : guest vm identity service object
* @param vmId : id of the virtual machine
* @param timeout: waiting time period to guest info
* @throws Exception
*/
public static void waitForGuestInfoReady(Identity identityService, String vmId, int timeout) throws Exception {
System.out.println("Waiting for guest info to be ready.");
int start=(int) System.currentTimeMillis()/1000;
timeout = start + timeout;
IdentityTypes.Info result;
while(timeout > (int) System.currentTimeMillis()/1000) {
System.out.println("Waiting for guest info to be ready");
Thread.sleep(1000);
try{
result=identityService.get(vmId);
break;
}
catch(ServiceUnavailable e) {
System.out.println("Got ServiceUnavailable waiting for guest info");
continue;
}
catch(Exception e) {
System.out.println("Unexpected exception %s waiting for guest info");
throw new Exception(e.getMessage());
}
}
if((int) System.currentTimeMillis()/1000 >= timeout) {
throw new Exception("Timed out waiting for guest info to be available.Be sure the VM has VMware Tools");
}
else {
int duration=((int) System.currentTimeMillis()/1000)-start;
System.out.println("Took "+duration+" seconds for guest info to be available");
}
}
}
127 changes: 127 additions & 0 deletions src/main/java/vmware/samples/vcenter/vm/guest/GuestInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* *******************************************************
* Copyright VMware, Inc. 2021. All Rights Reserved.
* SPDX-License-Identifier: MIT
* *******************************************************
*
* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN,
* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED
* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY,
* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
*/
package vmware.samples.vcenter.vm.guest;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.commons.cli.Option;

import com.vmware.vcenter.vm.Power;
import com.vmware.vcenter.vm.PowerTypes;
import com.vmware.vcenter.vm.guest.Identity;
import com.vmware.vcenter.vm.guest.IdentityTypes;
import com.vmware.vcenter.vm.guest.LocalFilesystem;
import com.vmware.vcenter.vm.guest.LocalFilesystemTypes;

import vmware.samples.common.SamplesAbstractBase;
import vmware.samples.vcenter.helpers.VmHelper;

/**
Demonstrates the virtual machine guest information.
Sample Prerequisites:
The sample needs an existing VM with VMware Tools.
**/
public class GuestInfo extends SamplesAbstractBase{

private String vmName;
private String vmId;
private Power vmPowerService;
private Identity identityService;
private LocalFilesystem filesystemService;
@Override
protected void parseArgs(String[] args) {
// Parse the command line options or use config file
Option vmNameOption = Option.builder()
.longOpt("vmname")
.desc("Name of the VM on which the power operations"
+ " would be performed")
.required(true)
.hasArg()
.argName("VM NAME")
.build();
List<Option> optionList = Collections.singletonList(vmNameOption);
super.parseArgs(optionList, args);
this.vmName = (String) parsedOptions.get("vmname");
}

@Override
protected void setup() throws Exception {
this.vmPowerService = this.vapiAuthHelper.getStubFactory().createStub(
Power.class, this.sessionStubConfig);
identityService=this.vapiAuthHelper.getStubFactory().createStub(Identity.class, sessionStubConfig);
filesystemService=this.vapiAuthHelper.getStubFactory().createStub(LocalFilesystem.class, sessionStubConfig);
}
@Override
protected void run() throws Exception {
this.vmId = VmHelper.getVM(vapiAuthHelper.getStubFactory(),
sessionStubConfig,
vmName);
if(vmId == null) {
throw new Exception("Sample requires an existing vm with name "+this.vmName+"Please create the vm first");
}
else {
System.out.println("Using VM "+this.vmName+" "+this.vmId+" for Guest Info Sample");
}

System.out.println("# Example: Get current vm power state");
PowerTypes.Info powerInfo = this.vmPowerService.get(vmId);
if (!(PowerTypes.State.POWERED_ON.equals(powerInfo.getState())))
{
System.out.println("Powering on VM.");
this.vmPowerService.start(vmId);
}
VmHelper.waitForGuestInfoReady(identityService, vmId, 600);
IdentityTypes.Info info=identityService.get(vmId);
System.out.println("vm.guest.Identity.get({"+vmName+"})");
System.out.println("Identity: {"+info.toString()+"}");

Map<String, LocalFilesystemTypes.Info> filesystemInfo=filesystemService.get(vmId);
System.out.println("vm.guest.Identity.get({"+vmName+"})");
System.out.println("LocalFilesystem: {"+filesystemInfo.toString()+"}");
}

@Override
protected void cleanup() throws Exception {

this.vmPowerService.stop(vmId);
System.out.println("vm.power->stop()");
PowerTypes.Info powerInfo = this.vmPowerService.get(vmId);

//Power off the vm if it is on
if (PowerTypes.State.POWERED_OFF.equals(powerInfo.getState()))
{
System.out.println("VM is powered off" );
}
else {
System.out.println("vm.Power Warning: Could not power off vm" );
}
}

public static void main(String[] args) throws Exception {
/*
* Execute the sample using the command line arguments or parameters
* from the configuration file. This executes the following steps:
* 1. Parse the arguments required by the sample
* 2. Login to the server
* 3. Setup any resources required by the sample run
* 4. Run the sample
* 5. Cleanup any data created by the sample run, if cleanup=true
* 6. Logout of the server
*/
new GuestInfo().execute(args);
}

}
Loading

0 comments on commit 041232d

Please sign in to comment.