# 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: 'RegionDisk'
api_resource_type_kind: Disk
kind: 'compute#disk'
description: |
  Persistent disks are durable storage devices that function similarly to
  the physical disks in a desktop or a server. Compute Engine manages the
  hardware behind these devices to ensure data redundancy and optimize
  performance for you. Persistent disks are available as either standard
  hard disk drives (HDD) or solid-state drives (SSD).

  Persistent disks are located independently from your virtual machine
  instances, so you can detach or move persistent disks to keep your data
  even after you delete your instances. Persistent disk performance scales
  automatically with size, so you can resize your existing persistent disks
  or add more persistent disks to an instance to meet your performance and
  storage space requirements.

  Add a persistent disk to your instance when you need reliable and
  affordable storage with consistent performance characteristics.
references:
  guides:
    'Adding or Resizing Regional Persistent Disks': 'https://cloud.google.com/compute/docs/disks/regional-persistent-disk'
  api: 'https://cloud.google.com/compute/docs/reference/rest/v1/regionDisks'
docs:
base_url: 'projects/{{project}}/regions/{{region}}/disks'
has_self_link: true
immutable: true
timeouts:
  insert_minutes: 20
  update_minutes: 20
  delete_minutes: 20
async:
  actions: ['create', 'delete', 'update']
  type: 'OpAsync'
  operation:
    base_url: '{{op_id}}'
  result:
    resource_inside_response: false
collection_url_key: 'items'
iam_policy:
  parent_resource_attribute: 'name'
  base_url: 'projects/{{project}}/regions/{{region}}/disks/{{name}}'
  example_config_body: 'templates/terraform/iam/iam_attributes.go.tmpl'
  import_format:
    - 'projects/{{project}}/regions/{{region}}/disks/{{name}}'
    - '{{name}}'
custom_code:
  encoder: 'templates/terraform/encoders/disk.tmpl'
  decoder: 'templates/terraform/decoders/disk.tmpl'
  pre_delete: 'templates/terraform/pre_delete/detach_disk.tmpl'
custom_diff:
  - 'customdiff.ForceNewIfChange("size", IsDiskShrinkage)'
  - 'hyperDiskIopsUpdateDiffSuppress'
examples:
  - name: 'region_disk_basic'
    primary_resource_id: 'regiondisk'
    primary_resource_name: 'fmt.Sprintf("tf-test-my-region-disk%s", context["random_suffix"])'
    vars:
      region_disk_name: 'my-region-disk'
      disk_name: 'my-disk'
      snapshot_name: 'my-snapshot'
  - name: 'region_disk_async'
    primary_resource_id: 'primary'
    primary_resource_name: 'fmt.Sprintf("tf-test-my-region-disk%s", context["random_suffix"])'
    vars:
      region_disk_name: 'primary-region-disk'
      secondary_region_disk_name: 'secondary-region-disk'
  - name: 'region_disk_features'
    primary_resource_id: 'regiondisk'
    primary_resource_name: 'fmt.Sprintf("tf-test-my-region-disk%s", context["random_suffix"])'
    vars:
      region_disk_name: 'my-region-features-disk'
parameters:
  - name: 'region'
    type: ResourceRef
    description: 'A reference to the region where the disk resides.'
    required: false
    default_from_api: true
    custom_flatten: 'templates/terraform/custom_flatten/name_from_self_link.tmpl'
    custom_expand: 'templates/terraform/custom_expand/resourceref_with_validation.go.tmpl'
    resource: 'Region'
    imports: 'name'
  - name: 'snapshot'
    type: ResourceRef
    description: |
      The source snapshot used to create this disk. You can provide this as
      a partial or full URL to the resource. For example, the following are
      valid values:

      * `https://www.googleapis.com/compute/v1/projects/project/global/snapshots/snapshot`
      * `projects/project/global/snapshots/snapshot`
      * `global/snapshots/snapshot`
      * `snapshot`
    api_name: sourceSnapshot
    custom_expand: 'templates/terraform/custom_expand/resourceref_with_validation.go.tmpl'
    resource: 'Snapshot'
    imports: 'selfLink'
