Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement DiscoverIntegrationAssetUseCase
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
1 parent
1765f47
commit 1e7f71e
Showing
14 changed files
with
890 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...main/java/io/gravitee/apim/core/api/domain_service/ValidateFederatedApiDomainService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...c/main/java/io/gravitee/apim/core/integration/exception/IntegrationNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...ee-apim-rest-api-service/src/main/java/io/gravitee/apim/core/integration/model/Asset.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...est-api-service/src/main/java/io/gravitee/apim/core/integration/spi/IntegrationAgent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
111 changes: 111 additions & 0 deletions
111
...main/java/io/gravitee/apim/core/integration/use_case/DiscoverIntegrationAssetUseCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...tee-apim-rest-api-service/src/test/java/fixtures/core/model/IntegrationAssetFixtures.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
Oops, something went wrong.