syntax = "proto3";
package etcdserverpb;

import "kv.proto";

message ResponseHeader {
  uint64 cluster_id = 1;
  uint64 member_id = 2;
  int64 revision = 3;
  uint64 raft_term = 4;
}

message RangeRequest {
  bytes key = 1;
  bytes range_end = 2;
  int64 limit = 3;
  int64 revision = 4;

  enum SortOrder {
    NONE = 0;
    ASCEND = 1;
    DESCEND = 2;
  }

  enum SortTarget {
    KEY = 0;
    VERSION = 1;
    CREATE = 2;
    MOD = 3;
    VALUE = 4;
  }

  SortOrder sort_order = 5;
  SortTarget sort_target = 6;
  bool serializable = 7;
  bool keys_only = 8;
  bool count_only = 9;
  int64 min_mod_revision = 10;
  int64 max_mod_revision = 11;
  int64 min_create_revision = 12;
  int64 max_create_revision = 13;
}

message RangeResponse {
  ResponseHeader header = 1;
  repeated mvccpb.KeyValue kvs = 2;
  bool more = 3;
  int64 count = 4;
}

message PutRequest {
  bytes key = 1;
  bytes value = 2;
  int64 lease = 3;
  bool prev_kv = 4;
  bool ignore_value = 5;
  bool ignore_lease = 6;
}

message PutResponse {
  ResponseHeader header = 1;
  mvccpb.KeyValue prev_kv = 2;
}

message DeleteRangeRequest {
  bytes key = 1;
  bytes range_end = 2;
  bool prev_kv = 3;
}

message DeleteRangeResponse {
  ResponseHeader header = 1;
  int64 deleted = 2;
  repeated mvccpb.KeyValue prev_kvs = 3;
}

message WatchRequest {
  oneof request_union {
    WatchCreateRequest create_request = 1;
    WatchCancelRequest cancel_request = 2;
    WatchProgressRequest progress_request = 3;
  }
}

message WatchCreateRequest {
  bytes key = 1;
  bytes range_end = 2;
  int64 start_revision = 3;
  bool progress_notify = 4;
  bool prev_kv = 6;
  int64 watch_id = 7;
}

message WatchCancelRequest {
  int64 watch_id = 1;
}

message WatchProgressRequest {}

message WatchResponse {
  ResponseHeader header = 1;
  int64 watch_id = 2;
  bool created = 3;
  bool canceled = 4;
  int64 compact_revision = 5;
  string cancel_reason = 6;
  repeated mvccpb.Event events = 11;
}

message LeaseGrantRequest {
  int64 TTL = 1;
  int64 ID = 2;
}

message LeaseGrantResponse {
  ResponseHeader header = 1;
  int64 ID = 2;
  int64 TTL = 3;
  string error = 4;
}

message LeaseRevokeRequest {
  int64 ID = 1;
}

message LeaseRevokeResponse {
  ResponseHeader header = 1;
}

message LeaseKeepAliveRequest {
  int64 ID = 1;
}

message LeaseKeepAliveResponse {
  ResponseHeader header = 1;
  int64 ID = 2;
  int64 TTL = 3;
}

message LeaseTimeToLiveRequest {
  int64 ID = 1;
  bool keys = 2;
}

message LeaseTimeToLiveResponse {
  ResponseHeader header = 1;
  int64 ID = 2;
  int64 TTL = 3;
  int64 grantedTTL = 4;
  repeated bytes keys = 5;
}

message LeaseLeasesRequest {
}

message LeaseStatus {
  int64 ID = 1;
}

message LeaseLeasesResponse {
  ResponseHeader header = 1;
  repeated LeaseStatus leases = 2;
}

message CompactionRequest {
  int64 revision = 1;
  bool physical = 2;
}

message CompactionResponse {
  ResponseHeader header = 1;
}

message StatusRequest {
}

message StatusResponse {
  ResponseHeader header = 1;
  string version = 2;
  int64 dbSize = 3;
  uint64 leader = 4;
  uint64 raftIndex = 5;
  uint64 raftTerm = 6;
  uint64 raftAppliedIndex = 7;
  repeated string errors = 8;
  int64 dbSizeInUse = 9;
  bool isLearner = 10;
}

