Skip to content

Commit 9ba35f1

Browse files
karolgorcshuyama1
authored andcommitted
Add google_compute_images data source (GoogleCloudPlatform#13019)
Co-authored-by: Shuya Ma <[email protected]>
1 parent 72294fc commit 9ba35f1

File tree

4 files changed

+263
-0
lines changed

4 files changed

+263
-0
lines changed

mmv1/third_party/terraform/provider/provider_mmv1_resources.go.tmpl

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
8282
"google_compute_ha_vpn_gateway": compute.DataSourceGoogleComputeHaVpnGateway(),
8383
"google_compute_health_check": compute.DataSourceGoogleComputeHealthCheck(),
8484
"google_compute_image": compute.DataSourceGoogleComputeImage(),
85+
"google_compute_images": compute.DataSourceGoogleComputeImages(),
8586
"google_compute_instance": compute.DataSourceGoogleComputeInstance(),
8687
"google_compute_instance_group": compute.DataSourceGoogleComputeInstanceGroup(),
8788
"google_compute_instance_group_manager": compute.DataSourceGoogleComputeInstanceGroupManager(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package compute
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
8+
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
9+
)
10+
11+
func DataSourceGoogleComputeImages() *schema.Resource {
12+
return &schema.Resource{
13+
Read: dataSourceGoogleComputeImagesRead,
14+
15+
Schema: map[string]*schema.Schema{
16+
"filter": {
17+
Type: schema.TypeString,
18+
Optional: true,
19+
},
20+
"project": {
21+
Type: schema.TypeString,
22+
Optional: true,
23+
},
24+
"images": {
25+
Type: schema.TypeList,
26+
Computed: true,
27+
Elem: &schema.Resource{
28+
Schema: map[string]*schema.Schema{
29+
"name": {
30+
Type: schema.TypeString,
31+
Computed: true,
32+
},
33+
"family": {
34+
Type: schema.TypeString,
35+
Computed: true,
36+
},
37+
"self_link": {
38+
Type: schema.TypeString,
39+
Computed: true,
40+
},
41+
"archive_size_bytes": {
42+
Type: schema.TypeInt,
43+
Computed: true,
44+
},
45+
"creation_timestamp": {
46+
Type: schema.TypeString,
47+
Computed: true,
48+
},
49+
"description": {
50+
Type: schema.TypeString,
51+
Computed: true,
52+
},
53+
"disk_size_gb": {
54+
Type: schema.TypeInt,
55+
Computed: true,
56+
},
57+
"image_id": {
58+
Type: schema.TypeInt,
59+
Computed: true,
60+
},
61+
"labels": {
62+
Type: schema.TypeMap,
63+
Elem: &schema.Schema{
64+
Type: schema.TypeString,
65+
},
66+
Computed: true,
67+
},
68+
"source_disk": {
69+
Type: schema.TypeString,
70+
Computed: true,
71+
},
72+
"source_disk_id": {
73+
Type: schema.TypeString,
74+
Computed: true,
75+
},
76+
"source_image_id": {
77+
Type: schema.TypeString,
78+
Computed: true,
79+
},
80+
},
81+
},
82+
},
83+
},
84+
}
85+
}
86+
87+
func dataSourceGoogleComputeImagesRead(d *schema.ResourceData, meta interface{}) error {
88+
config := meta.(*transport_tpg.Config)
89+
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
90+
if err != nil {
91+
return err
92+
}
93+
94+
project, err := tpgresource.GetProject(d, config)
95+
if err != nil {
96+
return fmt.Errorf("Error fetching project for image: %s", err)
97+
}
98+
99+
filter := d.Get("filter").(string)
100+
101+
images := make([]map[string]interface{}, 0)
102+
103+
imageList, err := config.NewComputeClient(userAgent).Images.List(project).Filter(filter).Do()
104+
if err != nil {
105+
return transport_tpg.HandleDataSourceNotFoundError(err, d, fmt.Sprintf("Images : %s", project), fmt.Sprintf("Images : %s", project))
106+
}
107+
108+
for _, image := range imageList.Items {
109+
images = append(images, map[string]interface{}{
110+
"name": image.Name,
111+
"family": image.Family,
112+
"self_link": image.SelfLink,
113+
"archive_size_bytes": image.ArchiveSizeBytes,
114+
"creation_timestamp": image.CreationTimestamp,
115+
"description": image.Description,
116+
"disk_size_gb": image.DiskSizeGb,
117+
"image_id": image.Id,
118+
"labels": image.Labels,
119+
"source_disk": image.SourceDisk,
120+
"source_disk_id": image.SourceDiskId,
121+
"source_image_id": image.SourceImageId,
122+
})
123+
}
124+
125+
if err := d.Set("images", images); err != nil {
126+
return fmt.Errorf("Error retrieving images: %s", err)
127+
}
128+
129+
d.SetId(fmt.Sprintf(
130+
"projects/%s/global/images",
131+
project,
132+
))
133+
134+
return nil
135+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
package compute_test
4+
5+
import (
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
9+
"github.com/hashicorp/terraform-provider-google/google/acctest"
10+
)
11+
12+
func TestAccDataSourceComputeImages_basic(t *testing.T) {
13+
t.Parallel()
14+
15+
context := map[string]interface{}{
16+
"random_suffix": acctest.RandString(t, 10),
17+
"image": "debian-cloud/debian-11",
18+
}
19+
20+
acctest.VcrTest(t, resource.TestCase{
21+
PreCheck: func() { acctest.AccTestPreCheck(t) },
22+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
23+
Steps: []resource.TestStep{
24+
{
25+
Config: testAccCheckGoogleComputeImagesConfig(context),
26+
Check: resource.ComposeTestCheckFunc(
27+
// Test schema
28+
resource.TestCheckResourceAttrSet("data.google_compute_images.all", "images.0.name"),
29+
resource.TestCheckResourceAttrSet("data.google_compute_images.all", "images.1.name"),
30+
resource.TestCheckResourceAttrSet("data.google_compute_images.all", "images.0.self_link"),
31+
resource.TestCheckResourceAttrSet("data.google_compute_images.all", "images.1.self_link"),
32+
resource.TestCheckResourceAttrSet("data.google_compute_images.all", "images.0.image_id"),
33+
resource.TestCheckResourceAttrSet("data.google_compute_images.all", "images.1.image_id"),
34+
),
35+
},
36+
},
37+
})
38+
}
39+
40+
func testAccCheckGoogleComputeImagesConfig(context map[string]interface{}) string {
41+
return acctest.Nprintf(`
42+
resource "google_compute_disk" "test-disk" {
43+
name = "tf-test-disk-%{random_suffix}"
44+
type = "pd-standard"
45+
image = "%{image}"
46+
size = 10
47+
}
48+
49+
resource "google_compute_image" "foo" {
50+
name = "tf-test-image1-%{random_suffix}"
51+
source_disk = google_compute_disk.test-disk.self_link
52+
}
53+
54+
resource "google_compute_image" "bar" {
55+
name = "tf-test-image2-%{random_suffix}"
56+
source_image = google_compute_image.foo.self_link
57+
}
58+
59+
data "google_compute_images" "all" {
60+
}
61+
`, context)
62+
}
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)