// Copyright 2021 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.clouddms.v1;

import "google/api/field_behavior.proto";
import "google/api/resource.proto";
import "google/protobuf/duration.proto";
import "google/protobuf/timestamp.proto";
import "google/protobuf/wrappers.proto";
import "google/rpc/status.proto";
import "google/api/annotations.proto";

option csharp_namespace = "Google.Cloud.CloudDms.V1";
option go_package = "google.golang.org/genproto/googleapis/cloud/clouddms/v1;clouddms";
option java_multiple_files = true;
option java_outer_classname = "ClouddmsResourcesProto";
option java_package = "com.google.cloud.clouddms.v1";
option php_namespace = "Google\\Cloud\\CloudDms\\V1";
option ruby_package = "Google::Cloud::CloudDMS::V1";

// SSL configuration information.
message SslConfig {
  // Specifies The kind of ssl configuration used.
  enum SslType {
    // Unspecified.
    SSL_TYPE_UNSPECIFIED = 0;

    // Only 'ca_certificate' specified.
    SERVER_ONLY = 1;

    // Both server ('ca_certificate'), and client ('client_key',
    // 'client_certificate') specified.
    SERVER_CLIENT = 2;
  }

  // Output only. The ssl config type according to 'client_key', 'client_certificate' and
  // 'ca_certificate'.
  SslType type = 1 [(google.api.field_behavior) = OUTPUT_ONLY];

  // Input only. The unencrypted PKCS#1 or PKCS#8 PEM-encoded private key associated with
  // the Client Certificate. If this field is used then the 'client_certificate'
  // field is mandatory.
  string client_key = 2 [(google.api.field_behavior) = INPUT_ONLY];

  // Input only. The x509 PEM-encoded certificate that will be used by the replica to
  // authenticate against the source database server.If this field is used then
  // the 'client_key' field is mandatory.
  string client_certificate = 3 [(google.api.field_behavior) = INPUT_ONLY];

  // Required. Input only. The x509 PEM-encoded certificate of the CA that signed the source database
  // server's certificate. The replica will use this certificate to verify
  // it's connecting to the right host.
  string ca_certificate = 4 [
    (google.api.field_behavior) = INPUT_ONLY,
    (google.api.field_behavior) = REQUIRED
  ];
}

// Specifies connection parameters required specifically for MySQL databases.
message MySqlConnectionProfile {
  // Required. The IP or hostname of the source MySQL database.
  string host = 1 [(google.api.field_behavior) = REQUIRED];

  // Required. The network port of the source MySQL database.
  int32 port = 2 [(google.api.field_behavior) = REQUIRED];

  // Required. The username that Database Migration Service will use to connect to the
  // database. The value is encrypted when stored in Database Migration Service.
  string username = 3 [(google.api.field_behavior) = REQUIRED];

  // Required. Input only. The password for the user that Database Migration Service will be using to
  // connect to the database. This field is not returned on request, and the
  // value is encrypted when stored in Database Migration Service.
  string password = 4 [
    (google.api.field_behavior) = INPUT_ONLY,
    (google.api.field_behavior) = REQUIRED
  ];

  // Output only. Indicates If this connection profile password is stored.
  bool password_set = 5 [(google.api.field_behavior) = OUTPUT_ONLY];

  // SSL configuration for the destination to connect to the source database.
  SslConfig ssl = 6;

  // If the source is a Cloud SQL database, use this field to
  // provide the Cloud SQL instance ID of the source.
  string cloud_sql_id = 7;
}

