syntax = "proto3";

package yandex.cloud.mdb.mysql.v1;

import "google/protobuf/wrappers.proto";
import "yandex/cloud/validation.proto";

option go_package = "github.com/yandex-cloud/go-genproto/yandex/cloud/mdb/mysql/v1;mysql";
option java_package = "yandex.cloud.api.mdb.mysql.v1";

// A MySQL user. For more information, see
// the [documentation](/docs/managed-mysql/concepts).
message User {
  // Name of the MySQL user.
  string name = 1;

  // ID of the MySQL cluster the user belongs to.
  string cluster_id = 2;

  // Set of permissions granted to the user.
  repeated Permission permissions = 3;

  // Set of global permissions to grant to the user.
  repeated GlobalPermission global_permissions = 4;

  // Set of user connection limits.
  ConnectionLimits connection_limits = 5;

  // User authentication plugin.
  AuthPlugin authentication_plugin = 6;
}

message Permission {
  enum Privilege {
    PRIVILEGE_UNSPECIFIED = 0;

    // All privileges that can be made available to the user.
    ALL_PRIVILEGES = 1;

    // Altering tables.
    ALTER = 2;

    // Altering stored routines (stored procedures and functions).
    ALTER_ROUTINE = 3;

    // Creating tables or indexes.
    CREATE = 4;

    // Creating stored routines.
    CREATE_ROUTINE = 5;

    // Creating temporary tables.
    CREATE_TEMPORARY_TABLES = 6;

    // Creating views.
    CREATE_VIEW = 7;

    // Deleting tables.
    DELETE = 8;

    // Removing tables or views.
    DROP = 9;

    // Creating, altering, dropping, or displaying events for the Event Scheduler.
    EVENT = 10;

    // Executing stored routines.
    EXECUTE = 11;

    // Creating and removing indexes.
    INDEX = 12;

    // Inserting rows into the database.
    INSERT = 13;

    // Using LOCK TABLES statement for tables available with SELECT privilege.
    LOCK_TABLES = 14;

    // Selecting rows from tables.
    //
    // Some SELECT statements can be allowed without the SELECT privilege. All
    // statements that read column values require the SELECT privilege. See
    // details in [MySQL documentation](https://dev.mysql.com/doc/refman/5.7/en/privileges-provided.html#priv_select).
    SELECT = 15;

    // Using the SHOW CREATE VIEW statement. Also needed for views used with EXPLAIN.
    SHOW_VIEW = 16;

    // Creating, removing, executing, or displaying triggers for a table.
    TRIGGER = 17;

    // Updating rows in the database.
    UPDATE = 18;

    // Creation of a foreign key constraint for the parent table.
    REFERENCES = 19;
  }

  // Name of the database that the permission grants access to.
  string database_name = 1;

  // Roles granted to the user within the database.
  repeated Privilege roles = 2 [(size) = ">=1"];
}

enum GlobalPermission {
  GLOBAL_PERMISSION_UNSPECIFIED = 0;

  // Enables use of the SHOW MASTER STATUS, SHOW SLAVE STATUS, and SHOW BINARY LOGS statements.
  REPLICATION_CLIENT = 1;

  // Enables the account to request updates that have been made to databases on the master server,
  // using the SHOW SLAVE HOSTS, SHOW RELAYLOG EVENTS, and SHOW BINLOG EVENTS statements.
  REPLICATION_SLAVE = 2;

  // Enables display of information about the threads executing within the server
  // (that is, information about the statements being executed by sessions).
  // The privilege enables use of SHOW PROCESSLIST or mysqladmin processlist to see threads belonging
  // to other accounts; you can always see your own threads. The PROCESS privilege also enables use of SHOW ENGINE.
  PROCESS = 3;
}

message ConnectionLimits {

  // The maximum permitted number of user questions per hour.
  google.protobuf.Int64Value max_questions_per_hour = 1 [(value) = ">=0"];

  // The maximum permitted number of user updates per hour.
  google.protobuf.Int64Value max_updates_per_hour = 2 [(value) = ">=0"];

  // The maximum permitted number of simultaneous client connections per hour.
  google.protobuf.Int64Value max_connections_per_hour = 3 [(value) = ">=0"];

  // The maximum number of simultaneous connections permitted to any given MySQL user account.
  google.protobuf.Int64Value max_user_connections = 4 [(value) = ">=0"];
}

enum AuthPlugin {
  AUTH_PLUGIN_UNSPECIFIED = 0;

  // Use [Native Pluggable Authentication](https://dev.mysql.com/doc/refman/8.0/en/native-pluggable-authentication.html).
  MYSQL_NATIVE_PASSWORD = 1;

  // Use [Caching SHA-2 Pluggable Authentication](https://dev.mysql.com/doc/refman/8.0/en/caching-sha2-pluggable-authentication.html).
  CACHING_SHA2_PASSWORD = 2;

  // Use [SHA-256 Pluggable Authentication](https://dev.mysql.com/doc/refman/8.0/en/sha256-pluggable-authentication.html).
  SHA256_PASSWORD = 3;
}

message UserSpec {
  // Name of the MySQL user.
  string name = 1 [(required) = true, (length) = "<=32", (pattern) = "[a-zA-Z0-9_]*"];

  // Password of the MySQL user.
  string password = 2 [(required) = true, (length) = "8-128"];

  // Set of permissions to grant to the user.
  repeated Permission permissions = 3;

  // Set of global permissions to grant to the user.
  repeated GlobalPermission global_permissions = 4;

  // Set of user connection limits.
  ConnectionLimits connection_limits = 5;

  // User authentication plugin.
  AuthPlugin authentication_plugin = 6;
}
