Skip to content

Commit

Permalink
Merge pull request #103 from sumitAgrawal007/master
Browse files Browse the repository at this point in the history
Binding and samples and ReadMe update for vsphere  7.0 GA
  • Loading branch information
sumitAgrawal007 authored Apr 14, 2020
2 parents 77e3c46 + c91be2d commit 3d66577
Show file tree
Hide file tree
Showing 60 changed files with 3,722 additions and 1,146 deletions.
25 changes: 13 additions & 12 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
vCenter 6.0, 6.5, 6.7, 6.7U1, 6.7U2, 6.7U3, 7.0

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.0.jar vmware.samples.vcenter.vm.list.ListVMs
$java -ea -cp target/vsphere-samples-7.0.0.1.jar vmware.samples.vcenter.vm.list.ListVMs

java -cp target/vsphere-samples-7.0.0.jar vmware.samples.vcenter.vm.list.ListVMs [--config-file <CONFIGURATION FILE>]
java -cp target/vsphere-samples-7.0.0.1.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.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.0.1.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.0.jar vmware.samples.vcenter.vm.list.ListVMs --config-file sample.properties
$java -ea -cp target/vsphere-samples-7.0.0.1.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.0.jar vmware.samples.vcenter.vm.list.ListVMs --config-file sample.properties --server servername
$java -ea -cp target/vsphere-samples-7.0.0.1.jar vmware.samples.vcenter.vm.list.ListVMs --config-file sample.properties --server servername
````

### Importing the samples to eclipse
Expand All @@ -150,12 +150,12 @@ This will import a basic sample class to your package, which you can then custom
## API Documentation

