Skip to content

Commit 8fe5113

Browse files
authored
Move the logic for deleting existing service keys (#1763)
1 parent 4f59da3 commit 8fe5113

File tree

9 files changed

+368
-65
lines changed

9 files changed

+368
-65
lines changed

multiapps-controller-core/src/main/java/org/cloudfoundry/multiapps/controller/core/cf/clients/CustomServiceKeysClient.java

Lines changed: 79 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import java.util.Collections;
44
import java.util.List;
55
import java.util.Map;
6-
import java.util.function.Function;
6+
import java.util.Objects;
7+
import java.util.UUID;
78
import java.util.stream.Collectors;
89

910
import org.cloudfoundry.client.v3.serviceinstances.ServiceInstanceType;
@@ -29,45 +30,87 @@ public CustomServiceKeysClient(ApplicationConfiguration configuration, WebClient
2930
super(configuration, webClientFactory, credentials, correlationId);
3031
}
3132

32-
public List<DeployedMtaServiceKey> getServiceKeysByMetadataAndGuids(String spaceGuid, String mtaId, String mtaNamespace,
33-
List<DeployedMtaService> services) {
34-
String labelSelector = MtaMetadataCriteriaBuilder.builder()
35-
.label(MtaMetadataLabels.SPACE_GUID)
36-
.hasValue(spaceGuid)
37-
.and()
38-
.label(MtaMetadataLabels.MTA_NAMESPACE)
39-
.hasValueOrIsntPresent(MtaMetadataUtil.getHashedLabel(mtaNamespace))
40-
.and()
41-
.label(MtaMetadataLabels.MTA_ID)
42-
.hasValue(MtaMetadataUtil.getHashedLabel(mtaId))
43-
.build()
44-
.get();
45-
46-
return new CustomControllerClientErrorHandler().handleErrorsOrReturnResult(
47-
() -> getServiceKeysByMetadataInternal(labelSelector, services));
33+
public List<DeployedMtaServiceKey> getServiceKeysByMetadataAndExistingGuids(
34+
String spaceGuid,
35+
String mtaId,
36+
String mtaNamespace,
37+
List<String> existingServiceGuids) {
38+
39+
String labelSelector = buildMtaMetadataLabelSelector(spaceGuid, mtaId, mtaNamespace);
40+
41+
List<String> allServiceGuids = existingServiceGuids.stream()
42+
.filter(Objects::nonNull)
43+
.toList();
44+
45+
if (allServiceGuids.isEmpty()) {
46+
return List.of();
47+
}
48+
49+
return new CustomControllerClientErrorHandler()
50+
.handleErrorsOrReturnResult(
51+
() -> getServiceKeysByMetadataInternal(labelSelector, allServiceGuids)
52+
);
4853
}
4954

50-
private List<DeployedMtaServiceKey> getServiceKeysByMetadataInternal(String labelSelector, List<DeployedMtaService> services) {
51-
String uriSuffix = INCLUDE_SERVICE_INSTANCE_RESOURCES_PARAM;
52-
List<DeployedMtaService> managedServices = getManagedServices(services);
53-
if (managedServices != null) {
54-
uriSuffix += "&service_instance_guids=" + managedServices.stream()
55-
.map(service -> service.getGuid()
56-
.toString())
57-
.collect(Collectors.joining(","));
55+
public List<DeployedMtaServiceKey> getServiceKeysByMetadataAndManagedServices(
56+
String spaceGuid,
57+
String mtaId,
58+
String mtaNamespace,
59+
List<DeployedMtaService> services) {
60+
61+
String labelSelector = buildMtaMetadataLabelSelector(spaceGuid, mtaId, mtaNamespace);
62+
63+
List<String> managedGuids = extractManagedServiceGuids(services);
64+
65+
if (managedGuids.isEmpty()) {
66+
return List.of();
5867
}
59-
return getListOfResources(new ServiceKeysResponseMapper(managedServices), SERVICE_KEYS_BY_METADATA_SELECTOR_URI + uriSuffix,
68+
69+
return new CustomControllerClientErrorHandler()
70+
.handleErrorsOrReturnResult(
71+
() -> getServiceKeysByMetadataInternal(labelSelector, managedGuids)
72+
);
73+
}
74+
75+
private String buildMtaMetadataLabelSelector(String spaceGuid,
76+
String mtaId,
77+
String mtaNamespace) {
78+
79+
return MtaMetadataCriteriaBuilder.builder()
80+
.label(MtaMetadataLabels.SPACE_GUID)
81+
.hasValue(spaceGuid)
82+
.and()
83+
.label(MtaMetadataLabels.MTA_NAMESPACE)
84+
.hasValueOrIsntPresent(MtaMetadataUtil.getHashedLabel(mtaNamespace))
85+
.and()
86+
.label(MtaMetadataLabels.MTA_ID)
87+
.hasValue(MtaMetadataUtil.getHashedLabel(mtaId))
88+
.build()
89+
.get();
90+
}
91+
92+
private List<String> extractManagedServiceGuids(List<DeployedMtaService> services) {
93+
return getManagedServices(services).stream()
94+
.map(DeployedMtaService::getGuid)
95+
.map(UUID::toString)
96+
.filter(Objects::nonNull)
97+
.toList();
98+
}
99+
100+
private List<DeployedMtaServiceKey> getServiceKeysByMetadataInternal(String labelSelector, List<String> guids) {
101+
102+
String uriSuffix = INCLUDE_SERVICE_INSTANCE_RESOURCES_PARAM
103+
+ "&service_instance_guids=" + String.join(",", guids);
104+
105+
return getListOfResources(new ServiceKeysResponseMapper(),
106+
SERVICE_KEYS_BY_METADATA_SELECTOR_URI + uriSuffix,
60107
labelSelector);
61108
}
62109

63110
private List<DeployedMtaService> getManagedServices(List<DeployedMtaService> services) {
64-
if (services == null) {
65-
return null;
66-
}
67-
List<DeployedMtaService> managedServices = services.stream()
68-
.filter(this::serviceIsNotUserProvided)
69-
.collect(Collectors.toList());
70-
return managedServices.isEmpty() ? null : managedServices;
111+
return services.stream()
112+
.filter(this::serviceIsNotUserProvided)
113+
.toList();
71114
}
72115

73116
private boolean serviceIsNotUserProvided(DeployedMtaService service) {
@@ -76,23 +119,12 @@ private boolean serviceIsNotUserProvided(DeployedMtaService service) {
76119
}
77120

78121
protected class ServiceKeysResponseMapper extends ResourcesResponseMapper<DeployedMtaServiceKey> {
79-
80-
List<DeployedMtaService> mtaServices;
81-
82-
public ServiceKeysResponseMapper(List<DeployedMtaService> mtaServices) {
83-
this.mtaServices = mtaServices;
122+
public ServiceKeysResponseMapper() {
84123
}
85124

86125
@Override
87126
public List<DeployedMtaServiceKey> getMappedResources() {
88-
Map<String, CloudServiceInstance> serviceMapping;
89-
if (mtaServices != null) {
90-
serviceMapping = mtaServices.stream()
91-
.collect(Collectors.toMap(service -> service.getGuid()
92-
.toString(), Function.identity()));
93-
} else {
94-
serviceMapping = getIncludedServiceInstancesMapping();
95-
}
127+
Map<String, CloudServiceInstance> serviceMapping = getIncludedServiceInstancesMapping();
96128
return getQueriedResources().stream()
97129
.map(resource -> resourceMapper.mapServiceKeyResource(resource, serviceMapping))
98130
.collect(Collectors.toList());
@@ -108,4 +140,4 @@ public Map<String, CloudServiceInstance> getIncludedServiceInstancesMapping() {
108140

109141
}
110142
}
111-
}
143+
}

multiapps-controller-core/src/main/java/org/cloudfoundry/multiapps/controller/core/util/NameUtil.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ public static String computeNamespacedNameWithLength(String name, String namespa
7373

7474
private static String getNameWithNamespaceSuffix(String name, String namespace, int maxLength) {
7575
String namespaceSuffix = getNamespaceSuffix(namespace);
76-
String shortenedName = getNameWithProperLength(name, calculateNameLengthWithoutNamespaceAndBlueGreenSuffix(namespaceSuffix, maxLength));
76+
String shortenedName = getNameWithProperLength(name,
77+
calculateNameLengthWithoutNamespaceAndBlueGreenSuffix(namespaceSuffix, maxLength));
7778

7879
return correctNameSuffix(shortenedName, name, namespaceSuffix);
7980
}
@@ -120,6 +121,7 @@ public static String computeUserNamespaceWithSystemNamespace(String systemNamesp
120121
}
121122
return systemNamespace;
122123
}
124+
123125
private static String getShortenedName(String name, int maxLength) {
124126
String nameHashCode = getHashCodeAsHexString(name);
125127
if (maxLength < nameHashCode.length()) {
@@ -157,6 +159,14 @@ public static String getServiceName(Resource resource) {
157159
.get(SupportedParameters.SERVICE_NAME);
158160
}
159161

162+
public static String getServiceInstanceNameOrDefault(Resource resource) {
163+
String serviceInstanceName = getServiceName(resource);
164+
if (StringUtils.isBlank(serviceInstanceName)) {
165+
return resource.getName();
166+
}
167+
return serviceInstanceName;
168+
}
169+
160170
public static class NameRequirements {
161171

162172
public static final String XS_APP_NAME_PATTERN = "(?!sap_system)[a-zA-Z0-9\\._\\-\\\\/]{1,240}";

multiapps-controller-core/src/test/java/org/cloudfoundry/multiapps/controller/core/util/NameUtilTest.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
package org.cloudfoundry.multiapps.controller.core.util;
22

3-
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
import static org.junit.jupiter.api.Assertions.assertTrue;
5-
3+
import java.util.HashMap;
4+
import java.util.Map;
65
import java.util.stream.Stream;
76

7+
import org.cloudfoundry.multiapps.controller.core.model.SupportedParameters;
88
import org.cloudfoundry.multiapps.controller.core.util.NameUtil.NameRequirements;
9+
import org.cloudfoundry.multiapps.mta.model.Resource;
910
import org.junit.jupiter.api.Test;
1011
import org.junit.jupiter.params.ParameterizedTest;
1112
import org.junit.jupiter.params.provider.Arguments;
1213
import org.junit.jupiter.params.provider.MethodSource;
1314

15+
import static org.junit.jupiter.api.Assertions.assertEquals;
16+
import static org.junit.jupiter.api.Assertions.assertTrue;
17+
1418
class NameUtilTest {
1519

1620
static Stream<Arguments> testGetNameWithProperLength() {
@@ -64,6 +68,13 @@ static Stream<Arguments> testComputeNamespacedNameWithLength() {
6468
"long-long-name-long-long-name-long-long-nam18cc4f54-limit-idle"));
6569
}
6670

71+
static Stream<Arguments> serviceInstanceNameCases() {
72+
return Stream.of(Arguments.of("resource-name", "service-name-from-param", "service-name-from-param"),
73+
Arguments.of("resource-name-1", null, "resource-name-1"),
74+
Arguments.of("resource-name-2", " ", "resource-name-2"),
75+
Arguments.of("resource-name-3", "", "resource-name-3"));
76+
}
77+
6778
@ParameterizedTest
6879
@MethodSource
6980
void testGetNameWithProperLength(String name, int maxLength, String expectedName) {
@@ -94,4 +105,25 @@ void testComputeNamespacedNameWithLength(String name, String namespace, boolean
94105
NameUtil.computeNamespacedNameWithLength(name, namespace, applyNamespace, applyNamespaceAsSuffix, maxLength));
95106
}
96107

108+
@ParameterizedTest
109+
@MethodSource
110+
void serviceInstanceNameCases(String resourceName, String serviceNameParam, String expected) {
111+
Resource resource = createResource(resourceName, serviceNameParam);
112+
String serviceInstanceName = NameUtil.getServiceInstanceNameOrDefault(resource);
113+
114+
assertEquals(expected, serviceInstanceName);
115+
}
116+
117+
private Resource createResource(String resourceName, String serviceInstanceName) {
118+
Resource resource = Resource.createV3();
119+
resource.setName(resourceName);
120+
121+
Map<String, Object> params = new HashMap<>();
122+
if (serviceInstanceName != null) {
123+
params.put(SupportedParameters.SERVICE_NAME, serviceInstanceName);
124+
}
125+
resource.setParameters(params);
126+
127+
return resource;
128+
}
97129
}

multiapps-controller-process/src/main/java/org/cloudfoundry/multiapps/controller/process/Messages.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,9 @@ public class Messages {
790790

791791
public static final String TOTAL_SIZE_OF_ALL_RESOLVED_CONTENT_0 = "Total size for all resolved content {0}";
792792

793+
public static final String IGNORING_NOT_FOUND_OPTIONAL_SERVICE = "Service {0} not found but is optional";
794+
public static final String IGNORING_NOT_FOUND_INACTIVE_SERVICE = "Service {0} not found but is inactive";
795+
793796
// Not log messages
794797
public static final String SERVICE_TYPE = "{0}/{1}";
795798
public static final String PARSE_NULL_STRING_ERROR = "Cannot parse null string";

0 commit comments

Comments
 (0)