# Copyright 2025 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.

apiVersion: blueprints.cloud.google.com/v1alpha1
kind: BlueprintMetadata
metadata:
  name: terraform-google-cloud-spanner
  annotations:
    config.kubernetes.io/local-config: "true"
spec:
  info:
    title: terraform-google-cloud-spanner
    source:
      repo: https://github.com/GoogleCloudPlatform/terraform-google-cloud-spanner
      sourceType: git
    version: 1.1.3
    actuationTool:
      flavor: Terraform
      version: ">= 1.3"
    description: {}
    icon: assets/icon.png
  content:
    subBlueprints:
      - name: schedule_spanner_backup
        location: modules/schedule_spanner_backup
    examples:
      - name: example_with_backup
        location: examples/example_with_backup
      - name: example_with_kms
        location: examples/example_with_kms
      - name: simple_example
        location: examples/simple_example
  interfaces:
    variables:
      - name: project_id
        description: The project ID to deploy to
        varType: string
        required: true
      - name: instance_name
        description: A unique identifier for the instance, which cannot be changed after the instance is created. The name must be between 6 and 30 characters in length.
        varType: string
        required: true
      - name: instance_display_name
        description: The descriptive name for this instance as it appears in UIs.
        varType: string
        defaultValue: spanner-instance
      - name: instance_config
        description: The name of the instance's configuration (similar but not quite the same as a region) which defines the geographic placement and replication of your databases in this instance. It determines where your data is stored. Values are typically of the form regional-europe-west1, us-central etc. In order to obtain a valid list please consult the https://cloud.google.com/spanner/docs/instance-configurations
        varType: string
        required: true
      - name: instance_size
        description: The sizing configuration of Spanner Instance based on num of nodes OR instance processing units.
        varType: |-
          object({
              num_nodes        = optional(number)
              processing_units = optional(number)
            })
        required: true
      - name: create_instance
        description: "Switch to use create OR use existing Spanner Instance "
        varType: bool
        defaultValue: true
      - name: enable_autoscaling
        description: Enable autoscaling for the Spanner Instance
        varType: bool
        defaultValue: false
      - name: instance_iam
        description: The list of permissions on spanner instance
        varType: list(string)
        defaultValue: []
        connections:
          - source:
              source: github.com/terraform-google-modules/terraform-google-service-accounts//modules/simple-sa
              version: ">= 4.4"
            spec:
              outputExpr: iam_email
          - source:
              source: github.com/GoogleCloudPlatform/terraform-google-cloud-run//modules/v2
              version: ">= 0.14"
            spec:
              outputExpr: service_account_id.member
          - source:
              source: github.com/terraform-google-modules/terraform-google-vm//modules/instance_template
              version: ">= 13.2.0"
            spec:
              outputExpr: service_account_info.member
      - name: instance_labels
        description: A set of key/value label pairs to assign to the spanner instance
        varType: map(string)
        defaultValue: {}
      - name: database_config
        description: "The list of databases with their configuration to be created "
        varType: |-
          map(object({
              version_retention_period = string
              ddl                      = optional(list(string), [])
              kms_key_name             = optional(string)
              deletion_protection      = bool
              database_iam             = optional(list(string), [])
              enable_backup            = optional(bool)
              backup_retention         = optional(string)
              create_db                = optional(bool)
            }))
        defaultValue:
          db1:
            backup_retention: 86400s
            create_db: true
            database_iam: []
            ddl: []
            deletion_protection: false
            enable_backup: true
            version_retention_period: 3d
      - name: cron_spec_text
        description: The cron expression for the backup schedule.
        varType: string
        defaultValue: 0 2 * * *
      - name: min_processing_units
        description: Minimum number of processing units for autoscaling.
        varType: number
        defaultValue: 1000
      - name: max_processing_units
        description: Maximum number of processing units for autoscaling.
        varType: number
        defaultValue: 3000
      - name: min_nodes
        description: Minimum number of nodes for autoscaling.
        varType: number
        defaultValue: 1
      - name: max_nodes
        description: Maximum number of nodes for autoscaling.
        varType: number
        defaultValue: 3
      - name: high_priority_cpu_utilization_percent
        description: Target high priority CPU utilization percentage for autoscaling.
        varType: number
        defaultValue: 60
      - name: storage_utilization_percent
        description: Target storage utilization percentage for autoscaling.
        varType: number
        defaultValue: 70
      - name: replica_location
        description: Location of the replica for asymmetric autoscaling.
        varType: string
        defaultValue: us-central1
      - name: override_min_nodes
        description: Minimum number of nodes for specific replica overrides.
        varType: number
        defaultValue: 1
      - name: override_max_nodes
        description: Maximum number of nodes for specific replica overrides.
        varType: number
        defaultValue: 3
      - name: edition
        description: The edition of the Spanner instance.
        varType: string
        defaultValue: STANDARD
      - name: default_backup_schedule_type
        description: Default backup schedule type for new databases.
        varType: string
        defaultValue: NONE
      - name: force_destroy
        description: Whether to force destroy the instance and its backups.
        varType: bool
        defaultValue: false
      - name: use_full_backup_spec
        description: Whether to use full backup specification.
        varType: bool
        defaultValue: true
      - name: use_incremental_backup_spec
        description: Whether to use incremental backup specification.
        varType: bool
        defaultValue: false
    outputs:
      - name: env_vars
        description: Map of environment variables for Spanner databases with keys as project_id:instance_name:database_name and values as fully qualified database paths
        type:
          - map
          - string
      - name: spanner_db_details
        description: Spanner Databases information map
        type:
          - object
          - db1:
              - object
              - database_dialect: string
                ddl:
                  - list
                  - string
                deletion_protection: bool
                enable_drop_protection: bool
                encryption_config:
                  - list
                  - - object
                    - kms_key_name: string
                      kms_key_names:
                        - list
                        - string
                id: string
                instance: string
                name: string
                project: string
                state: string
                timeouts:
                  - object
                  - create: string
                    delete: string
                    update: string
                version_retention_period: string
      - name: spanner_instance_id
        description: Spanner Instance ID
        type: string
      - name: spanner_schedule_backup_id
        description: Spanner Backup Workflow ID
        type: string
  requirements:
    roles:
      - level: Project
        roles:
          - roles/owner
    services:
      - iam.googleapis.com
      - cloudresourcemanager.googleapis.com
      - storage-api.googleapis.com
      - serviceusage.googleapis.com
      - workflows.googleapis.com
      - cloudscheduler.googleapis.com
      - spanner.googleapis.com
      - pubsub.googleapis.com
      - logging.googleapis.com
      - storage.googleapis.com
      - appengine.googleapis.com
      - cloudkms.googleapis.com
    providerVersions:
      - source: hashicorp/google
        version: ">= 6.1, < 7"
      - source: hashicorp/google-beta
        version: ">= 6.1, < 7"
      - source: hashicorp/null
        version: ~> 3.1
      - source: hashicorp/random
        version: ~> 3.1
