io.pivotal.cfenv.core.CfService Java Examples

The following examples show how to use io.pivotal.cfenv.core.CfService. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RedisTests.java    From java-cfenv with Apache License 2.0 6 votes vote down vote up
@Test
public void redisServiceCreation() {
	mockVcapServices(
			getServicesPayload(
					getRedisServicePayload("redis-1", hostname, port, password, "redis-db"),
					getRedisServicePayload("redis-2", hostname, port, password, "redis-db")));

	CfEnv cfEnv = new CfEnv();
	List<CfService> cfServices = cfEnv.findServicesByName("redis-1", "redis-2");
	assertThat(cfServices).hasSize(2);
	assertThat(cfServices).allMatch(cfService -> cfService.getLabel().equals("rediscloud"));
	assertThat(cfServices).allMatch(
			cfService -> cfService.getCredentials().getUriInfo("redis").getUriString()
					.equals("redis://10.20.30.40:1234"));

}
 
Example #2
Source File: MySqlJdbcUrlCreator.java    From java-cfenv with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isDatabaseService(CfService cfService) {
	if (jdbcUrlMatchesScheme(cfService, MYSQL_SCHEME)
			|| cfService.existsByTagIgnoreCase(MYSQL_TAG)
			|| cfService.existsByLabelStartsWith(MYSQL_LABEL)
			|| cfService.existsByUriSchemeStartsWith(MYSQL_SCHEME)
			|| cfService.existsByCredentialsContainsUriField(MYSQL_SCHEME)) {
		return true;
	}
	return false;
}
 
Example #3
Source File: GcpCloudFoundryEnvironmentPostProcessor.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static Properties retrieveCfProperties(CfEnv cfEnv,
		String gcpServiceName, String cfServiceName, Map<String, String> fieldsToMap) {
	Properties properties = new Properties();

	try {
		List<CfService> serviceBindings = cfEnv.findServicesByLabel(cfServiceName);

		// If finding by label fails, attempt to find the service by tag.
		if (serviceBindings.isEmpty()) {
			serviceBindings = cfEnv.findServicesByTag(cfServiceName);
		}

		if (serviceBindings.size() != 1) {
			if (serviceBindings.size() > 1) {
				LOGGER.warn("The service " + cfServiceName + " has to be bound to a "
						+ "Cloud Foundry application once and only once.");
			}
			return properties;
		}

		CfService cfService = serviceBindings.get(0);
		CfCredentials credentials = cfService.getCredentials();
		String prefix = SPRING_CLOUD_GCP_PROPERTY_PREFIX + gcpServiceName + ".";
		fieldsToMap.forEach(
				(cfPropKey, gcpPropKey) -> properties.put(
						prefix + gcpPropKey,
						credentials.getMap().get(cfPropKey)));
	}
	catch (ClassCastException ex) {
		LOGGER.warn("Unexpected format of CF (VCAP) properties", ex);
	}

	return properties;
}
 
Example #4
Source File: RedisTests.java    From java-cfenv with Apache License 2.0 5 votes vote down vote up
@Test
public void redisServiceCreationNoLabelNoTags() {
	mockVcapServices(
		getServicesPayload(
			getRedisServicePayloadNoLabelNoTags("redis-1", hostname, port, password, "redis-db"),
			getRedisServicePayloadNoLabelNoTags("redis-2", hostname, port, password, "redis-db")));

	CfEnv cfEnv = new CfEnv();
	List<CfService> cfServices = cfEnv.findServicesByName("redis-1", "redis-2");
	assertThat(cfServices).hasSize(2);
	assertThat(cfServices).allMatch(
		cfService -> cfService.getCredentials().getUri("rediss")
			.equals("rediss://myuser:[email protected]:1234"));
}
 
