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

Data Sources should error when not found #7024

Merged
merged 13 commits into from May 20, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -2,7 +2,6 @@ package apimanagement

import (
"fmt"
"log"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -62,9 +61,7 @@ func dataSourceApiManagementGroupRead(d *schema.ResourceData, meta interface{})
resp, err := client.Get(ctx, resourceGroup, serviceName, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] Group %q (Resource Group %q / API Management Service %q) was not found - removing from state!", name, resourceGroup, serviceName)
d.SetId("")
return nil
return fmt.Errorf("Group %q (Resource Group %q / API Management Service %q) was not found", name, resourceGroup, serviceName)
}

return fmt.Errorf("making Read request for Group %q (Resource Group %q / API Management Service %q): %+v", name, resourceGroup, serviceName, err)
Expand Down
11 changes: 5 additions & 6 deletions azurerm/internal/services/compute/image_data_source.go
Expand Up @@ -152,7 +152,7 @@ func dataSourceArmImageRead(d *schema.ResourceData, meta interface{}) error {
resp, err := client.ListByResourceGroupComplete(ctx, resGroup)
if err != nil {
if utils.ResponseWasNotFound(resp.Response().Response) {
return fmt.Errorf("Error: Unable to list images for Resource Group %q", resGroup)
return fmt.Errorf("No Images were found for Resource Group %q", resGroup)
}
return fmt.Errorf("[ERROR] Error getting list of images (resource group %q): %+v", resGroup, err)
}
Expand All @@ -162,21 +162,20 @@ func dataSourceArmImageRead(d *schema.ResourceData, meta interface{}) error {
if r.Match(([]byte)(*img.Name)) {
list = append(list, img)
}
err = resp.Next()
err = resp.NextWithContext(ctx)

if err != nil {
return err
}
}

if len(list) < 1 {
d.SetId("")
return nil
if 1 > len(list) {
return fmt.Errorf("No Images were found for Resource Group %q", resGroup)
}

if len(list) > 1 {
desc := d.Get("sort_descending").(bool)
log.Printf("[DEBUG] arm_image - multiple results found and `sort_descending` is set to: %t", desc)
log.Printf("[DEBUG] Image - multiple results found and `sort_descending` is set to: %t", desc)

sort.Slice(list, func(i, j int) bool {
return (!desc && *list[i].Name < *list[j].Name) ||
Expand Down
Expand Up @@ -3,7 +3,6 @@ package compute
import (
"context"
"fmt"
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01/compute"
Expand Down Expand Up @@ -95,17 +94,11 @@ func dataSourceArmSharedImageVersionRead(d *schema.ResourceData, meta interface{
galleryName := d.Get("gallery_name").(string)
resourceGroup := d.Get("resource_group_name").(string)

image, found, err := obtainImage(client, ctx, resourceGroup, galleryName, imageName, imageVersion)
image, err := obtainImage(client, ctx, resourceGroup, galleryName, imageName, imageVersion)
if err != nil {
d.SetId("")
return err
}

if !found {
d.SetId("")
return nil
}

d.SetId(*image.ID)
d.Set("name", image.Name)
d.Set("image_name", imageName)
Expand Down Expand Up @@ -136,60 +129,64 @@ func dataSourceArmSharedImageVersionRead(d *schema.ResourceData, meta interface{
return tags.FlattenAndSet(d, image.Tags)
}

func obtainImage(client *compute.GalleryImageVersionsClient, ctx context.Context, resourceGroup string, galleryName string, galleryImageName string, galleryImageVersionName string) (compute.GalleryImageVersion, bool, error) {
var (
image compute.GalleryImageVersion
err error
found bool
)
func obtainImage(client *compute.GalleryImageVersionsClient, ctx context.Context, resourceGroup string, galleryName string, galleryImageName string, galleryImageVersionName string) (*compute.GalleryImageVersion, error) {
notFoundError := fmt.Errorf("A Version was not found for Shared Image %q / Gallery %q / Resource Group %q", galleryImageName, galleryName, resourceGroup)

switch galleryImageVersionName {
case "latest":
images, err := client.ListByGalleryImage(ctx, resourceGroup, galleryName, galleryImageName)
if err != nil {
if utils.ResponseWasNotFound(images.Response().Response) {
log.Printf("[DEBUG] Shared Image Versions (Image %q / Gallery %q / Resource Group %q) was not found - removing from state", galleryImageName, galleryName, resourceGroup)
return image, false, nil
return nil, notFoundError
}
return image, false, fmt.Errorf("retrieving Shared Image Versions (Image %q / Gallery %q / Resource Group %q): %+v", galleryImageName, galleryName, resourceGroup, err)
return nil, fmt.Errorf("retrieving Shared Image Versions (Image %q / Gallery %q / Resource Group %q): %+v", galleryImageName, galleryName, resourceGroup, err)
}

// the last image in the list is the latest version
if len(images.Values()) > 0 {
image = images.Values()[len(images.Values())-1]
found = true
image := images.Values()[len(images.Values())-1]
return &image, nil
}

return nil, notFoundError

case "recent":
images, err := client.ListByGalleryImage(ctx, resourceGroup, galleryName, galleryImageName)
if err != nil {
if utils.ResponseWasNotFound(images.Response().Response) {
log.Printf("[DEBUG] Shared Image Versions (Image %q / Gallery %q / Resource Group %q) was not found - removing from state", galleryImageName, galleryName, resourceGroup)
return image, false, nil
return nil, notFoundError
}
return image, false, fmt.Errorf("retrieving Shared Image Versions (Image %q / Gallery %q / Resource Group %q): %+v", galleryImageName, galleryName, resourceGroup, err)
return nil, fmt.Errorf("retrieving Shared Image Versions (Image %q / Gallery %q / Resource Group %q): %+v", galleryImageName, galleryName, resourceGroup, err)
}
var recentDate time.Time
var image *compute.GalleryImageVersion
var recentDate *time.Time
// compare dates until we find the image that was updated most recently
for _, currImage := range images.Values() {
if profile := currImage.PublishingProfile; profile != nil {
if profile.PublishedDate != nil && profile.PublishedDate.Time.After(recentDate) {
recentDate = profile.PublishedDate.Time
image = currImage
found = true
if profile.PublishedDate != nil && (recentDate == nil || profile.PublishedDate.Time.After(*recentDate)) {
recentDate = &profile.PublishedDate.Time
image = &currImage
}
}
}

if image != nil {
return image, nil
}

return nil, notFoundError

default:
image, err = client.Get(ctx, resourceGroup, galleryName, galleryImageName, galleryImageVersionName, compute.ReplicationStatusTypesReplicationStatus)
image, err := client.Get(ctx, resourceGroup, galleryName, galleryImageName, galleryImageVersionName, compute.ReplicationStatusTypesReplicationStatus)
if err != nil {
if utils.ResponseWasNotFound(image.Response) {
log.Printf("[DEBUG] Shared Image Version %q (Image %q / Gallery %q / Resource Group %q) was not found - removing from state", galleryImageVersionName, galleryImageName, galleryName, resourceGroup)
return image, false, nil
return nil, notFoundError
}
return image, false, fmt.Errorf("Error retrieving Shared Image Version %q (Image %q / Gallery %q / Resource Group %q): %+v", galleryImageVersionName, galleryImageName, galleryName, resourceGroup, err)
return nil, fmt.Errorf("Error retrieving Shared Image Version %q (Image %q / Gallery %q / Resource Group %q): %+v", galleryImageVersionName, galleryImageName, galleryName, resourceGroup, err)
}
found = true
}

return image, found, nil
return &image, nil
}
}

func flattenSharedImageVersionDataSourceTargetRegions(input *[]compute.TargetRegion) []interface{} {
Expand Down
Expand Up @@ -2,7 +2,6 @@ package compute

import (
"fmt"
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01/compute"
Expand Down Expand Up @@ -106,9 +105,7 @@ func dataSourceArmSharedImageVersionsRead(d *schema.ResourceData, meta interface
resp, err := client.ListByGalleryImage(ctx, resourceGroup, galleryName, imageName)
if err != nil {
if utils.ResponseWasNotFound(resp.Response().Response) {
log.Printf("[DEBUG] Shared Image Versions (Image %q / Gallery %q / Resource Group %q) was not found - removing from state", imageName, galleryName, resourceGroup)
d.SetId("")
return nil
return fmt.Errorf("Shared Image Versions (Image %q / Gallery %q / Resource Group %q) was not found", imageName, galleryName, resourceGroup)
}
return fmt.Errorf("retrieving Shared Image Versions (Image %q / Gallery %q / Resource Group %q): %+v", imageName, galleryName, resourceGroup, err)
}
Expand Down
Expand Up @@ -2,7 +2,6 @@ package datalake

import (
"fmt"
"log"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -72,11 +71,9 @@ func dataSourceArmDateLakeStoreAccountRead(d *schema.ResourceData, meta interfac
resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[WARN] DataLakeStoreAccount %q was not found (Resource Group %q)", name, resourceGroup)
d.SetId("")
return nil
return fmt.Errorf("Data Lake Store Account %q was not found in Resource Group %q", name, resourceGroup)
}
return fmt.Errorf("Error making Read request on Azure Data Lake %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("retrieving Data Lake Store Account %q (Resource Group %q): %+v", name, resourceGroup, err)
}

d.SetId(*resp.ID)
Expand Down
Expand Up @@ -2,7 +2,6 @@ package datashare

import (
"fmt"
"log"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -68,9 +67,7 @@ func dataSourceArmDataShareAccountRead(d *schema.ResourceData, meta interface{})
resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[INFO] DataShare %q does not exist - removing from state", d.Id())
d.SetId("")
return nil
return fmt.Errorf("DataShare Account %q does not exist in Resource Group %q", name, resourceGroup)
}
return fmt.Errorf("retrieving DataShare Account %q (Resource Group %q): %+v", name, resourceGroup, err)
}
Expand Down
Expand Up @@ -2,7 +2,6 @@ package hdinsight

import (
"fmt"
"log"
"strings"
"time"

Expand Down Expand Up @@ -111,9 +110,7 @@ func dataSourceArmHDInsightClusterRead(d *schema.ResourceData, meta interface{})
resp, err := clustersClient.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] HDInsight Cluster %q was not found in Resource Group %q - removing from state!", name, resourceGroup)
d.SetId("")
return nil
return fmt.Errorf("HDInsight Cluster %q was not found in Resource Group %q", name, resourceGroup)
}

return fmt.Errorf("Error retrieving HDInsight Cluster %q (Resource Group %q): %+v", name, resourceGroup, err)
Expand Down
Expand Up @@ -2,7 +2,6 @@ package healthcare

import (
"fmt"
"log"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -126,8 +125,6 @@ func dataSourceArmHealthcareServiceRead(d *schema.ResourceData, meta interface{}
resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[WARN] Healthcare Service %q was not found (Resource Group %q)", name, resourceGroup)
d.SetId("")
return fmt.Errorf("HealthCare Service %q was not found in Resource Group %q", name, resourceGroup)
}
return fmt.Errorf("Error making Read request on Azure Healthcare Service %q (Resource Group %q): %+v", name, resourceGroup, err)
Expand All @@ -138,25 +135,23 @@ func dataSourceArmHealthcareServiceRead(d *schema.ResourceData, meta interface{}
d.Set("kind", kind)
}

if properties := resp.Properties; properties != nil {
if accessPolicies := properties.AccessPolicies; accessPolicies != nil {
d.Set("access_policy_object_ids", flattenHealthcareAccessPolicies(accessPolicies))
if props := resp.Properties; props != nil {
if err := d.Set("access_policy_object_ids", flattenHealthcareAccessPolicies(props.AccessPolicies)); err != nil {
return fmt.Errorf("Error setting `access_policy_object_ids`: %+v", err)
}

if config := properties.CosmosDbConfiguration; config != nil {
d.Set("cosmosdb_throughput", config.OfferThroughput)
cosmosThroughput := 0
if props.CosmosDbConfiguration != nil && props.CosmosDbConfiguration.OfferThroughput != nil {
cosmosThroughput = int(*props.CosmosDbConfiguration.OfferThroughput)
}
d.Set("cosmosdb_throughput", cosmosThroughput)

if authConfig := properties.AuthenticationConfiguration; authConfig != nil {
if err := d.Set("authentication_configuration", flattenHealthcareAuthConfig(authConfig)); err != nil {
return fmt.Errorf("Error setting `authentication_configuration`: %+v", flattenHealthcareAuthConfig(authConfig))
}
if err := d.Set("authentication_configuration", flattenHealthcareAuthConfig(props.AuthenticationConfiguration)); err != nil {
return fmt.Errorf("Error setting `authentication_configuration`: %+v", err)
}

if corsConfig := properties.CorsConfiguration; corsConfig != nil {
if err := d.Set("cors_configuration", flattenHealthcareCorsConfig(corsConfig)); err != nil {
return fmt.Errorf("Error setting `cors_configuration`: %+v", flattenHealthcareCorsConfig(corsConfig))
}
if err := d.Set("cors_configuration", flattenHealthcareCorsConfig(props.CorsConfiguration)); err != nil {
return fmt.Errorf("Error setting `cors_configuration`: %+v", err)
}
}

Expand Down