You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2021/01/11 11:51:29 UTC

[camel-k-runtime] branch master updated: Cannot use kamelets in yaml integrations #581

This is an automated email from the ASF dual-hosted git repository.

lburgazzoli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k-runtime.git


The following commit(s) were added to refs/heads/master by this push:
     new aa70150  Cannot use kamelets in yaml integrations #581
aa70150 is described below

commit aa7015040704e1aec4d021cf9a8d179268beb478
Author: Luca Burgazzoli <lb...@gmail.com>
AuthorDate: Mon Jan 11 11:34:36 2021 +0100

    Cannot use kamelets in yaml integrations #581
---
 .../k/loader/yaml/support/StepParserSupport.java   | 89 ++++++++++++----------
 .../component/kameletreify/kamelet-reify.json      |  2 +-
 .../apache/camel/component/kamelet/kamelet.json    |  2 +-
 .../apache/camel/component/knative/knative.json    |  2 +-
 .../org/apache/camel/k/quarkus/it/Application.java |  6 +-
 .../org/apache/camel/k/quarkus/it/RuntimeTest.java | 14 +++-
 .../src/test/resources/routes.properties           |  4 +-
 .../src/test/resources/routes/process-params.yaml  | 24 ++++++
 8 files changed, 95 insertions(+), 48 deletions(-)