Example #5
Source File: AmqpTests.java    From java-cfenv with Apache License 2.0 5 votes vote down vote up
@Test
public void qpidServiceCreationNoLabelNoTags() throws Exception {
	mockVcapServices(getServicesPayload(
			getQpidServicePayloadNoLabelNoTags("qpid-1", hostname, port, username, password, "q-1", "vhost1"),
			getQpidServicePayloadNoLabelNoTags("qpid-2", hostname, port, username, password, "q-2", "vhost2")));
	CfEnv cfEnv = new CfEnv();
	assertThat(cfEnv.findServicesByName("qpid-1","qpid-2")).hasSize(2);
	CfService cfService = cfEnv.findServiceByName("qpid-1");
	assertThat(cfService.getCredentials().getUsername()).isEqualTo(username);
	assertThat(cfService.getCredentials().getPassword()).isEqualTo(password);
	assertThat(cfService.getCredentials().getUriInfo().getUri().getQuery())
		.contains(String.format("tcp://%s:%s", hostname, port));
}
 
Example #6
Source File: AmqpCfEnvProcessor.java    From java-cfenv with Apache License 2.0 5 votes vote down vote up
@Override
public boolean accept(CfService service) {
	boolean serviceIsBound = service.existsByTagIgnoreCase("rabbitmq", "amqp") ||
			service.existsByLabelStartsWith("rabbitmq") ||
			service.existsByLabelStartsWith("cloudamqp") ||
			service.existsByUriSchemeStartsWith(amqpSchemes) ||
			service.existsByCredentialsContainsUriField(amqpSchemes);
	if (serviceIsBound) {
		ConnectorLibraryDetector.assertNoConnectorLibrary();
	}
	return serviceIsBound;
}
 
Example #7
Source File: CassandraCfEnvProcessor.java    From java-cfenv with Apache License 2.0 5 votes vote down vote up
@Override
public boolean accept(CfService service) {
	boolean serviceIsBound = service.existsByTagIgnoreCase("cassandra") &&
			cassandraCredentialsPresent(service.getCredentials().getMap());
	if (serviceIsBound) {
		ConnectorLibraryDetector.assertNoConnectorLibrary();
	}
	return serviceIsBound;
}
 
Example #8
Source File: MongoCfEnvProcessor.java    From java-cfenv with Apache License 2.0 5 votes vote down vote up
@Override
public boolean accept(CfService service) {
	boolean serviceIsBound = service.existsByTagIgnoreCase("mongodb") ||
			service.existsByLabelStartsWith("mongolab") ||
			service.existsByUriSchemeStartsWith(mongoScheme) ||
			service.existsByCredentialsContainsUriField(mongoScheme);
	if (serviceIsBound) {
		ConnectorLibraryDetector.assertNoConnectorLibrary();
	}
	return serviceIsBound;
}
 
Example #9
Source File: RedisCfEnvProcessor.java    From java-cfenv with Apache License 2.0 5 votes vote down vote up
@Override
public boolean accept(CfService service) {
	boolean serviceIsBound = service.existsByTagIgnoreCase("redis") ||
			service.existsByLabelStartsWith("rediscloud") ||
			service.existsByUriSchemeStartsWith(redisSchemes) ||
			service.existsByCredentialsContainsUriField(redisSchemes);
	if (serviceIsBound) {
		ConnectorLibraryDetector.assertNoConnectorLibrary();
	}
	return serviceIsBound;
}
 
Example #10
Source File: ButlerCfEnvProcessor.java    From cf-butler with Apache License 2.0 5 votes vote down vote up
@Override
public boolean accept(CfService service) {
    return
        service.getName().equalsIgnoreCase(SERVICE_NAME);
}
 
Example #11
Source File: SqlServerJdbcUrlCreator.java    From java-cfenv with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isDatabaseService(CfService cfService) {
	// Match tags
	return jdbcUrlMatchesScheme(cfService, SQLSERVER_SCHEME)
			|| cfService.existsByLabelStartsWith(SQLSERVER_LABEL)
			|| cfService.existsByUriSchemeStartsWith(SQLSERVER_SCHEME)
			|| cfService.existsByCredentialsContainsUriField(SQLSERVER_SCHEME);
}
 
Example #12
Source File: AbstractJdbcUrlCreator.java    From java-cfenv with Apache License 2.0 5 votes vote down vote up
@Override
public String createJdbcUrl(CfService cfService) {
	CfCredentials cfCredentials = cfService.getCredentials();
	String jdbcUrl = (String) cfCredentials.getMap().get("jdbcUrl");
	if (jdbcUrl != null) {
		return jdbcUrl;
	}
	else {
		return buildJdbcUrlFromUriField(cfCredentials);
	}
}
 
