syntax = "proto3";

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

service Targets {
  rpc Create(CreateTargetReq) returns (CreateTargetResp);
  rpc GetTargetsForService(GetTargetsForServiceReq) returns (GetTargetsForServiceResp);
  rpc Delete(DeleteTargetReq) returns (DeleteTargetResp);
  rpc Validate(ValidateTargetReq) returns (ValidateTargetResp);
}

message Target {
  // The ID of the service which this target belongs to
  string id = 1;
  string service_id = 2;

  // Only one type of target can be created.
  // This encapsulates target-specific details and also effectively declares
  // what kind of target this is at the same time.
  oneof details {
    SQSDetails sqs = 3;
    HTTPDetails http = 4;
    LambdaDetails lambda = 5;
  }

  string name = 6;
  string aws_account_id = 7;
}

message CreateTargetReq {
  reserved 4;
  reserved 7;
  reserved 8;

  string service_id = 1;
  string name = 2;
  TargetType type = 3;
  // string sqs_assume_role_arn = 4; // only needed for SQS type
  string http_endpoint = 5; // only needed for HTTP type
  string lambda_function_arn = 6; // only needed for Lambda type
  // string aws_account_id = 7;

  // If provided, select a retention profile for this queue.
  // For compatibility with older clients, omitting this selects the default profile.
  // string retention_profile_id = 8;

  oneof details {
    SQSDetails sqs = 9;
  }
}

message CreateTargetResp {
  Target target = 1;
}

message DeleteTargetReq {
  string subscription_target_id = 1;
}

message DeleteTargetResp {
  string subscription_target_id = 1;
}

message GetTargetsForServiceReq {
  // TODO: someday this could have pagination support,
  // or filtering on type, etc
  string service_id = 1;
}

message GetTargetsForServiceResp {
  // TODO: someday this could have pagination support,
  // or filtering on type, etc
  string service_id = 1;
  repeated Target targets = 2;
}

message ValidateTargetReq {
  string sqs_queue_url = 1;
}

message ValidateTargetResp {
  bool isValid = 1;
  string message = 2;
}

message SQSDetails {
  // The cross-account role to assume to create this SQS queue, and
  // potentially do other things like fetch metrics
  string assume_role_arn = 1;

  // The SQS Queue ARN for this target. The controlplane convergence process
  // will use the assume_role_arn to create this queue. When this value is empty,
  // it is assumed that the SQS queue has not yet been created.
  string queue_arn = 2;

  SQSStatus sqs_status = 3;

  string status_message = 4;

  // The SQS Queue URL for this target. This is given to clients as needed.
  string queue_url = 5;

  // Dead-letter queue ARN and URL are blank if dead-letter queue is disabled.
  string deadletter_queue_arn = 6;
  string deadletter_queue_url = 7;
}

// TODO: not implemented yet
// HTTP endpoints are always created in the PENDING_VALIDATION state.
// This is because SNS will make a request to the endpoint with a special validation
// type of message and the service providing said endpoint is required to contact either
// an API or special confirm URL to confirm the subscription is valid, before any messages
// are delivered.
message HTTPDetails {
  // an SNS HTTP Delivery endpoint. Must be fully qualified HTTP URL that resolves
  // and is ready to accept SNS JSON POSTs.
  string endpoint = 1;
}

// TODO: not implemented yet
message LambdaDetails {
  // The ARN of the Lambda Function that will handle these messages
  string function_arn = 1;
}

// The dashboard can use this information to report the state of the SQS target back to the user.
enum SQSStatus {
  UNKNOWN = 0; // Mask out the zero value
  PENDING_CREATE = 1; // The SQS target has been declared, but the converger is yet to attempt creation
  PENDING_DELETE = 2; // The SQS target has been marked for deletion, but the converger is yet to attempt deleting
  CREATED = 3; // The converger has created this SQS target. The converger will not re-attempt to create this SQS target or modify it in any way.
  DELETED = 4; // The converger successfully deleted this SQS target
  ERROR = 5;   // There was an error when the converger attempted to create this SQS target. It will be retried.
}

enum TargetType {
  TARGET_TYPE_UNKNOWN = 0;
  TARGET_TYPE_SQS = 1;
  TARGET_TYPE_HTTP = 2;
  TARGET_TYPE_LAMBDA = 3;
}

