syntax = "proto3";

package code.justin.tv.eventbus.controlplane.infrastructure;
option go_package = "rpc";

service Infrastructure {
  // AllowAccountsPublish is used by tooling to allow services to publish to an event stream
  rpc AllowAccountsPublish(AllowAccountsPublishReq) returns (AllowAccountsPublishResp) {
    option deprecated = true;
  };

  // AllowIAMRolePublish is used by tooling to allow an IAM role to publish to an event stream
  rpc AllowIAMRolePublish(AllowIAMRolePublishReq) returns (AllowIAMRolePublishResp);

  // RegisterEventDefinitions is used by tooling to register one or more event types into the system
  rpc RegisterEventDefinitions(RegisterEventDefinitionsReq) returns (RegisterEventDefinitionsResp);

  // CreateAuthorizedFieldSubscriberGrant is used to give a subscriber permission to access an authorized field
  rpc CreateAuthorizedFieldSubscriberGrant(CreateAuthorizedFieldSubscriberGrantReq) returns (CreateAuthorizedFieldSubscriberGrantResp);

  // CreateAuthorizedFieldPublisherGrant is used to give a publisher permission to build authorized fields
  rpc CreateAuthorizedFieldPublisherGrant(CreateAuthorizedFieldPublisherGrantReq) returns (CreateAuthorizedFieldPublisherGrantResp) {
    option deprecated = true;
  };

  // RevokeAuthorizedFieldGrant revokes either a publisher or a subscriber grant 
  rpc RevokeAuthorizedFieldGrant(RevokeAuthorizedFieldGrantReq) returns (RevokeAuthorizedFieldGrantResp);

  // DeletePublication is used by admin cli to remove a publication to a deprecated event stream
  rpc DeletePublication(DeletePublicationReq) returns (DeletePublicationResp) {
    option deprecated = true;
  };

  // DeleteIAMRolePublication deletes a publication given an event type, environment, and IAM role.
  // Does the full tear down idempotently, including revoking the associated authorized field
  // publisher grant and deleting that DB record as well
  rpc DeleteIAMRolePublication(DeleteIAMRolePublicationReq) returns (DeleteIAMRolePublicationResp);

  // ListPublicationsByEventStream lists all of the publishers for a given event stream
  rpc ListPublicationsByEventStream(ListPublicationsByEventStreamReq) returns (ListPublicationsByEventStreamResp);

  // DeleteAuthorizedFieldGrant is used by admin cli to remove authorized field rows in the backend
  rpc DeleteAuthorizedFieldGrant(DeleteAuthorizedFieldGrantReq) returns (DeleteAuthorizedFieldGrantResp) {
    option deprecated = true;
  };

  // DeleteAuthorizedFieldSubscriberGrant revokes the KMS grant and removes the DB row idempotently
  rpc DeleteAuthorizedFieldSubscriberGrant(DeleteAuthorizedFieldSubscriberGrantReq) returns (DeleteAuthorizedFieldSubscriberGrantResp);

  // DeleteEventType removes the associated event from the backend
  rpc DeleteEventType(DeleteEventTypeReq) returns (DeleteEventTypeResp);

  // DeleteEventStreamsForEventType removes all of the event streams for the event type name
  rpc DeleteEventStreamsForEventType(DeleteEventStreamsForEventTypeReq) returns (DeleteEventStreamsForEventTypeResp);

  // DeleteAuthorizedField removes an authed field
  rpc DeleteAuthorizedField(DeleteAuthorizedFieldReq) returns (DeleteAuthorizedFieldResp);

  // ListAuthorizedFieldSubscriberGrantsByAuthorizedField lists the subscriber grants related to the given authed field
  rpc ListAuthorizedFieldSubscriberGrantsByAuthorizedField(ListAuthorizedFieldSubscriberGrantsByAuthorizedFieldReq) returns (ListAuthorizedFieldSubscriberGrantsByAuthorizedFieldResp);
}

// EventDefinition is some subset of all the information in an EventStream record,
// which will get generated one-per-environment
message EventDefinition {
  string event_type = 1; // A canonical event ID, e.g. "ClockTick"

  string display_name = 3; // Display name; currently the same as the id
  string short_description = 4; // Short description, usually 2 sentences or less.
  string description = 6; // Long description. May contain markdown-formatted paragraphs and links.
  string schema = 8; // The plaintext content of the protobuf file for this event type
  string repo_filepath = 9; // The path to the proto file in the repo
  bool deprecated = 11; // Indicates if the event is deprecated

  repeated AuthorizedField authorized_fields = 10; // List of private fields defined on the event

  // The LDAP group that hold authorization granting power over the event type.
  string ldap_group = 12;
}

