/** * Counts active integrations (in DB) of the owner of the specified integration. * * @param integration The specified integration. * @return The number of integrations (excluding the current). */ private int countActiveIntegrationsOfSameUserAs(Integration integration) { String integrationId = integration.getId().orElseThrow(() -> new IllegalStateException("Couldn't find the id of the integration.")); String username = integration.getUserId().orElseThrow(() -> new IllegalStateException("Couldn't find the user of the integration")); return (int) dataManager.fetchAll(IntegrationDeployment.class).getItems() .stream() .filter(i -> !i.getIntegrationId().get().equals(integrationId)) //The "current" integration will already be in the database. .filter(i -> IntegrationDeploymentState.Active == i.getCurrentState()) .map(i -> i.getIntegrationId().get()) .distinct() .map(i -> dataManager.fetch(Integration.class, i)) .filter(i -> i.getUserId().map(username::equals).orElse(Boolean.FALSE)) .count(); }
/** * Count the deployments of the owner of the specified integration. * * @param integration The specified integration. * @return The number of deployed integrations (excluding the current). */ private int countDeployments(Integration integration) { String id = Labels.sanitize(integration.getId().orElseThrow(() -> new IllegalStateException("Couldn't find the id of the integration"))); String username = integration.getUserId().orElseThrow(() -> new IllegalStateException("Couldn't find the user of the integration")); Map<String, String> labels = new HashMap<>(); labels.put(OpenShiftService.USERNAME_LABEL, Labels.sanitize(username)); return (int) openShiftService().getDeploymentsByLabel(labels) .stream() .filter(d -> !id.equals(d.getMetadata().getLabels().get(OpenShiftService.INTEGRATION_ID_LABEL))) .filter(d -> d.getSpec().getReplicas() > 0) .count(); }
private DeploymentData createDeploymentData(Integration integration, IntegrationDeployment integrationDeployment) { Properties applicationProperties = IntegrationSupport.buildApplicationProperties(integrationDeployment, dataManager, encryptionComponent); String username = integration.getUserId().orElseThrow(() -> new IllegalStateException("Couldn't find the user of the integration")); return DeploymentData.builder() .addLabel(OpenShiftService.INTEGRATION_ID_LABEL, Labels.sanitize(integrationDeployment.getIntegrationId().orElseThrow(() -> new IllegalStateException("IntegrationDeployment should have an integrationId")))) .addLabel(OpenShiftService.DEPLOYMENT_ID_LABEL, integrationDeployment.getVersion().orElse(0).toString()) .addLabel(OpenShiftService.USERNAME_LABEL, Labels.sanitize(username)) .addSecretEntry("application.properties", propsToString(applicationProperties)) .build(); }