use of io.syndesis.common.model.openapi.OpenApi in project syndesis by syndesisio.
the class IntegrationSpecificationHandler method update.
@PUT
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Operation(description = "For an integration that is generated from a specification updates it so it conforms to the updated specification")
@RequestBody(content = @Content(mediaType = "multipart/form-data", schema = @Schema(implementation = ApiHandler.APIFormData.class)), description = "Next revision of the specification")
public void update(@NotNull @PathParam("id") @Parameter(required = true, example = "integration-id", description = "The ID of the integration") final String id, @NotNull @MultipartForm final ApiHandler.APIFormData apiFormData) {
final Integration existing = integrationHandler.getIntegration(id);
final APIIntegration apiIntegration = ApiGeneratorHelper.generateIntegrationUpdateFrom(existing, apiFormData, dataManager, apiGenerator);
final Integration givenIntegration = apiIntegration.getIntegration();
final Integration updated = ApiGeneratorHelper.updateFlowsAndStartAndEndDataShapes(existing, givenIntegration);
final OpenApi existingApiSpecification = ApiGeneratorHelper.specificationFrom(resourceManager, existing).orElse(null);
if (Objects.equals(existing.getFlows(), updated.getFlows()) && Objects.equals(existingApiSpecification, apiIntegration.getSpec())) {
// no changes were made to the flows or to the specification
return;
}
// store the OpenAPI resource, we keep the old one as it might
// be referenced from Integration's stored in IntegrationDeployent's
// this gives us a rollback mechanism
dataManager.store(apiIntegration.getSpec(), OpenApi.class);
// perform the regular update
integrationHandler.update(id, updated);
}
use of io.syndesis.common.model.openapi.OpenApi in project syndesis by syndesisio.
the class OpenApiCustomizer method customize.
@Override
public Integration customize(IntegrationDeployment deployment, Integration integration, EnumSet<Exposure> exposure) {
IntegrationSpec.Builder spec = new IntegrationSpec.Builder();
if (integration.getSpec() != null) {
spec = spec.from(integration.getSpec());
}
// assuming that we have a single swagger definition for the moment
Optional<ResourceIdentifier> rid = deployment.getSpec().getResources().stream().filter(Kind.OpenApi::sameAs).findFirst();
if (!rid.isPresent()) {
return integration;
}
final ResourceIdentifier openApiResource = rid.get();
final Optional<String> maybeOpenApiResourceId = openApiResource.getId();
if (!maybeOpenApiResourceId.isPresent()) {
return integration;
}
final String openApiResourceId = maybeOpenApiResourceId.get();
Optional<OpenApi> res = resourceManager.loadOpenApiDefinition(openApiResourceId);
if (!res.isPresent()) {
return integration;
}
try {
spec.addResources(generateOpenAPIResource(res.get()));
spec.addSources(generateOpenAPIRestDSL(res.get()));
spec.addSources(generateOpenAPIRestEndpoint());
} catch (Exception e) {
throw new IllegalStateException(e);
}
integration.setSpec(spec.build());
return integration;
}
use of io.syndesis.common.model.openapi.OpenApi in project syndesis by syndesisio.
the class IntegrationSpecificationHandlerTest method shouldStoreUpdatedSpecificationForNonFlowChanges.
@Test
public void shouldStoreUpdatedSpecificationForNonFlowChanges() {
final Step step = new Step.Builder().action(new ConnectorAction.Builder().descriptor(new ConnectorDescriptor.Builder().build()).build()).build();
final Integration integration = new Integration.Builder().id("integration-1").addFlow(new Flow.Builder().putMetadata(OpenApi.OPERATION_ID, "flow1").addSteps(step, step).build()).build();
final byte[] updatedSpecificationDocument = "updated specification".getBytes(StandardCharsets.UTF_8);
final OpenApi updatedSpecification = new OpenApi.Builder().document(updatedSpecificationDocument).build();
final APIIntegration updatedApiIntegration = new APIIntegration(integration, updatedSpecification);
when(dataManager.fetch(Connection.class, "api-provider")).thenReturn(new Connection.Builder().connectorId("api-provider-connector").build());
when(dataManager.fetch(Connector.class, "api-provider-connector")).thenReturn(new Connector.Builder().build());
when(dataManager.fetch(Integration.class, "integration-1")).thenReturn(integration);
when(encryptionSupport.encrypt(integration)).thenReturn(integration);
when(apiGenerator.generateIntegration(any(String.class), any(ProvidedApiTemplate.class))).thenReturn(updatedApiIntegration);
when(apiGenerator.updateFlowExcerpts(any(Integration.class))).then(ctx -> ctx.getArguments()[0]);
final APIFormData openApiUpdate = new APIFormData();
openApiUpdate.setSpecification(new ByteArrayInputStream(updatedSpecificationDocument));
handler.update("integration-1", openApiUpdate);
verify(dataManager).store(updatedSpecification, OpenApi.class);
verify(dataManager).update(ArgumentMatchers.<Integration>argThat(v -> {
assertThat(v).isEqualToIgnoringGivenFields(integration, "version", "updatedAt");
assertThat(v.getVersion()).isEqualTo(2);
return true;
}));
}
use of io.syndesis.common.model.openapi.OpenApi in project syndesis by syndesisio.
the class ProjectGenerator method addRestDefinition.
private void addRestDefinition(TarArchiveOutputStream tos, Integration integration) throws IOException {
// assuming that we have a single swagger definition for the moment
Optional<ResourceIdentifier> rid = integration.getResources().stream().filter(Kind.OpenApi::sameAs).findFirst();
if (!rid.isPresent()) {
return;
}
final ResourceIdentifier openApiResource = rid.get();
final Optional<String> maybeOpenApiResourceId = openApiResource.getId();
if (!maybeOpenApiResourceId.isPresent()) {
return;
}
final String openApiResourceId = maybeOpenApiResourceId.get();
Optional<OpenApi> res = resourceManager.loadOpenApiDefinition(openApiResourceId);
if (!res.isPresent()) {
return;
}
final byte[] openApiBytes = res.get().getDocument();
final Document openApiDoc = Library.readDocumentFromJSONString(new String(openApiBytes, StandardCharsets.UTF_8));
if (!(openApiDoc instanceof OasDocument)) {
throw new IllegalArgumentException(String.format("Unsupported OpenAPI document type: %s - %s", openApiDoc.getClass(), openApiDoc.getDocumentType()));
}
final StringBuilder code = new StringBuilder();
RestDslGenerator.toAppendable((OasDocument) openApiDoc).withClassName("RestRoute").withPackageName("io.syndesis.example").withoutSourceCodeTimestamps().generate(code);
addTarEntry(tos, "src/main/java/io/syndesis/example/RestRoute.java", code.toString().getBytes(StandardCharsets.UTF_8));
addTarEntry(tos, "src/main/java/io/syndesis/example/RestRouteConfiguration.java", ProjectGeneratorHelper.generate(integration, restRoutesMustache));
addTarEntry(tos, "src/main/resources/openapi.json", openApiBytes);
}
use of io.syndesis.common.model.openapi.OpenApi in project syndesis by syndesisio.
the class IntegrationExportSource method getOpenApis.
@Override
public Map<String, OpenApi> getOpenApis() {
Map<String, OpenApi> openApis = new HashMap<>();
try {
JsonNode apis = model.get("open-apis");
if (apis != null) {
Iterator<Map.Entry<String, JsonNode>> fields = apis.fields();
while (fields.hasNext()) {
Map.Entry<String, JsonNode> fieldEntry = fields.next();
openApis.put(fieldEntry.getKey(), JsonUtils.reader().forType(OpenApi.class).readValue(fieldEntry.getValue()));
}
}
} catch (IOException e) {
throw new IllegalStateException("Failed to read open apis from export", e);
}
return openApis;
}
Aggregations