io.pivotal.cfenv.core.CfEnv Java Examples

The following examples show how to use io.pivotal.cfenv.core.CfEnv. 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: AmqpTests.java    From java-cfenv with Apache License 2.0 5 votes vote down vote up
@Test
public void rabbitServiceCreationWithTags() {
	mockVcapServices(getServicesPayload(
			getRabbitServicePayloadWithTags("rabbit-1", hostname, port, username, password, "q-1", "vhost1"),
			getRabbitServicePayloadWithTags("rabbit-2", hostname, port, username, password, "q-2", "vhost2")));

	CfEnv cfEnv = new CfEnv();
	assertThat(cfEnv.findServicesByName("rabbit-1","rabbit-2")).hasSize(2);
}
 
Example #3
Source File: AmqpTests.java    From java-cfenv with Apache License 2.0 5 votes vote down vote up
@Test
public void rabbitServiceCreationWithManagementUri() {
		mockVcapServices(getServicesPayload(
			getRabbitServicePayloadWithTags("rabbit-1", hostname, port, username, password, "q-1", "vhost1")));

	String expectedManagementUri = "http://" + username + ":" + password + "@" + hostname + "/api";
	Map<String, String> credentials = (Map)new CfEnv().findServiceByName("rabbit-1").getMap().get("credentials");
	assertThat(credentials.get("http_api_uri")).isEqualTo(expectedManagementUri);
}
 
Example #4
Source File: AmqpTests.java    From java-cfenv with Apache License 2.0 5 votes vote down vote up
@Test
public void rabbitServiceCreationWithoutManagementUri() {
	mockVcapServices(getServicesPayload(
			getRabbitServicePayloadNoLabelNoTags("rabbit-1", hostname, port, username, password, "q-1", "vhost1")));
	Map<String, ?> credentials = (Map)new CfEnv().findServiceByName("rabbit-1").getMap().get("credentials");
	assertThat(credentials.get("http_api_uri")).isNull();
}
 
Example #5
Source File: AmqpTests.java    From java-cfenv with Apache License 2.0 5 votes vote down vote up
@Test
public void rabbitServiceCreationWithoutTags() {
	mockVcapServices(getServicesPayload(
			getRabbitServicePayloadWithoutTags("rabbit-1", hostname, port, username, password, "q-1", "vhost1"),
			getRabbitServicePayloadWithoutTags("rabbit-2", hostname, port, username, password, "q-2", "vhost2")));
	CfEnv cfEnv = new CfEnv();
	assertThat(cfEnv.findServicesByName("rabbit-1","rabbit-2")).hasSize(2);
}
 
Example #6
Source File: AmqpTests.java    From java-cfenv with Apache License 2.0 5 votes vote down vote up
@Test
public void rabbitServiceCreationMultipleUris() {
	mockVcapServices(getServicesPayload(
			getRabbitServicePayloadMultipleUris("rabbit-1", hostname, hostname2, port, username, password, "q-1",
				"v/host1"),
			getRabbitServicePayloadMultipleUris("rabbit-2", hostname, hostname2, port, username, password, "q-2",
				"v/host2")));

	CfEnv cfEnv = new CfEnv();
	assertThat(cfEnv.findServicesByName("rabbit-1","rabbit-2")).hasSize(2);


	assertThat(cfEnv.findServiceByName("rabbit-1").getCredentials().getUriInfo().getUriString())
		.isEqualTo(String.format("amqp://%s:%s@%s/v/host1",username,password,hostname));

	String expectedManagementUri = "http://" + username + ":" + password + "@" + hostname + "/api";
	Map<String, ?> credentials = (Map)cfEnv.findServiceByName("rabbit-1").getMap().get("credentials");
	assertThat(credentials.get("http_api_uri")).isEqualTo(expectedManagementUri);

	List<String> uris = (List<String>) credentials.get("uris");
	assertThat(uris).hasSize(2);
	assertThat(uris.get(0)).contains(hostname);
	assertThat(uris.get(1)).contains(hostname2);

	List<String> managementUris = (List<String>) credentials.get("http_api_uris");
	assertThat(managementUris).hasSize(2);
	assertThat(managementUris.get(0)).contains(hostname);
	assertThat(managementUris.get(1)).contains(hostname2);
}
 
Example #7
Source File: AmqpTests.java    From java-cfenv with Apache License 2.0 5 votes vote down vote up
@Test
public void rabbitServiceCreationNoLabelNoTags() {
	mockVcapServices(getServicesPayload(
			getRabbitServicePayloadNoLabelNoTags("rabbit-1", hostname, port, username, password, "q-1", "vhost1"),
			getRabbitServicePayloadNoLabelNoTags("rabbit-2", hostname, port, username, password, "q-2", "vhost2")));
	CfEnv cfEnv = new CfEnv();
	assertThat(cfEnv.findServicesByName("rabbit-1","rabbit-2")).hasSize(2);
}
 
Example #8
Source File: AmqpTests.java    From java-cfenv with Apache License 2.0 5 votes vote down vote up
@Test
public void rabbitServiceCreationNoLabelNoTagsSecure() {
	mockVcapServices(getServicesPayload(
			getRabbitServicePayloadNoLabelNoTagsSecure("rabbit-1", hostname, port, username, password, "q-1", "vhost1"),
			getRabbitServicePayloadNoLabelNoTagsSecure("rabbit-2", hostname, port, username, password, "q-2", "vhost2")));
	CfEnv cfEnv = new CfEnv();
	assertThat(cfEnv.findServicesByName("rabbit-1","rabbit-2")).hasSize(2);
}
 
Example #9
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 #10
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 #11
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 #12
Source File: ButlerConfig.java    From cf-butler with Apache License 2.0 4 votes vote down vote up
@Bean
@Profile("cloud")
public CfEnv cfEnv() {
    return new CfEnv();
}
 
Example #13
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 #14
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));
	}
}