Skip to content

Commit

Permalink
Make google_compute_instance_guest_attributes return empty results …
Browse files Browse the repository at this point in the history
…when queried for nonexistent values (#12487) (#20760)

[upstream:55ec2413bf9eee962665d452b46aa629e3252a49]

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Dec 19, 2024
1 parent f11ffee commit 5c2707b
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/12487.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
compute: `google_compute_instance_guest_attributes` will return empty arrays and lists when queried values don't exist instead of throwing an error
```
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,14 @@ func dataSourceGoogleComputeInstanceGuestAttributesRead(d *schema.ResourceData,
return err
}

//Check if instance exists
id := fmt.Sprintf("projects/%s/zones/%s/instances/%s", project, zone, name)
instanceGuestAttributes := &compute.GuestAttributes{}
_, err = config.NewComputeClient(userAgent).Instances.Get(project, zone, name).Do()
if err != nil {
return transport_tpg.HandleDataSourceNotFoundError(err, d, fmt.Sprintf("Instance %s", name), id)
}

instanceGuestAttributes := &compute.GuestAttributes{}
// You can either query based on variable_key, query_path or just get the first value
if d.Get("query_path").(string) != "" {
instanceGuestAttributes, err = config.NewComputeClient(userAgent).Instances.GetGuestAttributes(project, zone, name).QueryPath(d.Get("query_path").(string)).Do()
Expand All @@ -102,7 +107,7 @@ func dataSourceGoogleComputeInstanceGuestAttributesRead(d *schema.ResourceData,
instanceGuestAttributes, err = config.NewComputeClient(userAgent).Instances.GetGuestAttributes(project, zone, name).Do()
}
if err != nil {
return transport_tpg.HandleDataSourceNotFoundError(err, d, fmt.Sprintf("Instance's Guest Attributes %s", name), id)
return nil
}

// Set query results
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ func TestAccDataSourceComputeInstanceGuestAttributes_basic(t *testing.T) {
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckComputeInstanceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccDataSourceComputeInstanceGuestAttributesConfig_queryPath_empty(instanceName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckNoResourceAttr("data.google_compute_instance_guest_attributes.bar", "query_value"),
),
},
{
//need to create the guest_attributes metadata from startup script first
Config: testAccDataSourceComputeInstanceGuestAttributesInitialConfig(instanceName),
Expand All @@ -42,6 +48,12 @@ func TestAccDataSourceComputeInstanceGuestAttributes_basic(t *testing.T) {
resource.TestCheckResourceAttr("data.google_compute_instance_guest_attributes.bar", "query_value.1.value", "test2"),
),
},
{
Config: testAccDataSourceComputeInstanceGuestAttributesConfig_queryPath_nonExistent(instanceName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckNoResourceAttr("data.google_compute_instance_guest_attributes.bar", "query_value"),
),
},
},
})
}
Expand Down Expand Up @@ -116,6 +128,77 @@ data "google_compute_instance_guest_attributes" "bar" {
`, instanceName)
}

func testAccDataSourceComputeInstanceGuestAttributesConfig_queryPath_nonExistent(instanceName string) string {
return fmt.Sprintf(`
resource "google_compute_instance" "foo" {
name = "%s"
machine_type = "n1-standard-1"
zone = "us-central1-a"
boot_disk {
initialize_params {
image = "debian-8-jessie-v20160803"
}
}
network_interface {
network = "default"
access_config {
// Ephemeral IP
}
}
metadata = {
enable-guest-attributes = "TRUE"
}
metadata_startup_script = <<-EOF
curl -X PUT --data "test1" http://metadata.google.internal/computeMetadata/v1/instance/guest-attributes/testing/key1 -H "Metadata-Flavor: Google"
curl -X PUT --data "test2" http://metadata.google.internal/computeMetadata/v1/instance/guest-attributes/testing/key2 -H "Metadata-Flavor: Google"
EOF
}
data "google_compute_instance_guest_attributes" "bar" {
name = google_compute_instance.foo.name
zone = "us-central1-a"
query_path = "test/"
}
`, instanceName)
}

func testAccDataSourceComputeInstanceGuestAttributesConfig_queryPath_empty(instanceName string) string {
return fmt.Sprintf(`
resource "google_compute_instance" "foo" {
name = "%s"
machine_type = "n1-standard-1"
zone = "us-central1-a"
boot_disk {
initialize_params {
image = "debian-8-jessie-v20160803"
}
}
network_interface {
network = "default"
access_config {
// Ephemeral IP
}
}
metadata = {
enable-guest-attributes = "TRUE"
}
}
data "google_compute_instance_guest_attributes" "bar" {
name = google_compute_instance.foo.name
zone = "us-central1-a"
query_path = "test/"
}
`, instanceName)
}

func testAccDataSourceComputeInstanceGuestAttributesConfig_variableKey(instanceName string) string {
return fmt.Sprintf(`
resource "google_compute_instance" "foo" {
Expand Down

0 comments on commit 5c2707b

Please sign in to comment.