Skip to content

Commit ef44829

Browse files
Add google_compute_images data source (#13019) (#21872)
[upstream:ddcb451d36bda6d521c10c8d1b6a4328a567e51c] Signed-off-by: Modular Magician <[email protected]>
1 parent 197022d commit ef44829

File tree

5 files changed

+270
-0
lines changed

5 files changed

+270
-0
lines changed

.changelog/13019.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_images`
3+
```

google/provider/provider_mmv1_resources.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
212212
"google_compute_ha_vpn_gateway": compute.DataSourceGoogleComputeHaVpnGateway(),
213213
"google_compute_health_check": compute.DataSourceGoogleComputeHealthCheck(),
214214
"google_compute_image": compute.DataSourceGoogleComputeImage(),
215+
"google_compute_images": compute.DataSourceGoogleComputeImages(),
215216
"google_compute_instance": compute.DataSourceGoogleComputeInstance(),
216217
"google_compute_instance_group": compute.DataSourceGoogleComputeInstanceGroup(),
217218
"google_compute_instance_group_manager": compute.DataSourceGoogleComputeInstanceGroupManager(),
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
package compute
4+
5+
import (
6+
"fmt"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
10+
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
11+
)
12+
13+
func DataSourceGoogleComputeImages() *schema.Resource {
14+
return &schema.Resource{
15+
Read: dataSourceGoogleComputeImagesRead,
16+
17+
Schema: map[string]*schema.Schema{
18+
"filter": {
19+
Type: schema.TypeString,
20+
Optional: true,
21+
},
22+
"project": {
23+
Type: schema.TypeString,
24+
Optional: true,
25+
},
26+
"images": {
27+
Type: schema.TypeList,
28+
Computed: true,
29+
Elem: &schema.Resource{
30+
Schema: map[string]*schema.Schema{
31+
"name": {
32+
Type: schema.TypeString,
33+
Computed: true,
34+
},
35+
"family": {
36+
Type: schema.TypeString,
37+
Computed: true,
38+
},
39+
"self_link": {
40+
Type: schema.TypeString,
41+
Computed: true,
42+
},
43+
"archive_size_bytes": {
44+
Type: schema.TypeInt,
45+
Computed: true,
46+
},
47+
"creation_timestamp": {
48+
Type: schema.TypeString,
49+
Computed: true,
50+
},
51+
"description": {
52+
Type: schema.TypeString,
53+
Computed: true,
54+
},
55+
"disk_size_gb": {
56+
Type: schema.TypeInt,
57+
Computed: true,
58+
},
59+
"image_id": {
60+
Type: schema.TypeInt,
61+
Computed: true,
62+
},
63+
"labels": {
64+
Type: schema.TypeMap,
65+
Elem: &schema.Schema{
66+
Type: schema.TypeString,
67+
},
68+
Computed: true,
69+
},
70+
"source_disk": {
71+
Type: schema.TypeString,
72+
Computed: true,
73+
},
74+
"source_disk_id": {
75+
Type: schema.TypeString,
76+
Computed: true,
77+
},
78+
"source_image_id": {
79+
Type: schema.TypeString,
80+
Computed: true,
81+
},
82+
},
83+
},
84+
},
85+
},
86+
}
87+
}
88+
89+
func dataSourceGoogleComputeImagesRead(d *schema.ResourceData, meta interface{}) error {
90+
config := meta.(*transport_tpg.Config)
91+
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
92+
if err != nil {
93+
return err
94+
}
95+
96+
project, err := tpgresource.GetProject(d, config)
97+
if err != nil {
98+
return fmt.Errorf("Error fetching project for image: %s", err)
99+
}
100+
101+
filter := d.Get("filter").(string)
102+
103+
images := make([]map[string]interface{}, 0)
104+
105+
imageList, err := config.NewComputeClient(userAgent).Images.List(project).Filter(filter).Do()
106+
if err != nil {
107+
return transport_tpg.HandleDataSourceNotFoundError(err, d, fmt.Sprintf("Images : %s", project), fmt.Sprintf("Images : %s", project))
108+
}
109+
110+
for _, image := range imageList.Items {
111+
images = append(images, map[string]interface{}{
112+
"name": image.Name,
113+
"family": image.Family,
114+
"self_link": image.SelfLink,
115+
"archive_size_bytes": image.ArchiveSizeBytes,
116+
"creation_timestamp": image.CreationTimestamp,
117+
"description": image.Description,
118+
"disk_size_gb": image.DiskSizeGb,
119+
"image_id": image.Id,
120+
"labels": image.Labels,
121+
"source_disk": image.SourceDisk,
122+
"source_disk_id": image.SourceDiskId,
123+
"source_image_id": image.SourceImageId,
124+
})
125+
}
126+
127+
if err := d.Set("images", images); err != nil {
128+
return fmt.Errorf("Error retrieving images: %s", err)
129+
}
130+
131+
d.SetId(fmt.Sprintf(
132+
"projects/%s/global/images",
133+
project,
134+
))
135+
136+
return nil
137+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
// Copyright (c) HashiCorp, Inc.
4+
// SPDX-License-Identifier: MPL-2.0
5+
package compute_test
6+
7+
import (
8+
"testing"
9+
10+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
11+
"github.com/hashicorp/terraform-provider-google/google/acctest"
12+
)
13+
14+
func TestAccDataSourceComputeImages_basic(t *testing.T) {
15+
t.Parallel()
16+
17+
context := map[string]interface{}{
18+
"random_suffix": acctest.RandString(t, 10),
19+
"image": "debian-cloud/debian-11",
20+
}
21+
22+
acctest.VcrTest(t, resource.TestCase{
23+
PreCheck: func() { acctest.AccTestPreCheck(t) },
24+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
25+
Steps: []resource.TestStep{
26+
{
27+
Config: testAccCheckGoogleComputeImagesConfig(context),
28+
Check: resource.ComposeTestCheckFunc(
29+
// Test schema
30+
resource.TestCheckResourceAttrSet("data.google_compute_images.all", "images.0.name"),
31+
resource.TestCheckResourceAttrSet("data.google_compute_images.all", "images.1.name"),
32+
resource.TestCheckResourceAttrSet("data.google_compute_images.all", "images.0.self_link"),
33+
resource.TestCheckResourceAttrSet("data.google_compute_images.all", "images.1.self_link"),
34+
resource.TestCheckResourceAttrSet("data.google_compute_images.all", "images.0.image_id"),
35+
resource.TestCheckResourceAttrSet("data.google_compute_images.all", "images.1.image_id"),
36+
),
37+
},
38+
},
39+
})
40+
}
41+
42+
func testAccCheckGoogleComputeImagesConfig(context map[string]interface{}) string {
43+
return acctest.Nprintf(`
44+
resource "google_compute_disk" "test-disk" {
45+
name = "tf-test-disk-%{random_suffix}"
46+
type = "pd-standard"
47+
image = "%{image}"
48+
size = 10
49+
}
50+
51+
resource "google_compute_image" "foo" {
52+
name = "tf-test-image1-%{random_suffix}"
53+
source_disk = google_compute_disk.test-disk.self_link
54+
}
55+
56+
resource "google_compute_image" "bar" {
57+
name = "tf-test-image2-%{random_suffix}"
58+
source_image = google_compute_image.foo.self_link
59+
}
60+
61+
data "google_compute_images" "all" {
62+
}
63+
`, context)
64+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
subcategory: "Compute Engine"
3+
description: |-
4+
Get information about Google Compute Images.
5+
---
6+
7+
# google_compute_images
8+
9+
Get information about Google Compute Images. Check that your service account has the `compute.imageUser` role if you want to share [custom images](https://cloud.google.com/compute/docs/images/sharing-images-across-projects) from another project. If you want to use [public images][pubimg], do not forget to specify the dedicated project. For more information see
10+
[the official documentation](https://cloud.google.com/compute/docs/images) and its [API](https://cloud.google.com/compute/docs/reference/latest/images).
11+
12+
## Example Usage
13+
14+
```hcl
15+
data "google_compute_images" "debian" {
16+
filter = "name eq my-image.*"
17+
}
18+
19+
resource "google_compute_instance" "default" {
20+
name = "test"
21+
machine_type = "f1-micro"
22+
zone = "us-central1-a"
23+
24+
boot_disk {
25+
initialize_params {
26+
image = data.google_compute_images.debian.images[0].self_link
27+
}
28+
}
29+
30+
network_interface {
31+
network = google_compute_network.default.name
32+
}
33+
}
34+
```
35+
36+
## Argument Reference
37+
38+
The following arguments are supported:
39+
40+
* `filter` -Filter for the images to be returned by the data source. Syntax can be found [here](https://cloud.google.com/compute/docs/reference/rest/v1/images/list) in the filter section.
41+
42+
- - -
43+
44+
* `project` - (Optional) The project in which the resource belongs. If it is not
45+
provided, the provider project is used. If you are using a
46+
[public base image][pubimg], be sure to specify the correct Image Project.
47+
48+
## Attributes Reference
49+
50+
In addition to the arguments listed above, the following computed attributes are
51+
exported:
52+
53+
* `self_link` - The URI of the image.
54+
* `name` - The name of the image.
55+
* `family` - The family name of the image.
56+
* `disk_size_gb` - The size of the image when restored onto a persistent disk in gigabytes.
57+
* `archive_size_bytes` - The size of the image tar.gz archive stored in Google Cloud Storage in bytes.
58+
* `source_image_id` - The ID value of the image used to create this image.
59+
* `source_disk` - The URL of the source disk used to create this image.
60+
* `source_disk_id` - The ID value of the disk used to create this image.
61+
* `creation_timestamp` - The creation timestamp in RFC3339 text format.
62+
* `description` - An optional description of this image.
63+
* `labels` - All of labels (key/value pairs) present on the resource in GCP, including the labels configured through Terraform, other clients and services.
64+
65+
[pubimg]: https://cloud.google.com/compute/docs/images#os-compute-support "Google Cloud Public Base Images"

0 commit comments

Comments
 (0)