Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Image filters to google_compute_image. #4026

Merged
merged 9 commits into from
Oct 10, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,24 @@ func dataSourceGoogleComputeImage() *schema.Resource {

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
ConflictsWith: []string{"family"},
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
ExactlyOneOf: []string{"name", "family", "filter"},
},
"family": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
ConflictsWith: []string{"name"},
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
ExactlyOneOf: []string{"name", "family", "filter"},
},
"filter": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ExactlyOneOf: []string{"name", "family", "filter"},
},
"archive_size_bytes": {
Type: schema.TypeInt,
Expand Down Expand Up @@ -125,8 +131,21 @@ func dataSourceGoogleComputeImageRead(d *schema.ResourceData, meta interface{})
log.Printf("[DEBUG] Fetching latest non-deprecated image from family %s", v.(string))
image, err = config.NewComputeClient(userAgent).Images.GetFromFamily(project, v.(string)).Do()
log.Printf("[DEBUG] Fetched latest non-deprecated image from family %s", v.(string))
} else if v, ok := d.GetOk("filter"); ok {
images, err := config.NewComputeClient(userAgent).Images.List(project).Filter(v.(string)).Do()
if err != nil {
return fmt.Errorf("error retrieving list of images: %s", err)
}

if len(images.Items) == 1 {
for _, im := range images.Items {
image = im
}
} else {
return fmt.Errorf("your filter has returned more than one image or no image. Please refine your filter to return exactly one image")
}
} else {
return fmt.Errorf("one of name or family must be set")
return fmt.Errorf("one of name, family or filters must be set")
}

if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,38 @@ func TestAccDataSourceComputeImage(t *testing.T) {
})
}

func TestAccDataSourceComputeImageFilter(t *testing.T) {
t.Parallel()

family := fmt.Sprintf("tf-test-%d", randInt(t))
name := fmt.Sprintf("tf-test-%d", randInt(t))

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeImageDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccDataSourcePublicImageConfig,
Check: resource.ComposeTestCheckFunc(
testAccDataSourceCheckPublicImage(),
),
},
{
Config: testAccDataSourceCustomImageFilter(family, name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.google_compute_image.from_filter",
"name", name),
resource.TestCheckResourceAttr("data.google_compute_image.from_filter",
"family", family),
resource.TestCheckResourceAttrSet("data.google_compute_image.from_filter",
"self_link"),
),
},
},
})
}

func testAccDataSourceCheckPublicImage() resource.TestCheckFunc {
return func(s *terraform.State) error {
data_source_name := "data.google_compute_image.debian"
Expand Down Expand Up @@ -105,3 +137,27 @@ data "google_compute_image" "from_family" {
}
`, family, name, name)
}

func testAccDataSourceCustomImageFilter(family, name string) string {
return fmt.Sprintf(`
resource "google_compute_image" "image" {
family = "%s"
name = "%s"
source_disk = google_compute_disk.disk.self_link
labels = {
test = "%s"
}
}

resource "google_compute_disk" "disk" {
name = "%s-disk"
zone = "us-central1-b"
}

data "google_compute_image" "from_filter" {
project = google_compute_image.image.project
filter = "labels.test = %s"
}

`, family, name, name, name, name)
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ resource "google_compute_instance" "default" {

The following arguments are supported:

* `name` or `family` - (Required) The name of a specific image or a family.
Exactly one of `name` of `family` must be specified. If `name` is specified, it will fetch
the corresponding image. If `family` is specified, it will returns the latest image
that is part of an image family and is not deprecated.
* `name`, `family` or `filter` - (Required) The name of a specific image or a family.
Exactly one of `name`, `family` or `filter` must be specified. If `name` is specified, it will fetch
the corresponding image. If `family` is specified, it will return the latest image
that is part of an image family and is not deprecated. If you specify `filter`, your
filter must return exactly one image. Filter syntax can be found [here](https://cloud.google.com/compute/docs/reference/rest/v1/images/list) in the filter section.

- - -

Expand Down