Skip to content

Commit

Permalink
resource/aws_storagegateway_upload_buffer: Replace `Provider produced…
Browse files Browse the repository at this point in the history
… inconsistent result after apply` with actual error message

Reference: #16796
Reference: #17809

Output from acceptance testing:

```
--- PASS: TestAccAWSStorageGatewayUploadBuffer_basic (455.25s)
```
  • Loading branch information
bflad committed Mar 2, 2021
1 parent 1e40bc6 commit edb7dc5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 42 deletions.
3 changes: 3 additions & 0 deletions .changelog/pending.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

0 comments on commit edb7dc5

Please sign in to comment.