From 3b19d0def6419efb446b8e0211d08a66000054b8 Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Mon, 25 Apr 2022 22:50:11 +0000 Subject: [PATCH] datasource google_compute_disk (#5978) Signed-off-by: Modular Magician --- .changelog/5978.txt | 3 + google/data_source_google_compute_disk.go | 31 +++++ .../data_source_google_compute_disk_test.go | 42 +++++++ google/provider.go | 1 + website/docs/d/compute_disk.html.markdown | 116 ++++++++++++++++++ website/google.erb | 4 + 6 files changed, 197 insertions(+) create mode 100644 .changelog/5978.txt create mode 100644 google/data_source_google_compute_disk.go create mode 100644 google/data_source_google_compute_disk_test.go create mode 100644 website/docs/d/compute_disk.html.markdown diff --git a/.changelog/5978.txt b/.changelog/5978.txt new file mode 100644 index 00000000000..6950c225c33 --- /dev/null +++ b/.changelog/5978.txt @@ -0,0 +1,3 @@ +```release-note:new-datasource +google_compute_disk +``` diff --git a/google/data_source_google_compute_disk.go b/google/data_source_google_compute_disk.go new file mode 100644 index 00000000000..c7465103451 --- /dev/null +++ b/google/data_source_google_compute_disk.go @@ -0,0 +1,31 @@ +package google + +import ( + "fmt" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceGoogleComputeDisk() *schema.Resource { + + dsSchema := datasourceSchemaFromResourceSchema(resourceComputeDisk().Schema) + addRequiredFieldsToSchema(dsSchema, "name") + addOptionalFieldsToSchema(dsSchema, "project") + addOptionalFieldsToSchema(dsSchema, "zone") + + return &schema.Resource{ + Read: dataSourceGoogleComputeDiskRead, + Schema: dsSchema, + } +} + +func dataSourceGoogleComputeDiskRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + + id, err := replaceVars(d, config, "projects/{{project}}/zones/{{zone}}/disks/{{name}}") + if err != nil { + return fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + return resourceComputeDiskRead(d, meta) +} diff --git a/google/data_source_google_compute_disk_test.go b/google/data_source_google_compute_disk_test.go new file mode 100644 index 00000000000..2a435c4a252 --- /dev/null +++ b/google/data_source_google_compute_disk_test.go @@ -0,0 +1,42 @@ +package google + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccDataSourceGoogleComputeDisk_basic(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": randString(t, 10), + } + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckComputeDiskDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccDataSourceGoogleComputeDisk_basic(context), + Check: resource.ComposeTestCheckFunc( + checkDataSourceStateMatchesResourceState("data.google_compute_disk.foo", "google_compute_disk.foo"), + ), + }, + }, + }) +} + +func testAccDataSourceGoogleComputeDisk_basic(context map[string]interface{}) string { + return Nprintf(` +resource "google_compute_disk" "foo" { + name = "tf-test-compute-disk-%{random_suffix}" +} + +data "google_compute_disk" "foo" { + name = google_compute_disk.foo.name + project = google_compute_disk.foo.project +} +`, context) +} diff --git a/google/provider.go b/google/provider.go index 619e1ea514a..c1f2811eb16 100644 --- a/google/provider.go +++ b/google/provider.go @@ -737,6 +737,7 @@ func Provider() *schema.Provider { "google_compute_backend_service": dataSourceGoogleComputeBackendService(), "google_compute_backend_bucket": dataSourceGoogleComputeBackendBucket(), "google_compute_default_service_account": dataSourceGoogleComputeDefaultServiceAccount(), + "google_compute_disk": dataSourceGoogleComputeDisk(), "google_compute_forwarding_rule": dataSourceGoogleComputeForwardingRule(), "google_compute_global_address": dataSourceGoogleComputeGlobalAddress(), "google_compute_global_forwarding_rule": dataSourceGoogleComputeGlobalForwardingRule(), diff --git a/website/docs/d/compute_disk.html.markdown b/website/docs/d/compute_disk.html.markdown new file mode 100644 index 00000000000..d10b528217a --- /dev/null +++ b/website/docs/d/compute_disk.html.markdown @@ -0,0 +1,116 @@ +--- +subcategory: "Compute Engine" +layout: "google" +page_title: "Google: google_compute_disk" +sidebar_current: "docs-google-datasource-compute-disk" +description: |- + Get information about a Google Compute Persistent disks. +--- + +# google\_compute\_disk + +Get information about a Google Compute Persistent disks. + +[the official documentation](https://cloud.google.com/compute/docs/disks) and its [API](https://cloud.google.com/compute/docs/reference/latest/disks). + +## Example Usage + +```hcl +data "google_compute_disk" "persistent-boot-disk" { + name = "persistent-boot-disk" + project = "example" +} + +resource "google_compute_instance" "default" { + # ... + + boot_disk { + source = data.google_compute_disk.persistent-boot-disk.self_link + auto_delete = false + } +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) The name of a specific disk. + +- - - + +* `zone` - (Optional) A reference to the zone where the disk resides. + +* `project` - (Optional) The ID of the project in which the resource belongs. + If it is not provided, the provider project is used. + +## Attributes Reference + +In addition to the arguments listed above, the following computed attributes are exported: + +* `id` - an identifier for the resource with format `projects/{{project}}/zones/{{zone}}/disks/{{name}}` + +* `label_fingerprint` - + The fingerprint used for optimistic locking of this resource. Used + internally during updates. + +* `creation_timestamp` - + Creation timestamp in RFC3339 text format. + +* `last_attach_timestamp` - + Last attach timestamp in RFC3339 text format. + +* `last_detach_timestamp` - + Last detach timestamp in RFC3339 text format. + +* `users` - + Links to the users of the disk (attached instances) in form: + project/zones/zone/instances/instance + +* `source_image_id` - + The ID value of the image used to create this disk. This value + identifies the exact image that was used to create this persistent + disk. For example, if you created the persistent disk from an image + that was later deleted and recreated under the same name, the source + image ID would identify the exact version of the image that was used. + +* `source_snapshot_id` - + 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. + +* `description` - + The optional description of this resource. + +* `labels` - A map of labels applied to this disk. + +* `size` - + Size of the persistent disk, specified in GB. + +* `physical_block_size_bytes` - + Physical block size of the persistent disk, in bytes. + +* `type` - + URL of the disk type resource describing which disk type to use to + create the disk. + +* `image` - + The image from which to initialize this disk. + +* `zone` - + A reference to the zone where the disk resides. + +* `source_image_encryption_key` - + The customer-supplied encryption key of the source image. + +* `snapshot` - + The source snapshot used to create this disk. + +* `source_snapshot_encryption_key` - + (Optional) + The customer-supplied encryption key of the source snapshot. + +* `self_link` - The URI of the created resource. diff --git a/website/google.erb b/website/google.erb index e775846ec79..f028a6d7c76 100644 --- a/website/google.erb +++ b/website/google.erb @@ -1737,6 +1737,10 @@ google_compute_default_service_account +
  • + google_compute_disk +
  • +
  • google_compute_forwarding_rule