// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package google.cloud.sql.v1;

import "google/api/annotations.proto";
import "google/api/client.proto";
import "google/api/field_behavior.proto";
import "google/api/resource.proto";
import "google/cloud/sql/v1/cloud_sql_backup_runs.proto";
import "google/cloud/sql/v1/cloud_sql_instances.proto";
import "google/cloud/sql/v1/cloud_sql_resources.proto";
import "google/protobuf/field_mask.proto";
import "google/protobuf/timestamp.proto";
import "google/protobuf/wrappers.proto";
import "google/type/interval.proto";

option go_package = "cloud.google.com/go/sql/apiv1/sqlpb;sqlpb";
option java_multiple_files = true;
option java_outer_classname = "CloudSqlBackupsProto";
option java_package = "com.google.cloud.sql.v1";

service SqlBackupsService {
  option (google.api.default_host) = "sqladmin.googleapis.com";
  option (google.api.oauth_scopes) =
      "https://www.googleapis.com/auth/cloud-platform,"
      "https://www.googleapis.com/auth/sqlservice.admin";

  // Creates a backup for a Cloud SQL instance. This API can be used only to
  // create on-demand backups.
  rpc CreateBackup(CreateBackupRequest) returns (Operation) {
    option (google.api.http) = {
      post: "/v1/{parent=projects/*}/backups"
      body: "backup"
    };
    option (google.api.method_signature) = "parent, backup";
  }

  // Retrieves a resource containing information about a backup.
  rpc GetBackup(GetBackupRequest) returns (Backup) {
    option (google.api.http) = {
      get: "/v1/{name=projects/*/backups/*}"
    };
    option (google.api.method_signature) = "name";
  }

  // Lists all backups associated with the project.
  rpc ListBackups(ListBackupsRequest) returns (ListBackupsResponse) {
    option (google.api.http) = {
      get: "/v1/{parent=projects/*}/backups"
    };
    option (google.api.method_signature) = "parent";
  }

  // Updates the retention period and description of the backup. You can use
  // this API to update final backups only.
  rpc UpdateBackup(UpdateBackupRequest) returns (Operation) {
    option (google.api.http) = {
      patch: "/v1/{backup.name=projects/*/backups/*}"
      body: "backup"
    };
    option (google.api.method_signature) = "backup, update_mask";
  }

  // Deletes the backup.
  rpc DeleteBackup(DeleteBackupRequest) returns (Operation) {
    option (google.api.http) = {
      delete: "/v1/{name=projects/*/backups/*}"
    };
    option (google.api.method_signature) = "name";
  }
}

// The request payload to create the backup
message CreateBackupRequest {
  // Required. The parent resource where this backup is created.
  // Format: projects/{project}
  string parent = 1 [
    (google.api.field_behavior) = REQUIRED,
    (google.api.resource_reference) = {
      child_type: "sqladmin.googleapis.com/Backup"
    }
  ];

  // Required. The Backup to create.
  Backup backup = 2 [(google.api.field_behavior) = REQUIRED];
}

// The request payload to get the backup.
message GetBackupRequest {
  // Required. The name of the backup to retrieve.
  // Format: projects/{project}/backups/{backup}
  string name = 1 [
    (google.api.field_behavior) = REQUIRED,
    (google.api.resource_reference) = { type: "sqladmin.googleapis.com/Backup" }
  ];
}

// The request payload to list the backups.
message ListBackupsRequest {
  // Required. The parent that owns this collection of backups.
  // Format: projects/{project}
  string parent = 1 [
    (google.api.field_behavior) = REQUIRED,
    (google.api.resource_reference) = {
      child_type: "sqladmin.googleapis.com/Backup"
    }
  ];

  // The maximum number of backups to return per response. The service might
  // return fewer backups than this value. If a value for this parameter isn't
  // specified, then, at most, 500 backups are returned. The maximum value is
  // 2,000. Any values that you set, which are greater than 2,000, are changed
  // to 2,000.
  int32 page_size = 2;

  // A page token, received from a previous `ListBackups` call.
  // Provide this to retrieve the subsequent page.
  //
  // When paginating, all other parameters provided to `ListBackups` must match
  // the call that provided the page token.
  string page_token = 3;

  // Multiple filter queries are separated by spaces. For example,
  // 'instance:abc AND type:FINAL, 'location:us',
  // 'backupInterval.startTime>=1950-01-01T01:01:25.771Z'. You can filter by
  // type, instance, backupInterval.startTime (creation time), or location.
  string filter = 4;
}

// The response payload containing a list of the backups.
message ListBackupsResponse {
  // A list of backups.
  repeated Backup backups = 1;

  // A token, which can be sent as `page_token` to retrieve the next page.
  // If this field is omitted, then there aren't subsequent pages.
  string next_page_token = 2;

  // If a region isn't unavailable or if an unknown error occurs, then a warning
  // message is returned.
  repeated ApiWarning warnings = 3;
}

// The request payload to update the backup.
message UpdateBackupRequest {
  // Required. The backup to update.
  // The backup’s `name` field is used to identify the backup to update.
  // Format: projects/{project}/backups/{backup}
  Backup backup = 1 [(google.api.field_behavior) = REQUIRED];

  // The list of fields that you can update. You can update only the description
  // and retention period of the final backup.
  google.protobuf.FieldMask update_mask = 2;
}

// The request payload to delete the backup.
message DeleteBackupRequest {
  // Required. The name of the backup to delete.
  // Format: projects/{project}/backups/{backup}
  string name = 1 [
    (google.api.field_behavior) = REQUIRED,
    (google.api.resource_reference) = { type: "sqladmin.googleapis.com/Backup" }
  ];
}

