Skip to content

Commit a4ab5a8

Browse files
datasource google_compute_disk (#5978) (#4255)
Signed-off-by: Modular Magician <[email protected]>
1 parent 012cebe commit a4ab5a8

File tree

6 files changed

+197
-0
lines changed

6 files changed

+197
-0
lines changed

.changelog/5978.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-datasource
2+
google_compute_disk
3+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package google
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
)
8+
9+
func dataSourceGoogleComputeDisk() *schema.Resource {
10+
11+
dsSchema := datasourceSchemaFromResourceSchema(resourceComputeDisk().Schema)
12+
addRequiredFieldsToSchema(dsSchema, "name")
13+
addOptionalFieldsToSchema(dsSchema, "project")
14+
addOptionalFieldsToSchema(dsSchema, "zone")
15+
16+
return &schema.Resource{
17+
Read: dataSourceGoogleComputeDiskRead,
18+
Schema: dsSchema,
19+
}
20+
}
21+
22+
func dataSourceGoogleComputeDiskRead(d *schema.ResourceData, meta interface{}) error {
23+
config := meta.(*Config)
24+
25+
id, err := replaceVars(d, config, "projects/{{project}}/zones/{{zone}}/disks/{{name}}")
26+
if err != nil {
27+
return fmt.Errorf("Error constructing id: %s", err)
28+
}
29+
d.SetId(id)
30+
return resourceComputeDiskRead(d, meta)
31+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package google
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccDataSourceGoogleComputeDisk_basic(t *testing.T) {
10+
t.Parallel()
11+
12+
context := map[string]interface{}{
13+
"random_suffix": randString(t, 10),
14+
}
15+
16+
vcrTest(t, resource.TestCase{
17+
PreCheck: func() { testAccPreCheck(t) },
18+
Providers: testAccProviders,
19+
CheckDestroy: testAccCheckComputeDiskDestroyProducer(t),
20+
Steps: []resource.TestStep{
21+
{
22+
Config: testAccDataSourceGoogleComputeDisk_basic(context),
23+
Check: resource.ComposeTestCheckFunc(
24+
checkDataSourceStateMatchesResourceState("data.google_compute_disk.foo", "google_compute_disk.foo"),
25+
),
26+
},
27+
},
28+
})
29+
}
30+
31+
func testAccDataSourceGoogleComputeDisk_basic(context map[string]interface{}) string {
32+
return Nprintf(`
33+
resource "google_compute_disk" "foo" {
34+
name = "tf-test-compute-disk-%{random_suffix}"
35+
}
36+
37+
data "google_compute_disk" "foo" {
38+
name = google_compute_disk.foo.name
39+
project = google_compute_disk.foo.project
40+
}
41+
`, context)
42+
}

google-beta/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,7 @@ func Provider() *schema.Provider {
826826
"google_compute_backend_service": dataSourceGoogleComputeBackendService(),
827827
"google_compute_backend_bucket": dataSourceGoogleComputeBackendBucket(),
828828
"google_compute_default_service_account": dataSourceGoogleComputeDefaultServiceAccount(),
829+
"google_compute_disk": dataSourceGoogleComputeDisk(),
829830
"google_compute_forwarding_rule": dataSourceGoogleComputeForwardingRule(),
830831
"google_compute_global_address": dataSourceGoogleComputeGlobalAddress(),
831832
"google_compute_global_forwarding_rule": dataSourceGoogleComputeGlobalForwardingRule(),
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
---
2+
subcategory: "Compute Engine"
3+
layout: "google"
4+
page_title: "Google: google_compute_disk"
5+
sidebar_current: "docs-google-datasource-compute-disk"
6+
description: |-
7+
Get information about a Google Compute Persistent disks.
8+
---
9+
10+
# google\_compute\_disk
11+
12+
Get information about a Google Compute Persistent disks.
13+
14+
[the official documentation](https://cloud.google.com/compute/docs/disks) and its [API](https://cloud.google.com/compute/docs/reference/latest/disks).
15+
16+
## Example Usage
17+
18+
```hcl
19+
data "google_compute_disk" "persistent-boot-disk" {
20+
name = "persistent-boot-disk"
21+
project = "example"
22+
}
23+
24+
resource "google_compute_instance" "default" {
25+
# ...
26+
27+
boot_disk {
28+
source = data.google_compute_disk.persistent-boot-disk.self_link
29+
auto_delete = false
30+
}
31+
}
32+
```
33+
34+
## Argument Reference
35+
36+
The following arguments are supported:
37+
38+
* `name` - (Required) The name of a specific disk.
39+
40+
- - -
41+
42+
* `zone` - (Optional) A reference to the zone where the disk resides.
43+
44+
* `project` - (Optional) The ID of the project in which the resource belongs.
45+
If it is not provided, the provider project is used.
46+
47+
## Attributes Reference
48+
49+
In addition to the arguments listed above, the following computed attributes are exported:
50+
51+
* `id` - an identifier for the resource with format `projects/{{project}}/zones/{{zone}}/disks/{{name}}`
52+
53+
* `label_fingerprint` -
54+
The fingerprint used for optimistic locking of this resource. Used
55+
internally during updates.
56+
57+
* `creation_timestamp` -
58+
Creation timestamp in RFC3339 text format.
59+
60+
* `last_attach_timestamp` -
61+
Last attach timestamp in RFC3339 text format.
62+
63+
* `last_detach_timestamp` -
64+
Last detach timestamp in RFC3339 text format.
65+
66+
* `users` -
67+
Links to the users of the disk (attached instances) in form:
68+
project/zones/zone/instances/instance
69+
70+
* `source_image_id` -
71+
The ID value of the image used to create this disk. This value
72+
identifies the exact image that was used to create this persistent
73+
disk. For example, if you created the persistent disk from an image
74+
that was later deleted and recreated under the same name, the source
75+
image ID would identify the exact version of the image that was used.
76+
77+
* `source_snapshot_id` -
78+
The unique ID of the snapshot used to create this disk. This value
79+
identifies the exact snapshot that was used to create this persistent
80+
disk. For example, if you created the persistent disk from a snapshot
81+
that was later deleted and recreated under the same name, the source
82+
snapshot ID would identify the exact version of the snapshot that was
83+
used.
84+
85+
* `description` -
86+
The optional description of this resource.
87+
88+
* `labels` - A map of labels applied to this disk.
89+
90+
* `size` -
91+
Size of the persistent disk, specified in GB.
92+
93+
* `physical_block_size_bytes` -
94+
Physical block size of the persistent disk, in bytes.
95+
96+
* `type` -
97+
URL of the disk type resource describing which disk type to use to
98+
create the disk.
99+
100+
* `image` -
101+
The image from which to initialize this disk.
102+
103+
* `zone` -
104+
A reference to the zone where the disk resides.
105+
106+
* `source_image_encryption_key` -
107+
The customer-supplied encryption key of the source image.
108+
109+
* `snapshot` -
110+
The source snapshot used to create this disk.
111+
112+
* `source_snapshot_encryption_key` -
113+
(Optional)
114+
The customer-supplied encryption key of the source snapshot.
115+
116+
* `self_link` - The URI of the created resource.

website/google.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,6 +1737,10 @@
17371737
<a href="/docs/providers/google/d/compute_default_service_account.html">google_compute_default_service_account</a>
17381738
</li>
17391739

1740+
<li>
1741+
<a href="/docs/providers/google/d/compute_disk.html">google_compute_disk</a>
1742+
</li>
1743+
17401744
<li>
17411745
<a href="/docs/providers/google/d/compute_forwarding_rule.html">google_compute_forwarding_rule</a>
17421746
</li>

0 commit comments

Comments
 (0)