Skip to content

Commit

Permalink
existing Data Sources now return errors on 404s (#8858)
Browse files Browse the repository at this point in the history
  • Loading branch information
NickElliot authored Sep 11, 2023
1 parent 186acac commit df9725a
Show file tree
Hide file tree
Showing 86 changed files with 618 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func dataSourceAccessApprovalFolderServiceAccountRead(d *schema.ResourceData, me
UserAgent: userAgent,
})
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("AccessApprovalFolderServiceAccount %q", d.Id()))
return transport_tpg.HandleDataSourceNotFoundError(err, d, fmt.Sprintf("AccessApprovalFolderServiceAccount %q", d.Id()), url)
}

if err := d.Set("name", res["name"]); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func dataSourceAccessApprovalOrganizationServiceAccountRead(d *schema.ResourceDa
UserAgent: userAgent,
})
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("AccessApprovalOrganizationServiceAccount %q", d.Id()))
return transport_tpg.HandleDataSourceNotFoundError(err, d, fmt.Sprintf("AccessApprovalOrganizationServiceAccount %q", d.Id()), url)
}

if err := d.Set("name", res["name"]); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func dataSourceAccessApprovalProjectServiceAccountRead(d *schema.ResourceData, m
UserAgent: userAgent,
})
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("AccessApprovalProjectServiceAccount %q", d.Id()))
return transport_tpg.HandleDataSourceNotFoundError(err, d, fmt.Sprintf("AccessApprovalProjectServiceAccount %q", d.Id()), url)
}

if err := d.Set("name", res["name"]); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func dataSourceAlloydbLocationsRead(d *schema.ResourceData, meta interface{}) er
UserAgent: userAgent,
})
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("Locations %q", d.Id()))
return transport_tpg.HandleDataSourceNotFoundError(err, d, fmt.Sprintf("Locations %q", d.Id()), url)
}
var locations []map[string]interface{}
for {
Expand Down Expand Up @@ -142,7 +142,7 @@ func dataSourceAlloydbLocationsRead(d *schema.ResourceData, meta interface{}) er
UserAgent: userAgent,
})
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("Locations %q", d.Id()))
return transport_tpg.HandleDataSourceNotFoundError(err, d, fmt.Sprintf("Locations %q", d.Id()), url)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func dataSourceAlloydbSupportedDatabaseFlagsRead(d *schema.ResourceData, meta in
UserAgent: userAgent,
})
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("SupportedDatabaseFlags %q", d.Id()))
return transport_tpg.HandleDataSourceNotFoundError(err, d, fmt.Sprintf("SupportedDatabaseFlags %q", d.Id()), url)
}
var supportedDatabaseFlags []map[string]interface{}
for {
Expand Down Expand Up @@ -221,7 +221,7 @@ func dataSourceAlloydbSupportedDatabaseFlagsRead(d *schema.ResourceData, meta in
UserAgent: userAgent,
})
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("SupportedDatabaseFlags %q", d.Id()))
return transport_tpg.HandleDataSourceNotFoundError(err, d, fmt.Sprintf("SupportedDatabaseFlags %q", d.Id()), url)
}
}
if err := d.Set("supported_database_flags", supportedDatabaseFlags); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func dataSourceGoogleAppEngineDefaultServiceAccountRead(d *schema.ResourceData,

sa, err := config.NewIamClient(userAgent).Projects.ServiceAccounts.Get(serviceAccountName).Do()
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("Service Account %q", serviceAccountName))
return transport_tpg.HandleDataSourceNotFoundError(err, d, fmt.Sprintf("Service Account %q", serviceAccountName), serviceAccountName)
}