diff --git a/camel-k-loader-yaml/impl-common/src/main/java/org/apache/camel/k/loader/yaml/support/StepParserSupport.java b/camel-k-loader-yaml/impl-common/src/main/java/org/apache/camel/k/loader/yaml/support/StepParserSupport.java
index f4a8c36..9f9a94a 100644
--- a/camel-k-loader-yaml/impl-common/src/main/java/org/apache/camel/k/loader/yaml/support/StepParserSupport.java
+++ b/camel-k-loader-yaml/impl-common/src/main/java/org/apache/camel/k/loader/yaml/support/StepParserSupport.java
@@ -76,54 +76,65 @@ public final class StepParserSupport {
         String answer = uri;
 
         if (parameters == null || parameters.isEmpty()) {
+            //
+            // nothing to do here, there are no parameters so we can return the
+            // uri as it is.
+            //
             return answer;
         }
+        if (uri.indexOf('?') != -1) {
+            //
+            // we support URIs defined as scheme only or scheme and path params,
+            // query params are not supported so a definition like:
+            //
+            // - from:
+            //     uri: "foo:bar?option1=value1"
+            //     parameters:
+            //         option2: value2
+            //
+            // is not supported and leads to the an IllegalArgumentException being
+            // thrown.
+            //
+            throw new IllegalArgumentException("Uri should not contains query params (uri: " + uri + ")");
+        }
+
+        final String scheme = uri.contains(":") ? StringHelper.before(uri, ":") : uri;
+        final EndpointUriFactory factory = context.adapt(ExtendedCamelContext.class).getEndpointUriFactory(scheme);
 
-        if (uri.contains(":")) {
-            final String scheme = StringHelper.before(uri, ":");
-            final EndpointUriFactory factory = getEndpointUriFactory(context, scheme);
-
-            // we want sorted parameters
-            Map<String, Object> map = new TreeMap<>(parameters);
-            for (String secretParameter : factory.secretPropertyNames()) {
-                Object val = map.get(secretParameter);
-                if (val instanceof String) {
-                    String newVal = (String) val;
-                    if (!newVal.startsWith("#") && !newVal.startsWith("RAW(")) {
-                        map.put(secretParameter, "RAW(" + val + ")");
+        try {
+            if (factory != null && factory.isEnabled(scheme)) {
+                if (scheme.equals(uri)) {
+                    //
+                    // if the uri is expressed as simple scheme, then we can use the
+                    // discovered EndpointUriFactory to build the uri
+                    //
+                    answer = factory.buildUri(scheme, parameters, false);
+                } else {
+                    //
+                    // otherwise we have to compose it but we can still leverage the
+                    // discovered EndpointUriFactory to properly handle secrets
+                    //
+                    Map<String, Object> options = new TreeMap<>(parameters);
+
+                    for (String secretParameter : factory.secretPropertyNames()) {
+                        Object val = options.get(secretParameter);
+                        if (val instanceof String) {
+                            String newVal = (String) val;
+                            if (!newVal.startsWith("#") && !newVal.startsWith("RAW(")) {
+                                options.put(secretParameter, "RAW(" + val + ")");
+                            }
+                        }
                     }
-                }
-            }
 
-            try {
-                String queryString = URISupport.createQueryString(map, false);
-                if (ObjectHelper.isNotEmpty(queryString)) {
-                    answer += "?" + queryString;
+                    answer += "?" + URISupport.createQueryString(options, false);
                 }
-            } catch (URISyntaxException e) {
-                throw new IllegalArgumentException(e);
-            }
-        } else {
-            try {
-                answer = getEndpointUriFactory(context, uri).buildUri(uri, parameters);
-            } catch (URISyntaxException e) {
-                throw new IllegalArgumentException(e);
+            } else {
+                answer += "?" + URISupport.createQueryString(parameters, false);
             }
+        } catch (URISyntaxException e) {
+            throw new IllegalArgumentException(e);
         }
 
         return answer;
     }
-
-    public static EndpointUriFactory getEndpointUriFactory(CamelContext context, String scheme) {
-        final EndpointUriFactory factory = context.adapt(ExtendedCamelContext.class).getEndpointUriFactory(scheme);
-
-        if (factory == null) {
-            throw new IllegalArgumentException("Cannot compute endpoint URI: unable to find an EndpointUriFactory for scheme " + scheme);
-        }
-        if (!factory.isEnabled(scheme)) {
-            throw new IllegalArgumentException("Cannot compute endpoint URI: scheme " + scheme + " is not enabled");
-        }
-
-        return factory;
-    }
 }
diff --git a/components/camel-kamelet-reify/src/generated/resources/org/apache/camel/component/kameletreify/kamelet-reify.json b/components/camel-kamelet-reify/src/generated/resources/org/apache/camel/component/kameletreify/kamelet-reify.json
index 19bbb58..c446dfd 100644
--- a/components/camel-kamelet-reify/src/generated/resources/org/apache/camel/component/kameletreify/kamelet-reify.json
+++ b/components/camel-kamelet-reify/src/generated/resources/org/apache/camel/component/kameletreify/kamelet-reify.json
@@ -11,7 +11,7 @@
     "supportLevel": "Preview",
     "groupId": "org.apache.camel.k",
     "artifactId": "camel-kamelet-reify",
-    "version": "1.6.0-SNAPSHOT",
+    "version": "1.7.0-SNAPSHOT",
     "scheme": "kamelet-reify",
     "extendsScheme": "",
     "syntax": "kamelet-reify:delegateUri",
diff --git a/components/camel-kamelet/src/generated/resources/org/apache/camel/component/kamelet/kamelet.json b/components/camel-kamelet/src/generated/resources/org/apache/camel/component/kamelet/kamelet.json
index 4ed8146..3e813a2 100644
--- a/components/camel-kamelet/src/generated/resources/org/apache/camel/component/kamelet/kamelet.json
+++ b/components/camel-kamelet/src/generated/resources/org/apache/camel/component/kamelet/kamelet.json
@@ -11,7 +11,7 @@
     "supportLevel": "Preview",
     "groupId": "org.apache.camel.k",
     "artifactId": "camel-kamelet",
-    "version": "1.6.0-SNAPSHOT",
+    "version": "1.7.0-SNAPSHOT",
     "scheme": "kamelet",
     "extendsScheme": "",
     "syntax": "kamelet:templateId\/routeId",
diff --git a/components/camel-knative/camel-knative/src/generated/resources/org/apache/camel/component/knative/knative.json b/components/camel-knative/camel-knative/src/generated/resources/org/apache/camel/component/knative/knative.json
index d7753a9..59e3ee8 100644
--- a/components/camel-knative/camel-knative/src/generated/resources/org/apache/camel/component/knative/knative.json
+++ b/components/camel-knative/camel-knative/src/generated/resources/org/apache/camel/component/knative/knative.json
@@ -11,7 +11,7 @@
     "supportLevel": "Preview",
     "groupId": "org.apache.camel.k",
     "artifactId": "camel-knative",
-    "version": "1.6.0-SNAPSHOT",
+    "version": "1.7.0-SNAPSHOT",
     "scheme": "knative",
     "extendsScheme": "",
     "syntax": "knative:type\/typeId",
diff --git a/itests/camel-k-itests-kamelet/src/main/java/org/apache/camel/k/quarkus/it/Application.java b/itests/camel-k-itests-kamelet/src/main/java/org/apache/camel/k/quarkus/it/Application.java
index 2e111c5..0636b57 100644
--- a/itests/camel-k-itests-kamelet/src/main/java/org/apache/camel/k/quarkus/it/Application.java
+++ b/itests/camel-k-itests-kamelet/src/main/java/org/apache/camel/k/quarkus/it/Application.java
@@ -58,10 +58,10 @@ public class Application {
     }
 
     @GET
-    @Path("/execute")
+    @Path("/execute/{id}")
     @Produces(MediaType.TEXT_PLAIN)
-    public String execute() {
-        return template.to("direct:process").request(String.class);
+    public String execute(@PathParam("id") String id) {
+        return template.to("direct:" + id).request(String.class);
     }
 
     @POST
diff --git a/itests/camel-k-itests-kamelet/src/test/java/org/apache/camel/k/quarkus/it/RuntimeTest.java b/itests/camel-k-itests-kamelet/src/test/java/org/apache/camel/k/quarkus/it/RuntimeTest.java
index 5d44732..165a60e 100644
--- a/itests/camel-k-itests-kamelet/src/test/java/org/apache/camel/k/quarkus/it/RuntimeTest.java
+++ b/itests/camel-k-itests-kamelet/src/test/java/org/apache/camel/k/quarkus/it/RuntimeTest.java
@@ -49,10 +49,20 @@ public class RuntimeTest {
     }
 
     @Test
-    public void execute() {
+    public void invokeProcess() {
         RestAssured.given()
             .accept(MediaType.TEXT_PLAIN)
-            .get("/test/execute")
+            .get("/test/execute/process")
+            .then()
+                .statusCode(200)
+                .body(is("template"));
+    }
+
+    @Test
+    public void invokeProcessWithParams() {
+        RestAssured.given()
+            .accept(MediaType.TEXT_PLAIN)
+            .get("/test/execute/process-params")
             .then()
                 .statusCode(200)
                 .body(is("template"));
diff --git a/itests/camel-k-itests-kamelet/src/test/resources/routes.properties b/itests/camel-k-itests-kamelet/src/test/resources/routes.properties
index f8882db..8ffa3bd 100644
--- a/itests/camel-k-itests-kamelet/src/test/resources/routes.properties
+++ b/itests/camel-k-itests-kamelet/src/test/resources/routes.properties
@@ -30,4 +30,6 @@ camel.k.sources[1].property-names[0] = message
 # camel-k - sources (routes)
 #
 camel.k.sources[2].location          = file:{{env:ROUTES_DIR}}/process.yaml
-camel.k.sources[2].type              = source
\ No newline at end of file
+camel.k.sources[2].type              = source
+camel.k.sources[3].location          = file:{{env:ROUTES_DIR}}/process-params.yaml
+camel.k.sources[3].type              = source
\ No newline at end of file
diff --git a/itests/camel-k-itests-kamelet/src/test/resources/routes/process-params.yaml b/itests/camel-k-itests-kamelet/src/test/resources/routes/process-params.yaml
new file mode 100644
index 0000000..a0f9329
--- /dev/null
+++ b/itests/camel-k-itests-kamelet/src/test/resources/routes/process-params.yaml
@@ -0,0 +1,24 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+#
+
+- from:
+    uri: "direct:process-params"
+    steps:
+      - to:
+          uri: "kamelet:set-body"
+          parameters:
+            bodyValue: "template"
\ No newline at end of file