// A backup resource.
message Backup {
  option (google.api.resource) = {
    type: "sqladmin.googleapis.com/Backup"
    pattern: "projects/{project}/backups/{backup}"
  };

  // The backup type.
  enum SqlBackupType {
    // This is an unknown backup type.
    SQL_BACKUP_TYPE_UNSPECIFIED = 0;

    // The backup schedule triggers a backup automatically.
    AUTOMATED = 1;

    // The user triggers a backup manually.
    ON_DEMAND = 2;

    // The backup created when instance is deleted.
    FINAL = 3;
  }

  // The backup's state
  enum SqlBackupState {
    // The state of the backup is unknown.
    SQL_BACKUP_STATE_UNSPECIFIED = 0;

    // The backup that's added to a queue.
    ENQUEUED = 1;

    // The backup is in progress.
    RUNNING = 2;

    // The backup failed.
    FAILED = 3;

    // The backup is successful.
    SUCCESSFUL = 4;

    // The backup is being deleted.
    DELETING = 5;

    // Deletion of the backup failed.
    DELETION_FAILED = 6;
  }

  // Output only. The resource name of the backup.
  // Format: projects/{project}/backups/{backup}.
  string name = 1 [(google.api.field_behavior) = OUTPUT_ONLY];

  // Output only. This is always `sql#backup`.
  string kind = 2 [(google.api.field_behavior) = OUTPUT_ONLY];

  // Output only. The URI of this resource.
  string self_link = 3 [(google.api.field_behavior) = OUTPUT_ONLY];

  // Output only. The type of this backup. The type can be "AUTOMATED",
  // "ON_DEMAND" or “FINAL”.
  SqlBackupType type = 4 [(google.api.field_behavior) = OUTPUT_ONLY];

  // The description of this backup.
  string description = 5;

  // The name of the source database instance.
  string instance = 6;

  // The storage location of the backups. The location can be multi-regional.
  string location = 7;

  // Output only. This output contains the following values:
  // start_time: All database writes up to this time are available.
  // end_time: Any database writes after this time aren't available.
  google.type.Interval backup_interval = 8
      [(google.api.field_behavior) = OUTPUT_ONLY];

  // Output only. The status of this backup.
  SqlBackupState state = 9 [(google.api.field_behavior) = OUTPUT_ONLY];

  // Output only. Information about why the backup operation fails (for example,
  // when the backup state fails).
  OperationError error = 10 [(google.api.field_behavior) = OUTPUT_ONLY];

  // Output only. This output contains the encryption configuration for a backup
  // and the resource name of the KMS key for disk encryption.
  string kms_key = 11 [(google.api.field_behavior) = OUTPUT_ONLY];

  // Output only. This output contains the encryption status for a backup and
  // the version of the KMS key that's used to encrypt the Cloud SQL instance.
  string kms_key_version = 12 [(google.api.field_behavior) = OUTPUT_ONLY];

  // Output only. Specifies the kind of backup, PHYSICAL or DEFAULT_SNAPSHOT.
  SqlBackupKind backup_kind = 13 [(google.api.field_behavior) = OUTPUT_ONLY];

  // Output only. This output contains a backup time zone. If a Cloud SQL for
  // SQL Server instance has a different time zone from the backup's time zone,
  // then the restore to the instance doesn't happen.
  string time_zone = 15 [(google.api.field_behavior) = OUTPUT_ONLY];

  oneof expiration {
    // Input only. The time-to-live (TTL) interval for this resource (in days).
    // For example: ttlDays:7, means 7 days from the current time. The
    // expiration time can't exceed 365 days from the time that the backup is
    // created.
    int64 ttl_days = 16 [(google.api.field_behavior) = INPUT_ONLY];

    // Backup expiration time.
    // A UTC timestamp of when this backup expired.
    google.protobuf.Timestamp expiry_time = 17;
  }

  // Output only. The database version of the instance of at the time this
  // backup was made.
  SqlDatabaseVersion database_version = 20
      [(google.api.field_behavior) = OUTPUT_ONLY];

  // Output only. The maximum chargeable bytes for the backup.
  optional int64 max_chargeable_bytes = 23
      [(google.api.field_behavior) = OUTPUT_ONLY];

  // Optional. Output only. Timestamp in UTC of when the instance associated
  // with this backup is deleted.
  google.protobuf.Timestamp instance_deletion_time = 24 [
    (google.api.field_behavior) = OPTIONAL,
    (google.api.field_behavior) = OUTPUT_ONLY
  ];

  // Optional. Output only. The instance setting of the source instance that's
  // associated with this backup.
  DatabaseInstance instance_settings = 25 [
    (google.api.field_behavior) = OPTIONAL,
    (google.api.field_behavior) = OUTPUT_ONLY
  ];

  // Output only. The mapping to backup run resource used for IAM validations.
  string backup_run = 26 [(google.api.field_behavior) = OUTPUT_ONLY];

  // Output only. This status indicates whether the backup satisfies PZS.
  //
  // The status is reserved for future use.
  google.protobuf.BoolValue satisfies_pzs = 27
      [(google.api.field_behavior) = OUTPUT_ONLY];

  // Output only. This status indicates whether the backup satisfies PZI.
  //
  // The status is reserved for future use.
  google.protobuf.BoolValue satisfies_pzi = 28
      [(google.api.field_behavior) = OUTPUT_ONLY];
}
