diff --git a/.changelog/17880.txt b/.changelog/17880.txt new file mode 100644 index 00000000000..76a882ce3ba --- /dev/null +++ b/.changelog/17880.txt @@ -0,0 +1,3 @@ +```release-note:bug +resource/aws_storagegateway_upload_buffer: Replace `Provider produced inconsistent result after apply` with actual error message +``` diff --git a/aws/internal/service/storagegateway/finder/finder.go b/aws/internal/service/storagegateway/finder/finder.go new file mode 100644 index 00000000000..986d73fd63b --- /dev/null +++ b/aws/internal/service/storagegateway/finder/finder.go @@ -0,0 +1,33 @@ +package finder + +import ( + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/storagegateway" +) + +func UploadBufferDisk(conn *storagegateway.StorageGateway, gatewayARN string, diskID string) (*string, error) { + input := &storagegateway.DescribeUploadBufferInput{ + GatewayARN: aws.String(gatewayARN), + } + + var result *string + + output, err := conn.DescribeUploadBuffer(input) + + if err != nil { + return nil, err + } + + if output == nil { + return nil, nil + } + + for _, diskId := range output.DiskIds { + if aws.StringValue(diskId) == diskID { + result = diskId + break + } + } + + return result, err +} diff --git a/aws/resource_aws_storagegateway_upload_buffer.go b/aws/resource_aws_storagegateway_upload_buffer.go index 2dc26eec224..a07ef527efa 100644 --- a/aws/resource_aws_storagegateway_upload_buffer.go +++ b/aws/resource_aws_storagegateway_upload_buffer.go @@ -9,6 +9,7 @@ import ( "github.com/aws/aws-sdk-go/aws/arn" "github.com/aws/aws-sdk-go/service/storagegateway" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/storagegateway/finder" ) func resourceAwsStorageGatewayUploadBuffer() *schema.Resource { @@ -66,42 +67,29 @@ func resourceAwsStorageGatewayUploadBufferRead(d *schema.ResourceData, meta inte return err } - input := &storagegateway.DescribeUploadBufferInput{ - GatewayARN: aws.String(gatewayARN), - } - - log.Printf("[DEBUG] Reading Storage Gateway upload buffer: %s", input) - output, err := conn.DescribeUploadBuffer(input) - if err != nil { - if isAWSErrStorageGatewayGatewayNotFound(err) { - log.Printf("[WARN] Storage Gateway upload buffer %q not found - removing from state", d.Id()) - d.SetId("") - return nil - } - return fmt.Errorf("error reading Storage Gateway upload buffer: %s", err) - } + foundDiskID, err := finder.UploadBufferDisk(conn, gatewayARN, diskID) - if output == nil || len(output.DiskIds) == 0 { - log.Printf("[WARN] Storage Gateway upload buffer %q not found - removing from state", d.Id()) + if !d.IsNewResource() && isAWSErrStorageGatewayGatewayNotFound(err) { + log.Printf("[WARN] Storage Gateway Upload Buffer (%s) not found, removing from state", d.Id()) d.SetId("") return nil } - found := false - for _, existingDiskID := range output.DiskIds { - if aws.StringValue(existingDiskID) == diskID { - found = true - break - } + if err != nil { + return fmt.Errorf("error reading Storage Gateway Upload Buffer (%s): %w", d.Id(), err) } - if !found { - log.Printf("[WARN] Storage Gateway upload buffer %q not found - removing from state", d.Id()) + if foundDiskID == nil { + if d.IsNewResource() { + return fmt.Errorf("error reading Storage Gateway Upload Buffer (%s): not found", d.Id()) + } + + log.Printf("[WARN] Storage Gateway Upload Buffer (%s) not found, removing from state", d.Id()) d.SetId("") return nil } - d.Set("disk_id", diskID) + d.Set("disk_id", foundDiskID) d.Set("gateway_arn", gatewayARN) return nil diff --git a/aws/resource_aws_storagegateway_upload_buffer_test.go b/aws/resource_aws_storagegateway_upload_buffer_test.go index e2c4adc3b78..2a65867545a 100644 --- a/aws/resource_aws_storagegateway_upload_buffer_test.go +++ b/aws/resource_aws_storagegateway_upload_buffer_test.go @@ -4,11 +4,10 @@ import ( "fmt" "testing" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/storagegateway" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/storagegateway/finder" ) func TestDecodeStorageGatewayUploadBufferID(t *testing.T) { @@ -111,27 +110,17 @@ func testAccCheckAWSStorageGatewayUploadBufferExists(resourceName string) resour return err } - input := &storagegateway.DescribeUploadBufferInput{ - GatewayARN: aws.String(gatewayARN), - } - - output, err := conn.DescribeUploadBuffer(input) + foundDiskID, err := finder.UploadBufferDisk(conn, gatewayARN, diskID) if err != nil { - return fmt.Errorf("error reading Storage Gateway upload buffer: %s", err) - } - - if output == nil || len(output.DiskIds) == 0 { - return fmt.Errorf("Storage Gateway upload buffer %q not found", rs.Primary.ID) + return fmt.Errorf("error reading Storage Gateway Upload Buffer (%s): %w", rs.Primary.ID, err) } - for _, existingDiskID := range output.DiskIds { - if aws.StringValue(existingDiskID) == diskID { - return nil - } + if foundDiskID == nil { + return fmt.Errorf("Storage Gateway Upload Buffer (%s) not found", rs.Primary.ID) } - return fmt.Errorf("Storage Gateway upload buffer %q not found", rs.Primary.ID) + return nil } }