37
37
import javax .crypto .Mac ;
38
38
import javax .crypto .spec .SecretKeySpec ;
39
39
40
- import com .couchbase .client .core .deps .io .netty .handler .ssl .util .InsecureTrustManagerFactory ;
41
40
import com .couchbase .client .core .env .IoConfig ;
42
41
import com .couchbase .client .core .env .SecurityConfig ;
43
42
import com .couchbase .client .java .Bucket ;
53
52
54
53
/**
55
54
* 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>
57
62
*/
58
63
public class CapellaConnectSample {
59
64
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
+
62
73
static final String hostname = "cloudapi.cloud.couchbase.com" ;
63
74
static final HandshakeCertificates clientCertificates = new HandshakeCertificates .Builder ()
64
- .addPlatformTrustedCertificates ().addInsecureHost (hostname ).build ();
75
+ .addPlatformTrustedCertificates ()/* .addInsecureHost(hostname)*/ .build ();
65
76
static final OkHttpClient httpClient = new OkHttpClient .Builder ()
66
77
.sslSocketFactory (clientCertificates .sslSocketFactory (), clientCertificates .trustManager ()).build ();
67
78
@@ -72,18 +83,14 @@ public class CapellaConnectSample {
72
83
public static void main (String ... args ) {
73
84
String endpoint = null ; // "cb.zsibzkbgllfbcj8g.cloud.couchbase.com";
74
85
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.
75
88
for (String id : clusterIds ) {
76
89
endpoint = getClusterControlPlane (id );
77
90
}
78
91
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
-
85
92
ClusterEnvironment env = ClusterEnvironment .builder ()
86
- .securityConfig (SecurityConfig .enableTls (true ).trustManagerFactory (InsecureTrustManagerFactory .INSTANCE ))
93
+ .securityConfig (SecurityConfig .enableTls (true )/* .trustManagerFactory(InsecureTrustManagerFactory.INSTANCE)*/ )
87
94
.ioConfig (IoConfig .enableDnsSrv (true )).build ();
88
95
89
96
// Initialize the Connection
@@ -129,6 +136,8 @@ public static List<String> getClustersControlPlane() {
129
136
return clusterIds ;
130
137
}
131
138
139
+ // the methods below are required only to get the endpoint (host)
140
+
132
141
public static String getClusterControlPlane (String clusterId ) {
133
142
String endpointsSrv ;
134
143
Map <String , Object > decoded = doRequest (hostname , "GET" , "/v3/clusters/" + clusterId );
0 commit comments