syntax = "proto3";
package kong_plugin_protocol;
option go_package = "./kong_plugin_protocol";

import "google/protobuf/descriptor.proto";
import "google/protobuf/empty.proto";
import "google/protobuf/struct.proto";

// RPC

message CmdGetPluginNames {}

message CmdGetPluginInfo {
    string name = 1;
}

message CmdStartInstance {
    string name = 1;
    bytes config = 2;
}

message CmdGetInstanceStatus {
    int32 instance_id = 1;
}

message CmdCloseInstance {
    int32 instance_id = 1;
}

message CmdHandleEvent {
    int32 instance_id = 1;
    string event_name = 2;
}

message RpcCall {
    int64 sequence = 1;
    oneof call {
        CmdGetPluginNames cmd_get_plugin_names = 31;
        CmdGetPluginInfo cmd_get_plugin_info = 32;
        CmdStartInstance cmd_start_instance = 33;
        CmdGetInstanceStatus cmd_get_instance_status = 34;
        CmdCloseInstance cmd_close_instance = 35;
        CmdHandleEvent cmd_handle_event = 36;
    };
}

message PluginNames {
    repeated string names = 1;
}

message PluginInfo {
    string name = 1;
    int64 updated_at = 2;
    int64 loaded_at = 3;
    repeated string phases = 4;
    string version = 5;
    int32 priority = 6;
    string schema = 7;
}

message InstanceStatus {
    string name = 1;
    int32 instance_id = 2;
    google.protobuf.Value config = 3;
    int64 started_at = 4;
}

message RpcReturn {
    int64 sequence = 1;
    oneof return {
        PluginNames plugin_names = 31;
        PluginInfo plugin_info = 32;
        InstanceStatus instance_status = 33;
    }
}

// PDK

//message Empty {}

message KV {
    string k = 1;
    google.protobuf.Value v = 2;
}

message Bool {
    bool v = 1;
}

message Int {
    int32 v = 1;
}

message Number {
    double v = 1;
}

message String {
    string v = 1;
}

message ByteString {
    bytes v = 1;
}

message ExitArgs {
    int32 status = 1;
    bytes body = 2;
    google.protobuf.Struct headers = 3;
}

message ServiceKey {
    string id = 1;
}

message CertificateKey {
    string id = 1;
}

message RawBodyResult {
    oneof kind {
        bytes content = 1;
        string body_filepath = 2;
        string error = 3;
    }
}

message UriCapturesResult {
    // array part
    repeated bytes unnamed = 1;
    // map part, named captures
    map<string, bytes> named = 2;
}

message Route {
    string id = 1;
    int64 created_at = 2;
    int64 updated_at = 3;
    string name = 4;
    repeated string protocols = 5;
    repeated string methods = 6;
    repeated string hosts = 7;
    repeated string paths = 8;
    repeated string headers = 9;
    int32 https_redirect_status_code = 10;
    int32 regex_priority = 11;
    bool strip_path = 12;
    bool preserve_host = 13;
    repeated string snis = 14;
    repeated string sources = 15;
    repeated string destinations = 16;
    repeated string tags = 17;
    ServiceKey service = 18;
}

message Service {
    string id = 1;
    int64 created_at = 2;
    int64 updated_at = 3;
    string name = 4;
    int32 retries = 5;
    string protocol = 6;
    string host = 7;
    int32 port = 8;
    string path = 9;
    int32 connect_timeout = 10;
    int32 write_timeout = 11;
    int32 read_timeout = 12;
    repeated string tags = 13;
    CertificateKey client_certificate = 14;
}

message Target {
    string host = 1;
    int32 port = 2;
}

message ConsumerSpec {
    string id = 1;
    bool by_username = 2;
}

message Consumer {
    string id = 1;
    int64 created_at = 2;
    string username = 3;
    string custom_id = 4;
    repeated string tags = 5;
}

message AuthenticatedCredential {
    string id = 1;
    string consumer_id = 2;
}

message AuthenticateArgs {
    Consumer consumer = 1;
    AuthenticatedCredential credential = 2;
}

message MemoryStats {
    message LuaSharedDicts {
        message DictStats {
            int64 allocated_slabs = 1;
            int64 capacity = 2;
        }
        DictStats kong = 1;
        DictStats kong_db_cache = 2;
    }
    message WorkerLuaVm {
        int64 http_allocated_gc = 1;
        int64 pid = 2;
    }
    LuaSharedDicts lua_shared_dicts = 1;
    repeated WorkerLuaVm workers_lua_vms = 2;
}

message StringMap {
    map<string, string> m = 1;
}

message PdkArg {
    oneof data {
        bool b = 31;
        int64 i = 32;
        double f = 33;
        string s = 34;
        StringMap m = 35;

        string error = 36;

        AuthenticatedCredential credential = 40;
        Route route = 41;
        Service service = 42;
        Consumer consumer = 43;
        MemoryStats memory_stats = 44;
    }
}


message PdkCall {
    int64 sequence = 1;
    int64 event_id = 2;
    string cmd = 3;

    repeated PdkArg args = 31;
}

message PdkReturn {
    int64 sequence = 1;
    int64 event_id = 2;
    string cmd = 3;

    PdkArg arg = 31;
}


extend google.protobuf.MethodOptions {
  string MethodName = 50007;
}


