# ---------------------------------------------------------------------------
# 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/v1
kind: Kamelet
metadata:
  name: elasticsearch-index-sink
  labels:
    camel.apache.org/kamelet.type: "sink"
  annotations:
    camel.apache.org/kamelet.support.level: "Stable"
    camel.apache.org/catalog.version: "4.10.2"
    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 Index Sink"
    description: |-
      Stores JSON-formatted data into ElasticSearch.

      The input data must be formatted in JSON according to the requirements of the index. 

      If you specify the `certificate` property, you must base64 encode it before you pass it as a parameter.
      
      In the header, you can set the following properties:

      - `indexId` / `ce-indexid`: The index ID for ElasticSearch.
      
      - `indexName` / `ce-indexname`: The index name for ElasticSearch.
      
      If you do not set a property in the header, the Kamelet uses the exchange ID for the index setting.
    required:
      - clusterName
      - hostAddresses
    type: object
    properties:
      user:
        title: Username
        description: The username to connect to ElasticSearch.
        type: string
        x-descriptors:
        - urn:camel:group:credentials
      password:
        title: Password
        description: The password to connect to ElasticSearch.
        type: string
        format: password
        x-descriptors:
        - urn:camel:group:credentials
      enableSSL:
        title: Enable SSL
        description: Specifies to connect by using SSL.
        type: boolean
        default: true
      hostAddresses:
        title: Host Addresses
        description: A comma-separated list of remote transport addresses in `ip:port format`.
        type: string
        example: quickstart-es-http:9200
      clusterName:
        title: ElasticSearch Cluster Name
        description: The name of the ElasticSearch cluster.
        type: string
        example: quickstart
      indexName:
        title: Index in ElasticSearch
        description: The name of the ElasticSearch index.
        type: string
        example: data
      certificate:
        title: Certificate 
        description: The Certificate for accessing the Elasticsearch cluster. You must encode this value in base64.
        type: string
  types:
    out:
      mediaType: text/plain
    in:
      mediaType: application/json
  dependencies:
    - "camel:core"
    - "camel:jackson"
    - "camel:kamelet"
    - "camel:elasticsearch"
    - "camel:gson"
    - "camel:bean"
  template:
    beans:
      - name: local-es
        type: "#class:org.apache.camel.component.es.ElasticsearchComponent"
        properties:
            user: "{{?user}}"
            password: "{{?password}}"
    from:
      uri: kamelet:source
      steps:
      - choice:
          when:
          - simple: "${header[indexId]}"
            steps:
            - setHeader:
                name: "indexId"
                simple: "${header[indexId]}"
          - simple: "${header[ce-indexid]}"
            steps:
            - setHeader:
                name: "indexId"
                simple: "${header[ce-indexid]}"
      - choice:
          when:
          - simple: "${header[indexName]}"
            steps:
            - setHeader:
                name: "indexName"
                simple: "${header[indexName]}"
          - simple: "${header[ce-indexname]}"
            steps:
            - setHeader:
                name: "indexName"
                simple: "${header[ce-indexname]}"
      - unmarshal:
          json: {}
      - to:
          uri: "{{local-es}}:{{clusterName}}"
          parameters:
            operation: "INDEX"
            hostAddresses: "{{hostAddresses}}"
            enableSSL: "{{enableSSL}}"
            indexName: "{{?indexName}}"
            certificatePath: "base64:{{?certificate}}"