diff --git a/.changelog/6786.txt b/.changelog/6786.txt new file mode 100644 index 00000000000..7bd72b4a78e --- /dev/null +++ b/.changelog/6786.txt @@ -0,0 +1,3 @@ +```release-note:bug +container: throw exception if the data source `google_container_cluster` does not exist +``` diff --git a/google/data_source_google_container_cluster.go b/google/data_source_google_container_cluster.go index bcac7e9c659..359cbefdd3f 100644 --- a/google/data_source_google_container_cluster.go +++ b/google/data_source_google_container_cluster.go @@ -1,6 +1,8 @@ package google import ( + "fmt" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) @@ -35,7 +37,17 @@ func datasourceContainerClusterRead(d *schema.ResourceData, meta interface{}) er return err } - d.SetId(containerClusterFullName(project, location, clusterName)) + id := containerClusterFullName(project, location, clusterName) + + d.SetId(id) + + if err := resourceContainerClusterRead(d, meta); err != nil { + return err + } + + if d.Id() == "" { + return fmt.Errorf("%s not found", id) + } - return resourceContainerClusterRead(d, meta) + return nil }