### vSphere API Documentation
* [VMware vSphere APIs 7.0.0.1 (latest version)](https://vmware.github.io/vsphere-automation-sdk-java/vsphere/7.0.0.1/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).

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

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

### VMware Cloud on AWS API Documentation

Expand Down Expand Up @@ -229,8 +229,8 @@ Sample Exchange can be allowed to access your GitHub resources, by way of a link
Board members are volunteers from the SDK community and VMware staff members, board members are not held responsible for any issues which may occur from running of samples from this repository.
Members:
* Kunal Singh (VMware)
* Jobin Jeorge (VMware)
* Sumit Agrawal (VMware)
* Shyla Srinivas (VMware)
### Approval of Additions
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.
Expand All @@ -240,5 +240,6 @@ Items added to the repository, including items from the Board members, require 2
* [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 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/6.7.3/vsphereautomation-client-sdk/index.html).
* VMware vSphere [Java API Reference documentation](https://vmware.github.io/vsphere-automation-sdk-java/vsphere/7.0.0.1/vsphereautomation-client-sdk/index.html).
* [VMware Java forum](https://code.vmware.com/forums/7508/vsphere-automation-sdk-for-java)
Binary file added lib/oidc-oauth2-sdk-0.0.1.jar
Binary file not shown.
Binary file removed lib/vapi-authentication-2.14.0-javadoc.jar
Binary file not shown.
Binary file added lib/vapi-authentication-2.15.0-javadoc.jar
Binary file not shown.
Binary file not shown.
Binary file removed lib/vapi-runtime-2.14.0-javadoc.jar
Binary file not shown.
Binary file added lib/vapi-runtime-2.15.0-javadoc.jar
Binary file not shown.
Binary file not shown.
Binary file removed lib/vapi-samltoken-2.14.0-javadoc.jar
Binary file not shown.
Binary file added lib/vapi-samltoken-2.15.0-javadoc.jar
Binary file not shown.
Binary file not shown.
Binary file removed lib/vapi-vmc-sdk-2.14.0.jar
Binary file not shown.
Binary file added lib/vapi-vmc-sdk-2.15.0.jar
Binary file not shown.
Binary file not shown.
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.
26 changes: 23 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

<groupId>com.vmware</groupId>
<artifactId>vsphere-samples-nodeps</artifactId>
<version>7.0.0</version>
<version>7.0.0.1</version>
<packaging>jar</packaging>

<name>samples</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<vapi.version>2.14.0</vapi.version>
<vapi.version>2.15.0</vapi.version>
<slf4j.version>1.7.12</slf4j.version>
<httpclient.version>4.5.10</httpclient.version>
<httpasyncclient.version>4.1.4</httpasyncclient.version>
Expand All @@ -24,12 +24,13 @@
<commons-beanutils.version>1.9.4</commons-beanutils.version>
<lookupservice.version>1.0.0</lookupservice.version>
<vspherewssdk.version>6.7.3</vspherewssdk.version>
<vsphereautomationsdk.version>3.2.0</vsphereautomationsdk.version>
<vsphereautomationsdk.version>3.3.0</vsphereautomationsdk.version>
<vmc.version>1.24.0</vmc.version>
<nsxpolicysdk.version>2.5.1.0.1.15419398</nsxpolicysdk.version>
<nsxvmcawsint.version>2.5.1.0.1.15419398</nsxvmcawsint.version>
<nsxvmcsdkcommon.version>2.5.1.0.1.15419398</nsxvmcsdkcommon.version>
<vmc.draas.version>1.9.0</vmc.draas.version>
<oidc-oauth2.version>0.0.1</oidc-oauth2.version>
</properties>
<build>
<plugins>
Expand All @@ -51,6 +52,20 @@
<artifactId>maven-install-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>install-oidc-oauth2-sdk</id>
<phase>initialize</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<packaging>jar</packaging>
<groupId>com.vmware.vapi</groupId>
<artifactId>oidc-oauth2-sdk</artifactId>
<version>${oidc-oauth2.version}</version>
<file>./lib/oidc-oauth2-sdk-${oidc-oauth2.version}.jar</file>
</configuration>
</execution>
<execution>
<id>install-draas-bindings</id>
<phase>initialize</phase>
Expand Down Expand Up @@ -296,6 +311,11 @@
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.vmware.vapi</groupId>
<artifactId>oidc-oauth2-sdk</artifactId>
<version>${oidc-oauth2.version}</version>
</dependency>
<dependency>
<groupId>com.vmware.vmc</groupId>
<artifactId>vmc-draas-bindings</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package vmware.samples.common;

public class AuthorizationConstants {
public static final String CLIENT_ID="clientId";
public static final String CLIENT_SECRET="clientSecret";
public static final String REDIRECT_URI="redirect_uri";
public static final String REDIRECT_URIS="redirectUris";
}
63 changes: 63 additions & 0 deletions src/main/java/vmware/samples/common/AuthorizationUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* *******************************************************
* Copyright VMware, Inc. 2019. 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.common;

import java.util.Map;

import com.vmware.authorization.AuthorizationRequestExecutor;
import com.vmware.authorization.Constants;
import com.vmware.authorization.IDPFactory;
import com.vmware.authorization.IdentityProviderType;
import com.vmware.authorization.grant.AuthorizationGrant;
import com.vmware.vapi.bindings.Service;
import com.vmware.vapi.bindings.StubConfiguration;
import com.vmware.vapi.protocol.HttpConfiguration;
import com.vmware.vapi.saml.SamlToken;

import vmware.samples.common.authentication.VapiAuthenticationHelper;
import vmware.samples.vcenter.identity.helpers.VcenterIdentityProvider;
import vmware.samples.vcenter.identity.helpers.VcenterTokenExchange;

public class AuthorizationUtil {

private AuthorizationUtil() {}

public static IdentityProviderType getDefaultIdentityProviderWithType(String vcenterServer,
boolean skipServerVerification) throws Exception {
VcenterIdentityProvider vip = new VcenterIdentityProvider();
IdentityProviderType identityType = vip.getDefaultIdentityProviderWithType(vcenterServer,
skipServerVerification);
return identityType;

}
public static <T extends Service> T getStub(Class<T> serviceClass, IdentityProviderType identityType,
AuthorizationGrant grantType, String vcenterServer) throws Exception {

AuthorizationRequestExecutor grantExecutor = IDPFactory.getAuthRequestExecutor(identityType.getType(),
grantType);
Map<String, String> map = grantExecutor.getAccessAndIdToken();
VcenterTokenExchange tokenSer = new VcenterTokenExchange();
SamlToken samlToken = tokenSer.getSAMLToken(map.get(Constants.ACCESS_TOKEN),
map.get(Constants.ID_TOKEN), vcenterServer);
//System.out.println("SAML Bearer Token :" + samlToken);
VapiAuthenticationHelper vapiHelper = new VapiAuthenticationHelper();
HttpConfiguration httpConfig = vapiHelper.buildHttpConfiguration(true);
StubConfiguration sessionStubConfig = vapiHelper
.loginBySamlBearerToken(vcenterServer, samlToken, httpConfig);

return vapiHelper.getStubFactory().createStub(serviceClass,
sessionStubConfig);

}
}

56 changes: 50 additions & 6 deletions src/main/java/vmware/samples/common/ParametersHelper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* *******************************************************
* Copyright VMware, Inc. 2016. All Rights Reserved.
* Copyright VMware, Inc. 2019. All Rights Reserved.
* SPDX-License-Identifier: MIT
* *******************************************************
*
Expand All @@ -12,6 +12,8 @@
*/
package vmware.samples.common;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
Expand All @@ -32,13 +34,19 @@
import org.apache.commons.configuration.ConversionException;
import org.apache.commons.configuration.PropertiesConfiguration;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;

/**
* Helper class for parsing command line options or reading the options from a
* config file
*/
public class ParametersHelper {
private Map<Option, Boolean> optionMap;
private Map<String, Object> parsedOptions;
private static final String CONFIG_FILE = "config-file";
private static final String OAUTH_APP_JSON = "oauth_app_json";

/**
* Adds the common and sample-specific options to the list of options to
Expand Down Expand Up @@ -72,7 +80,7 @@ public ParametersHelper(List<Option> optionList) {
*/
public Map<String, Object> parse(String[] args, String sampleName)
throws ParseException, ConfigurationException {
Map<String, Object> parsedOptions = new HashMap<String, Object>();
parsedOptions = new HashMap<String, Object>();
if (args.length == 0) {
printUsage(sampleName);
}
Expand All @@ -97,7 +105,7 @@ public Map<String, Object> parse(String[] args, String sampleName)
Iterator<Option> optionsIter = optionsToParseFromConfig.iterator();
if (cmd.hasOption(CONFIG_FILE)) {
// Read from configuration file first
Configuration config = new PropertiesConfiguration(cmd.getOptionValue(CONFIG_FILE));
Configuration config = new PropertiesConfiguration(cmd.getOptionValue(CONFIG_FILE));
while (optionsIter.hasNext()) {
Option option = optionsIter.next();
Object optionValue =
Expand All @@ -113,7 +121,8 @@ public Map<String, Object> parse(String[] args, String sampleName)
parsedOptions.put(option.getLongOpt(), optionValue);
}
}
} else {
}
else {
optionsToParseFromCmdLine =
new ArrayList<Option>(this.optionMap.keySet());
}
Expand Down Expand Up @@ -141,7 +150,6 @@ private Options getOptions(List<Option> optionList) {
}
return options;
}

private Object getOptionValueFromConfig(
Option option, Configuration config) {
Object optionValue = null;
Expand Down Expand Up @@ -188,11 +196,47 @@ public int compare(Option option1, Option option2) {

});
formatter.printHelp(150,
"\njava -cp target/vsphere-samples-7.0.0.0.jar " + sampleName,
"\njava -cp target/vsphere-samples-7.0.0.1.jar " + sampleName,
"\nSample Options:",
getOptions(new ArrayList<Option>(this.optionMap.keySet())),
"",
true);
System.exit(0);
}

public Map<String, Object> parseJsonConfig(String[] args) {
List<Option> optionsToParseFromConfig = new ArrayList<Option>();
Iterator<Option> optionIter = this.optionMap.keySet().iterator();
while(optionIter.hasNext()) {
Option option = (Option)optionIter.next().clone();
option.setRequired(false);
optionsToParseFromConfig.add(option);
}
CommandLineParser parser = new DefaultParser();
try {
CommandLine cmd = parser.parse(
getOptions(optionsToParseFromConfig), args);
if (cmd.hasOption(OAUTH_APP_JSON) && !(parsedOptions.
containsKey(AuthorizationConstants.CLIENT_SECRET))
&& !(parsedOptions.containsKey(AuthorizationConstants.CLIENT_ID))) {
JsonNode argsJsonMapper=new ObjectMapper().readTree(new File(cmd.getOptionValue(OAUTH_APP_JSON)));
parsedOptions.put(AuthorizationConstants.CLIENT_ID,
argsJsonMapper.get(AuthorizationConstants.CLIENT_ID).asText());
parsedOptions.put(AuthorizationConstants.CLIENT_SECRET,
argsJsonMapper.get(AuthorizationConstants.CLIENT_SECRET).asText());
if(argsJsonMapper.has(AuthorizationConstants.REDIRECT_URIS)) {
ArrayNode arrayNode=(ArrayNode)argsJsonMapper.get(AuthorizationConstants.REDIRECT_URIS);
parsedOptions.put(AuthorizationConstants.REDIRECT_URI, arrayNode.get(0).asText());
}
}
}catch (IOException e) {
System.out.println(e.getMessage());
System.exit(0);
}
catch (ParseException e) {
System.out.println(e.getMessage());
System.exit(0);
}
return parsedOptions;
}
}
10 changes: 5 additions & 5 deletions src/main/java/vmware/samples/common/SslUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ public X509Certificate[] getAcceptedIssuers() {
public static void trustAllHttpsCertificates() {
try {
// Create the trust manager.
javax.net.ssl.TrustManager[] trustAllCerts =
new javax.net.ssl.TrustManager[1];
javax.net.ssl.TrustManager tm = new TrustAllTrustManager();
TrustManager[] trustAllCerts =
new TrustManager[1];
TrustManager tm = new TrustAllTrustManager();
trustAllCerts[0] = tm;

// Create the SSL context
javax.net.ssl.SSLContext sc =
javax.net.ssl.SSLContext.getInstance("SSL");
SSLContext sc =
SSLContext.getInstance("SSL");

// Create the session context
javax.net.ssl.SSLSessionContext sslsc =
Expand Down
Loading

0 comments on commit 3d66577

Please sign in to comment.