service Kong {
    rpc Client_GetIp(google.protobuf.Empty) returns (String);
    rpc Client_GetForwardedIp(google.protobuf.Empty) returns (String);
    rpc Client_GetPort(google.protobuf.Empty) returns (Int);
    rpc Client_GetForwardedPort(google.protobuf.Empty) returns (Int);
    rpc Client_GetCredential(google.protobuf.Empty) returns (AuthenticatedCredential);
    rpc Client_LoadConsumer(ConsumerSpec) returns (Consumer);
    rpc Client_GetConsumer(google.protobuf.Empty) returns (Consumer);
    rpc Client_Authenticate(AuthenticateArgs) returns (google.protobuf.Empty);
    rpc Client_GetProtocol(Bool) returns (String);

    rpc Ctx_SetShared(KV) returns (google.protobuf.Empty) { option (MethodName) = "kong.ctx.shared.set"; };
    rpc Ctx_GetShared(String) returns (google.protobuf.Value) { option (MethodName) = "kong.ctx.shared.get"; };

    rpc Ip_IsTrusted(String) returns (Bool);

    rpc Log_Alert(google.protobuf.ListValue) returns (google.protobuf.Empty);
    rpc Log_Crit(google.protobuf.ListValue) returns (google.protobuf.Empty);
    rpc Log_Err(google.protobuf.ListValue) returns (google.protobuf.Empty);
    rpc Log_Warn(google.protobuf.ListValue) returns (google.protobuf.Empty);
    rpc Log_Notice(google.protobuf.ListValue) returns (google.protobuf.Empty);
    rpc Log_Info(google.protobuf.ListValue) returns (google.protobuf.Empty);
    rpc Log_Debug(google.protobuf.ListValue) returns (google.protobuf.Empty);
    rpc Log_SetSerializeValue(KV) returns (google.protobuf.Empty);
    rpc Log_Serialize(google.protobuf.Empty) returns (String);

    rpc Nginx_GetVar(String) returns (String);
    rpc Nginx_GetTls1VersionStr(google.protobuf.Empty) returns (String);
    rpc Nginx_SetCtx(KV) returns (String);
    rpc Nginx_GetCtx(String) returns (google.protobuf.Value);
    rpc Nginx_ReqStartTime(google.protobuf.Empty) returns (Number);
    rpc Nginx_GetSubsystem(google.protobuf.Empty) returns (String);

    rpc Node_GetId(google.protobuf.Empty) returns (String);
    rpc Node_GetMemoryStats(google.protobuf.Empty) returns (MemoryStats);

    rpc Request_GetScheme(google.protobuf.Empty) returns (String);
    rpc Request_GetHost(google.protobuf.Empty) returns (String);
    rpc Request_GetPort(google.protobuf.Empty) returns (Int);
    rpc Request_GetForwardedScheme(google.protobuf.Empty) returns (String);
    rpc Request_GetForwardedHost(google.protobuf.Empty) returns (String);
    rpc Request_GetForwardedPort(google.protobuf.Empty) returns (Int);
    rpc Request_GetHttpVersion(google.protobuf.Empty) returns (Number);
    rpc Request_GetMethod(google.protobuf.Empty) returns (String);
    rpc Request_GetPath(google.protobuf.Empty) returns (String);
    rpc Request_GetPathWithQuery(google.protobuf.Empty) returns (String);
    rpc Request_GetRawQuery(google.protobuf.Empty) returns (String);
    rpc Request_GetQueryArg(String) returns (String);
    rpc Request_GetQuery(Int) returns (google.protobuf.Struct);
    rpc Request_GetHeader(String) returns (String);
    rpc Request_GetHeaders(Int) returns (google.protobuf.Struct);
    rpc Request_GetRawBody(google.protobuf.Empty) returns (RawBodyResult);
    rpc Request_GetUriCaptures(google.protobuf.Empty) returns (UriCapturesResult);

    rpc Response_GetStatus(google.protobuf.Empty) returns (Int);
    rpc Response_GetHeader(String) returns (String);
    rpc Response_GetHeaders(Int) returns (google.protobuf.Struct);
    rpc Response_GetSource(google.protobuf.Empty) returns (String);
    rpc Response_SetStatus(Int) returns (google.protobuf.Empty);
    rpc Response_SetHeader(KV) returns (google.protobuf.Empty);
    rpc Response_AddHeader(KV) returns (google.protobuf.Empty);
    rpc Response_ClearHeader(String) returns (google.protobuf.Empty);
    rpc Response_SetHeaders(google.protobuf.Struct) returns (google.protobuf.Empty);
    rpc Response_Exit(ExitArgs) returns (google.protobuf.Empty);

    rpc Router_GetRoute(google.protobuf.Empty) returns (Route);
    rpc Router_GetService(google.protobuf.Empty) returns (Service);

    rpc Service_SetUpstream(String) returns (Bool);
    rpc Service_SetTarget(Target) returns (google.protobuf.Empty);

    rpc Service_Request_SetScheme(String) returns (google.protobuf.Empty);
    rpc Service_Request_SetPath(String) returns (google.protobuf.Empty);
    rpc Service_Request_SetRawQuery(String) returns (google.protobuf.Empty);
    rpc Service_Request_SetMethod(String) returns (google.protobuf.Empty);
    rpc Service_Request_SetQuery(google.protobuf.Struct) returns (google.protobuf.Empty);
    rpc Service_Request_SetHeader(KV) returns (google.protobuf.Empty);
    rpc Service_Request_AddHeader(KV) returns (google.protobuf.Empty);
    rpc Service_Request_ClearHeader(String) returns (google.protobuf.Empty);
    rpc Service_Request_SetHeaders(google.protobuf.Struct) returns (google.protobuf.Empty);
    rpc Service_Request_SetRawBody(ByteString) returns (google.protobuf.Empty);

    rpc Service_Response_GetStatus(google.protobuf.Empty) returns (Int);
    rpc Service_Response_GetHeader(String) returns (String);
    rpc Service_Response_GetHeaders(Int) returns (google.protobuf.Struct);
    rpc Service_Response_GetRawBody(google.protobuf.Empty) returns (ByteString);
}
