Skip to content

Commit

Permalink
feat: implement DiscoverIntegrationAssetUseCase
Browse files Browse the repository at this point in the history
Implement business rules for the Federation Discovery feature. It is
currently not "callable" as we don't have any endpoint on the REST API
or an actual implementation of IntegrationAgent. However, it should
contain everything necessary for the creation of the new type of API

https://gravitee.atlassian.net/browse/APIM-4204
  • Loading branch information
jgiovaresco committed Mar 28, 2024
1 parent 1765f47 commit 1e7f71e
Show file tree
Hide file tree
Showing 14 changed files with 890 additions and 13 deletions.
Expand Up @@ -32,7 +32,8 @@ public enum DefinitionVersion {
@JsonEnumDefaultValue
V1("1.0.0"),
V2("2.0.0"),
V4("4.0.0");
V4("4.0.0"),
FEDERATED("FEDERATED");

private static final Map<String, DefinitionVersion> BY_LABEL = new HashMap<>();

Expand Down
Expand Up @@ -34,9 +34,11 @@
import io.gravitee.apim.core.parameters.query_service.ParametersQueryService;
import io.gravitee.apim.core.search.Indexer;
import io.gravitee.apim.core.workflow.crud_service.WorkflowCrudService;
import io.gravitee.definition.model.v4.flow.Flow;
import io.gravitee.rest.api.model.parameters.Key;
import io.gravitee.rest.api.model.parameters.ParameterReferenceType;
import java.util.Collections;
import java.util.List;
import java.util.function.UnaryOperator;

@DomainService
Expand Down Expand Up @@ -98,13 +100,13 @@ public ApiWithFlows create(Api api, PrimaryOwnerEntity primaryOwner, AuditInfo a

apiPrimaryOwnerDomainService.createApiPrimaryOwnerMembership(created.getId(), primaryOwner, auditInfo);

createDefaultMailNotification(created.getId());
createDefaultMailNotification(created);

apiMetadataDomainService.createDefaultApiMetadata(created.getId(), auditInfo);
createDefaultMetadata(created, auditInfo);

flowCrudService.saveApiFlows(api.getId(), api.getApiDefinitionV4().getFlows());
var createdFlows = saveApiFlows(api);

if (isApiReviewEnabled(auditInfo.organizationId(), auditInfo.environmentId())) {
if (isApiReviewEnabled(created, auditInfo.organizationId(), auditInfo.environmentId())) {
workflowCrudService.create(newApiReviewWorkflow(api.getId(), auditInfo.actor().userId()));
}

Expand All @@ -113,7 +115,7 @@ public ApiWithFlows create(Api api, PrimaryOwnerEntity primaryOwner, AuditInfo a
created,
primaryOwner
);
return new ApiWithFlows(created, api.getApiDefinitionV4().getFlows());
return new ApiWithFlows(created, createdFlows);
}

private void createAuditLog(Api created, AuditInfo auditInfo) {
Expand All @@ -132,14 +134,38 @@ private void createAuditLog(Api created, AuditInfo auditInfo) {
);
}

private void createDefaultMailNotification(String apiId) {
notificationConfigCrudService.create(NotificationConfig.defaultMailNotificationConfigFor(apiId));
private void createDefaultMailNotification(Api api) {
switch (api.getDefinitionVersion()) {
case V4 -> notificationConfigCrudService.create(NotificationConfig.defaultMailNotificationConfigFor(api.getId()));
case V1, V2, FEDERATED -> {
// nothing to do
}
}
}

private boolean isApiReviewEnabled(String organizationId, String environmentId) {
return parametersQueryService.findAsBoolean(
Key.API_REVIEW_ENABLED,
new ParameterContext(environmentId, organizationId, ParameterReferenceType.ENVIRONMENT)
);
private void createDefaultMetadata(Api api, AuditInfo auditInfo) {
switch (api.getDefinitionVersion()) {
case V4 -> apiMetadataDomainService.createDefaultApiMetadata(api.getId(), auditInfo);
case V1, V2, FEDERATED -> {
// nothing to do
}
}
}

private List<Flow> saveApiFlows(Api api) {
return switch (api.getDefinitionVersion()) {
case V4 -> flowCrudService.saveApiFlows(api.getId(), api.getApiDefinitionV4().getFlows());
case V1, V2, FEDERATED -> null;
};
}

private boolean isApiReviewEnabled(Api api, String organizationId, String environmentId) {
return switch (api.getDefinitionVersion()) {
case V1, V2, V4 -> parametersQueryService.findAsBoolean(
Key.API_REVIEW_ENABLED,
new ParameterContext(environmentId, organizationId, ParameterReferenceType.ENVIRONMENT)
);
case FEDERATED -> false;
};
}
}
@@ -0,0 +1,37 @@
/*
* Copyright © 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.apim.core.api.domain_service;

import io.gravitee.apim.core.DomainService;
import io.gravitee.apim.core.api.model.Api;
import io.gravitee.apim.core.exception.ValidationDomainException;
import io.gravitee.definition.model.DefinitionVersion;
import io.gravitee.rest.api.service.exceptions.InvalidDataException;

@DomainService
public class ValidateFederatedApiDomainService {

public Api validateAndSanitizeForCreation(final Api api) {
if (api.getDefinitionVersion() != DefinitionVersion.FEDERATED) {
throw new ValidationDomainException("Definition version is unsupported, should be FEDERATED");
}

// Reset lifecycle state as Federated API are not deployed on Gateway
api.setLifecycleState(null);

return api;
}
}
@@ -0,0 +1,25 @@
/*
* Copyright © 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.apim.core.integration.exception;

import io.gravitee.apim.core.exception.NotFoundDomainException;

public class IntegrationNotFoundException extends NotFoundDomainException {

public IntegrationNotFoundException(String id) {
super("Integration not found.", id);
}
}
@@ -0,0 +1,21 @@
/*
* Copyright © 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.apim.core.integration.model;

import lombok.Builder;

@Builder(toBuilder = true)
public record Asset(String integrationId, String id, String name, String description, String version) {}
Expand Up @@ -20,6 +20,7 @@
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.With;

/**
* @author Remi Baptiste (remi.baptiste at graviteesource.com)
Expand All @@ -31,7 +32,9 @@
@AllArgsConstructor
public class Integration {

@With
String id;

String name;
String description;
String provider;
Expand Down
@@ -0,0 +1,24 @@
/*
* Copyright © 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.apim.core.integration.spi;

import io.gravitee.apim.core.integration.model.Asset;
import io.gravitee.apim.core.integration.model.Integration;
import io.reactivex.rxjava3.core.Flowable;

public interface IntegrationAgent {
Flowable<Asset> fetchAllAssets(Integration integration);
}
@@ -0,0 +1,111 @@
/*
* Copyright © 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.apim.core.integration.use_case;

import io.gravitee.apim.core.UseCase;
import io.gravitee.apim.core.api.domain_service.CreateApiDomainService;
import io.gravitee.apim.core.api.domain_service.ValidateFederatedApiDomainService;
import io.gravitee.apim.core.api.model.Api;
import io.gravitee.apim.core.audit.model.AuditInfo;
import io.gravitee.apim.core.integration.crud_service.IntegrationCrudService;
import io.gravitee.apim.core.integration.exception.IntegrationNotFoundException;
import io.gravitee.apim.core.integration.model.Asset;
import io.gravitee.apim.core.integration.model.Integration;
import io.gravitee.apim.core.integration.spi.IntegrationAgent;
import io.gravitee.apim.core.membership.domain_service.ApiPrimaryOwnerFactory;
import io.gravitee.common.utils.TimeProvider;
import io.gravitee.definition.model.DefinitionVersion;
import io.gravitee.rest.api.service.common.UuidString;
import io.reactivex.rxjava3.core.Completable;
import io.reactivex.rxjava3.core.Single;
import lombok.extern.slf4j.Slf4j;

@UseCase
@Slf4j
public class DiscoverIntegrationAssetUseCase {

private final IntegrationCrudService integrationCrudService;
private final ApiPrimaryOwnerFactory apiPrimaryOwnerFactory;
private final ValidateFederatedApiDomainService validateFederatedApi;
private final CreateApiDomainService createApiDomainService;
private final IntegrationAgent integrationAgent;

public DiscoverIntegrationAssetUseCase(
IntegrationCrudService integrationCrudService,
ApiPrimaryOwnerFactory apiPrimaryOwnerFactory,
ValidateFederatedApiDomainService validateFederatedApi,
CreateApiDomainService apiCrudService,
IntegrationAgent integrationAgent
) {
this.integrationCrudService = integrationCrudService;
this.apiPrimaryOwnerFactory = apiPrimaryOwnerFactory;
this.validateFederatedApi = validateFederatedApi;
this.createApiDomainService = apiCrudService;
this.integrationAgent = integrationAgent;
}

public Completable execute(Input input) {
var integrationId = input.integrationId;
var auditInfo = input.auditInfo;
var organizationId = auditInfo.organizationId();
var environmentId = auditInfo.environmentId();

var primaryOwner = apiPrimaryOwnerFactory.createForNewApi(organizationId, environmentId, input.auditInfo.actor().userId());

return Single
.fromCallable(() ->
integrationCrudService
.findById(integrationId)
.filter(integration -> integration.getEnvironmentId().equals(environmentId))
.orElseThrow(() -> new IntegrationNotFoundException(integrationId))
)
.flatMapPublisher(integration ->
integrationAgent
.fetchAllAssets(integration)
.doOnNext(asset -> {
try {
createApiDomainService.create(
adaptAssetToApi(asset, integration),
primaryOwner,
auditInfo,
validateFederatedApi::validateAndSanitizeForCreation
);
} catch (Exception e) {
log.warn("An error occurred while importing asset {}", asset, e);
}
})
)
.ignoreElements();
}

public record Input(String integrationId, AuditInfo auditInfo) {}

public Api adaptAssetToApi(Asset asset, Integration integration) {
var now = TimeProvider.now();
return Api
.builder()
.id(UuidString.generateRandom())
.version(asset.version())
.definitionVersion(DefinitionVersion.FEDERATED)
.name(asset.name())
.description(asset.description())
.createdAt(now)
.updatedAt(now)
.environmentId(integration.getEnvironmentId())
.lifecycleState(null)
.build();
}
}
Expand Up @@ -265,4 +265,14 @@ public static Api aTcpApiV4(List<String> hosts) {
)
.build();
}

public static Api aFederatedApi() {
return BASE
.get()
.definitionVersion(DefinitionVersion.FEDERATED)
.lifecycleState(null)
.apiDefinitionV4(null)
.apiDefinition(null)
.build();
}
}
@@ -0,0 +1,37 @@
/*
* Copyright © 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package fixtures.core.model;

import io.gravitee.apim.core.integration.model.Asset;
import java.util.function.Supplier;

public class IntegrationAssetFixtures {

private IntegrationAssetFixtures() {}

private static final Supplier<Asset.AssetBuilder> BASE = () ->
Asset
.builder()
.integrationId("integration-id")
.id("asset-id")
.name("An alien API")
.description("An alien API description")
.version("1.0.0");

public static Asset anAssetForIntegration(String integrationId) {
return BASE.get().integrationId(integrationId).build();
}
}

0 comments on commit 1e7f71e

Please sign in to comment.