Example #13
Source File: AbstractJdbcUrlCreator.java    From java-cfenv with Apache License 2.0 5 votes vote down vote up
protected boolean jdbcUrlMatchesScheme(CfService cfService, String... uriSchemes) {
	CfCredentials cfCredentials = cfService.getCredentials();
	String jdbcUrl = (String) cfCredentials.getMap().get("jdbcUrl");
	if (jdbcUrl != null) {
		for (String uriScheme : uriSchemes) {
			if (jdbcUrl.startsWith(JDBC_PREFIX + uriScheme + ":")) {
				return true;
			}
		}
	}
	return false;
}
 
Example #14
Source File: PostgresqlJdbcUrlCreator.java    From java-cfenv with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isDatabaseService(CfService cfService) {
	if (jdbcUrlMatchesScheme(cfService, POSTGRESQL_SCHEME, POSTGRES_JDBC_SCHEME)
			|| cfService.existsByTagIgnoreCase(POSTGRESQL_TAG)
			|| cfService.existsByLabelStartsWith(POSTGRESQL_LABEL)
			|| cfService.existsByUriSchemeStartsWith(POSTGRESQL_SCHEME)
			|| cfService.existsByCredentialsContainsUriField(POSTGRESQL_SCHEME)) {
		return true;
	}
	return false;
}
 
Example #15
Source File: DB2JdbcUrlCreator.java    From java-cfenv with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isDatabaseService(CfService cfService) {
	if (jdbcUrlMatchesScheme(cfService, DB2_SCHEME)
			|| cfService.existsByTagIgnoreCase(DB2_TAGS)
			|| cfService.existsByLabelStartsWith(DB2_LABEL)
			|| cfService.existsByUriSchemeStartsWith(DB2_SCHEME)
			|| cfService.existsByCredentialsContainsUriField(DB2_SCHEME)) {
		return true;
	}
	return false;
}
 
Example #16
Source File: OracleJdbcUrlCreator.java    From java-cfenv with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isDatabaseService(CfService cfService) {
	// Match tags
	if (jdbcUrlMatchesScheme(cfService, ORACLE_SCHEME)
			|| cfService.existsByLabelStartsWith(ORACLE_LABEL)
			|| cfService.existsByUriSchemeStartsWith(ORACLE_SCHEME)
			|| cfService.existsByCredentialsContainsUriField(ORACLE_SCHEME)) {
		return true;
	}
	return false;
}
 
Example #17
Source File: CredHubCfEnvProcessor.java    From java-cfenv with Apache License 2.0 4 votes vote down vote up
@Override
public boolean accept(CfService service) {
	return service.existsByTagIgnoreCase("credhub") ||
			service.existsByLabelStartsWith("credhub");
}
 
Example #18
Source File: CfSingleSignOnLegacyProcessor.java    From java-cfenv with Apache License 2.0 4 votes vote down vote up
@Override
public boolean accept(CfService service) {
	return SpringSecurityDetector.isLegacySpringSecurityPresent() && service.existsByLabelStartsWith(PIVOTAL_SSO_LABEL);
}
 
Example #19
Source File: CfSingleSignOnProcessor.java    From java-cfenv with Apache License 2.0 4 votes vote down vote up
@Override
public boolean accept(CfService service) {
    return SpringSecurityDetector.isSpringSecurityPresent() && service.existsByLabelStartsWith(PIVOTAL_SSO_LABEL);
}
 
Example #20
Source File: CfEurekaClientProcessor.java    From java-cfenv with Apache License 2.0 4 votes vote down vote up
@Override
public boolean accept(CfService service) {
    return service.existsByTagIgnoreCase("eureka");
}
 
Example #21
Source File: DataSourceEnvironmentExtractor.java    From multiapps-controller with Apache License 2.0 4 votes vote down vote up
private CfCredentials extractDatabaseServiceCredentials(String serviceName) {
    CfEnv cfEnv = new CfEnv();
    CfService sourceService = cfEnv.findServiceByName(serviceName);
    return sourceService.getCredentials();
}
 