// Specifies connection parameters required specifically for PostgreSQL
// databases.
message PostgreSqlConnectionProfile {
  // Required. The IP or hostname of the source PostgreSQL database.
  string host = 1 [(google.api.field_behavior) = REQUIRED];

  // Required. The network port of the source PostgreSQL database.
  int32 port = 2 [(google.api.field_behavior) = REQUIRED];

  // Required. The username that Database Migration Service will use to connect to the
  // database. The value is encrypted when stored in Database Migration Service.
  string username = 3 [(google.api.field_behavior) = REQUIRED];

  // Required. Input only. The password for the user that Database Migration Service will be using to
  // connect to the database. This field is not returned on request, and the
  // value is encrypted when stored in Database Migration Service.
  string password = 4 [
    (google.api.field_behavior) = INPUT_ONLY,
    (google.api.field_behavior) = REQUIRED
  ];

  // Output only. Indicates If this connection profile password is stored.
  bool password_set = 5 [(google.api.field_behavior) = OUTPUT_ONLY];

  // SSL configuration for the destination to connect to the source database.
  SslConfig ssl = 6;

  // If the source is a Cloud SQL database, use this field to
  // provide the Cloud SQL instance ID of the source.
  string cloud_sql_id = 7;
}

// Specifies required connection parameters, and, optionally, the parameters
// required to create a Cloud SQL destination database instance.
message CloudSqlConnectionProfile {
  // Output only. The Cloud SQL instance ID that this connection profile is associated with.
  string cloud_sql_id = 1 [(google.api.field_behavior) = OUTPUT_ONLY];

  // Immutable. Metadata used to create the destination Cloud SQL database.
  CloudSqlSettings settings = 2 [(google.api.field_behavior) = IMMUTABLE];

  // Output only. The Cloud SQL database instance's private IP.
  string private_ip = 3 [(google.api.field_behavior) = OUTPUT_ONLY];

  // Output only. The Cloud SQL database instance's public IP.
  string public_ip = 4 [(google.api.field_behavior) = OUTPUT_ONLY];
}

// An entry for an Access Control list.
message SqlAclEntry {
  // The allowlisted value for the access control list.
  string value = 1;

  // The access control entry entry expiration.
  oneof expiration {
    // The time when this access control entry expires in
    // [RFC 3339](https://tools.ietf.org/html/rfc3339) format, for example:
    // `2012-11-15T16:19:00.094Z`.
    google.protobuf.Timestamp expire_time = 10;

    // Input only. The time-to-leave of this access control entry.
    google.protobuf.Duration ttl = 11 [(google.api.field_behavior) = INPUT_ONLY];
  }

  // A label to identify this entry.
  string label = 3;
}

// IP Management configuration.
message SqlIpConfig {
  // Whether the instance should be assigned an IPv4 address or not.
  google.protobuf.BoolValue enable_ipv4 = 1;

  // The resource link for the VPC network from which the Cloud SQL instance is
  // accessible for private IP. For example,
  // `projects/myProject/global/networks/default`. This setting can
  // be updated, but it cannot be removed after it is set.
  string private_network = 2;

  // Whether SSL connections over IP should be enforced or not.
  google.protobuf.BoolValue require_ssl = 3;

  // The list of external networks that are allowed to connect to the instance
  // using the IP. See
  // https://en.wikipedia.org/wiki/CIDR_notation#CIDR_notation, also known as
  // 'slash' notation (e.g. `192.168.100.0/24`).
  repeated SqlAclEntry authorized_networks = 4;
}

