Skip to content

Commit

Permalink
Added servlet registration for api-provider and webhook.
Browse files Browse the repository at this point in the history
  • Loading branch information
valdar committed May 24, 2019
1 parent f946885 commit d4e7de2
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 30 deletions.
2 changes: 1 addition & 1 deletion app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@

<!-- Global camel version used everywhere -->
<camel.version>2.21.0.fuse-740013</camel.version>
<camel-k-runtime.version>0.3.2</camel-k-runtime.version>
<camel-k-runtime.version>0.3.3.fuse-740002</camel-k-runtime.version>

<!-- Don't fork based on cores, doesn't work nicely in the cloud -->
<basepom.test.fork-count>1</basepom.test.fork-count>
Expand Down
6 changes: 6 additions & 0 deletions app/server/controller/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.syndesis.connector</groupId>
<artifactId>connector-webhook</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,28 @@ public final class CamelKSupport {
// IntegrationPhaseWaitingForPlatform IntegrationPhase = "Waiting For Platform"
// // IntegrationPhaseBuildingContext --
// IntegrationPhaseBuildingContext IntegrationPhase = "Building Context"
// // IntegrationPhaseBuildImageSubmitted --
// IntegrationPhaseBuildImageSubmitted IntegrationPhase = "Build Image Submitted"
// // IntegrationPhaseBuildImageRunning --
// IntegrationPhaseBuildImageRunning IntegrationPhase = "Build Image Running"
// // IntegrationPhaseResolvingContext --
// IntegrationPhaseResolvingContext IntegrationPhase = "Resolving Context"
// // IntegrationPhaseDeploying --
// IntegrationPhaseDeploying IntegrationPhase = "Deploying"
// // IntegrationPhaseRunning --
// IntegrationPhaseRunning IntegrationPhase = "Running"
// // IntegrationPhaseError --
// IntegrationPhaseError IntegrationPhase = "Error"
// // IntegrationPhaseBuildFailureRecovery --
// IntegrationPhaseBuildFailureRecovery IntegrationPhase = "Building Failure Recovery"
// // IntegrationPhaseDeleting --
// IntegrationPhaseDeleting IntegrationPhase = "Deleting"

public static final ImmutableSet<String> CAMEL_K_STARTED_STATES = ImmutableSet.of(
"Waiting For Platform",
"Building Context",
"Build Image Submitted",
"Build Image Running",
"Resolving Context",
"Deploying");
public static final ImmutableSet<String> CAMEL_K_FAILED_STATES = ImmutableSet.of(
"Error",
"Building Failure Recovery");
public static final ImmutableSet<String> CAMEL_K_RUNNING_STATES = ImmutableSet.of(
"Running" );
"Running",
"Deleting");

public static final String CAMEL_K_INTEGRATION_CRD_NAME = "integrations.camel.apache.org";
public static final String CAMEL_K_INTEGRATION_CRD_GROUP = "camel.apache.org";
Expand Down Expand Up @@ -182,21 +180,6 @@ public static Properties secretToProperties(Secret secret) {
return properties;
}

// @SuppressWarnings("unchecked")
// public static io.syndesis.server.controller.integration.camelk.crd.Integration getIntegrationCR(
// OpenShiftService openShiftService,
// CustomResourceDefinition integrationCRD,
// IntegrationDeployment integrationDeployment) {
//
// return openShiftService.getCR(
// integrationCRD,
// io.syndesis.server.controller.integration.camelk.crd.Integration.class,
// IntegrationList.class,
// DoneableIntegration.class,
// Names.sanitize(integrationDeployment.getIntegrationId().get())
// );
// }