message Compare {
  enum CompareResult {
    EQUAL = 0;
    GREATER = 1;
    LESS = 2;
    NOT_EQUAL = 3;
  }

  enum CompareTarget {
    VERSION = 0;
    CREATE = 1;
    MOD = 2;
    VALUE = 3;
    LEASE = 4;
  }

  CompareResult result = 1;
  CompareTarget target = 2;
  bytes key = 3;

  oneof target_union {
    int64 version = 4;
    int64 create_revision = 5;
    int64 mod_revision = 6;
    bytes value = 7;
    int64 lease = 8;
  }

  bytes range_end = 64;
}

message RequestOp {
  oneof request {
    RangeRequest request_range = 1;
    PutRequest request_put = 2;
    DeleteRangeRequest request_delete_range = 3;
  }
}

message ResponseOp {
  oneof response {
    RangeResponse response_range = 1;
    PutResponse response_put = 2;
    DeleteRangeResponse response_delete_range = 3;
  }
}

message TxnRequest {
  repeated Compare compare = 1;
  repeated RequestOp success = 2;
  repeated RequestOp failure = 3;
}

message TxnResponse {
  ResponseHeader header = 1;
  bool succeeded = 2;
  repeated ResponseOp responses = 3;
}

message AuthenticateRequest {
  string name = 1;
  string password = 2;
}

message AuthenticateResponse {
  ResponseHeader header = 1;
  string token = 2;
}

message AuthUserAddRequest {
  string name = 1;
  string password = 2;
}

message AuthUserAddResponse {
  ResponseHeader header = 1;
}

message AuthUserDeleteRequest {
  string name = 1;
}

message AuthUserDeleteResponse {
  ResponseHeader header = 1;
}

message AuthUserChangePasswordRequest {
  string name = 1;
  string password = 2;
}

message AuthUserChangePasswordResponse {
  ResponseHeader header = 1;
}

message AuthEnableRequest {}

message AuthEnableResponse {
  ResponseHeader header = 1;
}

message AuthDisableRequest {}

message AuthDisableResponse {
  ResponseHeader header = 1;
}

// Permission type for role permissions
message Permission {
  enum Type {
    READ = 0;
    WRITE = 1;
    READWRITE = 2;
  }
  Type permType = 1;
  bytes key = 2;
  bytes range_end = 3;
}

// Role represents a role with its permissions
message Role {
  bytes name = 1;
  repeated Permission keyPermission = 2;
}

// Role management messages
message AuthRoleAddRequest {
  string name = 1;
}

message AuthRoleAddResponse {
  ResponseHeader header = 1;
}

message AuthRoleDeleteRequest {
  string role = 1;
}

message AuthRoleDeleteResponse {
  ResponseHeader header = 1;
}

message AuthRoleGetRequest {
  string role = 1;
}

message AuthRoleGetResponse {
  ResponseHeader header = 1;
  repeated Permission perm = 2;
}

message AuthRoleListRequest {}

message AuthRoleListResponse {
  ResponseHeader header = 1;
  repeated string roles = 2;
}

message AuthRoleGrantPermissionRequest {
  string name = 1;
  Permission perm = 2;
}

message AuthRoleGrantPermissionResponse {
  ResponseHeader header = 1;
}

message AuthRoleRevokePermissionRequest {
  string role = 1;
  bytes key = 2;
  bytes range_end = 3;
}

message AuthRoleRevokePermissionResponse {
  ResponseHeader header = 1;
}

// User-role association messages
message AuthUserGrantRoleRequest {
  string user = 1;
  string role = 2;
}

message AuthUserGrantRoleResponse {
  ResponseHeader header = 1;
}

message AuthUserRevokeRoleRequest {
  string name = 1;
  string role = 2;
}

message AuthUserRevokeRoleResponse {
  ResponseHeader header = 1;
}

message AuthUserGetRequest {
  string name = 1;
}

message AuthUserGetResponse {
  ResponseHeader header = 1;
  repeated string roles = 2;
}

message AuthUserListRequest {}

message AuthUserListResponse {
  ResponseHeader header = 1;
  repeated string users = 2;
}