// Settings for creating a Cloud SQL database instance.
message CloudSqlSettings {
  // Specifies when the instance should be activated.
  enum SqlActivationPolicy {
    // unspecified policy.
    SQL_ACTIVATION_POLICY_UNSPECIFIED = 0;

    // The instance is always up and running.
    ALWAYS = 1;

    // The instance should never spin up.
    NEVER = 2;
  }

  // The storage options for Cloud SQL databases.
  enum SqlDataDiskType {
    // Unspecified.
    SQL_DATA_DISK_TYPE_UNSPECIFIED = 0;

    // SSD disk.
    PD_SSD = 1;

    // HDD disk.
    PD_HDD = 2;
  }

  // The database engine type and version.
  enum SqlDatabaseVersion {
    // Unspecified version.
    SQL_DATABASE_VERSION_UNSPECIFIED = 0;

    // MySQL 5.6.
    MYSQL_5_6 = 1;

    // MySQL 5.7.
    MYSQL_5_7 = 2;

    // PostgreSQL 9.6.
    POSTGRES_9_6 = 3;

    // PostgreSQL 11.
    POSTGRES_11 = 4;

    // PostgreSQL 10.
    POSTGRES_10 = 5;

    // MySQL 8.0.
    MYSQL_8_0 = 6;

    // PostgreSQL 12.
    POSTGRES_12 = 7;

    // PostgreSQL 13.
    POSTGRES_13 = 8;
  }

  // The database engine type and version.
  SqlDatabaseVersion database_version = 1;

  // The resource labels for a Cloud SQL instance to use to annotate any related
  // underlying resources such as Compute Engine VMs.
  // An object containing a list of "key": "value" pairs.
  //
  // Example: `{ "name": "wrench", "mass": "18kg", "count": "3" }`.
  map<string, string> user_labels = 2;

  // The tier (or machine type) for this instance, for example:
  // `db-n1-standard-1` (MySQL instances) or
  // `db-custom-1-3840` (PostgreSQL instances).
  // For more information, see
  // [Cloud SQL Instance
  // Settings](https://cloud.google.com/sql/docs/mysql/instance-settings).
  string tier = 3;

  // The maximum size to which storage capacity can be automatically increased.
  // The default value is 0, which specifies that there is no limit.
  google.protobuf.Int64Value storage_auto_resize_limit = 4;

  // The activation policy specifies when the instance is activated; it is
  // applicable only when the instance state is 'RUNNABLE'. Valid values:
  //
  // 'ALWAYS': The instance is on, and remains so even in
  // the absence of connection requests.
  //
  // `NEVER`: The instance is off; it is not activated, even if a
  // connection request arrives.
  SqlActivationPolicy activation_policy = 5;

  // The settings for IP Management. This allows to enable or disable the
  // instance IP and manage which external networks can connect to the instance.
  // The IPv4 address cannot be disabled.
  SqlIpConfig ip_config = 6;

  // [default: ON] If you enable this setting, Cloud SQL checks your available
  // storage every 30 seconds. If the available storage falls below a threshold
  // size, Cloud SQL automatically adds additional storage capacity. If the
  // available storage repeatedly falls below the threshold size, Cloud SQL
  // continues to add storage until it reaches the maximum of 30 TB.
  google.protobuf.BoolValue auto_storage_increase = 7;

  // The database flags passed to the Cloud SQL instance at startup.
  // An object containing a list of "key": value pairs.
  // Example: { "name": "wrench", "mass": "1.3kg", "count": "3" }.
  map<string, string> database_flags = 8;

  // The type of storage: `PD_SSD` (default) or `PD_HDD`.
  SqlDataDiskType data_disk_type = 9;

  // The storage capacity available to the database, in GB.
  // The minimum (and default) size is 10GB.
  google.protobuf.Int64Value data_disk_size_gb = 10;

  // The Google Cloud Platform zone where your Cloud SQL datdabse instance is
  // located.
  string zone = 11;

  // The Database Migration Service source connection profile ID,
  // in the format:
  // `projects/my_project_name/locations/us-central1/connectionProfiles/connection_profile_ID`
  string source_id = 12;

  // Input only. Initial root password.
  string root_password = 13 [(google.api.field_behavior) = INPUT_ONLY];

  // Output only. Indicates If this connection profile root password is stored.
  bool root_password_set = 14 [(google.api.field_behavior) = OUTPUT_ONLY];

  // The Cloud SQL default instance level collation.
  string collation = 15;
}

// The source database will allow incoming connections from the destination
// database's public IP. You can retrieve the Cloud SQL instance's public IP
// from the Cloud SQL console or using Cloud SQL APIs. No additional
// configuration is required.
message StaticIpConnectivity {

}

// The details needed to configure a reverse SSH tunnel between the source and
// destination databases. These details will be used when calling the
// generateSshScript method (see
// https://cloud.google.com/database-migration/docs/reference/rest/v1/projects.locations.migrationJobs/generateSshScript)
// to produce the script that will help set up the reverse SSH tunnel, and to
// set up the VPC peering between the Cloud SQL private network and the VPC.
message ReverseSshConnectivity {
  // Required. The IP of the virtual machine (Compute Engine) used as the bastion server
  // for the SSH tunnel.
  string vm_ip = 1 [(google.api.field_behavior) = REQUIRED];

  // Required. The forwarding port of the virtual machine (Compute Engine) used as the
  // bastion server for the SSH tunnel.
  int32 vm_port = 2 [(google.api.field_behavior) = REQUIRED];

  // The name of the virtual machine (Compute Engine) used as the bastion server
  // for the SSH tunnel.
  string vm = 3;

  // The name of the VPC to peer with the Cloud SQL private network.
  string vpc = 4;
}

