syntax = "proto3";

package controller.servers.services.v1;

option go_package = "github.com/hashicorp/boundary/internal/gen/controller/servers/services;services";

import "google/protobuf/timestamp.proto";
import "controller/servers/v1/servers.proto";

service ServerCoordinationService {
  // Status gets worker status requests which include the ongoing jobs the worker is handling and
  // returns the status response which includes the changes the controller would like to make to
  // jobs as well as provide a list of the controllers in the system.
  rpc Status(StatusRequest) returns (StatusResponse) {}
}

enum CONNECTIONSTATUS {
  CONNECTIONSTATUS_UNSPECIFIED = 0;
  CONNECTIONSTATUS_AUTHORIZED = 1;
  CONNECTIONSTATUS_CONNECTED = 2;
  CONNECTIONSTATUS_CLOSED = 3;
}

message Connection {
  string connection_id = 1;
  CONNECTIONSTATUS status = 2;
}

enum SESSIONSTATUS {
  SESSIONSTATUS_UNSPECIFIED = 0;
  SESSIONSTATUS_PENDING = 1;
  SESSIONSTATUS_ACTIVE = 2;
  SESSIONSTATUS_CANCELING = 3;
  SESSIONSTATUS_TERMINATED = 4;
}

message SessionJobInfo {
  string session_id = 1;
  SESSIONSTATUS status = 2;
  repeated Connection connections = 3;
}

enum JOBTYPE {
  JOBTYPE_UNSPECIFIED = 0;
  JOBTYPE_SESSION = 1;
}

message Job {
  JOBTYPE type = 1;
  oneof job_info {
    // This value is specified when type is JOBTYPE_SESSION.
    SessionJobInfo session_info = 2;
  }
}

message JobStatus {
  Job job = 1;
}

message StatusRequest {
  // The worker info. We could use information from the TLS connection but this
  // is easier and going the other route doesn't provijde much benefit -- if you
  // get access to the key and spoof the connection, you're already compromised.
  servers.v1.Server worker = 10;

  // Jobs which this worker wants to report the status.
  repeated JobStatus jobs = 20;
}

enum CHANGETYPE {
  CHANGETYPE_UNSPECIFIED = 0;
  // Indicates to the worker to update its knowledge of the state, which may
  // cause action to be taken.
  CHANGETYPE_UPDATE_STATE = 1;
}

message JobChangeRequest {
  Job job = 1;
  CHANGETYPE request_type = 2;
}

message StatusResponse {
  // Active controllers. This can be used (eventually) for connection
  // management.
  repeated servers.v1.Server controllers = 10;

  // List of jobs and the expected state changes.  For example, this will
  // include jobs witch change type of canceled for jobs which are active on a
  // worker but should be canceled. This could also contain a request to start a
  // job such as a worker -> worker proxy for establishing a session through an
  // enclave.
  repeated JobChangeRequest jobs_requests = 20;
}
