syntax = "proto3";

package fiberlb.v1;

option java_multiple_files = true;
option java_package = "cloud.fiberlb.v1";

// ============================================================================
// Load Balancer Service
// ============================================================================

service LoadBalancerService {
    // Create a new load balancer
    rpc CreateLoadBalancer(CreateLoadBalancerRequest) returns (CreateLoadBalancerResponse);
    // Get a load balancer by ID
    rpc GetLoadBalancer(GetLoadBalancerRequest) returns (GetLoadBalancerResponse);
    // List load balancers
    rpc ListLoadBalancers(ListLoadBalancersRequest) returns (ListLoadBalancersResponse);
    // Update a load balancer
    rpc UpdateLoadBalancer(UpdateLoadBalancerRequest) returns (UpdateLoadBalancerResponse);
    // Delete a load balancer
    rpc DeleteLoadBalancer(DeleteLoadBalancerRequest) returns (DeleteLoadBalancerResponse);
}

message LoadBalancer {
    string id = 1;
    string name = 2;
    string org_id = 3;
    string project_id = 4;
    string description = 5;
    LoadBalancerStatus status = 6;
    string vip_address = 7;
    uint64 created_at = 8;
    uint64 updated_at = 9;
}

enum LoadBalancerStatus {
    LOAD_BALANCER_STATUS_UNSPECIFIED = 0;
    LOAD_BALANCER_STATUS_PROVISIONING = 1;
    LOAD_BALANCER_STATUS_ACTIVE = 2;
    LOAD_BALANCER_STATUS_UPDATING = 3;
    LOAD_BALANCER_STATUS_ERROR = 4;
    LOAD_BALANCER_STATUS_DELETING = 5;
}

message CreateLoadBalancerRequest {
    string name = 1;
    string org_id = 2;
    string project_id = 3;
    string description = 4;
    string vip_address = 5;
}

message CreateLoadBalancerResponse {
    LoadBalancer loadbalancer = 1;
}

message GetLoadBalancerRequest {
    string id = 1;
}

message GetLoadBalancerResponse {
    LoadBalancer loadbalancer = 1;
}

message ListLoadBalancersRequest {
    string org_id = 1;
    string project_id = 2;
    int32 page_size = 3;
    string page_token = 4;
}

message ListLoadBalancersResponse {
    repeated LoadBalancer loadbalancers = 1;
    string next_page_token = 2;
}

message UpdateLoadBalancerRequest {
    string id = 1;
    string name = 2;
    string description = 3;
}

message UpdateLoadBalancerResponse {
    LoadBalancer loadbalancer = 1;
}

message DeleteLoadBalancerRequest {
    string id = 1;
}

message DeleteLoadBalancerResponse {}

// ============================================================================
// Pool Service
// ============================================================================

service PoolService {
    rpc CreatePool(CreatePoolRequest) returns (CreatePoolResponse);
    rpc GetPool(GetPoolRequest) returns (GetPoolResponse);
    rpc ListPools(ListPoolsRequest) returns (ListPoolsResponse);
    rpc UpdatePool(UpdatePoolRequest) returns (UpdatePoolResponse);
    rpc DeletePool(DeletePoolRequest) returns (DeletePoolResponse);
}

message Pool {
    string id = 1;
    string name = 2;
    string loadbalancer_id = 3;
    PoolAlgorithm algorithm = 4;
    PoolProtocol protocol = 5;
    SessionPersistence session_persistence = 6;
    uint64 created_at = 7;
    uint64 updated_at = 8;
}

enum PoolAlgorithm {
    POOL_ALGORITHM_UNSPECIFIED = 0;
    POOL_ALGORITHM_ROUND_ROBIN = 1;
    POOL_ALGORITHM_LEAST_CONNECTIONS = 2;
    POOL_ALGORITHM_IP_HASH = 3;
    POOL_ALGORITHM_WEIGHTED_ROUND_ROBIN = 4;
    POOL_ALGORITHM_RANDOM = 5;
    POOL_ALGORITHM_MAGLEV = 6;
}

enum PoolProtocol {
    POOL_PROTOCOL_UNSPECIFIED = 0;
    POOL_PROTOCOL_TCP = 1;
    POOL_PROTOCOL_UDP = 2;
    POOL_PROTOCOL_HTTP = 3;
    POOL_PROTOCOL_HTTPS = 4;
}

