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 d863b6b commit 2071783
Show file tree
Hide file tree
Showing 16 changed files with 941 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 @@ -1966,12 +1966,14 @@ components:
- $ref: "#/components/schemas/ApiV1"
- $ref: "#/components/schemas/ApiV2"
- $ref: "#/components/schemas/ApiV4"
- $ref: "#/components/schemas/ApiFederated"
discriminator:
propertyName: definitionVersion
mapping:
V1: ApiV1
V2: ApiV2
V4: ApiV4
FEDERATED: ApiFederated

BaseApi:
type: object
Expand Down Expand Up @@ -2106,6 +2108,7 @@ components:
V1: ApiV1
V2: ApiV2
V4: ApiV4
FEDERATED: ApiFederated
required:
- definitionVersion
ApiV1:
Expand Down Expand Up @@ -2210,6 +2213,13 @@ components:
$ref: "#/components/schemas/FlowV4"
services:
$ref: "#/components/schemas/ApiServices"

ApiFederated:
type: object
title: "ApiFederated"
allOf:
- $ref: "#/components/schemas/GenericApi"

ApiLinks:
type: object
properties:
Expand Down Expand Up @@ -2413,11 +2423,13 @@ components:
oneOf:
- $ref: "#/components/schemas/UpdateApiV2"
- $ref: "#/components/schemas/UpdateApiV4"
- $ref: "#/components/schemas/UpdateApiFederated"
discriminator:
propertyName: definitionVersion
mapping:
V2: UpdateApiV2
V4: UpdateApiV4
FEDERATED: UpdateApiFederated
UpdateGenericApi:
type: object
properties:
Expand Down Expand Up @@ -2498,6 +2510,7 @@ components:
mapping:
V2: UpdateApiV2
V4: UpdateApiV4
FEDERATED: UpdateApiFederated
UpdateApiV2:
type: object
title: "UpdateApiV2"
Expand Down Expand Up @@ -2555,6 +2568,12 @@ components:
$ref: "#/components/schemas/ApiServices"
required: [listeners, endpointGroups, type]

UpdateApiFederated:
type: object
title: "UpdateApiFederated"
allOf:
- $ref: "#/components/schemas/UpdateGenericApi"

ApiDeployment:
type: object
properties:
Expand Down Expand Up @@ -2761,6 +2780,7 @@ components:
- V1
- V2
- V4
- FEDERATED
EndpointV4:
type: object
properties:
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 comments on commit 2071783

Please sign in to comment.