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)
  • Loading branch information
karolgorc authored Dec 19, 2024
1 parent f1195ac commit 55ec241
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,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 @@ -104,7 +109,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 @@ -24,6 +24,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 @@ -44,6 +50,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 @@ -118,6 +130,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 55ec241

Please sign in to comment.