d.SetId(sa.Name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,17 @@ func dataSourceArtifactRegistryRepositoryRead(d *schema.ResourceData, meta inter
}

repository_id := d.Get("repository_id").(string)
d.SetId(fmt.Sprintf("projects/%s/locations/%s/repositories/%s", project, location, repository_id))
id := fmt.Sprintf("projects/%s/locations/%s/repositories/%s", project, location, repository_id)
d.SetId(id)

err = resourceArtifactRegistryRepositoryRead(d, meta)
if err != nil {
return err
}

if d.Id() == "" {
return fmt.Errorf("%s not found", id)
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,17 @@ func dataSourceGoogleBeyondcorpAppConnectionRead(d *schema.ResourceData, meta in
return err
}

d.SetId(fmt.Sprintf("projects/%s/locations/%s/appConnections/%s", project, region, name))
id := fmt.Sprintf("projects/%s/locations/%s/appConnections/%s", project, region, name)
d.SetId(id)

return resourceBeyondcorpAppConnectionRead(d, meta)
err = resourceBeyondcorpAppConnectionRead(d, meta)
if err != nil {
return err
}

if d.Id() == "" {
return fmt.Errorf("%s not found", id)
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,17 @@ func dataSourceGoogleBeyondcorpAppConnectorRead(d *schema.ResourceData, meta int
return err
}

d.SetId(fmt.Sprintf("projects/%s/locations/%s/appConnectors/%s", project, region, name))
id := fmt.Sprintf("projects/%s/locations/%s/appConnectors/%s", project, region, name)
d.SetId(id)

return resourceBeyondcorpAppConnectorRead(d, meta)
err = resourceBeyondcorpAppConnectorRead(d, meta)
if err != nil {
return err
}

if d.Id() == "" {
return fmt.Errorf("%s not found", id)
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,17 @@ func dataSourceGoogleBeyondcorpAppGatewayRead(d *schema.ResourceData, meta inter
return err
}

d.SetId(fmt.Sprintf("projects/%s/locations/%s/appGateways/%s", project, region, name))
id := fmt.Sprintf("projects/%s/locations/%s/appGateways/%s", project, region, name)
d.SetId(id)

return resourceBeyondcorpAppGatewayRead(d, meta)
err = resourceBeyondcorpAppGatewayRead(d, meta)
if err != nil {
return err
}

if d.Id() == "" {
return fmt.Errorf("%s not found", id)
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func dataSourceGoogleBigqueryDefaultServiceAccountRead(d *schema.ResourceData, m

projectResource, err := config.NewBigQueryClient(userAgent).Projects.GetServiceAccount(project).Do()
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, "BigQuery service account not found")
return transport_tpg.HandleDataSourceNotFoundError(err, d, fmt.Sprintf("Project %q BigQuery service account", project), fmt.Sprintf("Project %q BigQuery service account", project))
}

d.SetId(projectResource.Email)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func dataSourceBillingAccountRead(d *schema.ResourceData, meta interface{}) erro
if v, ok := d.GetOk("billing_account"); ok {
resp, err := config.NewBillingClient(userAgent).BillingAccounts.Get(CanonicalBillingAccountName(v.(string))).Do()
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("Billing Account Not Found : %s", v))
return transport_tpg.HandleDataSourceNotFoundError(err, d, fmt.Sprintf("Billing Account Not Found : %s", v), CanonicalBillingAccountName(v.(string)))
}

if openOk && resp.Open != open.(bool) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,16 @@ func dataSourceGoogleCloudBuildTriggerRead(d *schema.ResourceData, meta interfac
}

id = strings.ReplaceAll(id, "/locations/global/", "/")

d.SetId(id)
return resourceCloudBuildTriggerRead(d, meta)

err = resourceCloudBuildTriggerRead(d, meta)
if err != nil {
return err
}

if d.Id() == "" {
return fmt.Errorf("%s not found", id)
}

return nil
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cloudfunctions

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
Expand Down Expand Up @@ -48,5 +50,9 @@ func dataSourceGoogleCloudFunctionsFunctionRead(d *schema.ResourceData, meta int
return err
}

if d.Id() == "" {
return fmt.Errorf("%s not found", cloudFuncId.CloudFunctionId())
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,17 @@ func dataSourceGoogleCloudFunctions2FunctionRead(d *schema.ResourceData, meta in
return err
}

d.SetId(fmt.Sprintf("projects/%s/locations/%s/functions/%s", project, d.Get("location").(string), d.Get("name").(string)))
id := fmt.Sprintf("projects/%s/locations/%s/functions/%s", project, d.Get("location").(string), d.Get("name").(string))
d.SetId(id)

err = resourceCloudfunctions2functionRead(d, meta)
if err != nil {
return err
}

if d.Id() == "" {
return fmt.Errorf("%s not found", id)
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func dataSourceGoogleCloudIdentityGroupMembershipsRead(d *schema.ResourceData, m
return nil
})
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("CloudIdentityGroupMemberships %q", d.Id()))
return transport_tpg.HandleDataSourceNotFoundError(err, d, fmt.Sprintf("CloudIdentityGroupMemberships %q", d.Id()), "")
}

if err := d.Set("memberships", result); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func dataSourceGoogleCloudIdentityGroupsRead(d *schema.ResourceData, meta interf
return nil
})
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("CloudIdentityGroups %q", d.Id()))
return transport_tpg.HandleDataSourceNotFoundError(err, d, fmt.Sprintf("CloudIdentityGroups %q", d.Id()), "Groups")
}

