# Copyright 2021 TriggerMesh Inc.
#
# Licensed 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: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: azureeventgridsources.sources.triggermesh.io
  labels:
    eventing.knative.dev/source: 'true'
    duck.knative.dev/source: 'true'
    knative.dev/crd-install: 'true'
    triggermesh.io/crd-install: 'true'
  annotations:
    # User-defined. Determined by the value of spec.eventTypes.
    registry.knative.dev/eventTypes: |
      []
spec:
  group: sources.triggermesh.io
  scope: Namespaced
  names:
    kind: AzureEventGridSource
    plural: azureeventgridsources
    categories:
    - all
    - knative
    - eventing
    - sources
  versions:
  - name: v1alpha1
    served: true
    storage: true
    subresources:
      status: {}
    schema:
      openAPIV3Schema:
        description: TriggerMesh event source for Azure Event Grid.
        type: object
        properties:
          spec:
            description: Desired state of the event source.
            type: object
            properties:
              scope:
                description: |-
                  The resource ID the event subscription applies to.

                  Can be
                  - an Azure subscription
                    /subscriptions/{subscriptionId}
                  - a resource group
                    /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}
                  - a top-level resource from a resource provider (including Event Grid topic)
                    /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName}
                type: string
                pattern: ^\/subscriptions\/[a-z0-9-]+(?:\/resourceGroups\/[\w.()-]+(?:\/providers\/[A-Za-z.]+\/[a-zA-Z0-9][\w.-]+\/[a-zA-Z0-9][\w.-]+)?)?$
              eventTypes:
                description: |-
                  Types of events to subscribe to.

                  If not specified, Azure automatically selects all available event types for the provided 'scope'.

                  For a list of all available event types, please refer to the list of Azure services that support
                  system topics at https://docs.microsoft.com/en-us/azure/event-grid/system-topics.
                type: array
                items:
                  type: string
              endpoint:
                description: The intermediate destination of events subscribed via Event Grid, before they are retrieved
                  by TriggerMesh.
                type: object
                properties:
                  eventHubs:
                    description: Properties of an Event Hubs namespace to use as intermediate destination of events.
                    type: object
                    properties:
                      namespaceID:
                        description: |-
                          Resource ID of the Event Hubs namespace.

                          The expected format is
                            /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.EventHub/namespaces/{namespaceName}
                        type: string
                        pattern: ^\/subscriptions\/[a-z0-9-]+\/resourceGroups\/[\w.()-]+\/providers\/Microsoft.EventHub\/namespaces\/[A-Za-z0-9-]{6,50}$
                      hubName:
                        description: Name of the Event Hubs instance within the selected namespace. If omitted, an Event
                          Hubs instance is created on behalf of the user.
                        type: string
                        pattern: ^[A-Za-z0-9][\w.-]{0,49}$
                    required:
                    - namespaceID
                required:
                - eventHubs
              auth:
                description: Authentication method to interact with the Azure Event Grid and Azure Event Hubs REST APIs.
                  This event source only supports the Service Principal authentication.
                type: object
                properties:
                  servicePrincipal:
                    description: Credentials of an Azure Service Principal. For more information about service
                      principals, please refer to the Azure Active Directory documentation at
                      https://docs.microsoft.com/en-us/azure/active-directory/develop/app-objects-and-service-principals.
                    type: object
                    properties:
                      tenantID:
                        description: ID of the Azure Active Directory tenant.
                        type: object
                        properties:
                          value:
                            description: Literal value of the tenant ID.
                            type: string
                            format: guid
                            pattern: ^[0-9A-Fa-f]{8}(?:-[0-9A-Fa-f]{4}){3}-[0-9A-Fa-f]{12}$
                          valueFromSecret:
                            description: A reference to a Kubernetes Secret object containing the tenant ID.
                            type: object
                            properties:
                              name:
                                type: string
                              key:
                                type: string
                            required:
                            - name
                            - key
                        oneOf:
                        - required: [value]
                        - required: [valueFromSecret]
                      clientID:
                        description: ID of the registered client/application.
                        type: object
                        properties:
                          value:
                            description: Literal value of the client ID.
                            type: string
                            format: guid
                            pattern: ^[0-9A-Fa-f]{8}(?:-[0-9A-Fa-f]{4}){3}-[0-9A-Fa-f]{12}$
                          valueFromSecret:
                            description: A reference to a Kubernetes Secret object containing the client ID.
                            type: object
                            properties:
                              name:
                                type: string
                              key:
                                type: string
                            required:
                            - name
                            - key
                        oneOf:
                        - required: [value]
                        - required: [valueFromSecret]
                      clientSecret:
                        description: Secret associated with the registered client/application.
                        type: object
                        properties:
                          value:
                            description: Literal value of the client secret.
                            type: string
                            format: password
                          valueFromSecret:
                            description: A reference to a Kubernetes Secret object containing the client secret.
                            type: object
                            properties:
                              name:
                                type: string
                              key:
                                type: string
                            required:
                            - name
                            - key
                        oneOf:
                        - required: [value]
                        - required: [valueFromSecret]
                    required:
                    - tenantID
                    - clientID
                    - clientSecret
                required:
                - servicePrincipal
              sink:
                description: The destination of events sourced from Azure Event Grid.
                type: object
                properties:
                  ref:
                    description: Reference to an addressable Kubernetes object to be used as the destination of events.
                    type: object
                    properties:
                      apiVersion:
                        type: string
                      kind:
                        type: string
                      namespace:
                        type: string
                      name:
                        type: string
                    required:
                    - apiVersion
                    - kind
                    - name
                  uri:
                    description: URI to use as the destination of events.
                    type: string
                    format: uri
                oneOf:
                - required: [ref]
                - required: [uri]
            required:
            - scope
            - endpoint
            - auth
            - sink
          status:
            description: Reported status of the event source.
            type: object
            properties:
              eventSubscriptionID:
                description: Resource ID of the Event Grid subscription that is currently registered for the
                  user-provided scope.
                type: string
              eventHubID:
                description: Resource ID of the Event Hubs instance that is currently receiving events from the Azure
                  Event Grid subscription.
                type: string
              sinkUri:
                description: URI of the sink where events are currently sent to.
                type: string
                format: uri
              ceAttributes:
                type: array
                items:
                  type: object
                  properties:
                    type:
                      type: string
                    source:
                      type: string
                  required:
                  - type
                  - source
              observedGeneration:
                type: integer
                format: int64
              conditions:
                type: array
                items:
                  type: object
                  properties:
                    type:
                      type: string
                    status:
                      type: string
                      enum: ['True', 'False', Unknown]
                    severity:
                      type: string
                      enum: [Error, Warning, Info]
                    reason:
                      type: string
                    message:
                      type: string
                    lastTransitionTime:
                      type: string
                      format: date-time
                  required:
                  - type
                  - status
    additionalPrinterColumns:
    - name: Ready
      type: string
      jsonPath: .status.conditions[?(@.type=='Ready')].status
    - name: Reason
      type: string
      jsonPath: .status.conditions[?(@.type=='Ready')].reason
    - name: Sink
      type: string
      jsonPath: .status.sinkUri
    - name: Age
      type: date
      jsonPath: .metadata.creationTimestamp
