# Copyright 2025 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: 'InstancePartition'
description: |
  A Cloud Spanner instance partition is a unit of Cloud Spanner database capacity
  that can be used to partition data and processing capacity within an instance.
references:
  guides:
    'Official Documentation': 'https://cloud.google.com/spanner/docs/geo-partitioning'
  api: 'https://cloud.google.com/spanner/docs/reference/rest/v1/projects.instances.instancePartitions'

id_format: 'projects/{{project}}/instances/{{instance}}/instancePartitions/{{name}}'
base_url: 'projects/{{project}}/instances/{{instance}}/instancePartitions'
self_link: 'projects/{{project}}/instances/{{instance}}/instancePartitions/{{name}}'
create_url: 'projects/{{project}}/instances/{{instance}}/instancePartitions?instancePartitionId={{name}}'
delete_url: 'projects/{{project}}/instances/{{instance}}/instancePartitions/{{name}}'
update_verb: 'PATCH'

timeouts:
  insert_minutes: 30
  update_minutes: 30
  delete_minutes: 30
autogen_async: true
async:
  actions: ['create', 'update']
  type: 'OpAsync'
  operation:
    base_url: '{{op_id}}'
  result:
    resource_inside_response: true
import_format:
  - 'projects/{{project}}/instances/{{instance}}/instancePartitions/{{name}}'
  - '{{project}}/{{instance}}/{{name}}'
  - '{{instance}}/{{name}}'
examples:
  - name: 'spanner_instance_partition_basic'
    primary_resource_id: 'partition'
    vars:
      instance_name: 'test-instance'
      partition_name: 'test-partition'
custom_code: !ruby/object:Provider::Terraform::CustomCode
  encoder: 'templates/terraform/encoders/spanner_instance_partition.go.tmpl'
  pre_update: 'templates/terraform/pre_update/spanner_instance_partition.go.tmpl'
parameters:
  - name: 'instance'
    type: ResourceRef
    description: 'The instance to create the instance partition in.'
    required: true
    immutable: true
    resource: 'Instance'
    imports: 'name'
    url_param_only: true

properties:
  - name: 'name'
    description: |
      A unique identifier for the instance partition, which cannot be changed after
      the instance partition is created. The name must be between 2 and 64 characters
      and match the regular expression [a-z][a-z0-9\\-]{0,61}[a-z0-9].
    type: String
    required: true
    immutable: true
    validation:
      regex: '^[a-z][a-z0-9-]{0,61}[a-z0-9]$'
    custom_flatten: 'templates/terraform/custom_flatten/name_from_self_link.tmpl'

  - name: 'displayName'
    description: |
      The descriptive name for this instance partition as it appears in UIs.
      Must be unique per project and between 4 and 30 characters in length.
    type: String
    required: true
    validation:
      function: 'validation.StringLenBetween(4,30)'

  - name: 'nodeCount'
    description: |
      The number of nodes allocated to this instance partition. One node equals
      1000 processing units. Exactly one of either node_count or processing_units
      must be present.
    type: Integer
    validation:
      function: 'validation.IntAtLeast(1)'
    exactly_one_of:
      - 'node_count'
      - 'processing_units'

  - name: 'processingUnits'
    description: |
      The number of processing units allocated to this instance partition.
      Exactly one of either node_count or processing_units must be present.
    type: Integer
    validation:
      function: 'validation.IntAtLeast(1000)'
    exactly_one_of:
      - 'node_count'
      - 'processing_units'

  - name: 'config'
    description: |
      The name of the instance partition's configuration (similar to a region) which
      defines the geographic placement and replication of data in this instance partition.
    type: ResourceRef
    required: true
    immutable: true
    custom_expand: 'templates/terraform/custom_expand/spanner_instance_config.go.tmpl'
    resource: 'InstanceConfig'
    imports: 'name'

  - name: 'state'
    description: |
      The current instance partition state. Possible values are:
      CREATING: The instance partition is being created. Resources are being
      allocated for the instance partition.
      READY: The instance partition has been allocated resources and is ready for use.
    type: Enum
    output: true
    enum_values:
      - 'CREATING'
      - 'READY'
