Skip to content
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 @@ -3,7 +3,8 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;

import org.cloudfoundry.client.v3.serviceinstances.ServiceInstanceType;
Expand All @@ -29,8 +30,12 @@ public CustomServiceKeysClient(ApplicationConfiguration configuration, WebClient
super(configuration, webClientFactory, credentials, correlationId);
}

public List<DeployedMtaServiceKey> getServiceKeysByMetadataAndGuids(String spaceGuid, String mtaId, String mtaNamespace,
List<DeployedMtaService> services) {
public List<DeployedMtaServiceKey> getServiceKeysByMetadataAndExistingGuids(
String spaceGuid,
String mtaId,
String mtaNamespace,
List<String> existingServiceGuids) {

String labelSelector = MtaMetadataCriteriaBuilder.builder()
.label(MtaMetadataLabels.SPACE_GUID)
.hasValue(spaceGuid)
Expand All @@ -43,31 +48,73 @@ public List<DeployedMtaServiceKey> getServiceKeysByMetadataAndGuids(String space
.build()
.get();

return new CustomControllerClientErrorHandler().handleErrorsOrReturnResult(
() -> getServiceKeysByMetadataInternal(labelSelector, services));
List<String> allServiceGuids = existingServiceGuids.stream()
.filter(Objects::nonNull)
.toList();

if (allServiceGuids.isEmpty()) {
return List.of();
}

return new CustomControllerClientErrorHandler()
.handleErrorsOrReturnResult(
() -> getServiceKeysByMetadataInternal(labelSelector, allServiceGuids)
);
}

private List<DeployedMtaServiceKey> getServiceKeysByMetadataInternal(String labelSelector, List<DeployedMtaService> services) {
String uriSuffix = INCLUDE_SERVICE_INSTANCE_RESOURCES_PARAM;
List<DeployedMtaService> managedServices = getManagedServices(services);
if (managedServices != null) {
uriSuffix += "&service_instance_guids=" + managedServices.stream()
.map(service -> service.getGuid()
.toString())
.collect(Collectors.joining(","));
public List<DeployedMtaServiceKey> getServiceKeysByMetadataAndManagedServices(
String spaceGuid,
String mtaId,
String mtaNamespace,
List<DeployedMtaService> services) {

String labelSelector = MtaMetadataCriteriaBuilder.builder()
.label(MtaMetadataLabels.SPACE_GUID)
.hasValue(spaceGuid)
.and()
.label(MtaMetadataLabels.MTA_NAMESPACE)
.hasValueOrIsntPresent(MtaMetadataUtil.getHashedLabel(mtaNamespace))
.and()
.label(MtaMetadataLabels.MTA_ID)
.hasValue(MtaMetadataUtil.getHashedLabel(mtaId))
.build()
.get();

List<String> managedGuids = extractManagedServiceGuids(services).stream()
.filter(Objects::nonNull)
.toList();

if (managedGuids.isEmpty()) {
return List.of();
}
return getListOfResources(new ServiceKeysResponseMapper(managedServices), SERVICE_KEYS_BY_METADATA_SELECTOR_URI + uriSuffix,

return new CustomControllerClientErrorHandler()
.handleErrorsOrReturnResult(
() -> getServiceKeysByMetadataInternal(labelSelector, managedGuids)
);
}

private List<String> extractManagedServiceGuids(List<DeployedMtaService> services) {
return getManagedServices(services).stream()
.map(DeployedMtaService::getGuid)
.map(UUID::toString)
.toList();
}

private List<DeployedMtaServiceKey> getServiceKeysByMetadataInternal(String labelSelector, List<String> guids) {

String uriSuffix = INCLUDE_SERVICE_INSTANCE_RESOURCES_PARAM
+ "&service_instance_guids=" + String.join(",", guids);

return getListOfResources(new ServiceKeysResponseMapper(),
SERVICE_KEYS_BY_METADATA_SELECTOR_URI + uriSuffix,
labelSelector);
}

private List<DeployedMtaService> getManagedServices(List<DeployedMtaService> services) {
if (services == null) {
return null;
}
List<DeployedMtaService> managedServices = services.stream()
.filter(this::serviceIsNotUserProvided)
.collect(Collectors.toList());
return managedServices.isEmpty() ? null : managedServices;
return services.stream()
.filter(this::serviceIsNotUserProvided)
.toList();
}

private boolean serviceIsNotUserProvided(DeployedMtaService service) {
Expand All @@ -76,23 +123,12 @@ private boolean serviceIsNotUserProvided(DeployedMtaService service) {
}

protected class ServiceKeysResponseMapper extends ResourcesResponseMapper<DeployedMtaServiceKey> {

List<DeployedMtaService> mtaServices;

public ServiceKeysResponseMapper(List<DeployedMtaService> mtaServices) {
this.mtaServices = mtaServices;
public ServiceKeysResponseMapper() {
}

@Override
public List<DeployedMtaServiceKey> getMappedResources() {
Map<String, CloudServiceInstance> serviceMapping;
if (mtaServices != null) {
serviceMapping = mtaServices.stream()
.collect(Collectors.toMap(service -> service.getGuid()
.toString(), Function.identity()));
} else {
serviceMapping = getIncludedServiceInstancesMapping();
}
Map<String, CloudServiceInstance> serviceMapping = getIncludedServiceInstancesMapping();
return getQueriedResources().stream()
.map(resource -> resourceMapper.mapServiceKeyResource(resource, serviceMapping))
.collect(Collectors.toList());
Expand All @@ -108,4 +144,4 @@ public Map<String, CloudServiceInstance> getIncludedServiceInstancesMapping() {

}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ public static String computeNamespacedNameWithLength(String name, String namespa

private static String getNameWithNamespaceSuffix(String name, String namespace, int maxLength) {
String namespaceSuffix = getNamespaceSuffix(namespace);
String shortenedName = getNameWithProperLength(name, calculateNameLengthWithoutNamespaceAndBlueGreenSuffix(namespaceSuffix, maxLength));
String shortenedName = getNameWithProperLength(name,
calculateNameLengthWithoutNamespaceAndBlueGreenSuffix(namespaceSuffix, maxLength));

return correctNameSuffix(shortenedName, name, namespaceSuffix);
}
Expand Down Expand Up @@ -120,6 +121,7 @@ public static String computeUserNamespaceWithSystemNamespace(String systemNamesp
}
return systemNamespace;
}

private static String getShortenedName(String name, int maxLength) {
String nameHashCode = getHashCodeAsHexString(name);
if (maxLength < nameHashCode.length()) {
Expand Down Expand Up @@ -157,6 +159,14 @@ public static String getServiceName(Resource resource) {
.get(SupportedParameters.SERVICE_NAME);
}

public static String getServiceInstanceNameOrDefault(Resource resource) {
String serviceInstanceName = getServiceName(resource);
if (serviceInstanceName == null || serviceInstanceName.isBlank()) {
return resource.getName();
}
return serviceInstanceName;
}

public static class NameRequirements {

public static final String XS_APP_NAME_PATTERN = "(?!sap_system)[a-zA-Z0-9\\._\\-\\\\/]{1,240}";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package org.cloudfoundry.multiapps.controller.core.util;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

import org.cloudfoundry.multiapps.controller.core.model.SupportedParameters;
import org.cloudfoundry.multiapps.controller.core.util.NameUtil.NameRequirements;
import org.cloudfoundry.multiapps.mta.model.Resource;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class NameUtilTest {

static Stream<Arguments> testGetNameWithProperLength() {
Expand Down Expand Up @@ -94,4 +98,43 @@ void testComputeNamespacedNameWithLength(String name, String namespace, boolean
NameUtil.computeNamespacedNameWithLength(name, namespace, applyNamespace, applyNamespaceAsSuffix, maxLength));
}

@Test
void testGetServiceInstanceNameOrDefault_UsesServiceNameParameter() {
Resource resource = createResource("resource-name", "service-name-from-param");

String serviceInstanceName = NameUtil.getServiceInstanceNameOrDefault(resource);

assertEquals("service-name-from-param", serviceInstanceName);
}

@Test
void testGetServiceInstanceNameOrDefault_FallsBackToResourceNameWhenServiceNameMissing() {
Resource resource = createResource("resource-name-1", null);

String serviceInstanceName = NameUtil.getServiceInstanceNameOrDefault(resource);

assertEquals("resource-name-1", serviceInstanceName);
}

@Test
void testGetServiceInstanceNameOrDefault_FallsBackToResourceNameWhenServiceNameBlank() {
Resource resource = createResource("resource-name-2", " ");

String serviceInstanceName = NameUtil.getServiceInstanceNameOrDefault(resource);

assertEquals("resource-name-2", serviceInstanceName);
}

private Resource createResource(String resourceName, String serviceInstanceName) {
Resource resource = Resource.createV3();
resource.setName(resourceName);

Map<String, Object> params = new HashMap<>();
if (serviceInstanceName != null) {
params.put(SupportedParameters.SERVICE_NAME, serviceInstanceName);
}
resource.setParameters(params);

return resource;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,9 @@ public class Messages {

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

public static final String IGNORING_NOT_FOUND_OPTIONAL_SERVICE = "Service {0} not found but is optional";
public static final String IGNORING_NOT_FOUND_INACTIVE_SERVICE = "Service {0} not found but is inactive";

// Not log messages
public static final String SERVICE_TYPE = "{0}/{1}";
public static final String PARSE_NULL_STRING_ERROR = "Cannot parse null string";
Expand Down
Loading
Loading