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

resource/aws_storagegateway_upload_buffer: Replace Provider produced inconsistent result after apply with actual error message #17880

Merged
merged 2 commits into from
Mar 5, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/17880.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_storagegateway_upload_buffer: Replace `Provider produced inconsistent result after apply` with actual error message
```
33 changes: 33 additions & 0 deletions aws/internal/service/storagegateway/finder/finder.go
Original file line number Diff line number Diff line change
@@ -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
}
38 changes: 13 additions & 25 deletions aws/resource_aws_storagegateway_upload_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
23 changes: 6 additions & 17 deletions aws/resource_aws_storagegateway_upload_buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
}

Expand Down