// The details of the VPC where the source database is located in Google Cloud.
// We will use this information to set up the VPC peering connection between
// Cloud SQL and this VPC.
message VpcPeeringConnectivity {
  // The name of the VPC network to peer with the Cloud SQL private network.
  string vpc = 1;
}

// A message defining the database engine and provider.
message DatabaseType {
  // The database provider.
  DatabaseProvider provider = 1;

  // The database engine.
  DatabaseEngine engine = 2;
}

// Represents a Database Migration Service migration job object.
message MigrationJob {
  option (google.api.resource) = {
    type: "datamigration.googleapis.com/MigrationJob"
    pattern: "projects/{project}/locations/{location}/migrationJobs/{migration_job}"
  };

  // The current migration job states.
  enum State {
    // The state of the migration job is unknown.
    STATE_UNSPECIFIED = 0;

    // The migration job is down for maintenance.
    MAINTENANCE = 1;

    // The migration job is in draft mode and no resources are created.
    DRAFT = 2;

    // The migration job is being created.
    CREATING = 3;

    // The migration job is created, not started and is fully editable.
    NOT_STARTED = 4;

    // The migration job is running.
    RUNNING = 5;

    // The migration job failed.
    FAILED = 6;

    // The migration job has been completed.
    COMPLETED = 7;

    // The migration job is being deleted.
    DELETING = 8;

    // The migration job is being stopped.
    STOPPING = 9;

    // The migration job is currently stopped.
    STOPPED = 10;

    // The migration job has been deleted.
    DELETED = 11;

    // The migration job is being updated.
    UPDATING = 12;

    // The migration job is starting.
    STARTING = 13;

    // The migration job is restarting.
    RESTARTING = 14;

    // The migration job is resuming.
    RESUMING = 15;
  }

  // The current migration job phase.
  enum Phase {
    // The phase of the migration job is unknown.
    PHASE_UNSPECIFIED = 0;

    // The migration job is in the full dump phase.
    FULL_DUMP = 1;

    // The migration job is CDC phase.
    CDC = 2;

    // The migration job is running the promote phase.
    PROMOTE_IN_PROGRESS = 3;

    // Only RDS flow - waiting for source writes to stop
    WAITING_FOR_SOURCE_WRITES_TO_STOP = 4;

    // Only RDS flow - the sources writes stopped, waiting for dump to begin
    PREPARING_THE_DUMP = 5;
  }

  // The type of migration job (one-time or continuous).
  enum Type {
    // The type of the migration job is unknown.
    TYPE_UNSPECIFIED = 0;

    // The migration job is a one time migration.
    ONE_TIME = 1;

    // The migration job is a continuous migration.
    CONTINUOUS = 2;
  }

  // The name (URI) of this migration job resource, in the form of:
  // projects/{project}/locations/{location}/instances/{instance}.
  string name = 1;

  // Output only. The timestamp when the migration job resource was created.
  // A timestamp in RFC3339 UTC "Zulu" format, accurate to nanoseconds.
  // Example: "2014-10-02T15:01:23.045123456Z".
  google.protobuf.Timestamp create_time = 2 [(google.api.field_behavior) = OUTPUT_ONLY];

  // Output only. The timestamp when the migration job resource was last updated.
  // A timestamp in RFC3339 UTC "Zulu" format, accurate to nanoseconds.
  // Example: "2014-10-02T15:01:23.045123456Z".
  google.protobuf.Timestamp update_time = 3 [(google.api.field_behavior) = OUTPUT_ONLY];

  // The resource labels for migration job to use to annotate any related
  // underlying resources such as Compute Engine VMs. An object containing a
  // list of "key": "value" pairs.
  //
  // Example: `{ "name": "wrench", "mass": "1.3kg", "count": "3" }`.
  map<string, string> labels = 4;

  // The migration job display name.
  string display_name = 5;

  // The current migration job state.
  State state = 6;

  // Output only. The current migration job phase.
  Phase phase = 7 [(google.api.field_behavior) = OUTPUT_ONLY];

  // Required. The migration job type.
  Type type = 8 [(google.api.field_behavior) = REQUIRED];

  // The path to the dump file in Google Cloud Storage,
  // in the format: (gs://[BUCKET_NAME]/[OBJECT_NAME]).
  string dump_path = 9;

  // Required. The resource name (URI) of the source connection profile.
  string source = 10 [(google.api.field_behavior) = REQUIRED];

  // Required. The resource name (URI) of the destination connection profile.
  string destination = 11 [(google.api.field_behavior) = REQUIRED];

  // The connectivity method.
  oneof connectivity {
    // The details needed to communicate to the source over Reverse SSH
    // tunnel connectivity.
    ReverseSshConnectivity reverse_ssh_connectivity = 101;

    // The details of the VPC network that the source database is located in.
    VpcPeeringConnectivity vpc_peering_connectivity = 102;

    // static ip connectivity data (default, no additional details needed).
    StaticIpConnectivity static_ip_connectivity = 103;
  }

  // Output only. The duration of the migration job (in seconds). A duration in seconds
  // with up to nine fractional digits, terminated by 's'. Example: "3.5s".
  google.protobuf.Duration duration = 12 [(google.api.field_behavior) = OUTPUT_ONLY];

  // Output only. The error details in case of state FAILED.
  google.rpc.Status error = 13 [(google.api.field_behavior) = OUTPUT_ONLY];

  // The database engine type and provider of the source.
  DatabaseType source_database = 14;

  // The database engine type and provider of the destination.
  DatabaseType destination_database = 15;

  // Output only. If the migration job is completed, the time when it was completed.
  google.protobuf.Timestamp end_time = 16 [(google.api.field_behavior) = OUTPUT_ONLY];
}

// A connection profile definition.
message ConnectionProfile {
  option (google.api.resource) = {
    type: "datamigration.googleapis.com/ConnectionProfile"
    pattern: "projects/{project}/locations/{location}/connectionProfiles/{connection_profile}"
  };

  // The current connection profile state (e.g. DRAFT, READY, or FAILED).
  enum State {
    // The state of the connection profile is unknown.
    STATE_UNSPECIFIED = 0;

    // The connection profile is in draft mode and fully editable.
    DRAFT = 1;

    // The connection profile is being created.
    CREATING = 2;

    // The connection profile is ready.
    READY = 3;

    // The connection profile is being updated.
    UPDATING = 4;

    // The connection profile is being deleted.
    DELETING = 5;

    // The connection profile has been deleted.
    DELETED = 6;

    // The last action on the connection profile failed.
    FAILED = 7;
  }

  // The name of this connection profile resource in the form of
  // projects/{project}/locations/{location}/instances/{instance}.
  string name = 1;

  // Output only. The timestamp when the resource was created.
  // A timestamp in RFC3339 UTC "Zulu" format, accurate to nanoseconds.
  // Example: "2014-10-02T15:01:23.045123456Z".
  google.protobuf.Timestamp create_time = 2 [(google.api.field_behavior) = OUTPUT_ONLY];

  // Output only. The timestamp when the resource was last updated.
  // A timestamp in RFC3339 UTC "Zulu" format, accurate to nanoseconds.
  // Example: "2014-10-02T15:01:23.045123456Z".
  google.protobuf.Timestamp update_time = 3 [(google.api.field_behavior) = OUTPUT_ONLY];

  // The resource labels for connection profile to use to annotate any related
  // underlying resources such as Compute Engine VMs. An object containing a
  // list of "key": "value" pairs.
  //
  // Example: `{ "name": "wrench", "mass": "1.3kg", "count": "3" }`.
  map<string, string> labels = 4;

  // The current connection profile state (e.g. DRAFT, READY, or FAILED).
  State state = 5;

  // The connection profile display name.
  string display_name = 6;

  // The connection profile definition.
  oneof connection_profile {
    // A MySQL database connection profile.
    MySqlConnectionProfile mysql = 100;

    // A PostgreSQL database connection profile.
    PostgreSqlConnectionProfile postgresql = 101;

    // A CloudSQL database connection profile.
    CloudSqlConnectionProfile cloudsql = 102;
  }

  // Output only. The error details in case of state FAILED.
  google.rpc.Status error = 7 [(google.api.field_behavior) = OUTPUT_ONLY];

  // The database provider.
  DatabaseProvider provider = 8;
}

