# ---------------------------------------------------------------------------
# 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.
# ---------------------------------------------------------------------------
apiVersion: camel.apache.org/v1alpha1
kind: Kamelet
metadata:
  name: elasticsearch-search-source
  labels:
    camel.apache.org/kamelet.type: "source"
  annotations:
    camel.apache.org/kamelet.support.level: "Stable"
    camel.apache.org/catalog.version: "3.21.0"
    camel.apache.org/kamelet.group: "ElasticSearch"
    camel.apache.org/kamelet.namespace: "Search"
    camel.apache.org/provider: "Apache Software Foundation"
    camel.apache.org/kamelet.icon: "data:image/svg+xml;base64,[encoded data, 6752 characters]"
spec:
  definition:
    title: "ElasticSearch Search Source"
    description: |-
      Search data on ElasticSearch
      
      The `query` parameter must be formatted in JSON.

      If you specify the `certificate` property, you must base64 encode it before you pass it as a parameter.
    required:
      - query
      - clusterName
      - indexName
      - hostAddresses
    type: object
    properties:
      period:
        title: Period
        description: The time interval between two searches
        type: integer
        default: 1000
      query:
        title: Query
        description: The query we want to use to search on ElasticSearch.
        type: string
      user:
        title: Username
        description: Username to connect to ElasticSearch.
        type: string
        x-descriptors:
        - urn:camel:group:credentials
      password:
        title: Password
        description: Password to connect to ElasticSearch.
        type: string
        format: password
        x-descriptors:
        - urn:alm:descriptor:com.tectonic.ui:password
        - urn:camel:group:credentials
      enableSSL:
        title: Enable SSL
        description: Do we want to connect using SSL?
        type: boolean
        default: true
        x-descriptors:
        - 'urn:alm:descriptor:com.tectonic.ui:checkbox'
      hostAddresses:
        title: Host Addresses
        description: Comma separated list with ip:port formatted remote transport addresses to use.
        type: string
      indexName:
        title: Index in ElasticSearch
        description: The name of the index to act against.
        type: string
      clusterName:
        title: ElasticSearch Cluster Name
        description: The name of the cluster.
        type: string
      certificate:
        title: Certificate 
        description: The Certificate for accessing the Elasticsearch cluster. You must encode this value in base64.
        type: string
  types:
    out:
      mediaType: application/json
  dependencies:
      - "camel:core"
      - "camel:kamelet"
      - "camel:timer"
      - "camel:elasticsearch"
      - "camel:gson"
  template:
    beans:
      - name: local-es
        type: "#class:org.apache.camel.component.es.ElasticsearchComponent"
        properties:
            user: "{{?user}}"
            password: "{{?password}}"
    from:
      uri: timer:tick
      parameters:
        period: "{{period}}"
      steps:
        - set-body:
            constant: "{{query?nested=false}}"
        - to:
            uri: "{{local-es}}:{{clusterName}}"
            parameters:
              operation: "SEARCH"
              indexName: "{{indexName}}"
              hostAddresses: "{{hostAddresses}}"
              enableSSL: "{{enableSSL}}"
              certificatePath: "base64:{{?certificate}}"
        - marshal:
            json:
              library: Gson
        - to: kamelet:sink