Search in sources :

Example 1 with ResourceIdentifier

use of io.syndesis.common.model.ResourceIdentifier 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 2 with ResourceIdentifier

use of io.syndesis.common.model.ResourceIdentifier 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 3 with ResourceIdentifier

use of io.syndesis.common.model.ResourceIdentifier in project syndesis by syndesisio.

the class IntegrationSupportHandler method addResourcesToExport.

private void addResourcesToExport(final JsonDB export, final Integration integration) {
    for (ResourceIdentifier resourceIdentifier : integration.getResources()) {
        if (resourceIdentifier.getKind() == Kind.OpenApi) {
            final Optional<OpenApi> openApiResource = resourceManager.loadOpenApiDefinition(resourceIdentifier.getId().get());
            openApiResource.ifPresent(openApi -> addModelToExport(export, openApi));
        }
    }
}
Also used : ResourceIdentifier(io.syndesis.common.model.ResourceIdentifier) OpenApi(io.syndesis.common.model.openapi.OpenApi)

Example 4 with ResourceIdentifier

use of io.syndesis.common.model.ResourceIdentifier in project syndesis by syndesisio.

the class ApiIntegrationUpdateITCase method shouldUpdateApiIntegrationViaApiGenerator.

@Test
public void shouldUpdateApiIntegrationViaApiGenerator() throws IOException {
    final MultiValueMap<Object, Object> update = specification("/io/syndesis/server/runtime/updated-test-swagger.json");
    update.add("integration", integration(existing));
    final ResponseEntity<Integration> updateResponse = put("/api/v1/apis/generator", update, Integration.class, tokenRule.validToken(), HttpStatus.ACCEPTED, MULTIPART);
    final Integration updated = updateResponse.getBody();
    existing.getId().get();
    // not present in updated-test-swagger.json
    assertThat(updated.findFlowBy(operationIdEquals("fetch-task"))).isNotPresent();
    // new operation added in updated-test-swagger.json
    assertThat(updated.findFlowBy(operationIdEquals("count-tasks"))).isPresent();
    // modified in updated-test-swagger.json
    assertThat(updated.findFlowBy(operationIdEquals("create-task"))).hasValueSatisfying(flow -> {
        final List<Step> steps = flow.getSteps();
        assertThat(steps).hasSize(3);
        assertThat(flow.findStepById("log-step")).isPresent();
        final Step firstStep = steps.get(0);
        final DataShape outputDataShape = firstStep.outputDataShape().get();
        final String outputSpecification = outputDataShape.getSpecification();
        assertThat(outputSpecification).contains("debug", "task");
    });
    final List<ResourceIdentifier> resources = updated.getResources();
    assertThat(resources).hasSize(1);
    final ResourceIdentifier openApiResource = resources.get(0);
    assertThat(openApiResource.getKind()).isEqualTo(Kind.OpenApi);
    final OpenApi openApi = resourceManager.loadOpenApiDefinition(openApiResource.getId().get()).get();
    final String givenJson = getResourceAsText("io/syndesis/server/runtime/updated-test-swagger.json");
    assertThatJson(new String(openApi.getDocument(), StandardCharsets.UTF_8)).whenIgnoringPaths("$..operationId").isEqualTo(givenJson);
}
Also used : ResourceIdentifier(io.syndesis.common.model.ResourceIdentifier) Integration(io.syndesis.common.model.integration.Integration) Step(io.syndesis.common.model.integration.Step) DataShape(io.syndesis.common.model.DataShape) OpenApi(io.syndesis.common.model.openapi.OpenApi) Test(org.junit.Test)

Aggregations

ResourceIdentifier (io.syndesis.common.model.ResourceIdentifier)4 OpenApi (io.syndesis.common.model.openapi.OpenApi)4 Kind (io.syndesis.common.model.Kind)2 Document (io.apicurio.datamodels.core.models.Document)1 OasDocument (io.apicurio.datamodels.openapi.models.OasDocument)1 DataShape (io.syndesis.common.model.DataShape)1 Integration (io.syndesis.common.model.integration.Integration)1 Step (io.syndesis.common.model.integration.Step)1 StepKind (io.syndesis.common.model.integration.StepKind)1 IntegrationSpec (io.syndesis.server.controller.integration.camelk.crd.IntegrationSpec)1 IOException (java.io.IOException)1 Test (org.junit.Test)1