message SessionPersistence {
    PersistenceType type = 1;
    string cookie_name = 2;
    uint32 timeout_seconds = 3;
}

enum PersistenceType {
    PERSISTENCE_TYPE_UNSPECIFIED = 0;
    PERSISTENCE_TYPE_SOURCE_IP = 1;
    PERSISTENCE_TYPE_COOKIE = 2;
    PERSISTENCE_TYPE_APP_COOKIE = 3;
}

message CreatePoolRequest {
    string name = 1;
    string loadbalancer_id = 2;
    PoolAlgorithm algorithm = 3;
    PoolProtocol protocol = 4;
    SessionPersistence session_persistence = 5;
}

message CreatePoolResponse {
    Pool pool = 1;
}

message GetPoolRequest {
    string id = 1;
}

message GetPoolResponse {
    Pool pool = 1;
}

message ListPoolsRequest {
    string loadbalancer_id = 1;
    int32 page_size = 2;
    string page_token = 3;
}

message ListPoolsResponse {
    repeated Pool pools = 1;
    string next_page_token = 2;
}

message UpdatePoolRequest {
    string id = 1;
    string name = 2;
    PoolAlgorithm algorithm = 3;
    SessionPersistence session_persistence = 4;
}

message UpdatePoolResponse {
    Pool pool = 1;
}

message DeletePoolRequest {
    string id = 1;
}

message DeletePoolResponse {}

// ============================================================================
// Backend Service
// ============================================================================

service BackendService {
    rpc CreateBackend(CreateBackendRequest) returns (CreateBackendResponse);
    rpc GetBackend(GetBackendRequest) returns (GetBackendResponse);
    rpc ListBackends(ListBackendsRequest) returns (ListBackendsResponse);
    rpc UpdateBackend(UpdateBackendRequest) returns (UpdateBackendResponse);
    rpc DeleteBackend(DeleteBackendRequest) returns (DeleteBackendResponse);
}

message Backend {
    string id = 1;
    string name = 2;
    string pool_id = 3;
    string address = 4;
    uint32 port = 5;
    uint32 weight = 6;
    BackendAdminState admin_state = 7;
    BackendStatus status = 8;
    uint64 created_at = 9;
    uint64 updated_at = 10;
}

enum BackendAdminState {
    BACKEND_ADMIN_STATE_UNSPECIFIED = 0;
    BACKEND_ADMIN_STATE_ENABLED = 1;
    BACKEND_ADMIN_STATE_DISABLED = 2;
    BACKEND_ADMIN_STATE_DRAIN = 3;
}

enum BackendStatus {
    BACKEND_STATUS_UNSPECIFIED = 0;
    BACKEND_STATUS_ONLINE = 1;
    BACKEND_STATUS_OFFLINE = 2;
    BACKEND_STATUS_CHECKING = 3;
    BACKEND_STATUS_UNKNOWN = 4;
}

message CreateBackendRequest {
    string name = 1;
    string pool_id = 2;
    string address = 3;
    uint32 port = 4;
    uint32 weight = 5;
}

message CreateBackendResponse {
    Backend backend = 1;
}

message GetBackendRequest {
    string id = 1;
}

message GetBackendResponse {
    Backend backend = 1;
}

message ListBackendsRequest {
    string pool_id = 1;
    int32 page_size = 2;
    string page_token = 3;
}

message ListBackendsResponse {
    repeated Backend backends = 1;
    string next_page_token = 2;
}

message UpdateBackendRequest {
    string id = 1;
    string name = 2;
    uint32 weight = 3;
    BackendAdminState admin_state = 4;
}

message UpdateBackendResponse {
    Backend backend = 1;
}

message DeleteBackendRequest {
    string id = 1;
}

message DeleteBackendResponse {}

// ============================================================================
// Listener Service
// ============================================================================

service ListenerService {
    rpc CreateListener(CreateListenerRequest) returns (CreateListenerResponse);
    rpc GetListener(GetListenerRequest) returns (GetListenerResponse);
    rpc ListListeners(ListListenersRequest) returns (ListListenersResponse);
    rpc UpdateListener(UpdateListenerRequest) returns (UpdateListenerResponse);
    rpc DeleteListener(DeleteListenerRequest) returns (DeleteListenerResponse);
}