if err := d.Set("groups", result); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,14 @@ func dataSourceGoogleCloudRunServiceRead(d *schema.ResourceData, meta interface{
return fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)
return resourceCloudRunServiceRead(d, meta)
err = resourceCloudRunServiceRead(d, meta)
if err != nil {
return err
}

if d.Id() == "" {
return fmt.Errorf("%s not found", id)
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,16 @@ func dataSourceGoogleComposerEnvironmentRead(d *schema.ResourceData, meta interf
}
envName := d.Get("name").(string)

d.SetId(fmt.Sprintf("projects/%s/locations/%s/environments/%s", project, region, envName))
id := fmt.Sprintf("projects/%s/locations/%s/environments/%s", project, region, envName)
d.SetId(id)
err = resourceComposerEnvironmentRead(d, meta)
if err != nil {
return err
}

if d.Id() == "" {
return fmt.Errorf("%s not found", id)
}

return resourceComposerEnvironmentRead(d, meta)
return nil
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package compute

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
Expand Down Expand Up @@ -29,5 +31,14 @@ func dataSourceGoogleComputeHealthCheckRead(d *schema.ResourceData, meta interfa
}
d.SetId(id)

return resourceComputeHealthCheckRead(d, meta)
err = resourceComputeHealthCheckRead(d, meta)
if err != nil {
return err
}

if d.Id() == "" {
return fmt.Errorf("%s not found", id)
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func DataSourceGoogleComputeNetworkEndpointGroup() *schema.Resource {

func dataSourceComputeNetworkEndpointGroupRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
id := ""
if name, ok := d.GetOk("name"); ok {
project, err := tpgresource.GetProject(d, config)
if err != nil {
Expand All @@ -36,7 +37,8 @@ func dataSourceComputeNetworkEndpointGroupRead(d *schema.ResourceData, meta inte
if err != nil {
return err
}
d.SetId(fmt.Sprintf("projects/%s/zones/%s/networkEndpointGroups/%s", project, zone, name.(string)))
id = fmt.Sprintf("projects/%s/zones/%s/networkEndpointGroups/%s", project, zone, name.(string))
d.SetId(id)
} else if selfLink, ok := d.GetOk("self_link"); ok {
parsed, err := tpgresource.ParseNetworkEndpointGroupFieldValue(selfLink.(string), d, config)
if err != nil {
Expand All @@ -51,10 +53,20 @@ func dataSourceComputeNetworkEndpointGroupRead(d *schema.ResourceData, meta inte
if err := d.Set("project", parsed.Project); err != nil {
return fmt.Errorf("Error setting project: %s", err)
}
d.SetId(fmt.Sprintf("projects/%s/zones/%s/networkEndpointGroups/%s", parsed.Project, parsed.Zone, parsed.Name))
id = fmt.Sprintf("projects/%s/zones/%s/networkEndpointGroups/%s", parsed.Project, parsed.Zone, parsed.Name)
d.SetId(id)
} else {
return errors.New("Must provide either `self_link` or `zone/name`")
}

return resourceComputeNetworkEndpointGroupRead(d, meta)
err := resourceComputeNetworkEndpointGroupRead(d, meta)
if err != nil {
return err
}

if d.Id() == "" {
return fmt.Errorf("%s not found", id)
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,17 @@ func dataSourceComputeNetworkPeeringRead(d *schema.ResourceData, meta interface{
if err != nil {
return err
}
d.SetId(fmt.Sprintf("%s/%s", networkFieldValue.Name, d.Get("name").(string)))
id := fmt.Sprintf("%s/%s", networkFieldValue.Name, d.Get("name").(string))
d.SetId(id)

return resourceComputeNetworkPeeringRead(d, meta)
err = resourceComputeNetworkPeeringRead(d, meta)
if err != nil {
return err
}

if d.Id() == "" {
return fmt.Errorf("%s not found", id)
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,11 @@ func dataSourceGoogleComputeAddressRead(d *schema.ResourceData, meta interface{}
}
name := d.Get("name").(string)

id := fmt.Sprintf("projects/%s/regions/%s/addresses/%s", project, region, name)

address, err := config.NewComputeClient(userAgent).Addresses.Get(project, region, name).Do()
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("Address Not Found : %s", name))
return transport_tpg.HandleDataSourceNotFoundError(err, d, fmt.Sprintf("Address Not Found : %s", name), id)
}

if err := d.Set("address", address.Address); err != nil {
Expand Down Expand Up @@ -147,7 +149,7 @@ func dataSourceGoogleComputeAddressRead(d *schema.ResourceData, meta interface{}
return fmt.Errorf("Error setting region: %s", err)
}

d.SetId(fmt.Sprintf("projects/%s/regions/%s/addresses/%s", project, region, name))
d.SetId(id)
return nil
}

Expand Down
Loading

0 comments on commit df9725a

Please sign in to comment.