Skip to content

Commit 8ffcf0e

Browse files
authored
resource/aws_storagegateway_upload_buffer: Replace Provider produced inconsistent result after apply with actual error message (#17880)
Reference: #16796 Reference: #17809 Output from acceptance testing: ``` --- PASS: TestAccAWSStorageGatewayUploadBuffer_basic (455.25s) ```
1 parent 0c820b1 commit 8ffcf0e

4 files changed

Lines changed: 55 additions & 42 deletions

File tree

.changelog/17880.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
resource/aws_storagegateway_upload_buffer: Replace `Provider produced inconsistent result after apply` with actual error message
3+
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package finder
2+
3+
import (
4+
"github.com/aws/aws-sdk-go/aws"
5+
"github.com/aws/aws-sdk-go/service/storagegateway"
6+
)
7+
8+
func UploadBufferDisk(conn *storagegateway.StorageGateway, gatewayARN string, diskID string) (*string, error) {
9+
input := &storagegateway.DescribeUploadBufferInput{
10+
GatewayARN: aws.String(gatewayARN),
11+
}
12+
13+
var result *string
14+
15+
output, err := conn.DescribeUploadBuffer(input)
16+
17+
if err != nil {
18+
return nil, err
19+
}
20+
21+
if output == nil {
22+
return nil, nil
23+
}
24+
25+
for _, diskId := range output.DiskIds {
26+
if aws.StringValue(diskId) == diskID {
27+
result = diskId
28+
break
29+
}
30+
}
31+
32+
return result, err
33+
}

aws/resource_aws_storagegateway_upload_buffer.go

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/aws/aws-sdk-go/aws/arn"
1010
"github.com/aws/aws-sdk-go/service/storagegateway"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/storagegateway/finder"
1213
)
1314

1415
func resourceAwsStorageGatewayUploadBuffer() *schema.Resource {
@@ -66,42 +67,29 @@ func resourceAwsStorageGatewayUploadBufferRead(d *schema.ResourceData, meta inte
6667
return err
6768
}
6869

69-
input := &storagegateway.DescribeUploadBufferInput{
70-
GatewayARN: aws.String(gatewayARN),
71-
}
72-
73-
log.Printf("[DEBUG] Reading Storage Gateway upload buffer: %s", input)
74-
output, err := conn.DescribeUploadBuffer(input)
75-
if err != nil {
76-
if isAWSErrStorageGatewayGatewayNotFound(err) {
77-
log.Printf("[WARN] Storage Gateway upload buffer %q not found - removing from state", d.Id())
78-
d.SetId("")
79-
return nil
80-
}
81-
return fmt.Errorf("error reading Storage Gateway upload buffer: %s", err)
82-
}
70+
foundDiskID, err := finder.UploadBufferDisk(conn, gatewayARN, diskID)
8371

84-
if output == nil || len(output.DiskIds) == 0 {
85-
log.Printf("[WARN] Storage Gateway upload buffer %q not found - removing from state", d.Id())
72+
if !d.IsNewResource() && isAWSErrStorageGatewayGatewayNotFound(err) {
73+
log.Printf("[WARN] Storage Gateway Upload Buffer (%s) not found, removing from state", d.Id())
8674
d.SetId("")
8775
return nil
8876
}
8977

90-
found := false
91-
for _, existingDiskID := range output.DiskIds {
92-
if aws.StringValue(existingDiskID) == diskID {
93-
found = true
94-
break
95-
}
78+
if err != nil {
79+
return fmt.Errorf("error reading Storage Gateway Upload Buffer (%s): %w", d.Id(), err)
9680
}
9781

98-
if !found {
99-
log.Printf("[WARN] Storage Gateway upload buffer %q not found - removing from state", d.Id())
82+
if foundDiskID == nil {
83+
if d.IsNewResource() {
84+
return fmt.Errorf("error reading Storage Gateway Upload Buffer (%s): not found", d.Id())
85+
}
86+
87+
log.Printf("[WARN] Storage Gateway Upload Buffer (%s) not found, removing from state", d.Id())
10088
d.SetId("")
10189
return nil
10290
}
10391

104-
d.Set("disk_id", diskID)
92+
d.Set("disk_id", foundDiskID)
10593
d.Set("gateway_arn", gatewayARN)
10694

10795
return nil

aws/resource_aws_storagegateway_upload_buffer_test.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ import (
44
"fmt"
55
"testing"
66

7-
"github.com/aws/aws-sdk-go/aws"
8-
"github.com/aws/aws-sdk-go/service/storagegateway"
97
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
108
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
119
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
10+
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/storagegateway/finder"
1211
)
1312

1413
func TestDecodeStorageGatewayUploadBufferID(t *testing.T) {
@@ -111,27 +110,17 @@ func testAccCheckAWSStorageGatewayUploadBufferExists(resourceName string) resour
111110
return err
112111
}
113112

114-
input := &storagegateway.DescribeUploadBufferInput{
115-
GatewayARN: aws.String(gatewayARN),
116-
}
117-
118-
output, err := conn.DescribeUploadBuffer(input)
113+
foundDiskID, err := finder.UploadBufferDisk(conn, gatewayARN, diskID)
119114

120115
if err != nil {
121-
return fmt.Errorf("error reading Storage Gateway upload buffer: %s", err)
122-
}
123-
124-
if output == nil || len(output.DiskIds) == 0 {
125-
return fmt.Errorf("Storage Gateway upload buffer %q not found", rs.Primary.ID)
116+
return fmt.Errorf("error reading Storage Gateway Upload Buffer (%s): %w", rs.Primary.ID, err)
126117
}
127118

128-
for _, existingDiskID := range output.DiskIds {
129-
if aws.StringValue(existingDiskID) == diskID {
130-
return nil
131-
}
119+
if foundDiskID == nil {
120+
return fmt.Errorf("Storage Gateway Upload Buffer (%s) not found", rs.Primary.ID)
132121
}
133122

134-
return fmt.Errorf("Storage Gateway upload buffer %q not found", rs.Primary.ID)
123+
return nil
135124
}
136125
}
137126

0 commit comments

Comments
 (0)