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

bugfix: data sources should surface an error rather than using d.SetID("") #20479

Merged
merged 8 commits into from
Feb 15, 2023
8 changes: 4 additions & 4 deletions internal/services/aadb2c/aadb2c_directory_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package aadb2c
import (
"context"
"fmt"
"net/http"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"

"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-sdk/resource-manager/aadb2c/2021-04-01-preview/tenants"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
Expand Down Expand Up @@ -104,11 +105,10 @@ func (r AadB2cDirectoryDataSource) Read() sdk.ResourceFunc {

id := tenants.NewB2CDirectoryID(subscriptionId, state.ResourceGroup, state.DomainName)

metadata.Logger.Infof("Reading %s", id)
resp, err := client.Get(ctx, id)
if err != nil {
if resp.HttpResponse.StatusCode == http.StatusNotFound {
return metadata.MarkAsGone(id)
if response.WasNotFound(resp.HttpResponse) {
return fmt.Errorf("%s was not found", id)
}
return fmt.Errorf("retrieving %s: %v", id, err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,21 +220,19 @@ func resourceArmConsumptionBudgetResourceGroupDataSource() *pluginsdk.Resource {

func resourceArmConsumptionBudgetResourceGroupDataSourceRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Consumption.BudgetsClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id := budgets.NewScopedBudgetID(d.Get("resource_group_id").(string), d.Get("name").(string))
d.SetId(id.ID())

resp, err := client.Get(ctx, id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
d.SetId("")
return nil
return fmt.Errorf("%s was not found", id)
}
return fmt.Errorf("making read request on %s: %+v", id, err)
}

d.SetId(id.ID())
d.Set("name", id.BudgetName)
if model := resp.Model; model != nil {
if props := model.Properties; props != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package databoxedge
import (
"context"
"fmt"
"log"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
Expand Down Expand Up @@ -138,8 +137,7 @@ func (d EdgeDeviceDataSource) Read() sdk.ResourceFunc {
resp, err := client.Get(ctx, id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
log.Printf("[INFO] %s was not found - removing from state", id)
return metadata.MarkAsGone(id)
return fmt.Errorf("%s was not found", id)
}
return fmt.Errorf("retrieving %s: %+v", id, err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dataprotection

import (
"fmt"
"log"
"regexp"
"time"

Expand Down Expand Up @@ -76,9 +75,7 @@ func dataSourceDataProtectionBackupVaultRead(d *pluginsdk.ResourceData, meta int
resp, err := client.Get(ctx, id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
log.Printf("[INFO] DataProtection BackupVault %q does not exist - removing from state", d.Id())
d.SetId("")
return nil
return fmt.Errorf("%s was not found", id)
}
return fmt.Errorf("retrieving DataProtection BackupVault (%q): %+v", id, err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ func dataSourceHealthcareApisDicomServiceRead(d *pluginsdk.ResourceData, meta in
resp, err := client.Get(ctx, id.ResourceGroup, id.WorkspaceName, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
return fmt.Errorf("%s was not found", id)
}
return fmt.Errorf("retrieving %s: %+v", id, err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ func dataSourceHealthcareApisFhirServiceRead(d *pluginsdk.ResourceData, meta int
resp, err := client.Get(ctx, id.ResourceGroup, id.WorkspaceName, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
return fmt.Errorf("%s was not found", id)
}
return fmt.Errorf("retrieving %s: %+v", id, err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ func dataSourceHealthcareIotConnectorRead(d *pluginsdk.ResourceData, meta interf
resp, err := client.Get(ctx, id.ResourceGroup, id.WorkspaceName, id.IotConnectorName)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
return fmt.Errorf("%s was not found", id)
}
return fmt.Errorf("retrieving %s: %+v", id, err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ func (r HybridComputeMachineDataSource) Read() sdk.ResourceFunc {
resp, err := client.Get(ctx, id, machines.GetOperationOptions{})
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
return metadata.MarkAsGone(id)
return fmt.Errorf("%s was not found", id)
}

return fmt.Errorf("retrieving %s: %+v", id, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ func dataSourceArmKeyVaultCertificateDataRead(d *pluginsdk.ResourceData, meta in
cert, err := client.GetCertificate(ctx, *keyVaultBaseUri, name, version)
if err != nil {
if utils.ResponseWasNotFound(cert.Response) {
log.Printf("[DEBUG] Certificate %q was not found in Key Vault at URI %q - removing from state", name, *keyVaultBaseUri)
d.SetId("")
return nil
return fmt.Errorf("the Certificate %q was not found in Key Vault at URI %q", name, *keyVaultBaseUri)
}

return fmt.Errorf("reading Key Vault Certificate: %+v", err)
Expand All @@ -121,12 +119,11 @@ func dataSourceArmKeyVaultCertificateDataRead(d *pluginsdk.ResourceData, meta in
return fmt.Errorf("failure reading Key Vault Certificate ID for %q", name)
}

d.SetId(*cert.ID)

id, err := parse.ParseNestedItemID(d.Id())
id, err := parse.ParseNestedItemID(*cert.ID)
if err != nil {
return err
}
d.SetId(id.ID())

d.Set("name", id.Name)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/base64"
"encoding/hex"
"fmt"
"log"
"strings"
"time"

Expand Down Expand Up @@ -272,9 +271,7 @@ func dataSourceKeyVaultCertificateRead(d *pluginsdk.ResourceData, meta interface
cert, err := client.GetCertificate(ctx, *keyVaultBaseUri, name, version)
if err != nil {
if utils.ResponseWasNotFound(cert.Response) {
log.Printf("[DEBUG] Certificate %q was not found in Key Vault at URI %q - removing from state", name, *keyVaultBaseUri)
d.SetId("")
return nil
return fmt.Errorf("a Certificate named %q was not found in Key Vault at URI %q", name, *keyVaultBaseUri)
}

return fmt.Errorf("reading Key Vault Certificate: %+v", err)
Expand All @@ -284,12 +281,11 @@ func dataSourceKeyVaultCertificateRead(d *pluginsdk.ResourceData, meta interface
return fmt.Errorf("failure reading Key Vault Certificate ID for %q", name)
}

d.SetId(*cert.ID)

id, err := parse.ParseNestedItemID(*cert.ID)
if err != nil {
return err
}
d.SetId(id.ID())

d.Set("name", id.Name)

Expand Down
9 changes: 2 additions & 7 deletions internal/services/loadbalancer/outbound_rule_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package loadbalancer

import (
"fmt"
"log"
"time"

"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down Expand Up @@ -95,18 +94,14 @@ func dataSourceArmLoadBalancerOutboundRuleRead(d *pluginsdk.ResourceData, meta i
loadBalancer, err := client.Get(ctx, loadBalancerId.ResourceGroup, loadBalancerId.Name, "")
if err != nil {
if utils.ResponseWasNotFound(loadBalancer.Response) {
d.SetId("")
log.Printf("[INFO] Load Balancer %q not found. Removing from state", loadBalancerId.Name)
return nil
return fmt.Errorf("parent %s was not found", *loadBalancerId)
}
return fmt.Errorf("failed to retrieve Load Balancer %q (resource group %q) for Outbound Rule %q: %+v", loadBalancerId.Name, loadBalancerId.ResourceGroup, name, err)
return fmt.Errorf("retrieving parent %s: %+v", *loadBalancerId, err)
}

id := parse.NewLoadBalancerOutboundRuleID(loadBalancerId.SubscriptionId, loadBalancerId.ResourceGroup, loadBalancerId.Name, name)
config, _, exists := FindLoadBalancerOutboundRuleByName(&loadBalancer, id.OutboundRuleName)
if !exists {
d.SetId("")
log.Printf("[INFO] Load Balancer Outbound Rule %q not found. Removing from state", name)
return fmt.Errorf("%s was not found", id)
}

Expand Down
Loading