|
21 | 21 | import static java.util.Collections.emptyList; |
22 | 22 | import static java.util.Collections.singletonList; |
23 | 23 |
|
| 24 | +import io.gravitee.apim.core.api.domain_service.OAIDomainService; |
| 25 | +import io.gravitee.apim.core.api.exception.InvalidPathsException; |
24 | 26 | import io.gravitee.apim.core.api.model.UpdateNativeApi; |
25 | 27 | import io.gravitee.apim.core.api.model.crd.IDExportStrategy; |
| 28 | +import io.gravitee.apim.core.api.model.import_definition.ImportDefinition; |
26 | 29 | import io.gravitee.apim.core.api.model.utils.MigrationResult; |
27 | 30 | import io.gravitee.apim.core.api.use_case.DetachAutomatedApiUseCase; |
28 | 31 | import io.gravitee.apim.core.api.use_case.ExportApiCRDUseCase; |
|
31 | 34 | import io.gravitee.apim.core.api.use_case.GetExposedEntrypointsUseCase; |
32 | 35 | import io.gravitee.apim.core.api.use_case.MigrateApiUseCase; |
33 | 36 | import io.gravitee.apim.core.api.use_case.RollbackApiUseCase; |
| 37 | +import io.gravitee.apim.core.api.use_case.UpdateApiDefinitionUseCase; |
34 | 38 | import io.gravitee.apim.core.api.use_case.UpdateFederatedApiUseCase; |
35 | 39 | import io.gravitee.apim.core.api.use_case.UpdateNativeApiUseCase; |
36 | 40 | import io.gravitee.apim.core.audit.model.AuditActor; |
37 | 41 | import io.gravitee.apim.core.audit.model.AuditInfo; |
38 | 42 | import io.gravitee.apim.core.audit.model.Excludable; |
| 43 | +import io.gravitee.apim.core.flow.crud_service.FlowCrudService; |
39 | 44 | import io.gravitee.apim.core.promotion.use_case.CreatePromotionUseCase; |
40 | 45 | import io.gravitee.apim.infra.adapter.ApiAdapter; |
41 | 46 | import io.gravitee.common.component.Lifecycle; |
42 | 47 | import io.gravitee.common.data.domain.Page; |
43 | 48 | import io.gravitee.common.http.MediaType; |
44 | 49 | import io.gravitee.definition.model.Proxy; |
45 | 50 | import io.gravitee.definition.model.VirtualHost; |
| 51 | +import io.gravitee.definition.model.v4.ApiType; |
| 52 | +import io.gravitee.definition.model.v4.flow.AbstractFlow; |
| 53 | +import io.gravitee.definition.model.v4.flow.Flow; |
| 54 | +import io.gravitee.definition.model.v4.flow.selector.HttpSelector; |
46 | 55 | import io.gravitee.definition.model.v4.listener.Listener; |
47 | 56 | import io.gravitee.definition.model.v4.listener.ListenerType; |
48 | 57 | import io.gravitee.definition.model.v4.listener.http.HttpListener; |
|
59 | 68 | import io.gravitee.rest.api.management.v2.rest.model.ApiTransferOwnership; |
60 | 69 | import io.gravitee.rest.api.management.v2.rest.model.DuplicateApiOptions; |
61 | 70 | import io.gravitee.rest.api.management.v2.rest.model.Error; |
| 71 | +import io.gravitee.rest.api.management.v2.rest.model.ExportApiV4; |
| 72 | +import io.gravitee.rest.api.management.v2.rest.model.ImportSwaggerDescriptor; |
62 | 73 | import io.gravitee.rest.api.management.v2.rest.model.MigrationReportResponses; |
63 | 74 | import io.gravitee.rest.api.management.v2.rest.model.MigrationReportResponsesIssuesInner; |
64 | 75 | import io.gravitee.rest.api.management.v2.rest.model.MigrationStateType; |
|
84 | 95 | import io.gravitee.rest.api.management.v2.rest.resource.documentation.ApiPagesResource; |
85 | 96 | import io.gravitee.rest.api.management.v2.rest.resource.param.LifecycleAction; |
86 | 97 | import io.gravitee.rest.api.management.v2.rest.resource.param.PaginationParam; |
| 98 | +import io.gravitee.rest.api.model.ImportSwaggerDescriptorEntity; |
87 | 99 | import io.gravitee.rest.api.model.InlinePictureEntity; |
88 | 100 | import io.gravitee.rest.api.model.MembershipMemberType; |
89 | 101 | import io.gravitee.rest.api.model.RoleEntity; |
|
127 | 139 | import io.gravitee.rest.api.service.v4.ApiLicenseService; |
128 | 140 | import io.gravitee.rest.api.service.v4.ApiStateService; |
129 | 141 | import io.gravitee.rest.api.service.v4.ApiWorkflowStateService; |
| 142 | +import io.gravitee.rest.api.service.v4.exception.InvalidPathException; |
130 | 143 | import io.swagger.v3.oas.annotations.parameters.RequestBody; |
131 | 144 | import jakarta.annotation.Nullable; |
132 | 145 | import jakarta.inject.Inject; |
|
158 | 171 | import java.util.Map; |
159 | 172 | import java.util.Objects; |
160 | 173 | import java.util.Set; |
| 174 | +import java.util.function.Function; |
161 | 175 | import java.util.stream.Collectors; |
162 | 176 | import java.util.stream.Stream; |
163 | 177 | import lombok.CustomLog; |
@@ -247,6 +261,15 @@ public class ApiResource extends AbstractResource { |
247 | 261 | @Inject |
248 | 262 | private DetachAutomatedApiUseCase detachAutomatedApiUseCase; |
249 | 263 |
|
| 264 | + @Inject |
| 265 | + private UpdateApiDefinitionUseCase updateApiDefinitionUseCase; |
| 266 | + |
| 267 | + @Inject |
| 268 | + private OAIDomainService oaiDomainService; |
| 269 | + |
| 270 | + @Inject |
| 271 | + private FlowCrudService flowCrudService; |
| 272 | + |
250 | 273 | @Context |
251 | 274 | protected UriInfo uriInfo; |
252 | 275 |
|
@@ -322,6 +345,122 @@ public Response getApiById(@PathParam("apiId") String apiId) { |
322 | 345 | return apiResponse(apiEntity); |
323 | 346 | } |
324 | 347 |
|
| 348 | + @PUT |
| 349 | + @Path("/_import/definition") |
| 350 | + @Consumes(MediaType.APPLICATION_JSON) |
| 351 | + @Produces(MediaType.APPLICATION_JSON) |
| 352 | + @Permissions({ @Permission(value = RolePermission.API_DEFINITION, acls = RolePermissionAction.UPDATE) }) |
| 353 | + public Response updateApiWithDefinition(@PathParam("apiId") String apiId, @Valid ExportApiV4 apiToImport) { |
| 354 | + verifyApiImage(apiToImport.getApiPicture(), "picture"); |
| 355 | + verifyApiImage(apiToImport.getApiBackground(), "background"); |
| 356 | + |
| 357 | + ImportDefinition importDefinition = ImportExportApiMapper.INSTANCE.toImportDefinition(apiToImport); |
| 358 | + |
| 359 | + var apiExport = importDefinition.getApiExport(); |
| 360 | + if (apiExport != null && apiExport.getFlows() != null && !apiExport.getFlows().isEmpty()) { |
| 361 | + var incomingFlows = apiExport.getFlows(); |
| 362 | + List<? extends AbstractFlow> existingFlows = ApiType.NATIVE.equals(apiExport.getType()) |
| 363 | + ? flowCrudService.getNativeApiFlows(apiId) |
| 364 | + : flowCrudService.getApiV4Flows(apiId); |
| 365 | + for (int i = 0; i < incomingFlows.size() && i < existingFlows.size(); i++) { |
| 366 | + incomingFlows.get(i).setId(existingFlows.get(i).getId()); |
| 367 | + } |
| 368 | + } |
| 369 | + |
| 370 | + try { |
| 371 | + var audit = getAuditInfo(); |
| 372 | + UpdateApiDefinitionUseCase.Output output = updateApiDefinitionUseCase.execute( |
| 373 | + new UpdateApiDefinitionUseCase.Input(apiId, importDefinition, audit) |
| 374 | + ); |
| 375 | + |
| 376 | + boolean isSynchronized = apiStateService.isSynchronized( |
| 377 | + GraviteeContext.getExecutionContext(), |
| 378 | + getGenericApiEntityById(apiId, false) |
| 379 | + ); |
| 380 | + |
| 381 | + return Response.ok().entity(ApiMapper.INSTANCE.map(output.apiWithFlows(), uriInfo, isSynchronized)).build(); |
| 382 | + } catch (InvalidPathsException e) { |
| 383 | + throw new InvalidPathException("Cannot import API with invalid paths", e); |
| 384 | + } |
| 385 | + } |
| 386 | + |
| 387 | + @PUT |
| 388 | + @Path("/_import/swagger") |
| 389 | + @Consumes(MediaType.APPLICATION_JSON) |
| 390 | + @Produces(MediaType.APPLICATION_JSON) |
| 391 | + @Permissions({ @Permission(value = RolePermission.API_DEFINITION, acls = RolePermissionAction.UPDATE) }) |
| 392 | + public Response updateApiFromSwagger(@PathParam("apiId") String apiId, @Valid @NotNull ImportSwaggerDescriptor descriptor) { |
| 393 | + try { |
| 394 | + var audit = getAuditInfo(); |
| 395 | + var withPolicyPaths = Boolean.TRUE.equals(descriptor.getWithPolicyPaths()); |
| 396 | + var importSwaggerDescriptor = ImportSwaggerDescriptorEntity.builder() |
| 397 | + .payload(descriptor.getPayload()) |
| 398 | + .withDocumentation(Boolean.TRUE.equals(descriptor.getWithDocumentation())) |
| 399 | + .withPolicies(descriptor.getWithPolicies() != null ? new ArrayList<>(descriptor.getWithPolicies()) : null) |
| 400 | + .withPolicyPaths(withPolicyPaths) |
| 401 | + .build(); |
| 402 | + |
| 403 | + var importDefinition = oaiDomainService.convert( |
| 404 | + audit.organizationId(), |
| 405 | + audit.environmentId(), |
| 406 | + importSwaggerDescriptor, |
| 407 | + Boolean.TRUE.equals(descriptor.getWithDocumentation()), |
| 408 | + Boolean.TRUE.equals(descriptor.getWithOASValidationPolicy()) |
| 409 | + ); |
| 410 | + |
| 411 | + if (importDefinition == null) { |
| 412 | + throw new BadRequestException("Unable to read the swagger specification"); |
| 413 | + } |
| 414 | + |
| 415 | + var apiExport = importDefinition.getApiExport(); |
| 416 | + var flows = apiExport != null ? apiExport.getFlows() : null; |
| 417 | + if (!withPolicyPaths && flows != null && flows.stream().anyMatch(f -> f instanceof Flow flow && flow.getId() == null)) { |
| 418 | + Function<Flow, String> httpFlowKey = flow -> |
| 419 | + flow.getSelectors() == null |
| 420 | + ? "" |
| 421 | + : flow |
| 422 | + .getSelectors() |
| 423 | + .stream() |
| 424 | + .filter(HttpSelector.class::isInstance) |
| 425 | + .map(HttpSelector.class::cast) |
| 426 | + .map(http -> { |
| 427 | + var methods = http.getMethods() != null |
| 428 | + ? http.getMethods().stream().map(Enum::name).sorted().collect(Collectors.joining(",")) |
| 429 | + : ""; |
| 430 | + return http.getPath() + "|" + methods; |
| 431 | + }) |
| 432 | + .collect(Collectors.joining(";")); |
| 433 | + var idByKey = flowCrudService |
| 434 | + .getApiV4Flows(apiId) |
| 435 | + .stream() |
| 436 | + .filter(f -> f.getId() != null) |
| 437 | + .collect(Collectors.toMap(httpFlowKey, Flow::getId, (a, b) -> a)); |
| 438 | + flows |
| 439 | + .stream() |
| 440 | + .filter(f -> f instanceof Flow flow && flow.getId() == null) |
| 441 | + .map(Flow.class::cast) |
| 442 | + .forEach(flow -> { |
| 443 | + var existingId = idByKey.get(httpFlowKey.apply(flow)); |
| 444 | + if (existingId != null) { |
| 445 | + flow.setId(existingId); |
| 446 | + } |
| 447 | + }); |
| 448 | + } |
| 449 | + |
| 450 | + UpdateApiDefinitionUseCase.Output output = updateApiDefinitionUseCase.execute( |
| 451 | + new UpdateApiDefinitionUseCase.Input(apiId, importDefinition, audit) |
| 452 | + ); |
| 453 | + |
| 454 | + boolean isSynchronized = apiStateService.isSynchronized( |
| 455 | + GraviteeContext.getExecutionContext(), |
| 456 | + getGenericApiEntityById(apiId, false) |
| 457 | + ); |
| 458 | + return Response.ok().entity(ApiMapper.INSTANCE.map(output.apiWithFlows(), uriInfo, isSynchronized)).build(); |
| 459 | + } catch (InvalidPathsException e) { |
| 460 | + throw new InvalidPathException("Cannot import API with invalid paths", e); |
| 461 | + } |
| 462 | + } |
| 463 | + |
325 | 464 | @PUT |
326 | 465 | @Consumes(MediaType.APPLICATION_JSON) |
327 | 466 | @Produces(MediaType.APPLICATION_JSON) |
@@ -1169,4 +1308,13 @@ private void assertNoPrimaryOwnerReassignment(String poRole) { |
1169 | 1308 | throw new TransferOwnershipNotAllowedException(poRole); |
1170 | 1309 | } |
1171 | 1310 | } |
| 1311 | + |
| 1312 | + private static void verifyApiImage(String imageContent, String imageUsage) { |
| 1313 | + try { |
| 1314 | + ImageUtils.verify(imageContent); |
| 1315 | + } catch (InvalidImageException e) { |
| 1316 | + log.warn("Error while parsing {} while importing api", imageUsage, e); |
| 1317 | + throw new BadRequestException("Invalid image format for api " + imageUsage); |
| 1318 | + } |
| 1319 | + } |
1172 | 1320 | } |
0 commit comments