// Error message of a verification Migration job.
message MigrationJobVerificationError {
  // A general error code describing the type of error that occurred.
  enum ErrorCode {
    // An unknown error occurred
    ERROR_CODE_UNSPECIFIED = 0;

    // We failed to connect to one of the connection profile.
    CONNECTION_FAILURE = 1;

    // We failed to authenticate to one of the connection profile.
    AUTHENTICATION_FAILURE = 2;

    // One of the involved connection profiles has an invalid configuration.
    INVALID_CONNECTION_PROFILE_CONFIG = 3;

    // The versions of the source and the destination are incompatible.
    VERSION_INCOMPATIBILITY = 4;

    // The types of the source and the destination are incompatible.
    CONNECTION_PROFILE_TYPES_INCOMPATIBILITY = 5;

    // No pglogical extension installed on databases, applicable for postgres.
    NO_PGLOGICAL_INSTALLED = 7;

    // pglogical node already exists on databases, applicable for postgres.
    PGLOGICAL_NODE_ALREADY_EXISTS = 8;

    // The value of parameter wal_level is not set to logical.
    INVALID_WAL_LEVEL = 9;

    // The value of parameter shared_preload_libraries does not include
    // pglogical.
    INVALID_SHARED_PRELOAD_LIBRARY = 10;

    // The value of parameter max_replication_slots is not sufficient.
    INSUFFICIENT_MAX_REPLICATION_SLOTS = 11;

    // The value of parameter max_wal_senders is not sufficient.
    INSUFFICIENT_MAX_WAL_SENDERS = 12;

    // The value of parameter max_worker_processes is not sufficient.
    INSUFFICIENT_MAX_WORKER_PROCESSES = 13;

    // Extensions installed are either not supported or having unsupported
    // versions.
    UNSUPPORTED_EXTENSIONS = 14;

    // Unsupported migration type.
    UNSUPPORTED_MIGRATION_TYPE = 15;

    // Invalid RDS logical replication.
    INVALID_RDS_LOGICAL_REPLICATION = 16;

    // The gtid_mode is not supported, applicable for MySQL.
    UNSUPPORTED_GTID_MODE = 17;

    // The table definition is not support due to missing primary key or replica
    // identity.
    UNSUPPORTED_TABLE_DEFINITION = 18;

    // The definer is not supported.
    UNSUPPORTED_DEFINER = 19;

    // Migration is already running at the time of restart request.
    CANT_RESTART_RUNNING_MIGRATION = 21;
  }

  // Output only. An instance of ErrorCode specifying the error that occurred.
  ErrorCode error_code = 1 [(google.api.field_behavior) = OUTPUT_ONLY];

  // Output only. A formatted message with further details about the error and a CTA.
  string error_message = 2 [(google.api.field_behavior) = OUTPUT_ONLY];

  // Output only. A specific detailed error message, if supplied by the engine.
  string error_detail_message = 3 [(google.api.field_behavior) = OUTPUT_ONLY];
}

// The database engine types.
enum DatabaseEngine {
  // The source database engine of the migration job is unknown.
  DATABASE_ENGINE_UNSPECIFIED = 0;

  // The source engine is MySQL.
  MYSQL = 1;

  // The source engine is PostgreSQL.
  POSTGRESQL = 2;
}

// The database providers.
enum DatabaseProvider {
  // The database provider is unknown.
  DATABASE_PROVIDER_UNSPECIFIED = 0;

  // CloudSQL runs the database.
  CLOUDSQL = 1;

  // RDS runs the database.
  RDS = 2;
}
