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

Add Rekognition collection resource #35407

Merged
merged 11 commits into from
Feb 1, 2024
3 changes: 3 additions & 0 deletions .changelog/35407.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_rekognition_collection
```
239 changes: 239 additions & 0 deletions internal/service/rekognition/collection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package rekognition

import (
"context"
"errors"

"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-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-provider-aws/internal/create"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/framework"
"github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @FrameworkResource(name="Collection")
// @Tags(identifierAttribute="arn")
func newResourceCollection(_ context.Context) (resource.ResourceWithConfigure, error) {
r := &resourceCollection{}

return r, nil
}

type resourceCollection struct {
framework.ResourceWithConfigure
framework.WithImportByID
}

const (
ResNameCollection = "Collection"
)

func (r *resourceCollection) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = "aws_rekognition_collection"
}

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

resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"arn": framework.ARNAttributeComputedOnly(),
"collection_id": schema.StringAttribute{
Description: "The name of the Rekognition collection",
Required: true,
Validators: []validator.String{
stringvalidator.LengthAtMost(255),
stringvalidator.RegexMatches(collectionRegex, "must conform to: ^[a-zA-Z0-9_.\\-]+$"),
},
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"id": framework.IDAttribute(),
"face_model_version": schema.StringAttribute{
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
names.AttrTags: tftags.TagsAttribute(),
names.AttrTagsAll: tftags.TagsAttributeComputedOnly(),
},
}
}

func (r *resourceCollection) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
conn := r.Meta().RekognitionClient(ctx)

var plan resourceCollectionData

resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}

in := &rekognition.CreateCollectionInput{
CollectionId: plan.CollectionID.ValueStringPointer(),
Tags: getTagsIn(ctx),
}

out, err := conn.CreateCollection(ctx, in)
if err != nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.Rekognition, create.ErrActionCreating, ResNameCollection, plan.CollectionID.ValueString(), err),
err.Error(),
)
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
}

output, err := findCollectionByID(ctx, conn, plan.CollectionID.ValueString())

if err != nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.Rekognition, create.ErrActionCreating, ResNameCollection, plan.CollectionID.ValueString(), err),
err.Error(),
)
return
}

state := plan
state.ID = plan.CollectionID
state.ARN = flex.StringToFramework(ctx, output.CollectionARN)
state.FaceModelVersion = flex.StringToFramework(ctx, output.FaceModelVersion)

resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}

func (r *resourceCollection) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
conn := r.Meta().RekognitionClient(ctx)

var state resourceCollectionData

resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}

out, err := findCollectionByID(ctx, conn, state.ID.ValueString())

if tfresource.NotFound(err) {
resp.State.RemoveResource(ctx)
return
}

if err != nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.Rekognition, create.ErrActionReading, ResNameCollection, state.ID.ValueString(), err),
err.Error(),
)
return
}

state.ARN = flex.StringToFramework(ctx, out.CollectionARN)
state.FaceModelVersion = flex.StringToFramework(ctx, out.FaceModelVersion)
state.CollectionID = flex.StringToFramework(ctx, state.ID.ValueStringPointer())

resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}

func (r *resourceCollection) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var plan resourceCollectionData
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)

if resp.Diagnostics.HasError() {
return
}

resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
}

func (r *resourceCollection) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
conn := r.Meta().RekognitionClient(ctx)

var state resourceCollectionData
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}

in := &rekognition.DeleteCollectionInput{
CollectionId: state.ID.ValueStringPointer(),
}

_, err := conn.DeleteCollection(ctx, in)

if errs.IsA[*awstypes.ResourceNotFoundException](err) {
return
}

if err != nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.Rekognition, create.ErrActionDeleting, ResNameCollection, state.ID.ValueString(), err),
err.Error(),
)
return
}
}

func (r *resourceCollection) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) {
r.SetTagsAll(ctx, req, resp)
}

func findCollectionByID(ctx context.Context, conn *rekognition.Client, id string) (*rekognition.DescribeCollectionOutput, error) {
in := &rekognition.DescribeCollectionInput{
CollectionId: aws.String(id),
}

out, err := conn.DescribeCollection(ctx, in)

if errs.IsA[*awstypes.ResourceNotFoundException](err) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: in,
}
}

if err != nil {
return nil, err
}

if out == nil || out.CollectionARN == nil {
return nil, tfresource.NewEmptyResultError(in)
}

return out, nil
}

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"`
}
Loading
Loading