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

Make google_compute_instance_guest_attributes return empty results when queried for nonexistent values #20760

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading