Search in sources :

Example 1 with OpenApi

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);
}
Also used : APIIntegration(io.syndesis.server.api.generator.APIIntegration) Integration(io.syndesis.common.model.integration.Integration) APIIntegration(io.syndesis.server.api.generator.APIIntegration) OpenApi(io.syndesis.common.model.openapi.OpenApi) Consumes(javax.ws.rs.Consumes) Operation(io.swagger.v3.oas.annotations.Operation) PUT(javax.ws.rs.PUT) RequestBody(io.swagger.v3.oas.annotations.parameters.RequestBody)

Example 2 with OpenApi

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;
}
Also used : IntegrationSpec(io.syndesis.server.controller.integration.camelk.crd.IntegrationSpec) ResourceIdentifier(io.syndesis.common.model.ResourceIdentifier) Kind(io.syndesis.common.model.Kind) OpenApi(io.syndesis.common.model.openapi.OpenApi) IOException(java.io.IOException)

Example 3 with OpenApi

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;
    }));
}
Also used : ConnectorDescriptor(io.syndesis.common.model.action.ConnectorDescriptor) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ConnectorAction(io.syndesis.common.model.action.ConnectorAction) OpenApi(io.syndesis.common.model.openapi.OpenApi) ArgumentMatchers(org.mockito.ArgumentMatchers) Step(io.syndesis.common.model.integration.Step) ResourceIdentifier(io.syndesis.common.model.ResourceIdentifier) Kind(io.syndesis.common.model.Kind) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) APIIntegration(io.syndesis.server.api.generator.APIIntegration) EncryptionComponent(io.syndesis.server.dao.manager.EncryptionComponent) ByteArrayInputStream(java.io.ByteArrayInputStream) Connection(io.syndesis.common.model.connection.Connection) DataManager(io.syndesis.server.dao.manager.DataManager) Integration(io.syndesis.common.model.integration.Integration) Status(javax.ws.rs.core.Response.Status) Connector(io.syndesis.common.model.connection.Connector) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Flow(io.syndesis.common.model.integration.Flow) StandardCharsets(java.nio.charset.StandardCharsets) Mockito.verify(org.mockito.Mockito.verify) ProvidedApiTemplate(io.syndesis.server.api.generator.ProvidedApiTemplate) HttpHeaders(javax.ws.rs.core.HttpHeaders) Response(javax.ws.rs.core.Response) Optional(java.util.Optional) IntegrationResourceManager(io.syndesis.integration.api.IntegrationResourceManager) APIGenerator(io.syndesis.server.api.generator.APIGenerator) APIFormData(io.syndesis.server.endpoint.v1.handler.api.ApiHandler.APIFormData) Mockito.mock(org.mockito.Mockito.mock) APIIntegration(io.syndesis.server.api.generator.APIIntegration) Integration(io.syndesis.common.model.integration.Integration) Connection(io.syndesis.common.model.connection.Connection) Step(io.syndesis.common.model.integration.Step) Flow(io.syndesis.common.model.integration.Flow) ConnectorDescriptor(io.syndesis.common.model.action.ConnectorDescriptor) ByteArrayInputStream(java.io.ByteArrayInputStream) ProvidedApiTemplate(io.syndesis.server.api.generator.ProvidedApiTemplate) APIIntegration(io.syndesis.server.api.generator.APIIntegration) OpenApi(io.syndesis.common.model.openapi.OpenApi) APIFormData(io.syndesis.server.endpoint.v1.handler.api.ApiHandler.APIFormData) Test(org.junit.Test)

Example 4 with OpenApi

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);
}
Also used : OasDocument(io.apicurio.datamodels.openapi.models.OasDocument) ResourceIdentifier(io.syndesis.common.model.ResourceIdentifier) Kind(io.syndesis.common.model.Kind) StepKind(io.syndesis.common.model.integration.StepKind) Document(io.apicurio.datamodels.core.models.Document) OasDocument(io.apicurio.datamodels.openapi.models.OasDocument) OpenApi(io.syndesis.common.model.openapi.OpenApi)

Example 5 with OpenApi

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;
}
Also used : ZipEntry(java.util.zip.ZipEntry) HashMap(java.util.HashMap) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) OpenApi(io.syndesis.common.model.openapi.OpenApi) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

OpenApi (io.syndesis.common.model.openapi.OpenApi)11 ResourceIdentifier (io.syndesis.common.model.ResourceIdentifier)7 Integration (io.syndesis.common.model.integration.Integration)7 Kind (io.syndesis.common.model.Kind)5 APIIntegration (io.syndesis.server.api.generator.APIIntegration)5 Step (io.syndesis.common.model.integration.Step)4 Connection (io.syndesis.common.model.connection.Connection)3 Connector (io.syndesis.common.model.connection.Connector)3 Flow (io.syndesis.common.model.integration.Flow)3 APIGenerator (io.syndesis.server.api.generator.APIGenerator)3 ProvidedApiTemplate (io.syndesis.server.api.generator.ProvidedApiTemplate)3 DataManager (io.syndesis.server.dao.manager.DataManager)3 StandardCharsets (java.nio.charset.StandardCharsets)3 HashMap (java.util.HashMap)3 Optional (java.util.Optional)3 Test (org.junit.Test)3 OasDocument (io.apicurio.datamodels.openapi.models.OasDocument)2 Operation (io.swagger.v3.oas.annotations.Operation)2 ConnectorAction (io.syndesis.common.model.action.ConnectorAction)2 ConnectorDescriptor (io.syndesis.common.model.action.ConnectorDescriptor)2