// Copyright 2021 Google LLC
//
// 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.

syntax = "proto3";

package google.cloud.eventarc.v1;

import "google/api/annotations.proto";
import "google/api/field_behavior.proto";
import "google/api/resource.proto";
import "google/protobuf/timestamp.proto";

option go_package = "google.golang.org/genproto/googleapis/cloud/eventarc/v1;eventarc";
option java_multiple_files = true;
option java_outer_classname = "TriggerProto";
option java_package = "com.google.cloud.eventarc.v1";
option (google.api.resource_definition) = {
  type: "cloudfunctions.googleapis.com/CloudFunction"
  pattern: "projects/{project}/locations/{location}/functions/{function}"
};
option (google.api.resource_definition) = {
  type: "iam.googleapis.com/ServiceAccount"
  pattern: "projects/{project}/serviceAccounts/{service_account}"
};
option (google.api.resource_definition) = {
  type: "run.googleapis.com/Service"
  pattern: "*"
};

// A representation of the trigger resource.
message Trigger {
  option (google.api.resource) = {
    type: "eventarc.googleapis.com/Trigger"
    pattern: "projects/{project}/locations/{location}/triggers/{trigger}"
    plural: "triggers"
    singular: "trigger"
  };

  // Required. The resource name of the trigger. Must be unique within the
  // location on the project and must be in
  // `projects/{project}/locations/{location}/triggers/{trigger}` format.
  string name = 1 [(google.api.field_behavior) = REQUIRED];

  // Output only. Server assigned unique identifier for the trigger. The value
  // is a UUID4 string and guaranteed to remain unchanged until the resource is
  // deleted.
  string uid = 2 [(google.api.field_behavior) = OUTPUT_ONLY];

  // Output only. The creation time.
  google.protobuf.Timestamp create_time = 5
      [(google.api.field_behavior) = OUTPUT_ONLY];

  // Output only. The last-modified time.
  google.protobuf.Timestamp update_time = 6
      [(google.api.field_behavior) = OUTPUT_ONLY];

  // Required. null The list of filters that applies to event attributes. Only
  // events that match all the provided filters will be sent to the destination.
  repeated EventFilter event_filters = 8 [
    (google.api.field_behavior) = UNORDERED_LIST,
    (google.api.field_behavior) = REQUIRED
  ];

  // Optional. The IAM service account email associated with the trigger. The
  // service account represents the identity of the trigger.
  //
  // The principal who calls this API must have `iam.serviceAccounts.actAs`
  // permission in the service account. See
  // https://cloud.google.com/iam/docs/understanding-service-accounts?hl=en#sa_common
  // for more information.
  //
  // For Cloud Run destinations, this service account is used to generate
  // identity tokens when invoking the service. See
  // https://cloud.google.com/run/docs/triggering/pubsub-push#create-service-account
  // for information on how to invoke authenticated Cloud Run services.
  // In order to create Audit Log triggers, the service account should also
  // have `roles/eventarc.eventReceiver` IAM role.
  string service_account = 9 [
    (google.api.field_behavior) = OPTIONAL,
    (google.api.resource_reference) = {
      type: "iam.googleapis.com/ServiceAccount"
    }
  ];

  // Required. Destination specifies where the events should be sent to.
  Destination destination = 10 [(google.api.field_behavior) = REQUIRED];

  // Optional. In order to deliver messages, Eventarc may use other GCP
  // products as transport intermediary. This field contains a reference to that
  // transport intermediary. This information can be used for debugging
  // purposes.
  Transport transport = 11 [(google.api.field_behavior) = OPTIONAL];

  // Optional. User labels attached to the triggers that can be used to group
  // resources.
  map<string, string> labels = 12 [(google.api.field_behavior) = OPTIONAL];

  // Output only. This checksum is computed by the server based on the value of
  // other fields, and may be sent only on create requests to ensure the client
  // has an up-to-date value before proceeding.
  string etag = 99 [(google.api.field_behavior) = OUTPUT_ONLY];
}

// Filters events based on exact matches on the CloudEvents attributes.
message EventFilter {
  // Required. The name of a CloudEvents attribute. Currently, only a subset of
  // attributes are supported for filtering.
  //
  // All triggers MUST provide a filter for the 'type' attribute.
  string attribute = 1 [(google.api.field_behavior) = REQUIRED];

  // Required. The value for the attribute.
  string value = 2 [(google.api.field_behavior) = REQUIRED];
}

// Represents a target of an invocation over HTTP.
message Destination {
  oneof descriptor {
    // Cloud Run fully-managed service that receives the events. The service
    // should be running in the same project of the trigger.
    CloudRun cloud_run = 1;
  }
}

// Represents the transport intermediaries created for the trigger in order to
// deliver events.
message Transport {
  oneof intermediary {
    // The Pub/Sub topic and subscription used by Eventarc as delivery
    // intermediary.
    Pubsub pubsub = 1;
  }
}

// Represents a Cloud Run destination.
message CloudRun {
  // Required. The name of the Cloud Run service being addressed. See
  // https://cloud.google.com/run/docs/reference/rest/v1/namespaces.services.
  //
  // Only services located in the same project of the trigger object
  // can be addressed.
  string service = 1 [
    (google.api.field_behavior) = REQUIRED,
    (google.api.resource_reference) = { type: "run.googleapis.com/Service" }
  ];

  // Optional. The relative path on the Cloud Run service the events should be
  // sent to.
  //
  // The value must conform to the definition of URI path segment (section 3.3
  // of RFC2396). Examples: "/route", "route", "route/subroute".
  string path = 2 [(google.api.field_behavior) = OPTIONAL];

  // Required. The region the Cloud Run service is deployed in.
  string region = 3 [(google.api.field_behavior) = REQUIRED];
}

// Represents a Pub/Sub transport.
message Pubsub {
  // Optional. The name of the Pub/Sub topic created and managed by Eventarc
  // system as a transport for the event delivery. Format:
  // `projects/{PROJECT_ID}/topics/{TOPIC_NAME}`.
  //
  // You may set an existing topic for triggers of the type
  // `google.cloud.pubsub.topic.v1.messagePublished` only. The topic you provide
  // here will not be deleted by Eventarc at trigger deletion.
  string topic = 1 [(google.api.field_behavior) = OPTIONAL];

  // Output only. The name of the Pub/Sub subscription created and managed by
  // Eventarc system as a transport for the event delivery. Format:
  // `projects/{PROJECT_ID}/subscriptions/{SUBSCRIPTION_NAME}`.
  string subscription = 2 [(google.api.field_behavior) = OUTPUT_ONLY];
}
