# ---------------------------------------------------------------------------
# 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: opensearch-search-source
  labels:
    camel.apache.org/kamelet.type: "source"
  annotations:
    camel.apache.org/kamelet.support.level: "Stable"
    camel.apache.org/catalog.version: "4.10.2"
    camel.apache.org/kamelet.group: "OpenSearch"
    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, 8868 characters]"
spec:
  definition:
    title: "OpenSearch Search Source"
    description: |-
      Search data on OpenSearch

      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 OpenSearch.
        type: string
      user:
        title: Username
        description: Username to connect to OpenSearch.
        type: string
        x-descriptors:
        - urn:camel:group:credentials
      password:
        title: Password
        description: Password to connect to OpenSearch.
        type: string
        format: password
        x-descriptors:
        - urn:camel:group:credentials
      enableSSL:
        title: Enable SSL
        description: Do we want to connect using SSL?
        type: boolean
        default: false
      hostAddresses:
        title: Host Addresses
        description: Comma separated list with ip:port formatted remote transport addresses to use.
        type: string
      indexName:
        title: Index in OpenSearch
        description: The name of the index to act against.
        type: string
      clusterName:
        title: OpenSearch Cluster Name
        description: The name of the cluster.
        type: string
      certificate:
        title: Certificate
        description: The Certificate for accessing the Opensearch cluster. You must encode this value in base64.
        type: string
  types:
    out:
      mediaType: application/json
  dependencies:
      - "camel:core"
      - "camel:kamelet"
      - "camel:timer"
      - "camel:opensearch"
      - "camel:gson"
  template:
    beans:
      - name: local-opensearch
        type: "#class:org.apache.camel.component.opensearch.OpensearchComponent"
        properties:
            user: "{{?user}}"
            password: "{{?password}}"
    from:
      uri: timer:tick
      parameters:
        period: "{{period}}"
      steps:
        - setBody:
            constant: "{{query?nested=false}}"
        - to:
            uri: "{{local-opensearch}}:{{clusterName}}"
            parameters:
              operation: "SEARCH"
              indexName: "{{indexName}}"
              hostAddresses: "{{hostAddresses}}"
              enableSSL: "{{enableSSL}}"
              certificatePath: "base64:{{?certificate}}"
        - marshal:
            json:
              library: Gson
        - to: kamelet:sink