# Copyright 2024 Google Inc.
# 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.

---
name: 'Instance'
description: |
  A Google Cloud Redis instance.
references:
  guides:
    'Official Documentation': 'https://cloud.google.com/memorystore/docs/redis/'
  api: 'https://cloud.google.com/memorystore/docs/redis/reference/rest/v1/projects.locations.instances'
docs:
  attributes: |
    * `auth_string` - AUTH String set on the instance. This field will only be populated if auth_enabled is true.
base_url: 'projects/{{project}}/locations/{{region}}/instances'
create_url: 'projects/{{project}}/locations/{{region}}/instances?instanceId={{name}}'
update_verb: 'PATCH'
update_mask: true
timeouts:
  insert_minutes: 20
  update_minutes: 20
  delete_minutes: 20
autogen_async: true
async:
  actions: ['create', 'delete', 'update']
  type: 'OpAsync'
  operation:
    base_url: '{{op_id}}'
  result:
    resource_inside_response: false
custom_code:
  extra_schema_entry: 'templates/terraform/extra_schema_entry/redis_instance.tmpl'
  constants: 'templates/terraform/constants/redis_instance.go.tmpl'
  encoder: 'templates/terraform/encoders/redis_location_id_for_fallback_zone.go.tmpl'
  decoder: 'templates/terraform/decoders/redis_instance.go.tmpl'
custom_diff:
  - 'customdiff.ForceNewIfChange("redis_version", isRedisVersionDecreasing)'
  - 'tpgresource.DefaultProviderProject'
exclude_default_cdiff: true
examples:
  - name: 'redis_instance_basic'
    primary_resource_id: 'cache'
    vars:
      instance_name: 'memory-cache'
      prevent_destroy: 'true'
    test_vars_overrides:
      'prevent_destroy': 'false'
    oics_vars_overrides:
      'prevent_destroy': 'false'
  - name: 'redis_instance_full'
    primary_resource_id: 'cache'
    vars:
      instance_name: 'ha-memory-cache'
      network_name: 'redis-test-network'
      prevent_destroy: 'true'
    test_vars_overrides:
      'network_name': 'acctest.BootstrapSharedTestNetwork(t, "redis-full")'
      'prevent_destroy': 'false'
    oics_vars_overrides:
      'prevent_destroy': 'false'
  - name: 'redis_instance_full_with_persistence_config'
    primary_resource_id: 'cache-persis'
    vars:
      instance_name: 'ha-memory-cache-persis'
      network_name: 'redis-test-network'
      prevent_destroy: 'true'
    test_vars_overrides:
      'network_name': 'acctest.BootstrapSharedTestNetwork(t, "redis-full-persis")'
      'prevent_destroy': 'false'
    oics_vars_overrides:
      'prevent_destroy': 'false'
  - name: 'redis_instance_private_service'
    primary_resource_id: 'cache'
    vars:
      instance_name: 'private-cache'
      address_name: 'address'
      network_name: 'redis-test-network'
      prevent_destroy: 'true'
    test_vars_overrides:
      'prevent_destroy': 'false'
    oics_vars_overrides:
      'prevent_destroy': 'false'
    exclude_test: true
  - name: 'redis_instance_private_service_test'
    primary_resource_id: 'cache'
    vars:
      instance_name: 'private-cache'
      network_name: 'redis-test-network'
      prevent_destroy: 'true'
    test_vars_overrides:
      'network_name': 'acctest.BootstrapSharedServiceNetworkingConnection(t, "vpc-network-1")'
      'prevent_destroy': 'false'
    oics_vars_overrides:
      'prevent_destroy': 'false'
    exclude_docs: true
  - name: 'redis_instance_mrr'
    primary_resource_id: 'cache'
    vars:
      instance_name: 'mrr-memory-cache'
      network_name: 'redis-test-network'
      prevent_destroy: 'true'
    test_vars_overrides:
      'network_name': 'acctest.BootstrapSharedTestNetwork(t, "redis-mrr")'
      'prevent_destroy': 'false'
    oics_vars_overrides:
      'prevent_destroy': 'false'
  - name: 'redis_instance_cmek'
    primary_resource_id: 'cache'
    vars:
      instance_name: 'cmek-memory-cache'
      network_name: 'redis-test-network'
      prevent_destroy: 'true'
    test_vars_overrides:
      'network_name': 'acctest.BootstrapSharedTestNetwork(t, "redis-cmek")'
      'prevent_destroy': 'false'
    oics_vars_overrides:
      'prevent_destroy': 'false'
    exclude_test: true
