// Copyright 2023 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.gkebackup.v1;

import "google/api/field_behavior.proto";
import "google/api/resource.proto";
import "google/cloud/gkebackup/v1/common.proto";
import "google/protobuf/timestamp.proto";

option csharp_namespace = "Google.Cloud.GkeBackup.V1";
option go_package = "cloud.google.com/go/gkebackup/apiv1/gkebackuppb;gkebackuppb";
option java_multiple_files = true;
option java_outer_classname = "BackupPlanProto";
option java_package = "com.google.cloud.gkebackup.v1";
option php_namespace = "Google\\Cloud\\GkeBackup\\V1";
option ruby_package = "Google::Cloud::GkeBackup::V1";

// Defines the configuration and scheduling for a "line" of Backups.
message BackupPlan {
  option (google.api.resource) = {
    type: "gkebackup.googleapis.com/BackupPlan"
    pattern: "projects/{project}/locations/{location}/backupPlans/{backup_plan}"
  };

  // RetentionPolicy defines a Backup retention policy for a BackupPlan.
  message RetentionPolicy {
    // Minimum age for Backups created via this BackupPlan (in days).
    // This field MUST be an integer value between 0-90 (inclusive).
    // A Backup created under this BackupPlan will NOT be deletable until it
    // reaches Backup's (create_time + backup_delete_lock_days).
    // Updating this field of a BackupPlan does NOT affect existing Backups
    // under it. Backups created AFTER a successful update will inherit
    // the new value.
    //
    // Default: 0 (no delete blocking)
    int32 backup_delete_lock_days = 1;

    // The default maximum age of a Backup created via this BackupPlan.
    // This field MUST be an integer value >= 0 and <= 365.
    // If specified, a Backup created under this BackupPlan will be
    // automatically deleted after its age reaches (create_time +
    // backup_retain_days).
    // If not specified, Backups created under this BackupPlan will NOT be
    // subject to automatic deletion.
    // Updating this field does NOT affect existing Backups under it. Backups
    // created AFTER a successful update will automatically pick up the new
    // value.
    // NOTE: backup_retain_days must be >=
    // [backup_delete_lock_days][google.cloud.gkebackup.v1.BackupPlan.RetentionPolicy.backup_delete_lock_days].
    // If
    // [cron_schedule][google.cloud.gkebackup.v1.BackupPlan.Schedule.cron_schedule]
    // is defined, then this must be
    // <= 360 * the creation interval.
    //
    // Default: 0 (no automatic deletion)
    int32 backup_retain_days = 2;

    // This flag denotes whether the retention policy of this BackupPlan is
    // locked.  If set to True, no further update is allowed on this policy,
    // including the `locked` field itself.
    //
    // Default: False
    bool locked = 3;
  }

  // Defines scheduling parameters for automatically creating Backups
  // via this BackupPlan.
  message Schedule {
    // A standard [cron](https://wikipedia.com/wiki/cron) string that defines a
    // repeating schedule for creating Backups via this BackupPlan. If this is
    // defined, then
    // [backup_retain_days][google.cloud.gkebackup.v1.BackupPlan.RetentionPolicy.backup_retain_days]
    // must also be defined.
    //
    // Default (empty): no automatic backup creation will occur.
    string cron_schedule = 1;

    // This flag denotes whether automatic Backup creation is paused for this
    // BackupPlan.
    //
    // Default: False
    bool paused = 2;
  }

  // BackupConfig defines the configuration of Backups created via this
  // BackupPlan.
  message BackupConfig {
    // This defines the "scope" of the Backup - which namespaced
    // resources in the cluster will be included in a Backup.
    // Exactly one of the fields of backup_scope MUST be specified.
    oneof backup_scope {
      // If True, include all namespaced resources
      bool all_namespaces = 1;

      // If set, include just the resources in the listed namespaces.
      Namespaces selected_namespaces = 2;

      // If set, include just the resources referenced by the listed
      // ProtectedApplications.
      NamespacedNames selected_applications = 3;
    }

    // This flag specifies whether volume data should be backed up when
    // PVCs are included in the scope of a Backup.
    //
    // Default: False
    bool include_volume_data = 4;

    // This flag specifies whether Kubernetes Secret resources should be
    // included when they fall into the scope of Backups.
    //
    // Default: False
    bool include_secrets = 5;

    // This defines a customer managed encryption key that will be used to
    // encrypt the "config" portion (the Kubernetes resources) of Backups
    // created via this plan.
    //
    // Default (empty): Config backup artifacts will not be encrypted.
    EncryptionKey encryption_key = 6;
  }

  // State
  enum State {
    // Default first value for Enums.
    STATE_UNSPECIFIED = 0;

    // Waiting for cluster state to be RUNNING.
    CLUSTER_PENDING = 1;

    // The BackupPlan is in the process of being created.
    PROVISIONING = 2;

    // The BackupPlan has successfully been created and is ready for Backups.
    READY = 3;

    // BackupPlan creation has failed.
    FAILED = 4;

    // The BackupPlan has been deactivated.
    DEACTIVATED = 5;

    // The BackupPlan is in the process of being deleted.
    DELETING = 6;
  }

  // Output only. The full name of the BackupPlan resource.
  // Format: `projects/*/locations/*/backupPlans/*`
  string name = 1 [(google.api.field_behavior) = OUTPUT_ONLY];

  // Output only. Server generated global unique identifier of
  // [UUID](https://en.wikipedia.org/wiki/Universally_unique_identifier) format.
  string uid = 2 [(google.api.field_behavior) = OUTPUT_ONLY];

  // Output only. The timestamp when this BackupPlan resource was created.
  google.protobuf.Timestamp create_time = 3
      [(google.api.field_behavior) = OUTPUT_ONLY];

  // Output only. The timestamp when this BackupPlan resource was last
  // updated.
  google.protobuf.Timestamp update_time = 4
      [(google.api.field_behavior) = OUTPUT_ONLY];

  // User specified descriptive string for this BackupPlan.
  string description = 5;

  // Required. Immutable. The source cluster from which Backups will be created
  // via this BackupPlan. Valid formats:
  //
  // - `projects/*/locations/*/clusters/*`
  // - `projects/*/zones/*/clusters/*`
  string cluster = 6 [
    (google.api.field_behavior) = IMMUTABLE,
    (google.api.field_behavior) = REQUIRED,
    (google.api.resource_reference) = {
      type: "container.googleapis.com/Cluster"
    }
  ];

  // RetentionPolicy governs lifecycle of Backups created under this plan.
  RetentionPolicy retention_policy = 7;

  // A set of custom labels supplied by user.
  map<string, string> labels = 8;

  // Defines a schedule for automatic Backup creation via this BackupPlan.
  Schedule backup_schedule = 9;

  // Output only. `etag` is used for optimistic concurrency control as a way to
  // help prevent simultaneous updates of a backup plan from overwriting each
  // other. It is strongly suggested that systems make use of the 'etag' in the
  // read-modify-write cycle to perform BackupPlan updates in order to avoid
  // race conditions: An `etag` is returned in the response to `GetBackupPlan`,
  // and systems are expected to put that etag in the request to
  // `UpdateBackupPlan` or `DeleteBackupPlan` to ensure that their change
  // will be applied to the same version of the resource.
  string etag = 10 [(google.api.field_behavior) = OUTPUT_ONLY];

  // This flag indicates whether this BackupPlan has been deactivated.
  // Setting this field to True locks the BackupPlan such that no further
  // updates will be allowed (except deletes), including the deactivated field
  // itself. It also prevents any new Backups from being created via this
  // BackupPlan (including scheduled Backups).
  //
  // Default: False
  bool deactivated = 11;

  // Defines the configuration of Backups created via this BackupPlan.
  BackupConfig backup_config = 12;

  // Output only. The number of Kubernetes Pods backed up in the
  // last successful Backup created via this BackupPlan.
  int32 protected_pod_count = 13 [(google.api.field_behavior) = OUTPUT_ONLY];

  // Output only. State of the BackupPlan. This State field reflects the
  // various stages a BackupPlan can be in
  // during the Create operation. It will be set to "DEACTIVATED"
  // if the BackupPlan is deactivated on an Update
  State state = 14 [(google.api.field_behavior) = OUTPUT_ONLY];

  // Output only. Human-readable description of why BackupPlan is in the current
  // `state`
  string state_reason = 15 [(google.api.field_behavior) = OUTPUT_ONLY];
}
