Search in sources :

Example 1 with Kamelet

use of org.citrusframework.yaks.camelk.model.Kamelet in project yaks by citrusframework.

the class KameletBuilderTest method buildComplexKamelet.

@Test
public void buildComplexKamelet() throws IOException {
    Map<String, KameletSpec.TypeSpec> types = new HashMap<>();
    types.put("out", new KameletSpec.TypeSpec("text/plain"));
    KameletSpec.Definition definition = new KameletSpec.Definition();
    definition.setTitle("Timer Source");
    definition.getProperties().put("period", new KameletSpec.Definition.PropertyConfig("Period", "integer", 1000, null));
    definition.getProperties().put("message", new KameletSpec.Definition.PropertyConfig("Message", "string", null, "hello world"));
    definition.getRequired().add("message");
    Kamelet kamelet = new Kamelet.Builder().name("time-source").definition(definition).types(types).dependencies(Collections.singletonList("mvn:fake.dependency:id:version-1")).template("from:\n" + "  uri: timer:tick\n" + "  parameters:\n" + "    period: \"#property:period\"\n" + "  steps:\n" + "  - set-body:\n" + "      constant: \"{{message}}\"\n" + "  - to: \"kamelet:sink\"").build();
    final String json = KubernetesSupport.json().writeValueAsString(kamelet);
    Assert.assertEquals(StringUtils.trimAllWhitespace(FileUtils.readToString(new ClassPathResource("kamelet.json", KameletBuilderTest.class))), StringUtils.trimAllWhitespace(json));
}
Also used : HashMap(java.util.HashMap) Kamelet(org.citrusframework.yaks.camelk.model.Kamelet) ClassPathResource(org.springframework.core.io.ClassPathResource) KameletSpec(org.citrusframework.yaks.camelk.model.KameletSpec) Test(org.junit.Test)

Example 2 with Kamelet

use of org.citrusframework.yaks.camelk.model.Kamelet in project yaks by citrusframework.

the class CreateKameletAction method createKamelet.

private void createKamelet(TestContext context) {
    final Kamelet kamelet;
    if (resource != null) {
        try {
            String resolvedSource;
            if (supportVariables) {
                resolvedSource = context.replaceDynamicContentInString(FileUtils.readToString(resource));
            } else {
                resolvedSource = FileUtils.readToString(resource);
            }
            kamelet = KubernetesSupport.yaml().loadAs(resolvedSource, Kamelet.class);
        } catch (IOException e) {
            throw new CitrusRuntimeException(String.format("Failed to load Kamelet from resource %s", name + ".kamelet.yaml"));
        }
    } else {
        if (definition.getTitle() != null) {
            definition.setTitle(context.replaceDynamicContentInString(definition.getTitle()));
        }
        if (definition.getDescription() != null) {
            definition.setDescription(context.replaceDynamicContentInString(definition.getDescription()));
        }
        definition.setProperties(context.resolveDynamicValuesInMap(definition.getProperties()));
        definition.setRequired(context.resolveDynamicValuesInList(definition.getRequired()));
        final Kamelet.Builder builder = new Kamelet.Builder().name(context.replaceDynamicContentInString(name)).definition(definition);
        if (template != null) {
            builder.template(context.replaceDynamicContentInString(template));
        }
        if (source != null) {
            builder.source(source.getName(), context.replaceDynamicContentInString(source.getContent()));
        }
        if (dependencies != null && !dependencies.isEmpty()) {
            builder.dependencies(context.resolveDynamicValuesInList(dependencies));
        }
        if (types != null && !types.isEmpty()) {
            builder.types(context.resolveDynamicValuesInMap(types));
        }
        kamelet = builder.build();
    }
    if (LOG.isDebugEnabled()) {
        try {
            LOG.debug(KubernetesSupport.json().writeValueAsString(kamelet));
        } catch (JsonProcessingException e) {
            LOG.warn("Unable to dump Kamelet data", e);
        }
    }
    CustomResourceDefinitionContext ctx = CamelKSupport.kameletCRDContext(CamelKSettings.getKameletApiVersion());
    getKubernetesClient().customResources(ctx, Kamelet.class, KameletList.class).inNamespace(namespace(context)).createOrReplace(kamelet);
    LOG.info(String.format("Successfully created Kamelet '%s'", kamelet.getMetadata().getName()));
}
Also used : CustomResourceDefinitionContext(io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext) Kamelet(org.citrusframework.yaks.camelk.model.Kamelet) CitrusRuntimeException(com.consol.citrus.exceptions.CitrusRuntimeException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 3 with Kamelet

use of org.citrusframework.yaks.camelk.model.Kamelet in project yaks by citrusframework.

the class KameletBuilderTest method shouldDeserializeKamelet.

@Test
public void shouldDeserializeKamelet() throws IOException {
    Kamelet deserialized = new ObjectMapper().readValue(FileUtils.readToString(new ClassPathResource("timer-source.kamelet.json")), Kamelet.class);
    Assert.assertNull(deserialized.getSpec().getFlow());
    Assert.assertNotNull(deserialized.getSpec().getTemplate());
    Assert.assertEquals(1L, deserialized.getSpec().getTemplate().size());
}
Also used : Kamelet(org.citrusframework.yaks.camelk.model.Kamelet) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 4 with Kamelet

use of org.citrusframework.yaks.camelk.model.Kamelet in project yaks by citrusframework.

the class VerifyKameletAction method doExecute.

@Override
public void doExecute(TestContext context) {
    String kameletName = context.replaceDynamicContentInString(name);
    CustomResourceDefinitionContext ctx = CamelKSupport.kameletCRDContext(CamelKSettings.getKameletApiVersion());
    Kamelet kamelet = getKubernetesClient().customResources(ctx, Kamelet.class, KameletList.class).inNamespace(namespace(context)).withName(kameletName).get();
    if (kamelet == null) {
        throw new ValidationException(String.format("Failed to retrieve Kamelet '%s' in namespace '%s'", name, namespace(context)));
    }
    LOG.info("Kamlet validation successful - All values OK!");
    if (LOG.isDebugEnabled()) {
        try {
            LOG.debug(KubernetesSupport.json().writeValueAsString(kamelet));
        } catch (JsonProcessingException e) {
            LOG.warn("Unable to dump Kamelet data", e);
        }
    }
}
Also used : CustomResourceDefinitionContext(io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext) ValidationException(com.consol.citrus.exceptions.ValidationException) KameletList(org.citrusframework.yaks.camelk.model.KameletList) Kamelet(org.citrusframework.yaks.camelk.model.Kamelet) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

Kamelet (org.citrusframework.yaks.camelk.model.Kamelet)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 CustomResourceDefinitionContext (io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext)2 Test (org.junit.Test)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 CitrusRuntimeException (com.consol.citrus.exceptions.CitrusRuntimeException)1 ValidationException (com.consol.citrus.exceptions.ValidationException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 KameletList (org.citrusframework.yaks.camelk.model.KameletList)1 KameletSpec (org.citrusframework.yaks.camelk.model.KameletSpec)1