message Listener {
    string id = 1;
    string name = 2;
    string loadbalancer_id = 3;
    ListenerProtocol protocol = 4;
    uint32 port = 5;
    string default_pool_id = 6;
    TlsConfig tls_config = 7;
    uint32 connection_limit = 8;
    bool enabled = 9;
    uint64 created_at = 10;
    uint64 updated_at = 11;
}

enum ListenerProtocol {
    LISTENER_PROTOCOL_UNSPECIFIED = 0;
    LISTENER_PROTOCOL_TCP = 1;
    LISTENER_PROTOCOL_UDP = 2;
    LISTENER_PROTOCOL_HTTP = 3;
    LISTENER_PROTOCOL_HTTPS = 4;
    LISTENER_PROTOCOL_TERMINATED_HTTPS = 5;
}

message TlsConfig {
    string certificate_id = 1;
    TlsVersion min_version = 2;
    repeated string cipher_suites = 3;
}

enum TlsVersion {
    TLS_VERSION_UNSPECIFIED = 0;
    TLS_VERSION_TLS_1_2 = 1;
    TLS_VERSION_TLS_1_3 = 2;
}

message CreateListenerRequest {
    string name = 1;
    string loadbalancer_id = 2;
    ListenerProtocol protocol = 3;
    uint32 port = 4;
    string default_pool_id = 5;
    TlsConfig tls_config = 6;
    uint32 connection_limit = 7;
}

message CreateListenerResponse {
    Listener listener = 1;
}

message GetListenerRequest {
    string id = 1;
}

message GetListenerResponse {
    Listener listener = 1;
}

message ListListenersRequest {
    string loadbalancer_id = 1;
    int32 page_size = 2;
    string page_token = 3;
}

message ListListenersResponse {
    repeated Listener listeners = 1;
    string next_page_token = 2;
}

message UpdateListenerRequest {
    string id = 1;
    string name = 2;
    string default_pool_id = 3;
    TlsConfig tls_config = 4;
    uint32 connection_limit = 5;
    bool enabled = 6;
}

message UpdateListenerResponse {
    Listener listener = 1;
}

message DeleteListenerRequest {
    string id = 1;
}

message DeleteListenerResponse {}

// ============================================================================
// Health Check Service
// ============================================================================

service HealthCheckService {
    rpc CreateHealthCheck(CreateHealthCheckRequest) returns (CreateHealthCheckResponse);
    rpc GetHealthCheck(GetHealthCheckRequest) returns (GetHealthCheckResponse);
    rpc ListHealthChecks(ListHealthChecksRequest) returns (ListHealthChecksResponse);
    rpc UpdateHealthCheck(UpdateHealthCheckRequest) returns (UpdateHealthCheckResponse);
    rpc DeleteHealthCheck(DeleteHealthCheckRequest) returns (DeleteHealthCheckResponse);
}

message HealthCheck {
    string id = 1;
    string name = 2;
    string pool_id = 3;
    HealthCheckType type = 4;
    uint32 interval_seconds = 5;
    uint32 timeout_seconds = 6;
    uint32 healthy_threshold = 7;
    uint32 unhealthy_threshold = 8;
    HttpHealthConfig http_config = 9;
    bool enabled = 10;
    uint64 created_at = 11;
    uint64 updated_at = 12;
}

enum HealthCheckType {
    HEALTH_CHECK_TYPE_UNSPECIFIED = 0;
    HEALTH_CHECK_TYPE_TCP = 1;
    HEALTH_CHECK_TYPE_HTTP = 2;
    HEALTH_CHECK_TYPE_HTTPS = 3;
    HEALTH_CHECK_TYPE_UDP = 4;
    HEALTH_CHECK_TYPE_PING = 5;
}

message HttpHealthConfig {
    string method = 1;
    string path = 2;
    repeated uint32 expected_codes = 3;
    string host = 4;
}

