syntax = "proto3";

package yandex.cloud.mdb.postgresql.v1;

import "google/api/annotations.proto";
import "google/protobuf/field_mask.proto";
import "yandex/cloud/api/operation.proto";
import "yandex/cloud/validation.proto";
import "yandex/cloud/operation/operation.proto";
import "yandex/cloud/mdb/postgresql/v1/database.proto";

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

// A set of methods for managing PostgreSQL Database resources.
service DatabaseService {
  // Returns the specified PostgreSQL Database resource.
  //
  // To get the list of available PostgreSQL Database resources, make a [List] request.
  rpc Get (GetDatabaseRequest) returns (Database) {
    option (google.api.http) = { get: "/managed-postgresql/v1/clusters/{cluster_id}/databases/{database_name}" };
  }

  // Retrieves the list of PostgreSQL Database resources in the specified cluster.
  rpc List (ListDatabasesRequest) returns (ListDatabasesResponse) {
    option (google.api.http) = { get: "/managed-postgresql/v1/clusters/{cluster_id}/databases" };
  }

  // Creates a new PostgreSQL database in the specified cluster.
  rpc Create (CreateDatabaseRequest) returns (operation.Operation) {
    option (google.api.http) = { post: "/managed-postgresql/v1/clusters/{cluster_id}/databases" body: "*" };
    option (yandex.cloud.api.operation) = {
      metadata: "CreateDatabaseMetadata"
      response: "Database"
    };
  }

  // Updates the specified PostgreSQL database.
  rpc Update (UpdateDatabaseRequest) returns (operation.Operation) {
    option (google.api.http) = { patch: "/managed-postgresql/v1/clusters/{cluster_id}/databases/{database_name}" body: "*" };
    option (yandex.cloud.api.operation) = {
      metadata: "UpdateDatabaseMetadata"
      response: "Database"
    };
  }

  // Deletes the specified PostgreSQL database.
  rpc Delete (DeleteDatabaseRequest) returns (operation.Operation) {
    option (google.api.http) = { delete: "/managed-postgresql/v1/clusters/{cluster_id}/databases/{database_name}" };
    option (yandex.cloud.api.operation) = {
      metadata: "DeleteDatabaseMetadata"
      response: "google.protobuf.Empty"
    };
  }
}

message GetDatabaseRequest {
  // ID of the PostgreSQL cluster that the database belongs to.
  // To get the cluster ID use a [ClusterService.List] request.
  string cluster_id = 1 [(required) = true, (length) = "<=50"];

  // Name of the PostgreSQL Database resource to return.
  // To get the name of the database use a [DatabaseService.List] request.
  string database_name = 2 [(required) = true, (length) = "<=63", (pattern) = "[a-zA-Z0-9_-]*"];
}

message ListDatabasesRequest {
  // ID of the PostgreSQL cluster to list databases in.
  // To get the cluster ID use a [ClusterService.List] request.
  string cluster_id = 1 [(required) = true, (length) = "<=50"];

  // The maximum number of results per page to return. If the number of available
  // results is larger than [page_size], the service returns a [ListDatabasesResponse.next_page_token]
  // that can be used to get the next page of results in subsequent list requests.

  int64 page_size = 2 [(value) = "<=1000"];

  // Page token. To get the next page of results, Set [page_token] to the [ListDatabasesResponse.next_page_token]
  // returned by a previous list request.
  string page_token = 3 [(length) = "<=100"];
}

message ListDatabasesResponse {
  // List of PostgreSQL Database resources.
  repeated Database databases = 1;

  // This token allows you to get the next page of results for list requests. If the number of results
  // is larger than [ListDatabasesRequest.page_size], use the [next_page_token] as the value
  // for the [ListDatabasesRequest.page_token] parameter in the next list request. Each subsequent
  // list request will have its own [next_page_token] to continue paging through the results.
  string next_page_token = 2;
}

message CreateDatabaseRequest {
  // ID of the PostgreSQL cluster to create a database in.
  // To get the cluster ID use a [ClusterService.List] request.
  string cluster_id = 1 [(required) = true, (length) = "<=50"];

  // Configuration of the database to create.
  DatabaseSpec database_spec = 2 [(required) = true];
}

message CreateDatabaseMetadata {
  // ID of the PostgreSQL cluster where a database is being created.
  string cluster_id = 1;

  // Name of the PostgreSQL database that is being created.
  string database_name = 2;
}

message UpdateDatabaseRequest {
  // ID of the PostgreSQL cluster to update a database in.
  // To get the cluster ID use a [ClusterService.List] request.
  string cluster_id = 1 [(required) = true, (length) = "<=50"];

  // Name of the database to update.
  // To get the name of the database use a [DatabaseService.List] request.
  string database_name = 2 [(required) = true, (length) = "<=63", (pattern) = "[a-zA-Z0-9_-]*"];

  // Field mask that specifies which fields of the Database resource should be updated.
  google.protobuf.FieldMask update_mask = 3;

  // PostgreSQL extensions that should be enabled for the database.
  //
  // If the field is sent, the list of enabled extensions is rewritten entirely.
  // Therefore, to disable an active extension you should simply send the list omitting this extension.
  repeated Extension extensions = 4;
}

message UpdateDatabaseMetadata {
  // ID of the PostgreSQL cluster where a database is being updated.
  string cluster_id = 1;

  // Name of the PostgreSQL database that is being updated.
  string database_name = 2;
}

message DeleteDatabaseRequest {
  // ID of the PostgreSQL cluster to delete a database in.
  // To get the cluster ID, use a [ClusterService.List] request.
  string cluster_id = 1 [(required) = true, (length) = "<=50"];

  // Name of the database to delete.
  // To get the name of the database, use a [DatabaseService.List] request.
  string database_name = 2 [(required) = true, (length) = "<=63", (pattern) = "[a-zA-Z0-9_-]*"];
}

message DeleteDatabaseMetadata {
  // ID of the PostgreSQL cluster where a database is being deleted.
  string cluster_id = 1;

  // Name of the PostgreSQL database that is being deleted.
  string database_name = 2;
}
