Search in sources :

Example 31 with ServiceInfo

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);
}
Also used : ServiceInfo(org.springframework.cloud.service.ServiceInfo) SqlServerServiceInfo(org.springframework.cloud.service.common.SqlServerServiceInfo) Test(org.junit.Test)

Example 32 with ServiceInfo

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;
}
Also used : ServiceInfo(org.springframework.cloud.service.ServiceInfo) CompositeServiceInfo(org.springframework.cloud.service.CompositeServiceInfo) ArrayList(java.util.ArrayList)

Example 33 with ServiceInfo

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.&lt;property-name&gt;</code> form. <pre>
	 * cloud.application.app-id = helloworld
	 * cloud.application.instance-id = instance-0-0fab098f
	 * cloud.application.&lt;property-name&gt; = &lt;property-value&gt;
	 * </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.&lt;service-id&gt;</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.&lt;service-type&gt;</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;
}
Also used : ServiceInfo(org.springframework.cloud.service.ServiceInfo) CompositeServiceInfo(org.springframework.cloud.service.CompositeServiceInfo) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) Properties(java.util.Properties)

Example 34 with ServiceInfo

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));
}
Also used : ServiceInfo(org.springframework.cloud.service.ServiceInfo) AmqpServiceInfo(org.springframework.cloud.service.common.AmqpServiceInfo) URI(java.net.URI) AmqpServiceInfo(org.springframework.cloud.service.common.AmqpServiceInfo) Test(org.junit.Test)

Example 35 with ServiceInfo

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);
}
Also used : ServiceInfo(org.springframework.cloud.service.ServiceInfo) MysqlServiceInfo(org.springframework.cloud.service.common.MysqlServiceInfo) DB2ServiceInfo(org.springframework.cloud.service.common.DB2ServiceInfo) MysqlServiceInfo(org.springframework.cloud.service.common.MysqlServiceInfo) Test(org.junit.Test)

Aggregations

ServiceInfo (org.springframework.cloud.service.ServiceInfo)47 Test (org.junit.Test)37 MysqlServiceInfo (org.springframework.cloud.service.common.MysqlServiceInfo)16 AmqpServiceInfo (org.springframework.cloud.service.common.AmqpServiceInfo)8 HashMap (java.util.HashMap)6 CompositeServiceInfo (org.springframework.cloud.service.CompositeServiceInfo)5 MongoServiceInfo (org.springframework.cloud.service.common.MongoServiceInfo)5 PostgresqlServiceInfo (org.springframework.cloud.service.common.PostgresqlServiceInfo)5 RedisServiceInfo (org.springframework.cloud.service.common.RedisServiceInfo)4 ArrayList (java.util.ArrayList)3 UriBasedServiceInfo (org.springframework.cloud.service.UriBasedServiceInfo)3 DB2ServiceInfo (org.springframework.cloud.service.common.DB2ServiceInfo)3 OracleServiceInfo (org.springframework.cloud.service.common.OracleServiceInfo)3 SqlServerServiceInfo (org.springframework.cloud.service.common.SqlServerServiceInfo)3 StubCloudConnector (org.springframework.cloud.CloudTestUtil.StubCloudConnector)2 StubCompositeServiceInfo (org.springframework.cloud.CloudTestUtil.StubCompositeServiceInfo)2 StubServiceInfo (org.springframework.cloud.CloudTestUtil.StubServiceInfo)2 BaseServiceInfo (org.springframework.cloud.service.BaseServiceInfo)2 RelationalServiceInfo (org.springframework.cloud.service.common.RelationalServiceInfo)2 SmtpServiceInfo (org.springframework.cloud.service.common.SmtpServiceInfo)2