message CreateHealthCheckRequest {
    string name = 1;
    string pool_id = 2;
    HealthCheckType type = 3;
    uint32 interval_seconds = 4;
    uint32 timeout_seconds = 5;
    uint32 healthy_threshold = 6;
    uint32 unhealthy_threshold = 7;
    HttpHealthConfig http_config = 8;
}

message CreateHealthCheckResponse {
    HealthCheck health_check = 1;
}

message GetHealthCheckRequest {
    string id = 1;
}

message GetHealthCheckResponse {
    HealthCheck health_check = 1;
}

message ListHealthChecksRequest {
    string pool_id = 1;
    int32 page_size = 2;
    string page_token = 3;
}

message ListHealthChecksResponse {
    repeated HealthCheck health_checks = 1;
    string next_page_token = 2;
}

message UpdateHealthCheckRequest {
    string id = 1;
    string name = 2;
    uint32 interval_seconds = 3;
    uint32 timeout_seconds = 4;
    uint32 healthy_threshold = 5;
    uint32 unhealthy_threshold = 6;
    HttpHealthConfig http_config = 7;
    bool enabled = 8;
}

message UpdateHealthCheckResponse {
    HealthCheck health_check = 1;
}

message DeleteHealthCheckRequest {
    string id = 1;
}

message DeleteHealthCheckResponse {}

// ============================================================================
// L7 Policy Service
// ============================================================================

service L7PolicyService {
    rpc CreateL7Policy(CreateL7PolicyRequest) returns (CreateL7PolicyResponse);
    rpc GetL7Policy(GetL7PolicyRequest) returns (GetL7PolicyResponse);
    rpc ListL7Policies(ListL7PoliciesRequest) returns (ListL7PoliciesResponse);
    rpc UpdateL7Policy(UpdateL7PolicyRequest) returns (UpdateL7PolicyResponse);
    rpc DeleteL7Policy(DeleteL7PolicyRequest) returns (DeleteL7PolicyResponse);
}

message L7Policy {
    string id = 1;
    string listener_id = 2;
    string name = 3;
    uint32 position = 4;
    L7PolicyAction action = 5;
    string redirect_url = 6;
    string redirect_pool_id = 7;
    uint32 redirect_http_status_code = 8;
    bool enabled = 9;
    uint64 created_at = 10;
    uint64 updated_at = 11;
}

enum L7PolicyAction {
    L7_POLICY_ACTION_UNSPECIFIED = 0;
    L7_POLICY_ACTION_REDIRECT_TO_POOL = 1;
    L7_POLICY_ACTION_REDIRECT_TO_URL = 2;
    L7_POLICY_ACTION_REJECT = 3;
}

message CreateL7PolicyRequest {
    string listener_id = 1;
    string name = 2;
    uint32 position = 3;
    L7PolicyAction action = 4;
    string redirect_url = 5;
    string redirect_pool_id = 6;
    uint32 redirect_http_status_code = 7;
}

message CreateL7PolicyResponse {
    L7Policy l7_policy = 1;
}

message GetL7PolicyRequest {
    string id = 1;
}

message GetL7PolicyResponse {
    L7Policy l7_policy = 1;
}

message ListL7PoliciesRequest {
    string listener_id = 1;
    int32 page_size = 2;
    string page_token = 3;
}

message ListL7PoliciesResponse {
    repeated L7Policy l7_policies = 1;
    string next_page_token = 2;
}

message UpdateL7PolicyRequest {
    string id = 1;
    string name = 2;
    uint32 position = 3;
    L7PolicyAction action = 4;
    string redirect_url = 5;
    string redirect_pool_id = 6;
    uint32 redirect_http_status_code = 7;
    bool enabled = 8;
}

message UpdateL7PolicyResponse {
    L7Policy l7_policy = 1;
}

message DeleteL7PolicyRequest {
    string id = 1;
}

message DeleteL7PolicyResponse {}

// ============================================================================
// L7 Rule Service
// ============================================================================

service L7RuleService {
    rpc CreateL7Rule(CreateL7RuleRequest) returns (CreateL7RuleResponse);
    rpc GetL7Rule(GetL7RuleRequest) returns (GetL7RuleResponse);
    rpc ListL7Rules(ListL7RulesRequest) returns (ListL7RulesResponse);
    rpc UpdateL7Rule(UpdateL7RuleRequest) returns (UpdateL7RuleResponse);
    rpc DeleteL7Rule(DeleteL7RuleRequest) returns (DeleteL7RuleResponse);
}