message RegisterEventDefinitionsReq {
  // One or more events can be registered to the system.
  // Events are registered sequentially and if any failure occurs then you will
  // get an overall error response. There is no error if an event type already
  // exists; in this case we will only update the descriptive fields.
  repeated EventDefinition event_definitions = 1;
}

message RegisterEventDefinitionsResp {
  // Indexes in the results correspond to the event_types in the request.
  repeated Result results = 1;

  message Result {
    // True if we created a new event type
    bool created = 1; 

    // If returned, these are informational messages about this result. They are simply
    // for context on what the process has done, and are NOT error messages.
    repeated string messages = 2;

    string event_type = 3;
    string environment = 4;
  }
}

message AllowAccountsPublishReq {
  reserved 3; // removed field account_ids

  string event_type = 1;
  string service_id = 2;
  string environment = 4;

  repeated string aws_account_ids = 5;
}

message AllowAccountsPublishResp {
  bool added = 1;
}

message AllowIAMRolePublishReq {
  string event_type = 1;
  string environment = 2;
  string iam_role = 3;
}

message AllowIAMRolePublishResp {
  bool created = 1;
}

message DeletePublicationReq {
  string event_type = 1;
  string environment = 2;
  string service_id = 3;
}

message DeletePublicationResp {
  bool deleted = 1;
}

message DeleteIAMRolePublicationReq{
  string event_type = 1;
  string environment = 2;
  string iam_role_arn = 3;
}

message DeleteIAMRolePublicationResp{}

message ListPublicationsByEventStreamReq {
  string event_type = 1;
  string environment = 2;
}

message ListPublicationsByEventStreamResp {
  repeated Publication publications = 1;
}

message CreateAuthorizedFieldSubscriberGrantReq {
  string event_type = 1;
  string environment = 2;
  AuthorizedField authorized_field = 3;
  string iam_role = 4;
}

message CreateAuthorizedFieldSubscriberGrantResp {}

message CreateAuthorizedFieldPublisherGrantReq {
  string event_type = 1;
  string environment = 2;
  string iam_role = 3;
}

message CreateAuthorizedFieldPublisherGrantResp {}

message AuthorizedField {
  string message_name = 1;
  string field_name = 2;
}

message RevokeAuthorizedFieldGrantReq {
  oneof revocation_type {
    AuthorizedFieldPublisherGrant publisher = 1;
    AuthorizedFieldSubscriberGrant subscriber = 2;
  }
}

message RevokeAuthorizedFieldGrantResp {
  bool revoked = 1;
}

message ListAuthorizedFieldSubscriberGrantsByAuthorizedFieldReq {
  string event_type = 1;
  string environment = 2;
  AuthorizedField authorized_field = 3;
}

message ListAuthorizedFieldSubscriberGrantsByAuthorizedFieldResp {
  repeated AuthorizedFieldSubscriberGrant subscriber_grants = 1;
}

message DeleteAuthorizedFieldGrantReq {
  oneof grant {
    AuthorizedFieldPublisherGrant publisher = 1;
    AuthorizedFieldSubscriberGrant subscriber = 2;
  }
}

message DeleteAuthorizedFieldGrantResp {
  bool deleted = 1;
}

message DeleteAuthorizedFieldSubscriberGrantReq {
  AuthorizedFieldSubscriberGrant grant = 1;
}

message DeleteAuthorizedFieldSubscriberGrantResp {
  bool deleted = 1;
}

message DeleteEventTypeReq {
  string event_type = 1;
}

message DeleteEventTypeResp {
  bool deleted = 1;
}

message DeleteEventStreamsForEventTypeReq {
  string event_type = 1;
}

message DeleteEventStreamsForEventTypeResp {
  bool deleted = 1;
  int32 num_deleted = 2;
}

message DeleteAuthorizedFieldReq {
  string event_type = 1;
  string environment = 2;
  AuthorizedField authorized_field = 3;
}

message DeleteAuthorizedFieldResp {
  bool deleted = 1;
}

message AuthorizedFieldPublisherGrant {
  string event_type = 1;
  string environment = 2;
  string iam_role = 3;
}

message AuthorizedFieldSubscriberGrant {
  string event_type = 1;
  string environment = 2;
  AuthorizedField authorized_field = 3;
  string iam_role = 4;
  string kms_grant_id = 5;
}

message Publication {
  string event_type = 1;
  string environment = 2;
  string sns_topic_arn = 3;
  string event_description = 4;
  bool event_deprecated = 5;
  string publisher_iam_role = 6;
  string publisher_service_name = 7;
  string publisher_service_catalog_id = 8;
  string service_id = 9;
}
