From f1edb396ca6e314bb4f974169fb3ceec58a15b49 Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Thu, 17 Nov 2022 23:23:49 +0000 Subject: [PATCH] throw error if datasource not found (#6786) * throw error if datasource not found * update case Co-authored-by: Edward Sun Signed-off-by: Modular Magician --- .changelog/6786.txt | 3 +++ google/data_source_google_container_cluster.go | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 .changelog/6786.txt 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 }