DATACOUCH-19 - adding optional bucket creation step.
This commit is contained in:
7
pom.xml
7
pom.xml
@@ -74,6 +74,13 @@
|
|||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
|
<artifactId>httpclient</artifactId>
|
||||||
|
<version>4.1</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.fasterxml.jackson.core</groupId>
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
<artifactId>jackson-databind</artifactId>
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
|||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package org.springframework.data.couchbase;
|
||||||
|
|
||||||
|
import com.couchbase.client.ClusterManager;
|
||||||
|
import com.couchbase.client.clustermanager.BucketType;
|
||||||
|
import org.apache.http.auth.AuthScope;
|
||||||
|
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||||
|
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||||
|
import org.apache.http.impl.client.DefaultHttpClient;
|
||||||
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||||
|
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||||
|
import org.springframework.web.client.HttpClientErrorException;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class BucketCreator implements InitializingBean {
|
||||||
|
|
||||||
|
private final String hostUri;
|
||||||
|
private final String adminUser;
|
||||||
|
private final String adminPass;
|
||||||
|
|
||||||
|
public BucketCreator(String host, String user, String pass) {
|
||||||
|
hostUri = host;
|
||||||
|
adminUser = user;
|
||||||
|
adminPass = pass;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterPropertiesSet() throws Exception {
|
||||||
|
DefaultHttpClient client = new DefaultHttpClient();
|
||||||
|
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
||||||
|
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(adminUser, adminPass));
|
||||||
|
client.setCredentialsProvider(credentialsProvider);
|
||||||
|
ClientHttpRequestFactory rf = new HttpComponentsClientHttpRequestFactory(client);
|
||||||
|
|
||||||
|
RestTemplate template = new RestTemplate(rf);
|
||||||
|
|
||||||
|
String fullUri = hostUri + "/default/buckets/default";
|
||||||
|
|
||||||
|
try {
|
||||||
|
template.getForEntity(fullUri, String.class);
|
||||||
|
} catch (HttpClientErrorException ex) {
|
||||||
|
if (ex.getMessage().equals("404 Object Not Found")) {
|
||||||
|
createBucket();
|
||||||
|
} else {
|
||||||
|
throw new RuntimeException("Could not see if bucket is already created.", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createBucket() throws Exception {
|
||||||
|
ClusterManager bucketManager =
|
||||||
|
new ClusterManager(Arrays.asList(new URI(hostUri)), adminUser, adminPass);
|
||||||
|
bucketManager.createDefaultBucket(BucketType.COUCHBASE, 128, 0, true);
|
||||||
|
Thread.sleep(5000);
|
||||||
|
bucketManager.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -20,6 +20,7 @@ import com.couchbase.client.CouchbaseClient;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.DependsOn;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
|
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
|
||||||
|
|
||||||
@@ -35,14 +36,44 @@ public class TestApplicationConfig extends AbstractCouchbaseConfiguration {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private Environment env;
|
private Environment env;
|
||||||
|
|
||||||
@Override
|
|
||||||
@Bean
|
@Bean
|
||||||
public CouchbaseClient couchbaseClient() throws Exception {
|
public String couchbaseHost() {
|
||||||
String host = env.getProperty("couchbase.host", "http://127.0.0.1:8091/pools");
|
return env.getProperty("couchbase.host", "http://127.0.0.1:8091/pools");
|
||||||
String bucket = env.getProperty("couchbase.bucket", "default");
|
|
||||||
String password = env.getProperty("couchbase.password", "");
|
|
||||||
|
|
||||||
return new CouchbaseClient(Arrays.asList(new URI(host)), bucket, password);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public String couchbaseBucket() {
|
||||||
|
return env.getProperty("couchbase.bucket", "default");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public String couchbasePassword() {
|
||||||
|
return env.getProperty("couchbase.password", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public String couchbaseAdminUser() {
|
||||||
|
return env.getProperty("couchbase.adminUser", "Administrator");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public String couchbaseAdminPassword() {
|
||||||
|
return env.getProperty("couchbase.adminUser", "password");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public BucketCreator bucketCreator() throws Exception {
|
||||||
|
return new BucketCreator(couchbaseHost(), couchbaseAdminUser(), couchbaseAdminPassword());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Bean
|
||||||
|
@DependsOn("bucketCreator")
|
||||||
|
public CouchbaseClient couchbaseClient() throws Exception {
|
||||||
|
return new CouchbaseClient(
|
||||||
|
Arrays.asList(new URI(couchbaseHost())), couchbaseBucket(), couchbasePassword());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user