Example #22
Source File: GcpCloudFoundryEnvironmentPostProcessor.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment,
		SpringApplication application) {
	CfEnv cfEnv = new CfEnv();
	if (cfEnv.isInCf()) {
		Properties gcpCfServiceProperties = new Properties();

		Set<GcpCfService> servicesToMap = new HashSet<>(Arrays.asList(GcpCfService.values()));

		List<CfService> sqlServices = cfEnv.findServicesByLabel(GcpCfService.MYSQL.getCfServiceName(), GcpCfService.POSTGRES.getCfServiceName());
		if (sqlServices.size() == 2) {
			LOGGER.warn("Both MySQL and PostgreSQL bound to the app. "
					+ "Not configuring Cloud SQL.");
			servicesToMap.remove(GcpCfService.MYSQL);
			servicesToMap.remove(GcpCfService.POSTGRES);
		}

		servicesToMap.forEach(
				(service) -> gcpCfServiceProperties.putAll(
						retrieveCfProperties(
								cfEnv,
								service.getGcpServiceName(),
								service.getCfServiceName(),
								service.getCfPropNameToGcp())));

		// For Cloud SQL, there are some exceptions to the rule.
		// The instance connection name must be built from three fields.
		if (gcpCfServiceProperties.containsKey("spring.cloud.gcp.sql.instance-name")) {
			String instanceConnectionName =
					gcpCfServiceProperties.getProperty("spring.cloud.gcp.sql.project-id") + ":"
					+ gcpCfServiceProperties.getProperty("spring.cloud.gcp.sql.region") + ":"
					+ gcpCfServiceProperties.getProperty("spring.cloud.gcp.sql.instance-name");
			gcpCfServiceProperties.put("spring.cloud.gcp.sql.instance-connection-name",
					instanceConnectionName);
		}
		// The username and password should be in the generic DataSourceProperties.
		if (gcpCfServiceProperties.containsKey("spring.cloud.gcp.sql.username")) {
			gcpCfServiceProperties.put("spring.datasource.username",
					gcpCfServiceProperties.getProperty("spring.cloud.gcp.sql.username"));
		}
		if (gcpCfServiceProperties.containsKey("spring.cloud.gcp.sql.password")) {
			gcpCfServiceProperties.put("spring.datasource.password",
					gcpCfServiceProperties.getProperty("spring.cloud.gcp.sql.password"));
		}

		environment.getPropertySources()
				.addFirst(new PropertiesPropertySource("gcpCf", gcpCfServiceProperties));
	}
}
 
Example #23
Source File: CfSpringCloudConfigClientProcessor.java    From java-cfenv with Apache License 2.0 4 votes vote down vote up
@Override
public boolean accept(CfService service) {
	return service.existsByTagIgnoreCase("configuration");
}
 
Example #24
Source File: CfServiceEnablingEnvironmentPostProcessor.java    From java-cfenv with Apache License 2.0 2 votes vote down vote up
/**
 * Determine if a service is enabled.
 *
 * @param service a service to inspect.
 * @param environment the Environment.
 * @return {@code true} if the service is enabled; {@code false} otherwise
 */
default boolean isEnabled(CfService service, Environment environment) {
	return Boolean.valueOf(
		environment.getProperty(String.format("cfenv.service.%s.enabled", service.getName()),
			"true"));
}
 
Example #25
Source File: JdbcUrlCreator.java    From java-cfenv with Apache License 2.0 2 votes vote down vote up
/**
 * Identifies the provided service as a database service
 * @param cfService a Cloud Foundry service
 * @return {@code true} if the service describes a database service, {@code false}
 * otherwise.
 */
boolean isDatabaseService(CfService cfService);
 
Example #26
Source File: CfEnvProcessor.java    From java-cfenv with Apache License 2.0 2 votes vote down vote up
/**
 * Determine if a service is supported by this processor.
 *
 * @param service a service to inspect
 * @return {@code true} if the service matches criteria; {@code false} otherwise
 */
boolean accept(CfService service);
 
Example #27
Source File: JdbcUrlCreator.java    From java-cfenv with Apache License 2.0 votes vote down vote up
String createJdbcUrl(CfService cfService);