3737import javax .crypto .Mac ;
3838import javax .crypto .spec .SecretKeySpec ;
3939
40- import com .couchbase .client .core .deps .io .netty .handler .ssl .util .InsecureTrustManagerFactory ;
4140import com .couchbase .client .core .env .IoConfig ;
4241import com .couchbase .client .core .env .SecurityConfig ;
4342import com .couchbase .client .java .Bucket ;
5352
5453/**
5554 * Sample code for connecting to Capella through both the control-plane and the data-plane. An Access Key and a Secret
56- * Key are required and a bucket named "my_bucket" on the 'last' cluster.
55+ * Key are required and a bucket named "my_bucket" on the 'last' cluster. <br>
56+ * 1) Create a cluster that has data, index and query nodes. <br>
57+ * 2) Cluster -> Connectivity : allow your client ip address (or all ip address 0/0.0.0.0)<br>
58+ * 3) Create a user "user" in the cluster with password "Couch0base!" and Read/Write access to all buckets <br>
59+ * 4) Create a bucket named "my_bucket" <br>
60+ * 5) Get your access key from API Keys. The secret key is available only when the key is generated. If you have not
61+ * saved it, then generate a new key and save the secret key. <br>
5762 */
5863public class CapellaConnectSample {
5964
60- static final String cbc_access_key = "3gcpgyTBzOetdETYxOAtmLYBe3f9ZSVN" ;
61- static final String cbc_secret_key = "PWiACuJIZUlv0fCZaIQbhI44NDXVZCDdRBbpdaWlACioN7jkuOINCUVrU2QL1jVO" ;
65+ static final String cbc_access_key = "3gcpgyTBzOetdETYxOAtmLYBe3f9ZSVN" ; // replace with your access key and...
66+ static final String cbc_secret_key = "PWiACuJIZUlv0fCZaIQbhI44NDXVZCDdRBbpdaWlACioN7jkuOINCUVrU2QL1jVO" ; // secret key
67+ // Update this to your cluster
68+ static String bucketName = "my_bucket" ;
69+ static String username = "user" ;
70+ static String password = "Couch0base!" ;
71+ // User Input ends here.
72+
6273 static final String hostname = "cloudapi.cloud.couchbase.com" ;
6374 static final HandshakeCertificates clientCertificates = new HandshakeCertificates .Builder ()
64- .addPlatformTrustedCertificates ().addInsecureHost (hostname ).build ();
75+ .addPlatformTrustedCertificates ()/* .addInsecureHost(hostname)*/ .build ();
6576 static final OkHttpClient httpClient = new OkHttpClient .Builder ()
6677 .sslSocketFactory (clientCertificates .sslSocketFactory (), clientCertificates .trustManager ()).build ();
6778
@@ -72,18 +83,14 @@ public class CapellaConnectSample {
7283 public static void main (String ... args ) {
7384 String endpoint = null ; // "cb.zsibzkbgllfbcj8g.cloud.couchbase.com";
7485 List <String > clusterIds = getClustersControlPlane ();
86+ // the following loop assumes that the desired cluster is the last one in the list.
87+ // If this is not the case, then the endpoint for the desired cluster must be selected.
7588 for (String id : clusterIds ) {
7689 endpoint = getClusterControlPlane (id );
7790 }
7891
79- // Update this to your cluster
80- String bucketName = "my_bucket" ;
81- String username = "user" ;
82- String password = "Couch0base!" ;
83- // User Input ends here.
84-
8592 ClusterEnvironment env = ClusterEnvironment .builder ()
86- .securityConfig (SecurityConfig .enableTls (true ).trustManagerFactory (InsecureTrustManagerFactory .INSTANCE ))
93+ .securityConfig (SecurityConfig .enableTls (true )/* .trustManagerFactory(InsecureTrustManagerFactory.INSTANCE)*/ )
8794 .ioConfig (IoConfig .enableDnsSrv (true )).build ();
8895
8996 // Initialize the Connection
@@ -129,6 +136,8 @@ public static List<String> getClustersControlPlane() {
129136 return clusterIds ;
130137 }
131138
139+ // the methods below are required only to get the endpoint (host)
140+
132141 public static String getClusterControlPlane (String clusterId ) {
133142 String endpointsSrv ;
134143 Map <String , Object > decoded = doRequest (hostname , "GET" , "/v3/clusters/" + clusterId );
0 commit comments