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

import org.cloudfoundry.client.v3.serviceinstances.ServiceInstanceType;
import org.cloudfoundry.multiapps.controller.client.facade.CloudCredentials;
Expand All @@ -30,7 +32,8 @@ public CustomServiceKeysClient(ApplicationConfiguration configuration, WebClient
}

public List<DeployedMtaServiceKey> getServiceKeysByMetadataAndGuids(String spaceGuid, String mtaId, String mtaNamespace,
List<DeployedMtaService> services) {
List<DeployedMtaService> services,
List<String> existingServiceGuids) {
String labelSelector = MtaMetadataCriteriaBuilder.builder()
.label(MtaMetadataLabels.SPACE_GUID)
.hasValue(spaceGuid)
Expand All @@ -43,20 +46,46 @@ public List<DeployedMtaServiceKey> getServiceKeysByMetadataAndGuids(String space
.build()
.get();

return new CustomControllerClientErrorHandler().handleErrorsOrReturnResult(
() -> getServiceKeysByMetadataInternal(labelSelector, services));
List<String> managedGuids = extractManagedServiceGuids(services);

List<String> allServiceGuids = Stream.concat(managedGuids.stream(), existingServiceGuids.stream())
.filter(Objects::nonNull)
.distinct()
.toList();

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

return getServiceKeysByMetadataInternal(labelSelector, allServiceGuids);
}

private List<String> extractManagedServiceGuids(List<DeployedMtaService> services) {
if (services == null || services.isEmpty()) {
return List.of();
}
List<DeployedMtaService> managed = getManagedServices(services);
if (managed == null || managed.isEmpty()) {
return List.of();
}
return managed.stream()
.map(s -> s.getGuid() != null ? s.getGuid()
.toString() : null)
.filter(Objects::nonNull)
.distinct()
.toList();
}

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(","));
private List<DeployedMtaServiceKey> getServiceKeysByMetadataInternal(String labelSelector, List<String> guids) {

if (guids == null || guids.isEmpty()) {
return List.of();
}
return getListOfResources(new ServiceKeysResponseMapper(managedServices), SERVICE_KEYS_BY_METADATA_SELECTOR_URI + uriSuffix,
String uriSuffix = INCLUDE_SERVICE_INSTANCE_RESOURCES_PARAM
+ "&service_instance_guids=" + String.join(",", guids);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,24 @@
import org.apache.commons.lang3.StringUtils;
import org.cloudfoundry.multiapps.controller.client.facade.CloudControllerClient;
import org.cloudfoundry.multiapps.controller.client.facade.CloudCredentials;
import org.cloudfoundry.multiapps.controller.client.facade.domain.CloudServiceInstance;
import org.cloudfoundry.multiapps.controller.core.cf.clients.CustomServiceKeysClient;
import org.cloudfoundry.multiapps.controller.core.cf.clients.WebClientFactory;
import org.cloudfoundry.multiapps.controller.core.cf.detect.DeployedMtaDetector;
import org.cloudfoundry.multiapps.controller.core.cf.metadata.MtaMetadata;
import org.cloudfoundry.multiapps.controller.core.cf.v2.ResourceType;
import org.cloudfoundry.multiapps.controller.core.model.DeployedMta;
import org.cloudfoundry.multiapps.controller.core.model.DeployedMtaService;
import org.cloudfoundry.multiapps.controller.core.model.DeployedMtaServiceKey;
import org.cloudfoundry.multiapps.controller.core.security.serialization.SecureSerialization;
import org.cloudfoundry.multiapps.controller.core.security.token.TokenService;
import org.cloudfoundry.multiapps.controller.core.util.CloudModelBuilderUtil;
import org.cloudfoundry.multiapps.controller.core.util.NameUtil;
import org.cloudfoundry.multiapps.controller.process.Constants;
import org.cloudfoundry.multiapps.controller.process.Messages;
import org.cloudfoundry.multiapps.controller.process.variables.Variables;
import org.cloudfoundry.multiapps.mta.model.DeploymentDescriptor;
import org.cloudfoundry.multiapps.mta.model.Resource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
Expand Down Expand Up @@ -94,15 +99,51 @@ private void detectBackupMta(String mtaId, String mtaNamespace, CloudControllerC

private List<DeployedMtaServiceKey> detectDeployedServiceKeys(String mtaId, String mtaNamespace, DeployedMta deployedMta,
ProcessContext context) {
List<DeployedMtaService> deployedMtaServices = deployedMta == null ? null : deployedMta.getServices();
List<DeployedMtaService> deployedManagedMtaServices = deployedMta == null ? null : deployedMta.getServices();
String spaceGuid = context.getVariable(Variables.SPACE_GUID);
String user = context.getVariable(Variables.USER);
String userGuid = context.getVariable(Variables.USER_GUID);
var token = tokenService.getToken(user, userGuid);
var creds = new CloudCredentials(token, true);

CustomServiceKeysClient serviceKeysClient = getCustomServiceKeysClient(creds, context.getVariable(Variables.CORRELATION_ID));
return serviceKeysClient.getServiceKeysByMetadataAndGuids(spaceGuid, mtaId, mtaNamespace, deployedMtaServices);

List<String> existingInstanceGuids = getExistingServiceGuids(context);

return serviceKeysClient.getServiceKeysByMetadataAndGuids(
spaceGuid, mtaId, mtaNamespace, deployedManagedMtaServices, existingInstanceGuids
);
}

private List<String> getExistingServiceGuids(ProcessContext context) {
CloudControllerClient client = context.getControllerClient();
List<String> existingNames = getExistingServiceNamesFromDescriptor(context);

return existingNames.stream()
.map(name -> resolveServiceGuid(client, name))
.toList();
}

private String resolveServiceGuid(CloudControllerClient client, String serviceName) {
CloudServiceInstance instance = client.getServiceInstance(serviceName, false);
return instance != null && instance.getMetadata() != null
? instance.getMetadata()
.getGuid()
.toString() : null;
}

private List<String> getExistingServiceNamesFromDescriptor(ProcessContext context) {
DeploymentDescriptor descriptor = context.getVariable(Variables.DEPLOYMENT_DESCRIPTOR);

if (descriptor == null || descriptor.getResources() == null) {
return List.of();
}
return descriptor.getResources()
.stream()
.filter(resource -> CloudModelBuilderUtil.getResourceType(resource) == ResourceType.EXISTING_SERVICE)
.map(Resource::getName)
.distinct()
.toList();
}

private void logNoMtaDeployedDetected(String mtaId, String mtaNamespace) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import org.cloudfoundry.client.v3.Metadata;
import org.cloudfoundry.multiapps.common.test.TestUtil;
import org.cloudfoundry.multiapps.common.test.Tester.Expectation;
import org.cloudfoundry.multiapps.common.test.Tester;
import org.cloudfoundry.multiapps.common.util.JsonUtil;
import org.cloudfoundry.multiapps.controller.client.facade.CloudControllerClient;
import org.cloudfoundry.multiapps.controller.client.facade.CloudCredentials;
Expand All @@ -31,6 +31,12 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.when;

class DetectDeployedMtaStepTest extends SyncFlowableStepTest<DetectDeployedMtaStep> {
Expand Down Expand Up @@ -62,28 +68,47 @@ void testExecuteWithDeployedMta() {
.getKeys();
List<DeployedMta> deployedComponents = List.of(deployedMta);

when(deployedMtaDetector.detectDeployedMtas(Mockito.any(CloudControllerClient.class))).thenReturn(deployedComponents);
when(deployedMtaDetector.detectDeployedMtaByNameAndNamespace(Mockito.eq(MTA_ID), Mockito.eq(null),
Mockito.any(
CloudControllerClient.class))).thenReturn(
Optional.of(deployedMta));
when(customClientMock.getServiceKeysByMetadataAndGuids(Mockito.eq(SPACE_GUID), Mockito.eq(MTA_ID), Mockito.isNull(),
Mockito.eq(deployedMta.getServices()))).thenReturn(deployedKeys);
when(deployedMtaDetector.detectDeployedMtas(any(CloudControllerClient.class)))
.thenReturn(deployedComponents);
when(deployedMtaDetector.detectDeployedMtaByNameAndNamespace(eq(MTA_ID), eq(null),
any(CloudControllerClient.class)))
.thenReturn(Optional.of(deployedMta));

when(customClientMock.getServiceKeysByMetadataAndGuids(
eq(SPACE_GUID), eq(MTA_ID), isNull(),
eq(deployedMta.getServices()),
argThat(List::isEmpty)))
.thenReturn(deployedKeys);

when(customClientMock.getServiceKeysByMetadataAndGuids(
eq(SPACE_GUID), eq(MTA_ID), isNull(),
anyList(),
argThat(list -> list != null && !list.isEmpty())))
.thenReturn(Collections.emptyList());

step.execute(execution);

assertStepFinishedSuccessfully();

tester.test(() -> context.getVariable(Variables.DEPLOYED_MTA), new Expectation(Expectation.Type.JSON, DEPLOYED_MTA_LOCATION));
tester.test(() -> context.getVariable(Variables.DEPLOYED_MTA),
new Tester.Expectation(Tester.Expectation.Type.JSON, DEPLOYED_MTA_LOCATION));
assertEquals(deployedKeys, context.getVariable(Variables.DEPLOYED_MTA_SERVICE_KEYS));
}

@Test
void testExecuteWithoutDeployedMta() {
when(deployedMtaDetector.detectDeployedMtas(client)).thenReturn(Collections.emptyList());
when(deployedMtaDetector.detectDeployedMtaByNameAndNamespace(MTA_ID, null, client)).thenReturn(Optional.empty());
when(customClientMock.getServiceKeysByMetadataAndGuids(SPACE_GUID, MTA_ID, null,
Collections.emptyList())).thenReturn(Collections.emptyList());
when(customClientMock.getServiceKeysByMetadataAndGuids(
eq(SPACE_GUID), eq(MTA_ID), isNull(),
eq(Collections.emptyList()),
eq(Collections.emptyList())))
.thenReturn(Collections.emptyList());

when(customClientMock.getServiceKeysByMetadataAndGuids(
anyString(), anyString(), isNull(),
anyList(), anyList()))
.thenReturn(Collections.emptyList());

step.execute(execution);

Expand Down Expand Up @@ -130,10 +155,10 @@ void testExecuteWithBackupdMta() {
.build())
.build();

when(deployedMtaDetector.detectDeployedMtaByNameAndNamespace(Mockito.eq(MTA_ID), Mockito.eq(null),
when(deployedMtaDetector.detectDeployedMtaByNameAndNamespace(eq(MTA_ID), eq(null),
Mockito.any())).thenReturn(Optional.of(deployedMta));
when(deployedMtaDetector.detectDeployedMtaByNameAndNamespace(Mockito.eq(MTA_ID),
Mockito.eq(NameUtil.computeUserNamespaceWithSystemNamespace(
when(deployedMtaDetector.detectDeployedMtaByNameAndNamespace(eq(MTA_ID),
eq(NameUtil.computeUserNamespaceWithSystemNamespace(
Constants.MTA_BACKUP_NAMESPACE,
null)),
Mockito.any())).thenReturn(Optional.of(backupMta));
Expand Down
Loading