Skip to content
Merged
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 @@ -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 @@ -132,8 +132,10 @@ private List<String> getExistingServiceGuids(ProcessContext context) {
}

private Optional<String> resolveServiceGuid(CloudControllerClient client, Resource resource) {
String serviceInstanceName = NameUtil.getServiceInstanceNameOrDefault(resource);

try {
CloudServiceInstance instance = client.getServiceInstance(resource.getName());
CloudServiceInstance instance = client.getServiceInstance(serviceInstanceName);
return Optional.of(instance.getGuid()
.toString());
} catch (CloudOperationException e) {
Expand Down
Loading