parameters:
  # TODO: resourceref?
  - name: 'region'
    type: String
    description: |
      The name of the Redis region of the instance.
    url_param_only: true
    required: false
    immutable: true
    ignore_read: true
    default_from_api: true
properties:
  - name: 'alternativeLocationId'
    type: String
    description: |
      Only applicable to STANDARD_HA tier which protects the instance
      against zonal failures by provisioning it across two zones.
      If provided, it must be a different zone from the one provided in
      [locationId].
    immutable: true
    default_from_api: true
  - name: 'authEnabled'
    type: Boolean
    description: |
      Optional. Indicates whether OSS Redis AUTH is enabled for the
      instance. If set to "true" AUTH is enabled on the instance.
      Default value is "false" meaning AUTH is disabled.
    default_value: false
  - name: 'authorizedNetwork'
    type: String
    description: |
      The full name of the Google Compute Engine network to which the
      instance is connected. If left unspecified, the default network
      will be used.
    immutable: true
    default_from_api: true
    diff_suppress_func: 'tpgresource.CompareSelfLinkOrResourceName'
    custom_expand: 'templates/terraform/custom_expand/redis_instance_authorized_network.tmpl'
  - name: 'connectMode'
    type: Enum
    description: |
      The connection mode of the Redis instance.
    immutable: true
    default_value: "DIRECT_PEERING"
    enum_values:
      - 'DIRECT_PEERING'
      - 'PRIVATE_SERVICE_ACCESS'
  - name: 'createTime'
    type: Time
    description: |
      The time the instance was created in RFC3339 UTC "Zulu" format,
      accurate to nanoseconds.
    output: true
  - name: 'currentLocationId'
    type: String
    description: |
      The current zone where the Redis endpoint is placed.
      For Basic Tier instances, this will always be the same as the
      [locationId] provided by the user at creation time. For Standard Tier
      instances, this can be either [locationId] or [alternativeLocationId]
      and can change after a failover event.
    output: true
  - name: 'displayName'
    type: String
    description: |
      An arbitrary and optional user-provided name for the instance.
  - name: 'host'
    type: String
    description: |
      Hostname or IP address of the exposed Redis endpoint used by clients
      to connect to the service.
    output: true
  - name: 'labels'
    type: KeyValueLabels
    description: Resource labels to represent user provided metadata.
  - name: 'redisConfigs'
    type: KeyValuePairs
    description: |
      Redis configuration parameters, according to http://redis.io/topics/config.
      Please check Memorystore documentation for the list of supported parameters:
      https://cloud.google.com/memorystore/docs/redis/reference/rest/v1/projects.locations.instances#Instance.FIELDS.redis_configs
  - name: 'locationId'
    type: String
    description: |
      The zone where the instance will be provisioned. If not provided,
      the service will choose a zone for the instance. For STANDARD_HA tier,
      instances will be created across two zones for protection against
      zonal failures. If [alternativeLocationId] is also provided, it must
      be different from [locationId].
    immutable: true
    default_from_api: true
  - name: 'name'
    type: String
    description: |
      The ID of the instance or a fully qualified identifier for the instance.
    required: true
    immutable: true
    custom_flatten: 'templates/terraform/custom_flatten/name_from_self_link.tmpl'
    custom_expand: 'templates/terraform/custom_expand/shortname_to_url.go.tmpl'
    validation:
      regex: '^[a-z][a-z0-9-]{0,39}[a-z0-9]$'
  - name: 'persistenceConfig'
    type: NestedObject
    description: Persistence configuration for an instance.
    default_from_api: true
    properties:
      - name: 'persistenceMode'
        type: Enum
        description: |
          Optional. Controls whether Persistence features are enabled. If not provided, the existing value will be used.

          - DISABLED: 	Persistence is disabled for the instance, and any existing snapshots are deleted.
          - RDB: RDB based Persistence is enabled.
        required: true
        default_from_api: true
        enum_values:
          - 'DISABLED'
          - 'RDB'
      - name: 'rdbSnapshotPeriod'
        type: Enum
        description: |
          Optional. Available snapshot periods for scheduling.

          - ONE_HOUR:	Snapshot every 1 hour.
          - SIX_HOURS:	Snapshot every 6 hours.
          - TWELVE_HOURS:	Snapshot every 12 hours.
          - TWENTY_FOUR_HOURS:	Snapshot every 24 hours.
        required: false
        enum_values:
          - 'ONE_HOUR'
          - 'SIX_HOURS'
          - 'TWELVE_HOURS'
          - 'TWENTY_FOUR_HOURS'
      - name: 'rdbNextSnapshotTime'
        type: String
        description: |
          Output only. The next time that a snapshot attempt is scheduled to occur.
          A timestamp in RFC3339 UTC "Zulu" format, with nanosecond resolution and up
          to nine fractional digits.
          Examples: "2014-10-02T15:01:23Z" and "2014-10-02T15:01:23.045123456Z".
        output: true
      - name: 'rdbSnapshotStartTime'
        type: String
        description: |
          Optional. Date and time that the first snapshot was/will be attempted,
          and to which future snapshots will be aligned. If not provided,
          the current time will be used.
          A timestamp in RFC3339 UTC "Zulu" format, with nanosecond resolution
          and up to nine fractional digits.
          Examples: "2014-10-02T15:01:23Z" and "2014-10-02T15:01:23.045123456Z".
        default_from_api: true
  - name: 'maintenancePolicy'
    type: NestedObject
    description: Maintenance policy for an instance.
    properties:
      - name: 'createTime'
        type: String
        description: |
          Output only. The time when the policy was created.
          A timestamp in RFC3339 UTC "Zulu" format, with nanosecond
          resolution and up to nine fractional digits.
        output: true
      - name: 'updateTime'
        type: String
        description: |
          Output only. The time when the policy was last updated.
          A timestamp in RFC3339 UTC "Zulu" format, with nanosecond
          resolution and up to nine fractional digits.
        output: true
      - name: 'description'
        type: String
        description: |
          Optional. Description of what this policy is for.
          Create/Update methods return INVALID_ARGUMENT if the
          length is greater than 512.
      - name: 'weeklyMaintenanceWindow'
        type: Array
        description: |
          Optional. Maintenance window that is applied to resources covered by this policy.
          Minimum 1. For the current version, the maximum number
          of weekly_window is expected to be one.
        item_type:
          type: NestedObject
          properties:
            - name: 'day'
              type: Enum
              description: |
                Required. The day of week that maintenance updates occur.

                - DAY_OF_WEEK_UNSPECIFIED: The day of the week is unspecified.
                - MONDAY: Monday
                - TUESDAY: Tuesday
                - WEDNESDAY: Wednesday
                - THURSDAY: Thursday
                - FRIDAY: Friday
                - SATURDAY: Saturday
                - SUNDAY: Sunday
              required: true
              enum_values:
                - 'DAY_OF_WEEK_UNSPECIFIED'
                - 'MONDAY'
                - 'TUESDAY'
                - 'WEDNESDAY'
                - 'THURSDAY'
                - 'FRIDAY'
                - 'SATURDAY'
                - 'SUNDAY'
            - name: 'duration'
              type: String
              description: |
                Output only. Duration of the maintenance window.
                The current window is fixed at 1 hour.
                A duration in seconds with up to nine fractional digits,
                terminated by 's'. Example: "3.5s".
              output: true
            - name: 'startTime'
              type: NestedObject
              description: |
                Required. Start time of the window in UTC time.
              required: true
              send_empty_value: true
              allow_empty_object: true
              properties:
                - name: 'hours'
                  type: Integer
                  description: |
                    Hours of day in 24 hour format. Should be from 0 to 23.
                    An API may choose to allow the value "24:00:00" for scenarios like business closing time.
                  validation:
                    function: 'validation.IntBetween(0,23)'
                - name: 'minutes'
                  type: Integer
                  description: |
                    Minutes of hour of day. Must be from 0 to 59.
                  validation:
                    function: 'validation.IntBetween(0,59)'
                - name: 'seconds'
                  type: Integer
                  description: |
                    Seconds of minutes of the time. Must normally be from 0 to 59.
                    An API may allow the value 60 if it allows leap-seconds.
                  validation:
                    function: 'validation.IntBetween(0,60)'
                - name: 'nanos'
                  type: Integer
                  description: |
                    Fractions of seconds in nanoseconds. Must be from 0 to 999,999,999.
                  validation:
                    function: 'validation.IntBetween(0,999999999)'
  - name: 'maintenanceSchedule'
    type: NestedObject
    description: Upcoming maintenance schedule.
    output: true
    properties:
      - name: 'startTime'
        type: String
        description: |
          Output only. The start time of any upcoming scheduled maintenance for this instance.
          A timestamp in RFC3339 UTC "Zulu" format, with nanosecond
          resolution and up to nine fractional digits.
        output: true
      - name: 'endTime'
        type: String
        description: |
          Output only. The end time of any upcoming scheduled maintenance for this instance.
          A timestamp in RFC3339 UTC "Zulu" format, with nanosecond
          resolution and up to nine fractional digits.
        output: true
      - name: 'scheduleDeadlineTime'
        type: String
        description: |
          Output only. The deadline that the maintenance schedule start time
          can not go beyond, including reschedule.
          A timestamp in RFC3339 UTC "Zulu" format, with nanosecond
          resolution and up to nine fractional digits.
        output: true
  - name: 'maintenanceVersion'
    type: String
    description: The self service update maintenance version.
    required: false
    default_from_api: true
  - name: 'memorySizeGb'
    type: Integer
    description: Redis memory size in GiB.
    required: true
  - name: 'port'
    type: Integer
    description: The port number of the exposed Redis endpoint.
    output: true
  - name: 'persistenceIamIdentity'
    type: String
    description: |
      Output only. Cloud IAM identity used by import / export operations
      to transfer data to/from Cloud Storage. Format is "serviceAccount:".
      The value may change over time for a given instance so should be
      checked before each import/export operation.
    output: true
  - name: 'redisVersion'
    type: String
    description: |
      The version of Redis software. If not provided, latest supported
      version will be used. Please check the API documentation linked
      at the top for the latest valid values.
    default_from_api: true
    update_url: 'projects/{{project}}/locations/{{region}}/instances/{{name}}:upgrade'
    update_verb: 'POST'
  - name: 'reservedIpRange'
    type: String
    description: |
      The CIDR range of internal addresses that are reserved for this
      instance. If not provided, the service will choose an unused /29
      block, for example, 10.0.0.0/29 or 192.168.0.0/29. Ranges must be
      unique and non-overlapping with existing subnets in an authorized
      network.
    immutable: true
    # In some situations the returned IP range may not match the sent value
    # but will be a subset of the range.
    ignore_read: true
    default_from_api: true
  - name: 'tier'
    type: Enum
    description: |
      The service tier of the instance. Must be one of these values:

      - BASIC: standalone instance
      - STANDARD_HA: highly available primary/replica instances
    immutable: true
    default_value: "BASIC"
    enum_values:
      - 'BASIC'
      - 'STANDARD_HA'
  - name: 'transitEncryptionMode'
    type: Enum
    description: |
      The TLS mode of the Redis instance, If not provided, TLS is disabled for the instance.

      - SERVER_AUTHENTICATION: Client to Server traffic encryption enabled with server authentication
    immutable: true
    default_value: "DISABLED"
    enum_values:
      - 'SERVER_AUTHENTICATION'
      - 'DISABLED'
  - name: 'serverCaCerts'
    type: Array
    description: |
      List of server CA certificates for the instance.
    output: true
    item_type:
      type: NestedObject
      properties:
        - name: 'serialNumber'
          type: String
          description: |
            Serial number, as extracted from the certificate.
          output: true
        - name: 'cert'
          type: String
          description: |
            The certificate data in PEM format.
          output: true
        - name: 'createTime'
          type: String
          description: |
            The time when the certificate was created.
          output: true
        - name: 'expireTime'
          type: String
          description: |
            The time when the certificate expires.
          output: true
        - name: 'sha1Fingerprint'
          type: String
          description: |
            Sha1 Fingerprint of the certificate.
          output: true
  - name: 'replicaCount'
    type: Integer
    description: |
      Optional. The number of replica nodes. The valid range for the Standard Tier with
      read replicas enabled is [1-5] and defaults to 2. If read replicas are not enabled
      for a Standard Tier instance, the only valid value is 1 and the default is 1.
      The valid value for basic tier is 0 and the default is also 0.
    default_from_api: true
  - name: 'nodes'
    type: Array
    description: |
      Output only. Info per node.
    output: true
    item_type:
      type: NestedObject
      properties:
        - name: 'id'
          type: String
          description: |
            Node identifying string. e.g. 'node-0', 'node-1'
          output: true
        - name: 'zone'
          type: String
          description: |
            Location of the node.
          output: true
  - name: 'readEndpoint'
    type: String
    description: |
      Output only. Hostname or IP address of the exposed readonly Redis endpoint. Standard tier only.
      Targets all healthy replica nodes in instance. Replication is asynchronous and replica nodes
      will exhibit some lag behind the primary. Write requests must target 'host'.
    output: true
  - name: 'readEndpointPort'
    type: Integer
    description: |
      Output only. The port number of the exposed readonly redis endpoint. Standard tier only.
      Write requests should target 'port'.
    output: true
  - name: 'readReplicasMode'
    type: Enum
    description: |
      Optional. Read replica mode. Can only be specified when trying to create the instance.
      If not set, Memorystore Redis backend will default to READ_REPLICAS_DISABLED.
      - READ_REPLICAS_DISABLED: If disabled, read endpoint will not be provided and the
      instance cannot scale up or down the number of replicas.
      - READ_REPLICAS_ENABLED: If enabled, read endpoint will be provided and the instance
      can scale up and down the number of replicas.
    default_from_api: true
    enum_values:
      - 'READ_REPLICAS_DISABLED'
      - 'READ_REPLICAS_ENABLED'
  - name: 'secondaryIpRange'
    type: String
    description: |
      Optional. Additional IP range for node placement. Required when enabling read replicas on
      an existing instance. For DIRECT_PEERING mode value must be a CIDR range of size /28, or
      "auto". For PRIVATE_SERVICE_ACCESS mode value must be the name of an allocated address
      range associated with the private service access connection, or "auto".
    default_from_api: true
    diff_suppress_func: 'secondaryIpDiffSuppress'
  - name: 'customerManagedKey'
    type: String
    description: |
      Optional. The KMS key reference that you want to use to encrypt the data at rest for this Redis
      instance. If this is provided, CMEK is enabled.
    immutable: true
  - name: 'tags'
    type: KeyValuePairs
    description: |
      A map of resource manager tags.
      Resource manager tag keys and values have the same definition as resource manager tags.
      Keys must be in the format tagKeys/{tag_key_id}, and values are in the format tagValues/{tag_key_value}.
    immutable: true
    ignore_read: true
