metadata:
  name: Azure IoT Operations Integration
  version: 1.0.0
description: Bidirectional MQTT integration between Cybus Connectware and Azure IoT Operations
parameters:
  mqttHost:
    type: string
    description: Hostname or IP address of the Azure IoT Operations MQTT broker
  mqttPort:
    type: integer
    description: MQTT broker port (default 18883 for SAT authentication)
    default: 18883

definitions:
  SAT-Token: <Your SAT Token>
  ca-cert: <Your Cert>

resources:
  # MQTT Connection to Azure IoT Operations
  aioMqttConnection:
    type: Cybus::Connection
    properties:
      protocol: Mqtt
      connection:
        host: !ref mqttHost
        port: !ref mqttPort
        # Azure IoT Operations requires MQTT v5
        protocolVersion: 5
        # Only for TLS connections:
        caCert: !ref ca-cert # Use the provided CA certificate from Azure IoT Operations MQTT broker
        scheme: tls # Use MQTT over TLS for secure communication
        trustAllCertificates: true
        # Only for SAT authentication:
        authenticationMethod: 'K8S-SAT'
        authenticationData: !ref SAT-Token

  # Bidirectional Data Mapping
  mqttMap:
    type: Cybus::Mapping
    properties:
      mappings:
        # Azure IoT Operations → Cybus Connectware
        - subscribe:
            connection: !ref aioMqttConnection
            topic: 'topic/from/aio-broker'
          publish:
            topic: 'publish/to/cybus'
          rules:
            - transform:
                expression: |
                  {
                    "aioTimeStamp": $fromMillis($),
                    "cybusTimeStamp": $fromMillis($now()),
                    "payload": $
                  }

        # Cybus Connectware → Azure IoT Operations
        - subscribe:
            topic: 'subscribe/from/cybus'
          publish:
            connection: !ref aioMqttConnection
            topic: 'topic/to/aio-broker'
          rules:
            - transform:
                expression: |
                  {
                    "source": "cybus",
                    "timestamp": $fromMillis($now()),
                    "data": $
                  }