properties:
  - name: 'diskEncryptionKey'
    type: NestedObject
    description: |
      Encrypts the disk using a customer-supplied encryption key.

      After you encrypt a disk with a customer-supplied key, you must
      provide the same key if you use the disk later (e.g. to create a disk
      snapshot or an image, or to attach the disk to a virtual machine).

      Customer-supplied encryption keys do not protect access to metadata of
      the disk.

      If you do not provide an encryption key when creating the disk, then
      the disk will be encrypted using an automatically generated key and
      you do not need to provide a key to use the disk later.
    immutable: true
    properties:
      - name: 'rawKey'
        type: String
        description: |
          Specifies a 256-bit customer-supplied encryption key, encoded in
          RFC 4648 base64 to either encrypt or decrypt this resource.
        sensitive: true
      - name: 'rsaEncryptedKey'
        type: String
        description: |
          Specifies an RFC 4648 base64 encoded, RSA-wrapped 2048-bit
          customer-supplied encryption key to either encrypt or decrypt
          this resource. You can provide either the rawKey or the rsaEncryptedKey.
        sensitive: true
      - name: 'sha256'
        type: String
        description: |
          The RFC 4648 base64 encoded SHA-256 hash of the customer-supplied
          encryption key that protects this resource.
        output: true
        # TODO(chrisst) Change to ResourceRef once KMS is in Magic Modules
      - name: 'kmsKeyName'
        type: String
        description: |
          The name of the encryption key that is stored in Google Cloud KMS.
  - name: 'sourceSnapshotEncryptionKey'
    type: NestedObject
    description: |
      The customer-supplied encryption key of the source snapshot. Required
      if the source snapshot is protected by a customer-supplied encryption
      key.
    immutable: true
    properties:
      - name: 'rawKey'
        type: String
        description: |
          Specifies a 256-bit customer-supplied encryption key, encoded in
          RFC 4648 base64 to either encrypt or decrypt this resource.
        # TODO(chrisst) Change to ResourceRef once KMS is in Magic Modules
      - name: 'kmsKeyName'
        type: String
        description: |
          The name of the encryption key that is stored in Google Cloud KMS.
        min_version: 'beta'
      - name: 'sha256'
        type: String
        description: |
          The RFC 4648 base64 encoded SHA-256 hash of the customer-supplied
          encryption key that protects this resource.
        output: true
  - name: 'sourceSnapshotId'
    type: String
    description: |
      The unique ID of the snapshot used to create this disk. This value
      identifies the exact snapshot that was used to create this persistent
      disk. For example, if you created the persistent disk from a snapshot
      that was later deleted and recreated under the same name, the source
      snapshot ID would identify the exact version of the snapshot that was
      used.
    output: true
  - name: 'labelFingerprint'
    type: Fingerprint
    description: |
      The fingerprint used for optimistic locking of this resource.  Used
      internally during updates.
    output: true
    update_url: 'projects/{{project}}/regions/{{region}}/disks/{{name}}/setLabels'
    update_verb: 'POST'
    key_expander: ''
  - name: 'creationTimestamp'
    type: Time
    description: 'Creation timestamp in RFC3339 text format.'
    output: true
  - name: 'description'
    type: String
    description: |
      An optional description of this resource. Provide this property when
      you create the resource.
  - name: 'lastAttachTimestamp'
    type: Time
    description: 'Last attach timestamp in RFC3339 text format.'
    output: true
  - name: 'lastDetachTimestamp'
    type: Time
    description: 'Last detach timestamp in RFC3339 text format.'
    output: true
  - name: 'labels'
    type: KeyValueLabels
    description: |
      Labels to apply to this disk.  A list of key->value pairs.
    update_url: 'projects/{{project}}/regions/{{region}}/disks/{{name}}/setLabels'
    update_verb: 'POST'
  - name: 'name'
    type: String
    description: |
      Name of the resource. Provided by the client when the resource is
      created. The name must be 1-63 characters long, and comply with
      RFC1035. Specifically, the name must be 1-63 characters long and match
      the regular expression `[a-z]([-a-z0-9]*[a-z0-9])?` which means the
      first character must be a lowercase letter, and all following
      characters must be a dash, lowercase letter, or digit, except the last
      character, which cannot be a dash.
    required: true
    immutable: true
  - name: 'size'
    type: Integer
    description: |
      Size of the persistent disk, specified in GB. You can specify this
      field when creating a persistent disk using the sourceImage or
      sourceSnapshot parameter, or specify it alone to create an empty
      persistent disk.

      If you specify this field along with sourceImage or sourceSnapshot,
      the value of sizeGb must not be less than the size of the sourceImage
      or the size of the snapshot.
    api_name: sizeGb
    default_from_api: true
    update_url: 'projects/{{project}}/regions/{{region}}/disks/{{name}}/resize'
    update_verb: 'POST'
  - name: 'users'
    type: Array
    description: |
      Links to the users of the disk (attached instances) in form:
      project/zones/zone/instances/instance
    output: true
    custom_expand: 'templates/terraform/custom_expand/array_resourceref_with_validation.go.tmpl'
    item_type:
      name: 'user'
      type: ResourceRef
      description: 'A reference to a user of this disk'
      resource: 'Instance'
      imports: 'selfLink'
  - name: 'physicalBlockSizeBytes'
    type: Integer
    description: |
      Physical block size of the persistent disk, in bytes. If not present
      in a request, a default value is used. Currently supported sizes
      are 4096 and 16384, other sizes may be added in the future.
      If an unsupported value is requested, the error message will list
      the supported values for the caller's project.
    default_from_api: true
  - name: 'replicaZones'
    type: Array
    description: 'URLs of the zones where the disk should be replicated to.'
    required: true
    custom_expand: 'templates/terraform/custom_expand/array_resourceref_with_validation.go.tmpl'
    item_type:
      name: 'zone'
      type: ResourceRef
      description: |
        A reference to a zone where the disk should be replicated to.
      resource: 'Zone'
      imports: 'selfLink'
    min_size: 2
    max_size: 2
  - name: 'type'
    type: ResourceRef
    description: |
      URL of the disk type resource describing which disk type to use to
      create the disk. Provide this when creating the disk.
    custom_flatten: 'templates/terraform/custom_flatten/name_from_self_link.tmpl'
    custom_expand: 'templates/terraform/custom_expand/resourceref_with_validation.go.tmpl'
    default_value: "pd-standard"
    resource: 'RegionDiskType'
    imports: 'selfLink'
  - name: 'interface'
    type: String
    description: |
      Specifies the disk interface to use for attaching this disk, which is either SCSI or NVME. The default is SCSI.
    min_version: 'beta'
    # interface is removed using url_param_only to preserve schema definition
    # and prevent sending or reading in API requests
    url_param_only: true
    diff_suppress_func: 'AlwaysDiffSuppress'
    default_value: "SCSI"
    deprecation_message: '`interface` is deprecated and will be removed in a future major release. This field is no longer used and can be safely removed from your configurations; disk interfaces are automatically determined on attachment.'
  - name: 'sourceDisk'
    type: String
    description: |
      The source disk used to create this disk. You can provide this as a partial or full URL to the resource.
      For example, the following are valid values:

      * https://www.googleapis.com/compute/v1/projects/{project}/zones/{zone}/disks/{disk}
      * https://www.googleapis.com/compute/v1/projects/{project}/regions/{region}/disks/{disk}
      * projects/{project}/zones/{zone}/disks/{disk}
      * projects/{project}/regions/{region}/disks/{disk}
      * zones/{zone}/disks/{disk}
      * regions/{region}/disks/{disk}
    diff_suppress_func: 'sourceDiskDiffSuppress'
  - name: 'DiskId'
    type: String
    description: |
      The unique identifier for the resource. This identifier is defined by the server.
    api_name: id
    output: true
  - name: 'sourceDiskId'
    type: String
    description: |
      The ID value of the disk used to create this image. This value may
      be used to determine whether the image was taken from the current
      or a previous instance of a given disk name.
    output: true
  - name: 'asyncPrimaryDisk'
    type: NestedObject
    diff_suppress_func: 'tpgresource.CompareSelfLinkRelativePaths'
    properties:
      - name: 'disk'
        type: String
        description: |
          Primary disk for asynchronous disk replication.
        required: true
  - name: 'guestOsFeatures'
    type: Array
    description: |
      A list of features to enable on the guest operating system.
      Applicable only for bootable disks.
    is_set: true
    default_from_api: true
    item_type:
      type: NestedObject
      properties:
        - name: 'type'
          type: Enum
          description: |
            The type of supported feature. Read [Enabling guest operating system features](https://cloud.google.com/compute/docs/images/create-delete-deprecate-private-images#guest-os-features) to see a list of available options.
          required: true
          enum_values:
            - 'MULTI_IP_SUBNET'
            - 'SECURE_BOOT'
            - 'SEV_CAPABLE'
            - 'UEFI_COMPATIBLE'
            - 'VIRTIO_SCSI_MULTIQUEUE'
            - 'WINDOWS'
            - 'GVNIC'
            - 'SEV_LIVE_MIGRATABLE'
            - 'SEV_SNP_CAPABLE'
            - 'SUSPEND_RESUME_COMPATIBLE'
            - 'TDX_CAPABLE'
  - name: 'licenses'
    type: Array
    description: Any applicable license URI.
    immutable: true
    default_from_api: true
    custom_expand: 'templates/terraform/custom_expand/array_resourceref_with_validation.go.tmpl'
    item_type:
      name: 'license'
      type: ResourceRef
      description: 'An applicable license URI'
      resource: 'License'
      imports: 'selfLink'
virtual_fields:
  - name: 'create_snapshot_before_destroy'
    type: Boolean
    default_value: false
    description: |
      If set to true, a snapshot of the disk will be created before it is destroyed.
      If your disk is encrypted with customer managed encryption keys these will be reused for the snapshot creation.
      The name of the snapshot by default will be `{{disk-name}}-YYYYMMDD-HHmm`
  - name: 'create_snapshot_before_destroy_prefix'
    type: String
    description: |
      This will set a custom name prefix for the snapshot that's created when the disk is deleted.
