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

fix(cf): locationFilter is now spaceFilter and added spacesLive to jsonIgnore #5103

Merged
merged 9 commits into from
Nov 17, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ public static class ManagedAccount implements CredentialsDefinition {
maxCapiConnectionsForCache; // Deprecated in favor of cloudfoundry.apiRequestParallelism

private Permissions.Builder permissions = new Permissions.Builder();
private Map<String, Set<String>> locationFilter = Collections.emptyMap();
private Map<String, Set<String>> spaceFilter = Collections.emptyMap();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public AbstractCredentialsLoader<CloudFoundryCredentials> cloudFoundryCredential
cacheRepository,
a.getPermissions().build(),
cloudFoundryThreadPool,
a.getLocationFilter()),
a.getSpaceFilter()),
cloudFoundryCredentialsRepository);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"spaceSupplier",
"cacheRepository",
"forkJoinPool",
"filteredSpaces"
"filteredSpaces",
"spacesLive"
})
public class CloudFoundryCredentials extends AbstractAccountCredentials<CloudFoundryClient> {
private static final int SPACE_EXPIRY_SECONDS = 30;
Expand Down Expand Up @@ -97,7 +98,7 @@ public CloudFoundryCredentials(
CacheRepository cacheRepository,
Permissions permissions,
ForkJoinPool forkJoinPool,
Map<String, Set<String>> locationFilter) {
Map<String, Set<String>> spaceFilter) {
this.name = name;
this.appsManagerUri = appsManagerUri;
this.metricsUri = metricsUri;
Expand All @@ -110,7 +111,7 @@ public CloudFoundryCredentials(
this.cacheRepository = cacheRepository;
this.permissions = permissions == null ? Permissions.EMPTY : permissions;
this.forkJoinPool = forkJoinPool;
this.filteredSpaces = createFilteredSpaces(locationFilter);
this.filteredSpaces = createFilteredSpaces(spaceFilter);
}

public CloudFoundryClient getCredentials() {
Expand Down Expand Up @@ -157,7 +158,7 @@ protected List<CloudFoundrySpace> spaceSupplier() {
return getSpacesLive();
}

public List<CloudFoundrySpace> getSpacesLive() {
private List<CloudFoundrySpace> getSpacesLive() {
try {
return getClient().getSpaces().all();
} catch (CloudFoundryApiException e) {
Expand Down Expand Up @@ -220,23 +221,23 @@ public static <U> Memoizer<U> memoizeWithExpiration(
}
}

protected List<CloudFoundrySpace> createFilteredSpaces(Map<String, Set<String>> locationFilter) {
protected List<CloudFoundrySpace> createFilteredSpaces(Map<String, Set<String>> spaceFilter) {
List<CloudFoundrySpace> spaces = new ArrayList<>();
if (locationFilter.isEmpty() || locationFilter == null) {
if (spaceFilter.isEmpty() || spaceFilter == null) {
return emptyList();
}

Set<String> filteredRegions = new HashSet<>();
// IF an Org is provided without spaces -> add all spaces for the ORG
for (String orgName : locationFilter.keySet()) {
if (locationFilter.get(orgName).isEmpty() || locationFilter.get(orgName) == null) {
for (String orgName : spaceFilter.keySet()) {
if (spaceFilter.get(orgName).isEmpty() || spaceFilter.get(orgName) == null) {
List<CloudFoundrySpace> allSpacesByOrg =
this.getCredentials()
.getSpaces()
.findAllBySpaceNamesAndOrgNames(null, singletonList(orgName));
spaces.addAll(allSpacesByOrg);
} else {
for (String spaceName : locationFilter.get(orgName)) {
for (String spaceName : spaceFilter.get(orgName)) {
filteredRegions.add(orgName + " > " + spaceName);
}
}
Expand All @@ -246,17 +247,15 @@ protected List<CloudFoundrySpace> createFilteredSpaces(Map<String, Set<String>>
this.getCredentials()
.getSpaces()
.findAllBySpaceNamesAndOrgNames(
locationFilter.values().stream()
.flatMap(l -> l.stream())
.collect(Collectors.toList()),
List.copyOf(locationFilter.keySet()));
spaceFilter.values().stream().flatMap(l -> l.stream()).collect(Collectors.toList()),
List.copyOf(spaceFilter.keySet()));
allSpaces.stream()
.filter(s -> filteredRegions.contains(s.getRegion()))
.forEach(s -> spaces.add(s));

if (spaces.isEmpty())
throw new IllegalArgumentException(
"The locationFilter had Orgs and/or Spaces but CloudFoundry returned no spaces as a result. Spaces must not be null or empty when a locationFilter is included.");
"The spaceFilter had Orgs and/or Spaces but CloudFoundry returned no spaces as a result. Spaces must not be null or empty when a spaceFilter is included.");

return ImmutableList.copyOf(spaces);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class CloudFoundryCredentialsTest {
private final CloudFoundryClient cloudFoundryClient = new MockCloudFoundryClient();

@Test
void emptyLocationFilterShouldConvertToEmptyList() {
void emptySpaceFilterShouldConvertToEmptyList() {
CloudFoundryCredentials credentials =
new CloudFoundryCredentials(
"test",
Expand All @@ -63,7 +63,7 @@ void emptyLocationFilterShouldConvertToEmptyList() {
}

@Test
void singleOrgLocationFilterShouldConvert() {
void singleOrgSpaceFilterShouldConvert() {
CloudFoundryCredentials credentials =
new CloudFoundryCredentials(
"test",
Expand All @@ -88,7 +88,7 @@ public CloudFoundryClient getCredentials() {
}
};

Map<String, Set<String>> locationFilter = ImmutableMap.of("org", emptySet());
Map<String, Set<String>> spaceFilter = ImmutableMap.of("org", emptySet());

CloudFoundryOrganization organization =
CloudFoundryOrganization.builder().id("org123").name("org").build();
Expand All @@ -107,12 +107,12 @@ public CloudFoundryClient getCredentials() {

when(cloudFoundryClient.getSpaces().findAllBySpaceNamesAndOrgNames(isNull(), any()))
.thenReturn(List.of(space1, space2));
List<CloudFoundrySpace> result = credentials.createFilteredSpaces(locationFilter);
List<CloudFoundrySpace> result = credentials.createFilteredSpaces(spaceFilter);
assertThat(result).isEqualTo(List.of(space1, space2));
}

@Test
void singleOrgSingleSpaceLocationFilterShouldConvert() {
void singleOrgSingleSpaceSpaceFilterShouldConvert() {
CloudFoundryCredentials credentials =
new CloudFoundryCredentials(
"test",
Expand All @@ -137,7 +137,7 @@ public CloudFoundryClient getCredentials() {
}
};

Map<String, Set<String>> locationFilter = ImmutableMap.of("org", Set.of("space1"));
Map<String, Set<String>> spaceFilter = ImmutableMap.of("org", Set.of("space1"));

CloudFoundryOrganization organization =
CloudFoundryOrganization.builder().id("org123").name("org").build();
Expand All @@ -156,12 +156,12 @@ public CloudFoundryClient getCredentials() {

when(cloudFoundryClient.getSpaces().findAllBySpaceNamesAndOrgNames(any(), any()))
.thenReturn(List.of(space1, space2));
List<CloudFoundrySpace> result = credentials.createFilteredSpaces(locationFilter);
List<CloudFoundrySpace> result = credentials.createFilteredSpaces(spaceFilter);
assertThat(result).isEqualTo(List.of(space1));
}

@Test
void fakeOrgFakeSpaceLocationFilterShouldThrowError() {
void fakeOrgFakeSpaceSpaceFilterShouldThrowError() {
CloudFoundryCredentials credentials =
new CloudFoundryCredentials(
"test",
Expand All @@ -186,14 +186,14 @@ public CloudFoundryClient getCredentials() {
}
};

Map<String, Set<String>> locationFilter = ImmutableMap.of("org", Set.of("space1"));
Map<String, Set<String>> spaceFilter = ImmutableMap.of("org", Set.of("space1"));

when(cloudFoundryClient.getSpaces().findAllBySpaceNamesAndOrgNames(any(), any()))
.thenReturn(emptyList());
Exception e =
assertThrows(Exception.class, () -> credentials.createFilteredSpaces(locationFilter));
assertThrows(Exception.class, () -> credentials.createFilteredSpaces(spaceFilter));
assertThat(e)
.hasMessageContaining(
"The locationFilter had Orgs and/or Spaces but CloudFoundry returned no spaces as a result. Spaces must not be null or empty when a locationFilter is included.");
"The spaceFilter had Orgs and/or Spaces but CloudFoundry returned no spaces as a result. Spaces must not be null or empty when a spaceFilter is included.");
}
}