use of org.springframework.cloud.service.ServiceInfo in project spring-cloud-connectors by spring-cloud.
the class CloudFoundryConnectorSqlServerServiceTest method sqlServerServiceCreation.
@Test
public void sqlServerServiceCreation() {
when(mockEnvironment.getEnvValue("VCAP_SERVICES")).thenReturn(getServicesPayload(getUserProvidedServicePayload(SERVICE_NAME, hostname, port, username, password, INSTANCE_NAME, SQLSERVER_SCHEME + ":")));
List<ServiceInfo> serviceInfos = testCloudConnector.getServiceInfos();
ServiceInfo info = getServiceInfo(serviceInfos, SERVICE_NAME);
assertServiceFoundOfType(info, SqlServerServiceInfo.class);
assertJdbcUrlEqual(info, SQLSERVER_SCHEME, INSTANCE_NAME);
assertUriBasedServiceInfoFields(info, SQLSERVER_SCHEME, hostname, port, username, password, INSTANCE_NAME);
}
use of org.springframework.cloud.service.ServiceInfo in project spring-cloud-connectors by spring-cloud.
the class ServiceConnectorCreatorRegistry method getServiceInfos.
/**
* Get {@link ServiceInfo}s for the bound services that could be mapped to the given service connector type.
*
* <p>
* For example, if the connector type is {@link DataSource}, then the method will return all {@link ServiceInfo} objects
* matching bound relational database services.
* <p>
*
* @param <T> The class of the connector to find services for.
* @param serviceConnectorType service connector type.
* Passing null returns all {@link ServiceInfo}s (matching that of {@link Cloud#getServiceInfos()}
* @return information about services bound to the application that could be transformed into the given connector type
*/
public <T> List<ServiceInfo> getServiceInfos(Class<T> serviceConnectorType) {
List<ServiceInfo> allServiceInfos = getServiceInfos();
List<ServiceInfo> matchingServiceInfos = new ArrayList<ServiceInfo>();
for (ServiceInfo serviceInfo : allServiceInfos) {
if (serviceConnectorCreatorRegistry.canCreate(serviceConnectorType, serviceInfo)) {
matchingServiceInfos.add(serviceInfo);
}
}
return matchingServiceInfos;
}
use of org.springframework.cloud.service.ServiceInfo in project spring-cloud-connectors by spring-cloud.
the class ServiceConnectorCreatorRegistry method getCloudProperties.
/**
* Get properties for app and services.
*
*
*
* <p>
* Application properties always include <code>cloud.application.app-id</code> and <code>cloud.application.instance-id</code>
* with values bound to application id and instance id. The rest of the properties are cloud-provider specific, but take the
* <code>cloud.application.<property-name></code> form. <pre>
* cloud.application.app-id = helloworld
* cloud.application.instance-id = instance-0-0fab098f
* cloud.application.<property-name> = <property-value>
* </pre>
*
* <p>
* Service specific properties are exposed for each bound service, with each key starting in <code>cloud.services</code>. Like
* application properties, these too are cloud and service specific. Each key for a specific service starts with
* <code>cloud.services.<service-id></code> <pre>
* cloud.services.customerDb.type = mysql-5.1
* cloud.services.customerDb.plan = free
* cloud.services.customerDb.connection.hostname = ...
* cloud.services.customerDb.connection.port = ...
* etc...
* </pre>
*
* <p>
* If a there is only a single service of a given type (as defined by the {link ServiceInfo.ServiceLabel}
* annoation's value of the corresponding {@link ServiceInfo} class), that service is aliased
* to the service type. Keys for such properties start in <code>cloud.services.<service-type></code>.
* For example, if there is only a single MySQL service bound to the application, the service properties
* will also be exposed starting with '<code>cloud.services.mysql</code>' key: <pre>
* cloud.services.mysql.type = mysql-5.1
* cloud.services.mysql.plan = free
* cloud.services.mysql.connection.hostname = ...
* cloud.services.mysql.connection.port = ...
* etc...
* </pre>
*
* @return the properties object
*/
public Properties getCloudProperties() {
Map<String, List<ServiceInfo>> mappedServiceInfos = new HashMap<String, List<ServiceInfo>>();
for (ServiceInfo serviceInfo : getServiceInfos()) {
String key = getServiceLabel(serviceInfo);
List<ServiceInfo> serviceInfosForLabel = mappedServiceInfos.get(key);
if (serviceInfosForLabel == null) {
serviceInfosForLabel = new ArrayList<ServiceInfo>();
mappedServiceInfos.put(key, serviceInfosForLabel);
}
serviceInfosForLabel.add(serviceInfo);
}
final String servicePropKeyLead = "cloud.services.";
Properties cloudProperties = new Properties();
for (Entry<String, List<ServiceInfo>> mappedServiceInfo : mappedServiceInfos.entrySet()) {
List<ServiceInfo> serviceInfos = mappedServiceInfo.getValue();
for (ServiceInfo serviceInfo : serviceInfos) {
String idBasedKey = servicePropKeyLead + serviceInfo.getId();
cloudProperties.putAll(getServiceProperties(idBasedKey, serviceInfo));
// If there is only one service for a given label, put props with that label instead of just id
if (serviceInfos.size() == 1) {
String labelBasedKey = servicePropKeyLead + mappedServiceInfo.getKey();
cloudProperties.putAll(getServiceProperties(labelBasedKey, serviceInfo));
}
}
}
cloudProperties.putAll(getAppProperties());
return cloudProperties;
}
use of org.springframework.cloud.service.ServiceInfo in project spring-cloud-connectors by spring-cloud.
the class CloudFoundryConnectorAmqpServiceTest method qpidServiceCreationNoLabelNoTags.
@Test
public void qpidServiceCreationNoLabelNoTags() throws Exception {
when(mockEnvironment.getEnvValue("VCAP_SERVICES")).thenReturn(getServicesPayload(getQpidServicePayloadNoLabelNoTags("qpid-1", hostname, port, username, password, "q-1", "vhost1"), getQpidServicePayloadNoLabelNoTags("qpid-2", hostname, port, username, password, "q-2", "vhost2")));
List<ServiceInfo> serviceInfos = testCloudConnector.getServiceInfos();
assertServiceFoundOfType(serviceInfos, "qpid-1", AmqpServiceInfo.class);
assertServiceFoundOfType(serviceInfos, "qpid-2", AmqpServiceInfo.class);
AmqpServiceInfo serviceInfo = (AmqpServiceInfo) getServiceInfo(serviceInfos, "qpid-1");
assertNotNull(serviceInfo);
assertEquals(username, serviceInfo.getUserName());
assertEquals(password, serviceInfo.getPassword());
assertEquals("vhost1", serviceInfo.getVirtualHost());
URI uri = new URI(serviceInfo.getUri());
assertTrue(uri.getQuery().contains("tcp://" + hostname + ":" + port));
}
use of org.springframework.cloud.service.ServiceInfo in project spring-cloud-connectors by spring-cloud.
the class CloudFoundryConnectorDB2ServiceTest method db2ServiceCreationWithNoUri.
@Test
public void db2ServiceCreationWithNoUri() {
when(mockEnvironment.getEnvValue("VCAP_SERVICES")).thenReturn(getServicesPayload(getUserProvidedServicePayloadWithNoUri(SERVICE_NAME, hostname, port, username, password, INSTANCE_NAME)));
List<ServiceInfo> serviceInfos = testCloudConnector.getServiceInfos();
ServiceInfo info = getServiceInfo(serviceInfos, SERVICE_NAME);
assertNotNull(info);
// service was not detected as MySQL
assertFalse(MysqlServiceInfo.class.isAssignableFrom(info.getClass()));
assertNotNull(info);
}
Aggregations