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

New Resource: Amazon Rekognition Stream Processor & Collection #31889

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions .changelog/31889.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:new-resource
resource/aws_rekognition_stream_processor
```

```release-note:new-resource
resource/aws_rekognition_collection
```
1 change: 1 addition & 0 deletions .teamcity/components/generated/services_all.kt
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ val services = mapOf(
"redshift" to ServiceSpec("Redshift", vpcLock = true),
"redshiftdata" to ServiceSpec("Redshift Data"),
"redshiftserverless" to ServiceSpec("Redshift Serverless"),
"rekognition" to ServiceSpec("Rekognition"),
"resourceexplorer2" to ServiceSpec("Resource Explorer"),
"resourcegroups" to ServiceSpec("Resource Groups"),
"resourcegroupstaggingapi" to ServiceSpec("Resource Groups Tagging"),
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/iam v1.20.0 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.27 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.14.2 // indirect
github.com/aws/aws-sdk-go-v2/service/rekognition v1.28.0 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.12.10 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.14.10 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.19.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ github.com/aws/aws-sdk-go-v2/service/rbin v1.8.12 h1:fNxEf+LXsuisrVIFAK9ZqNYaQM0
github.com/aws/aws-sdk-go-v2/service/rbin v1.8.12/go.mod h1:kdlIXWL9Akk4tj/u9GMnO17ImBEvrhQmb0OnJ7LokQY=
github.com/aws/aws-sdk-go-v2/service/rds v1.45.0 h1:Yi23UNiGidNfT7tIW0lbE6JtRR1ZN+cNZGRTKLB+opk=
github.com/aws/aws-sdk-go-v2/service/rds v1.45.0/go.mod h1:rS6T0DrjdZ5LDr8ZC/J9iZdD1oSbie5reWWzqv5zLOw=
github.com/aws/aws-sdk-go-v2/service/rekognition v1.28.0 h1:CRfQrFGFGNCeobzrmTe3E2z1gnUuBQhk2Mj4+gzyjqE=
github.com/aws/aws-sdk-go-v2/service/rekognition v1.28.0/go.mod h1:4+jsheRchAmHBX+HOP2DMNwNmqHuH95FE3H7DAg0fyc=
github.com/aws/aws-sdk-go-v2/service/resourceexplorer2 v1.2.13 h1:9uj47asgRDlwNqZIlzDt5HjnD2wakHy4yUXSz3e9V0M=
github.com/aws/aws-sdk-go-v2/service/resourceexplorer2 v1.2.13/go.mod h1:2nZFXAepd6OTyH90JTsgjdgq4K6+jVh/5nXtiEpjHtw=
github.com/aws/aws-sdk-go-v2/service/rolesanywhere v1.2.0 h1:2T9nMoFYotUEyaNTSALQm7OOlzmHWM5DIxZ8zE9nYEg=
Expand Down
8 changes: 4 additions & 4 deletions internal/conns/awsclient_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions internal/conns/config_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions internal/provider/service_packages_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

200 changes: 200 additions & 0 deletions internal/service/rekognition/collection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
package rekognition

import (
// TIP: ==== IMPORTS ====
// This is a common set of imports but not customized to your code since
// your code hasn't been written yet. Make sure you, your IDE, or
// goimports -w <file> fixes these imports.
//
// The provider linter wants your imports to be in two groups: first,
// standard library (i.e., "fmt" or "strings"), second, everything else.
//
// Also, AWS Go SDK v2 may handle nested structures differently than v1,
// using the services/rekognition/types package. If so, you'll
// need to import types and reference the nested types, e.g., as
// types.<Type Name>.
"context"
"errors"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"log"
"strings"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/rekognition"
"github.com/aws/aws-sdk-go-v2/service/rekognition/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/create"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @SDKResource("aws_rekognition_collection")
// @Tags(identifierAttribute="arn")
func ResourceCollection() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceCollectionCreate,
ReadWithoutTimeout: resourceCollectionRead,
UpdateWithoutTimeout: resourceCollectionUpdate,
DeleteWithoutTimeout: resourceCollectionDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Update: schema.DefaultTimeout(30 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
},
Schema: map[string]*schema.Schema{
"collection_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"face_count": {
Type: schema.TypeInt,
Computed: true,
},
"face_model_version": {
Type: schema.TypeString,
Computed: true,
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
},
CustomizeDiff: verify.SetTagsDiff,
}
}

const (
ResNameCollection = "Collection"
)

func resourceCollectionCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).RekognitionClient()

in := &rekognition.CreateCollectionInput{
CollectionId: aws.String(d.Get("collection_id").(string)),
}

defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))

if len(tags) > 0 {
in.Tags = Tags(tags.IgnoreAWS())
}

out, err := conn.CreateCollection(ctx, in)
if err != nil {
return create.DiagError(names.Rekognition, create.ErrActionCreating, ResNameCollection, d.Get("collection_id").(string), err)
}

if out == nil || out.CollectionArn == nil {
return create.DiagError(names.Rekognition, create.ErrActionCreating, ResNameCollection, d.Get("collection_id").(string), errors.New("empty output"))
}

arn := aws.ToString(out.CollectionArn)
d.SetId(arn[strings.LastIndex(arn, "/")+1:])

return resourceCollectionRead(ctx, d, meta)
}

func resourceCollectionRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).RekognitionClient()

out, err := findCollectionByID(ctx, conn, d.Id())

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] Rekognition Collection (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return create.DiagError(names.Rekognition, create.ErrActionReading, ResNameCollection, d.Id(), err)
}

arn := aws.ToString(out.CollectionARN)
d.Set("collection_id", arn[strings.LastIndex(arn, "/")+1:])
d.Set("arn", out.CollectionARN)
d.Set("face_count", out.FaceCount)
d.Set("face_model_version", out.FaceModelVersion)

tags, err := ListTags(ctx, conn, d.Get("arn").(string))
if err != nil {
return create.DiagError(names.Rekognition, create.ErrActionReading, ResNameCollection, d.Id(), err)
}

defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig
tags = tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig)

if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return create.DiagError(names.Rekognition, create.ErrActionSetting, ResNameCollection, d.Id(), err)
}

if err := d.Set("tags_all", tags.Map()); err != nil {
return create.DiagError(names.Rekognition, create.ErrActionSetting, ResNameCollection, d.Id(), err)
}

return nil
}

func resourceCollectionUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
if d.HasChangesExcept("tags", "tags_all") {
return resourceStreamProcessorRead(ctx, d, meta)
} else {
return nil
}
}

func resourceCollectionDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).RekognitionClient()

log.Printf("[INFO] Deleting Rekognition Collection %s", d.Id())

_, err := conn.DeleteCollection(ctx, &rekognition.DeleteCollectionInput{
CollectionId: aws.String(d.Id()),
})

if err != nil {
var nfe *types.ResourceNotFoundException
if errors.As(err, &nfe) {
return nil
}

return create.DiagError(names.Rekognition, create.ErrActionDeleting, ResNameCollection, d.Id(), err)
}

return nil
}

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 err != nil {
var nfe *types.ResourceNotFoundException
if errors.As(err, &nfe) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: in,
}
}
return nil, err
}
if out == nil {
return nil, tfresource.NewEmptyResultError(in)
}

return out, nil
}
Loading