Merge pull request #176 from mp911de/cassandra

Add support for Cassandra
This commit is contained in:
Scott Frederick
2017-10-20 16:13:57 -05:00
committed by GitHub
28 changed files with 1305 additions and 4 deletions

View File

@@ -0,0 +1,53 @@
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.cloudfoundry;
import java.util.List;
import java.util.Map;
import org.springframework.cloud.service.common.CassandraServiceInfo;
/**
* Service info creator for Cassandra services.
*
* @author Mark Paluch
*/
public class CassandraServiceInfoCreator extends
CloudFoundryServiceInfoCreator<CassandraServiceInfo> {
public CassandraServiceInfoCreator() {
super(new Tags("cassandra"));
}
@Override
@SuppressWarnings("unchecked")
public CassandraServiceInfo createServiceInfo(Map<String, Object> serviceData) {
String id = (String) serviceData.get("name");
Map<String, Object> credentials = getCredentials(serviceData);
String username = getStringFromCredentials(credentials, "username");
String password = getStringFromCredentials(credentials, "password");
String port = getStringFromCredentials(credentials, "cqlsh_port");
List<String> contactpoints = (List<String>) credentials.get("node_ips");
return new CassandraServiceInfo(id, contactpoints, Integer.parseInt(port),
username, password);
}
}

View File

@@ -8,3 +8,4 @@ org.springframework.cloud.cloudfoundry.SmtpServiceInfoCreator
org.springframework.cloud.cloudfoundry.OracleServiceInfoCreator
org.springframework.cloud.cloudfoundry.DB2ServiceInfoCreator
org.springframework.cloud.cloudfoundry.SqlServerServiceInfoCreator
org.springframework.cloud.cloudfoundry.CassandraServiceInfoCreator

View File

@@ -0,0 +1,90 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.cloudfoundry;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.springframework.cloud.service.common.CassandraServiceInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Unit tests for link {@link CassandraServiceInfoCreator}.
*
* @author Mark Paluch
*/
public class CassandraServiceInfoCreatorTests extends AbstractCloudFoundryConnectorTest {
private ObjectMapper mapper = new ObjectMapper();
@Test
public void shouldCreateServiceInfo() throws Exception {
CassandraServiceInfoCreator creator = new CassandraServiceInfoCreator();
Map services = readServiceData("test-cassandra-service.json");
Map<String, Object> serviceData = getServiceData(services,
"p-dse-cassandra-acceptance");
CassandraServiceInfo info = creator.createServiceInfo(serviceData);
assertThat(info.getContactPoints(), hasItems("1.2.3.4", "5.6.7.8"));
assertThat(info.getPort(), is(equalTo(9042)));
assertThat(info.getUsername(), is(nullValue()));
assertThat(info.getPassword(), is(nullValue()));
}
@Test
public void shouldCreateServiceInfoWithCredentials() throws Exception {
CassandraServiceInfoCreator creator = new CassandraServiceInfoCreator();
Map services = readServiceData("test-cassandra-with-credentials.json");
Map<String, Object> serviceData = getServiceData(services,
"p-dse-cassandra-acceptance");
CassandraServiceInfo info = creator.createServiceInfo(serviceData);
assertThat(info.getContactPoints(), hasItems("1.2.3.4"));
assertThat(info.getPort(), is(equalTo(9042)));
assertThat(info.getUsername(), is(equalTo("user")));
assertThat(info.getPassword(), is(equalTo("pass")));
}
@Test
public void testAcceptService() throws Exception {
CassandraServiceInfoCreator creator = new CassandraServiceInfoCreator();
Map services = readServiceData("test-cassandra-service.json");
Map<String, Object> serviceData = getServiceData(services,
"p-dse-cassandra-acceptance");
assertThat(creator.accept(serviceData), is(true));
}
private Map readServiceData(String resource) throws java.io.IOException {
return mapper.readValue(readTestDataFile(resource), Map.class);
}
@SuppressWarnings("unchecked")
private Map<String, Object> getServiceData(Map services, String name) {
return (Map<String, Object>) ((List) services.get(name)).get(0);
}
}

View File

@@ -0,0 +1,25 @@
{
"p-dse-cassandra-acceptance": [
{
"credentials": {
"cqlsh_port": "9042",
"node_ips": [
"1.2.3.4",
"5.6.7.8"
],
"opscenter_url": "http://datastax-opscenter-cassandra.cf-app.com/opscenter/index.html"
},
"label": "p-dse-cassandra-acceptance",
"name": "mydse",
"plan": "single-node",
"provider": null,
"syslog_drain_url": null,
"tags": [
"pivotal",
"cassandra",
"dse",
"datastax"
]
}
]
}

View File

@@ -0,0 +1,26 @@
{
"p-dse-cassandra-acceptance": [
{
"credentials": {
"cqlsh_port": "9042",
"node_ips": [
"1.2.3.4"
],
"opscenter_url": "http://datastax-opscenter-cassandra.cf-app.com/opscenter/index.html",
"password": "pass",
"username": "user"
},
"label": "p-dse-cassandra-acceptance",
"name": "mydse",
"plan": "single-node",
"provider": null,
"syslog_drain_url": null,
"tags": [
"pivotal",
"cassandra",
"dse",
"datastax"
]
}
]
}