Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify the getServices() method so that it filters on default-query-tag #453

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.QueryParams;
Expand Down Expand Up @@ -119,12 +120,24 @@ public List<ServiceInstance> getAllInstances() {
public List<String> getServices() {
String aclToken = properties.getAclToken();

Map<String, List<String>> services;
if (StringUtils.hasText(aclToken)) {
return new ArrayList<>(client.getCatalogServices(QueryParams.DEFAULT, aclToken).getValue()
.keySet());
services = client.getCatalogServices(QueryParams.DEFAULT, aclToken).getValue();
fuleow marked this conversation as resolved.
Show resolved Hide resolved
} else {
return new ArrayList<>(client.getCatalogServices(QueryParams.DEFAULT).getValue()
.keySet());
services = client.getCatalogServices(QueryParams.DEFAULT).getValue();
}

return services.entrySet().stream()
.filter(e -> defaultQueryTagFilter(e.getValue()))
.map(Map.Entry::getKey)
.collect(Collectors.toList());
}

private boolean defaultQueryTagFilter(List<String> tags) {
if (StringUtils.isEmpty(properties.getDefaultQueryTag())) {
return true;
} else {
return tags.contains(properties.getDefaultQueryTag());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ public void shouldReturnOnlyIntgInstance() {
assertThat("instance is not intg", instances.get(0).getMetadata(), hasEntry("intg", "intg"));
}

@Test
public void shouldReturnOnlyIntgService() {
List<String> services = discoveryClient.getServices();
assertThat("instances was wrong size", services, hasSize(1));
assertThat("instance is not intg", services.get(0).equals(NAME));
}

private NewService serviceForEnvironment(String env, int port) {
NewService service = new NewService();
service.setAddress("localhost");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ public void getInstancesForServiceRespectsQueryParams() {
assertIpAddress(instance);
}

@Test
public void getServicesWorks() {
List<String> services = discoveryClient.getServices();
assertNotNull("services was null", services);
assertFalse("services was empty", services.isEmpty());
}

private void assertIpAddress(ServiceInstance instance) {
assertTrue("host isn't an ip address",
Character.isDigit(instance.getHost().charAt(0)));
Expand Down