@SuppressWarnings("unchecked")
public static List<io.syndesis.server.controller.integration.camelk.crd.Integration> getIntegrationCRbyLabels(
OpenShiftService openShiftService,
Expand Down Expand Up @@ -249,6 +232,10 @@ public static String integrationName(String integrationName) {
return Names.sanitize("i-" + integrationName);
}

public static boolean isWebhookPresent(io.syndesis.common.model.integration.Integration integration) {
return integration.getUsedConnectorIds().contains("webhook");
}

public static EnumSet<Exposure> determineExposure(ControllersConfigurationProperties properties, IntegrationDeployment integrationDeployment) {
boolean needsExposure = integrationDeployment.getSpec()
.getFlows().stream()
Expand All @@ -257,7 +244,7 @@ public static EnumSet<Exposure> determineExposure(ControllersConfigurationProper
.flatMap(action -> action.getTags().stream())
.anyMatch("expose"::equals);

boolean webHook = integrationDeployment.getSpec().getUsedConnectorIds().contains("webhook");
boolean webHook = CamelKSupport.isWebhookPresent(integrationDeployment.getSpec());

if (needsExposure) {
if (properties.isExposeVia3scale() && !webHook) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import io.syndesis.server.controller.ControllersConfigurationProperties;
import io.syndesis.server.controller.integration.camelk.CamelKPublishHandler;
import io.syndesis.server.controller.integration.camelk.CamelKSupport;
import io.syndesis.server.controller.integration.camelk.crd.ConfigurationSpec;
import io.syndesis.server.controller.integration.camelk.crd.DataSpec;
import io.syndesis.server.controller.integration.camelk.crd.Integration;
import io.syndesis.server.controller.integration.camelk.crd.IntegrationSpec;
Expand Down Expand Up @@ -88,6 +89,18 @@ public Integration customize(IntegrationDeployment deployment, Integration integ
spec.addResources(generateOpenAPIResource(res.get()));
spec.addSources(generateOpenAPIRestDSL(res.get()));
spec.addSources(generateOpenAPIRestEndpoint());
spec.addConfiguration(
new ConfigurationSpec.Builder()
.type("property")
.value("customizer.servletregistration.enabled=true")
.build()
);
spec.addConfiguration(
new ConfigurationSpec.Builder()
.type("property")
.value("customizer.servletregistration.path=/*")
.build()
);
} catch (Exception e) {
throw new IllegalStateException(e);
}
Expand All @@ -99,15 +112,15 @@ public Integration customize(IntegrationDeployment deployment, Integration integ

@SuppressWarnings("PMD.SignatureDeclareThrowsException")
private ResourceSpec generateOpenAPIResource(OpenApi openApi) throws Exception {
//we always compress open api definition
final byte[] openApiBytes = openApi.getDocument();
final String compressedContent = CamelKSupport.compress(openApiBytes);
// final String content = configuration.getCamelk().isCompression() ? CamelKSupport.compress(openApiBytes) : new String(openApiBytes, UTF_8);

return new ResourceSpec.Builder()
.dataSpec(new DataSpec.Builder()
//we always compress openapi spec document
.compression(true)
.name("openapi.json")
.content(compressedContent)
.content(CamelKSupport.compress(openApiBytes))
.build())
.type("data")
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (C) 2016 Red Hat, Inc.
*
* 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.syndesis.server.controller.integration.camelk.customizer;

import io.syndesis.common.model.integration.IntegrationDeployment;
import io.syndesis.server.controller.integration.camelk.CamelKSupport;
import io.syndesis.server.controller.integration.camelk.crd.ConfigurationSpec;
import io.syndesis.server.controller.integration.camelk.crd.Integration;
import io.syndesis.server.controller.integration.camelk.crd.IntegrationSpec;
import io.syndesis.server.openshift.Exposure;
import org.springframework.stereotype.Component;

import java.util.EnumSet;

/**
* Configure OpenApi
*/
@Component
public class WebhookCustomizer implements CamelKIntegrationCustomizer {

@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());
}

if (!CamelKSupport.isWebhookPresent(deployment.getSpec())) {
return integration;
}

try {
spec.addConfiguration(
new ConfigurationSpec.Builder()
.type("property")
.value("customizer.servletregistration.enabled=true")
.build()
);
spec.addConfiguration(
new ConfigurationSpec.Builder()
.type("property")
.value("customizer.servletregistration.path=/webhook/*")
.build()
);
} catch (Exception e) {
throw new IllegalStateException(e);
}

integration.setSpec(spec.build());

return integration;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import io.syndesis.server.controller.ControllersConfigurationProperties;
import io.syndesis.server.controller.integration.camelk.TestResourceManager;
import io.syndesis.server.openshift.Exposure;
import org.assertj.core.api.Condition;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -68,6 +69,15 @@ public void testOpenApiCustomizer() throws Exception {
EnumSet.of(Exposure.SERVICE)
);

assertThat(i.getSpec().getConfiguration()).hasSize(2);
assertThat(i.getSpec().getConfiguration()).anyMatch(
c -> Objects.equals("customizer.servletregistration.enabled=true", c.getValue())
&& Objects.equals("property", c.getType())
);
assertThat(i.getSpec().getConfiguration()).anyMatch(
c -> Objects.equals("customizer.servletregistration.path=/*", c.getValue())
&& Objects.equals("property", c.getType())
);
assertThat(i.getSpec().getSources()).anyMatch(
s -> Objects.equals("openapi-routes", s.getDataSpec().getName()) && Objects.equals("xml", s.getLanguage())
&& !s.getDataSpec().getCompression().booleanValue()
Expand All @@ -77,7 +87,7 @@ public void testOpenApiCustomizer() throws Exception {
&& !s.getDataSpec().getCompression().booleanValue()
);
assertThat(i.getSpec().getResources()).anyMatch(
s -> Objects.equals("openapi.json", s.getDataSpec().getName()) && Objects.equals("openapi", s.getType())
s -> Objects.equals("openapi.json", s.getDataSpec().getName()) && Objects.equals("data", s.getType())
&& s.getDataSpec().getCompression().booleanValue() && (s.getDataSpec().getContent().getBytes(UTF_8).length <= content.length)
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (C) 2016 Red Hat, Inc.
*
* 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.syndesis.server.controller.integration.camelk.customizer;

import io.syndesis.common.model.action.ConnectorAction;
import io.syndesis.common.model.action.ConnectorDescriptor;
import io.syndesis.common.model.connection.Connection;
import io.syndesis.common.model.connection.Connector;
import io.syndesis.common.model.integration.Integration;
import io.syndesis.common.model.integration.IntegrationDeployment;
import io.syndesis.common.model.integration.Step;
import io.syndesis.common.model.integration.StepKind;
import io.syndesis.common.util.Json;
import io.syndesis.server.controller.integration.camelk.TestResourceManager;
import io.syndesis.server.openshift.Exposure;
import org.junit.Test;

import java.io.InputStream;
import java.util.EnumSet;
import java.util.Objects;

import static org.assertj.core.api.Assertions.assertThat;

public class WebhookCustomizerTest {
@Test
public void testOpenApiCustomizer() throws Exception {
TestResourceManager manager = new TestResourceManager();

Connector connector;
ConnectorAction webhookIncomingAction;

try (InputStream is = WebhookCustomizerTest.class.getResourceAsStream("/META-INF/syndesis/connector/webhook.json")) {
connector = Json.readFromStream(is, Connector.class);
webhookIncomingAction = connector.getActions(ConnectorAction.class).stream()
.filter(a -> a.getId().get().equals("io.syndesis:webhook-incoming"))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException());
}

webhookIncomingAction = webhookIncomingAction.builder().descriptor(new ConnectorDescriptor.Builder().connectorId("webhook").build()).build();

Integration integration = manager.newIntegration(
new Step.Builder()
.stepKind(StepKind.endpoint)
.putConfiguredProperty("contextPath", "token")
.action(webhookIncomingAction)
.connection(new Connection.Builder()
.connector(connector)
.build())
.build()
);

IntegrationDeployment deployment = new IntegrationDeployment.Builder()
.userId("user")
.id("idId")
.spec(integration)
.build();

CamelKIntegrationCustomizer customizer = new WebhookCustomizer();

io.syndesis.server.controller.integration.camelk.crd.Integration i = customizer.customize(
deployment,
new io.syndesis.server.controller.integration.camelk.crd.Integration(),
EnumSet.of(Exposure.SERVICE)
);

assertThat(i.getSpec().getConfiguration()).hasSize(2);
assertThat(i.getSpec().getConfiguration()).anyMatch(
c -> Objects.equals("customizer.servletregistration.enabled=true", c.getValue())
&& Objects.equals("property", c.getType())
);
assertThat(i.getSpec().getConfiguration()).anyMatch(
c -> Objects.equals("customizer.servletregistration.path=/webhook/*", c.getValue())
&& Objects.equals("property", c.getType())
);
}
}

0 comments on commit d4e7de2

Please sign in to comment.