Skip to content

Commit a183d72

Browse files
authored
Merge pull request #35598 from hashicorp/b-rekognition_collection_retry
r/aws_rekognition_collection: retry on create
2 parents 39dc98f + 0fa43b4 commit a183d72

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

internal/service/rekognition/collection.go

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ package rekognition
55

66
import (
77
"context"
8-
"errors"
8+
"time"
99

1010
"github.com/YakDriver/regexache"
1111
"github.com/aws/aws-sdk-go-v2/aws"
1212
"github.com/aws/aws-sdk-go-v2/service/rekognition"
1313
awstypes "github.com/aws/aws-sdk-go-v2/service/rekognition/types"
14+
"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
1415
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
1516
"github.com/hashicorp/terraform-plugin-framework/resource"
1617
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
@@ -32,12 +33,14 @@ import (
3233
// @Tags(identifierAttribute="arn")
3334
func newResourceCollection(_ context.Context) (resource.ResourceWithConfigure, error) {
3435
r := &resourceCollection{}
36+
r.SetDefaultCreateTimeout(2 * time.Minute)
3537

3638
return r, nil
3739
}
3840

3941
type resourceCollection struct {
4042
framework.ResourceWithConfigure
43+
framework.WithTimeouts
4144
framework.WithImportByID
4245
}
4346

@@ -52,7 +55,7 @@ func (r *resourceCollection) Metadata(_ context.Context, req resource.MetadataRe
5255
func (r *resourceCollection) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
5356
collectionRegex := regexache.MustCompile(`^[a-zA-Z0-9_.\-]+$`)
5457

55-
resp.Schema = schema.Schema{
58+
s := schema.Schema{
5659
Attributes: map[string]schema.Attribute{
5760
"arn": framework.ARNAttributeComputedOnly(),
5861
"collection_id": schema.StringAttribute{
@@ -77,6 +80,15 @@ func (r *resourceCollection) Schema(ctx context.Context, req resource.SchemaRequ
7780
names.AttrTagsAll: tftags.TagsAttributeComputedOnly(),
7881
},
7982
}
83+
84+
if s.Blocks == nil {
85+
s.Blocks = make(map[string]schema.Block)
86+
}
87+
s.Blocks["timeouts"] = timeouts.Block(ctx, timeouts.Opts{
88+
Create: true,
89+
})
90+
91+
resp.Schema = s
8092
}
8193

8294
func (r *resourceCollection) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
@@ -94,7 +106,7 @@ func (r *resourceCollection) Create(ctx context.Context, req resource.CreateRequ
94106
Tags: getTagsIn(ctx),
95107
}
96108

97-
out, err := conn.CreateCollection(ctx, in)
109+
_, err := conn.CreateCollection(ctx, in)
98110
if err != nil {
99111
resp.Diagnostics.AddError(
100112
create.ProblemStandardMessage(names.Rekognition, create.ErrActionCreating, ResNameCollection, plan.CollectionID.ValueString(), err),
@@ -103,15 +115,11 @@ func (r *resourceCollection) Create(ctx context.Context, req resource.CreateRequ
103115
return
104116
}
105117

106-
if out == nil || out.CollectionArn == nil {
107-
resp.Diagnostics.AddError(
108-
create.ProblemStandardMessage(names.Rekognition, create.ErrActionCreating, ResNameCollection, plan.CollectionID.ValueString(), nil),
109-
errors.New("empty output").Error(),
110-
)
111-
return
112-
}
118+
createTimeout := r.CreateTimeout(ctx, plan.Timeouts)
113119

114-
output, err := findCollectionByID(ctx, conn, plan.CollectionID.ValueString())
120+
out, err := tfresource.RetryWhenNotFound(ctx, createTimeout, func() (interface{}, error) {
121+
return findCollectionByID(ctx, conn, plan.CollectionID.ValueString())
122+
})
115123

116124
if err != nil {
117125
resp.Diagnostics.AddError(
@@ -121,6 +129,8 @@ func (r *resourceCollection) Create(ctx context.Context, req resource.CreateRequ
121129
return
122130
}
123131

132+
output := out.(*rekognition.DescribeCollectionOutput)
133+
124134
state := plan
125135
state.ID = plan.CollectionID
126136
state.ARN = flex.StringToFramework(ctx, output.CollectionARN)
@@ -230,10 +240,11 @@ func findCollectionByID(ctx context.Context, conn *rekognition.Client, id string
230240
}
231241

232242
type resourceCollectionData struct {
233-
ARN types.String `tfsdk:"arn"`
234-
CollectionID types.String `tfsdk:"collection_id"`
235-
FaceModelVersion types.String `tfsdk:"face_model_version"`
236-
ID types.String `tfsdk:"id"`
237-
Tags types.Map `tfsdk:"tags"`
238-
TagsAll types.Map `tfsdk:"tags_all"`
243+
ARN types.String `tfsdk:"arn"`
244+
CollectionID types.String `tfsdk:"collection_id"`
245+
FaceModelVersion types.String `tfsdk:"face_model_version"`
246+
ID types.String `tfsdk:"id"`
247+
Tags types.Map `tfsdk:"tags"`
248+
TagsAll types.Map `tfsdk:"tags_all"`
249+
Timeouts timeouts.Value `tfsdk:"timeouts"`
239250
}

website/docs/r/rekognition_collection.html.markdown

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ This resource exports the following attributes in addition to the arguments abov
4040
* `tags_all` - A map of tags assigned to the resource, including those inherited from the provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block).
4141
* `face_model_version` - The Face Model Version that the collection was initialized with
4242

43+
## Timeouts
44+
45+
[Configuration options](https://developer.hashicorp.com/terraform/language/resources/syntax#operation-timeouts):
46+
47+
- `create` - (Default `2m`)
48+
4349
## Import
4450

4551
In Terraform v1.5.0 and later, use an [`import` block](https://developer.hashicorp.com/terraform/language/import) to import Rekognition Collection using the `example_id_arg`. For example:

0 commit comments

Comments
 (0)