# 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: azureactivitylogssources.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:
    registry.knative.dev/eventTypes: |
      [
        { "type": "com.microsoft.azure.monitor.activity-log" }
      ]
spec:
  group: sources.triggermesh.io
  scope: Namespaced
  names:
    kind: AzureActivityLogsSource
    singular: azureactivitylogssource
    plural: azureactivitylogssources
    categories:
    - all
    - knative
    - eventing
    - sources
  versions:
  - name: v1alpha1
    served: true
    storage: true
    subresources:
      status: {}
    schema:
      openAPIV3Schema:
        description: TriggerMesh event source for Azure Activity Logs.
        type: object
        properties:
          spec:
            description: Desired state of the event source.
            type: object
            properties:
              eventHubID:
                description: |-
                  Resource ID of either the Event Hubs instance or Event Hubs namespace to send activity logs to. This
                  resource ID also conveniently contains the ID of the subscription which activity logs are to be
                  subscribed to.

                  If the resource ID represents an Event Hubs namespace, Azure automatically creates an Event Hub with
                  the name 'insights-activity-logs' inside that namespace. Otherwise, the user-provided Event Hub is
                  used.

                  Accepted formats are
                    - /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.EventHub/namespaces/{namespaceName}/eventHubs/{eventHubName}
                    - /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}(?:\/eventHubs\/[a-zA-Z0-9][\w.-]{0,49})?$
              eventHubsSASPolicy:
                description: Name of a SAS policy with Manage permissions inside the Event Hubs namespace referenced in
                  the EventHubID field. Defaults to 'RootManageSharedAccessKey'.
                type: string
                pattern: ^[\w.-]+$'
              categories:
                description: Categories of Activity Logs to collect. All available categories are selected when this
                  attribute is empty. For a list of available Activity Logs category, please refer to
                  https://docs.microsoft.com/en-us/azure/azure-monitor/platform/activity-log-schema#categories.
                type: array
                items:
                  type: string
              auth:
                description: Authentication method to interact with the Azure Monitor REST API. 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 Activity Logs.
                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]
              ceOverrides:
                type: object
                properties:
                  extensions:
                    type: object
                    additionalProperties:
                      type: string
                      minLength: 1
                required:
                - extensions
            required:
            - eventHubID
            - auth
            - sink
          status:
            description: Reported status of the event source.
            type: object
            properties:
              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
