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

r/aws_rekognition_collection: retry on create #35598

Merged
merged 2 commits into from
Feb 1, 2024
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
45 changes: 28 additions & 17 deletions internal/service/rekognition/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ package rekognition

import (
"context"
"errors"
"time"

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

return r, nil
}

type resourceCollection struct {
framework.ResourceWithConfigure
framework.WithTimeouts
framework.WithImportByID
}

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

resp.Schema = schema.Schema{
s := schema.Schema{
Attributes: map[string]schema.Attribute{
"arn": framework.ARNAttributeComputedOnly(),
"collection_id": schema.StringAttribute{
Expand All @@ -77,6 +80,15 @@ func (r *resourceCollection) Schema(ctx context.Context, req resource.SchemaRequ
names.AttrTagsAll: tftags.TagsAttributeComputedOnly(),
},
}

if s.Blocks == nil {
s.Blocks = make(map[string]schema.Block)
}
s.Blocks["timeouts"] = timeouts.Block(ctx, timeouts.Opts{
Create: true,
})

resp.Schema = s
}

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

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

if out == nil || out.CollectionArn == nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.Rekognition, create.ErrActionCreating, ResNameCollection, plan.CollectionID.ValueString(), nil),
errors.New("empty output").Error(),
)
return
}
createTimeout := r.CreateTimeout(ctx, plan.Timeouts)

output, err := findCollectionByID(ctx, conn, plan.CollectionID.ValueString())
out, err := tfresource.RetryWhenNotFound(ctx, createTimeout, func() (interface{}, error) {
return findCollectionByID(ctx, conn, plan.CollectionID.ValueString())
})

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

output := out.(*rekognition.DescribeCollectionOutput)

state := plan
state.ID = plan.CollectionID
state.ARN = flex.StringToFramework(ctx, output.CollectionARN)
Expand Down Expand Up @@ -230,10 +240,11 @@ func findCollectionByID(ctx context.Context, conn *rekognition.Client, id string
}

type resourceCollectionData struct {
ARN types.String `tfsdk:"arn"`
CollectionID types.String `tfsdk:"collection_id"`
FaceModelVersion types.String `tfsdk:"face_model_version"`
ID types.String `tfsdk:"id"`
Tags types.Map `tfsdk:"tags"`
TagsAll types.Map `tfsdk:"tags_all"`
ARN types.String `tfsdk:"arn"`
CollectionID types.String `tfsdk:"collection_id"`
FaceModelVersion types.String `tfsdk:"face_model_version"`
ID types.String `tfsdk:"id"`
Tags types.Map `tfsdk:"tags"`
TagsAll types.Map `tfsdk:"tags_all"`
Timeouts timeouts.Value `tfsdk:"timeouts"`
}
6 changes: 6 additions & 0 deletions website/docs/r/rekognition_collection.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ This resource exports the following attributes in addition to the arguments abov
* `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).
* `face_model_version` - The Face Model Version that the collection was initialized with

## Timeouts

[Configuration options](https://developer.hashicorp.com/terraform/language/resources/syntax#operation-timeouts):

- `create` - (Default `2m`)

## Import

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:
Expand Down
Loading