message L7Rule {
    string id = 1;
    string policy_id = 2;
    L7RuleType rule_type = 3;
    L7CompareType compare_type = 4;
    string value = 5;
    string key = 6;
    bool invert = 7;
    uint64 created_at = 8;
    uint64 updated_at = 9;
}

enum L7RuleType {
    L7_RULE_TYPE_UNSPECIFIED = 0;
    L7_RULE_TYPE_HOST_NAME = 1;
    L7_RULE_TYPE_PATH = 2;
    L7_RULE_TYPE_FILE_TYPE = 3;
    L7_RULE_TYPE_HEADER = 4;
    L7_RULE_TYPE_COOKIE = 5;
    L7_RULE_TYPE_SSL_CONN_HAS_SNI = 6;
}

enum L7CompareType {
    L7_COMPARE_TYPE_UNSPECIFIED = 0;
    L7_COMPARE_TYPE_EQUAL_TO = 1;
    L7_COMPARE_TYPE_REGEX = 2;
    L7_COMPARE_TYPE_STARTS_WITH = 3;
    L7_COMPARE_TYPE_ENDS_WITH = 4;
    L7_COMPARE_TYPE_CONTAINS = 5;
}

message CreateL7RuleRequest {
    string policy_id = 1;
    L7RuleType rule_type = 2;
    L7CompareType compare_type = 3;
    string value = 4;
    string key = 5;
    bool invert = 6;
}

message CreateL7RuleResponse {
    L7Rule l7_rule = 1;
}

message GetL7RuleRequest {
    string id = 1;
}

message GetL7RuleResponse {
    L7Rule l7_rule = 1;
}

message ListL7RulesRequest {
    string policy_id = 1;
    int32 page_size = 2;
    string page_token = 3;
}

message ListL7RulesResponse {
    repeated L7Rule l7_rules = 1;
    string next_page_token = 2;
}

message UpdateL7RuleRequest {
    string id = 1;
    L7RuleType rule_type = 2;
    L7CompareType compare_type = 3;
    string value = 4;
    string key = 5;
    bool invert = 6;
}

message UpdateL7RuleResponse {
    L7Rule l7_rule = 1;
}

message DeleteL7RuleRequest {
    string id = 1;
}

message DeleteL7RuleResponse {}

// ============================================================================
// Certificate Service
// ============================================================================

service CertificateService {
    rpc CreateCertificate(CreateCertificateRequest) returns (CreateCertificateResponse);
    rpc GetCertificate(GetCertificateRequest) returns (GetCertificateResponse);
    rpc ListCertificates(ListCertificatesRequest) returns (ListCertificatesResponse);
    rpc DeleteCertificate(DeleteCertificateRequest) returns (DeleteCertificateResponse);
}

message Certificate {
    string id = 1;
    string loadbalancer_id = 2;
    string name = 3;
    string certificate = 4;
    string private_key = 5;
    CertificateType cert_type = 6;
    uint64 expires_at = 7;
    uint64 created_at = 8;
    uint64 updated_at = 9;
}

enum CertificateType {
    CERTIFICATE_TYPE_UNSPECIFIED = 0;
    CERTIFICATE_TYPE_SERVER = 1;
    CERTIFICATE_TYPE_CLIENT_CA = 2;
    CERTIFICATE_TYPE_SNI = 3;
}

message CreateCertificateRequest {
    string loadbalancer_id = 1;
    string name = 2;
    string certificate = 3;
    string private_key = 4;
    CertificateType cert_type = 5;
}

message CreateCertificateResponse {
    Certificate certificate = 1;
}

message GetCertificateRequest {
    string id = 1;
}

message GetCertificateResponse {
    Certificate certificate = 1;
}

message ListCertificatesRequest {
    string loadbalancer_id = 1;
    int32 page_size = 2;
    string page_token = 3;
}

message ListCertificatesResponse {
    repeated Certificate certificates = 1;
    string next_page_token = 2;
}

message DeleteCertificateRequest {
    string id = 1;
}

message DeleteCertificateResponse {}
