Merge pull request #39 from spring-cloud/27-eureka

Eureka Support
This commit is contained in:
E Casey
2020-05-20 19:27:36 -04:00
committed by GitHub
4 changed files with 141 additions and 1 deletions

View File

@@ -212,6 +212,17 @@ Disable Property: `org.springframework.cloud.bindings.boot.wavefront.enable`
| `management.metrics.export.wavefront.api-token` | `{secret/api-token}`
| `management.metrics.export.wavefront.uri` | `{secret/uri}`
### Eureka
Kind: `Eureka`
Disable Property: `org.springframework.cloud.bindings.boot.eureka.enable`
| Property | Value
| -------- | ------------------
| `eureka.client.oauth2.client-id` | `{secret/client-id}`
| `eureka.client.oauth2.access-token-uri` | `{secret/access-token-uri}`
| `eureka.client.region` | `default`
| `eureka.client.serviceUrl.defaultZone` | `{secret/uri}/eureka/`
## License
This buildpack is released under version 2.0 of the [Apache License][a].

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2020 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.bindings.boot;
import org.springframework.cloud.bindings.Binding;
import org.springframework.cloud.bindings.Bindings;
import org.springframework.core.env.Environment;
import java.util.Map;
import static org.springframework.cloud.bindings.boot.Guards.isKindEnabled;
/**
* An implementation of {@link BindingsPropertiesProcessor} that detects {@link Binding}s of kind: {@value KIND}.
*/
final class EurekaBindingsPropertiesProcessor implements BindingsPropertiesProcessor {
/**
* The {@link Binding} kind that this processor is interested in: {@value}.
**/
public static final String KIND = "Eureka";
@Override
public void process(Environment environment, Bindings bindings, Map<String, Object> properties) {
if (!isKindEnabled(environment, KIND)) {
return;
}
bindings.filterBindings(KIND).forEach(binding -> {
MapMapper map = new MapMapper(binding.getSecret(), properties);
map.from("client-id").to("eureka.client.oauth2.client-id");
map.from("access-token-uri").to("eureka.client.oauth2.access-token-uri");
map.from("access-token-uri").to("eureka.client.oauth2.access-token-uri");
map.from("uri").to("eureka.client.serviceUrl.defaultZone",
(uri) -> String.format("%s/eureka/", uri)
);
properties.put("eureka.client.region", "default");
});
}
}

View File

@@ -19,4 +19,5 @@ org.springframework.cloud.bindings.boot.BindingsPropertiesProcessor=\
org.springframework.cloud.bindings.boot.RabbitMqBindingsPropertiesProcessor, \
org.springframework.cloud.bindings.boot.RedisBindingsPropertiesProcessor, \
org.springframework.cloud.bindings.boot.SqlServerBindingsPropertiesProcessor, \
org.springframework.cloud.bindings.boot.WavefrontBindingsPropertiesProcessor
org.springframework.cloud.bindings.boot.WavefrontBindingsPropertiesProcessor, \
org.springframework.cloud.bindings.boot.EurekaBindingsPropertiesProcessor

View File

@@ -0,0 +1,72 @@
/*
* Copyright 2020 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.bindings.boot;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.cloud.bindings.Binding;
import org.springframework.cloud.bindings.Bindings;
import org.springframework.cloud.bindings.FluentMap;
import org.springframework.mock.env.MockEnvironment;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.HashMap;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.cloud.bindings.boot.EurekaBindingsPropertiesProcessor.KIND;
@DisplayName("Eureka BindingsPropertiesProcessor")
final class EurekaBindingsPropertiesProcessorTest {
private final Bindings bindings = new Bindings(
new Binding("test-name", Paths.get("test-path"),
Collections.singletonMap("kind", KIND),
new FluentMap()
.withEntry("uri", "test-uri")
.withEntry("client-id", "test-client-id")
.withEntry("client-secret", "test-client-secret")
.withEntry("access-token-uri", "test-access-token-uri")
)
);
private final MockEnvironment environment = new MockEnvironment();
private final HashMap<String, Object> properties = new HashMap<>();
@Test
@DisplayName("contributes properties")
void test() {
new EurekaBindingsPropertiesProcessor().process(environment, bindings, properties);
assertThat(properties)
.containsEntry("eureka.client.region", "default")
.containsEntry("eureka.client.oauth2.client-id", "test-client-id")
.containsEntry("eureka.client.oauth2.access-token-uri", "test-access-token-uri")
.containsEntry("eureka.client.serviceUrl.defaultZone", "test-uri/eureka/");
}
@Test
@DisplayName("can be disabled")
void disabled() {
environment.setProperty("org.springframework.cloud.bindings.boot.eureka.enable", "false");
new KafkaBindingsPropertiesProcessor().process(environment, bindings, properties);
assertThat(properties).isEmpty();
}
}