Skip to content

Commit

Permalink
Merge pull request #26951 from hashicorp/f-comprehend-document-classi…
Browse files Browse the repository at this point in the history
…fier

resource/aws_comprehend_document_classifier: New resource
  • Loading branch information
gdavison authored Oct 7, 2022
2 parents 3f29d67 + 7d10b0c commit b694181
Show file tree
Hide file tree
Showing 22 changed files with 4,730 additions and 185 deletions.
3 changes: 2 additions & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,8 @@ func New(_ context.Context) (*schema.Provider, error) {
"aws_cognito_user_pool_domain": cognitoidp.ResourceUserPoolDomain(),
"aws_cognito_user_pool_ui_customization": cognitoidp.ResourceUserPoolUICustomization(),

"aws_comprehend_entity_recognizer": comprehend.ResourceEntityRecognizer(),
"aws_comprehend_document_classifier": comprehend.ResourceDocumentClassifier(),
"aws_comprehend_entity_recognizer": comprehend.ResourceEntityRecognizer(),

"aws_config_aggregate_authorization": configservice.ResourceAggregateAuthorization(),
"aws_config_config_rule": configservice.ResourceConfigRule(),
Expand Down
9 changes: 9 additions & 0 deletions internal/service/comprehend/acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/aws/aws-sdk-go-v2/service/comprehend"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
)
Expand Down Expand Up @@ -56,3 +57,11 @@ resource "aws_subnet" "test" {
`, rName, subnetCount),
)
}

func uniqueIDPattern() string {
return prefixedUniqueIDPattern(resource.UniqueIdPrefix)
}

func prefixedUniqueIDPattern(prefix string) string {
return fmt.Sprintf("%s[[:xdigit:]]{%d}", prefix, resource.UniqueIDSuffixLength)
}
121 changes: 121 additions & 0 deletions internal/service/comprehend/common_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import (
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/comprehend/types"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
tfec2 "github.com/hashicorp/terraform-provider-aws/internal/service/ec2"
)

Expand Down Expand Up @@ -93,3 +96,121 @@ func statusNetworkInterfaces(ctx context.Context, conn *ec2.EC2, initialENIs map
return added, aws.ToString(added.Status), nil
}
}

type resourceGetter interface {
Get(key string) any
}

func flattenVPCConfig(apiObject *types.VpcConfig) []interface{} {
if apiObject == nil {
return nil
}

m := map[string]interface{}{
"security_group_ids": flex.FlattenStringValueSet(apiObject.SecurityGroupIds),
"subnets": flex.FlattenStringValueSet(apiObject.Subnets),
}

return []interface{}{m}
}

func expandVPCConfig(tfList []interface{}) *types.VpcConfig {
if len(tfList) == 0 {
return nil
}

tfMap := tfList[0].(map[string]interface{})

a := &types.VpcConfig{
SecurityGroupIds: flex.ExpandStringValueSet(tfMap["security_group_ids"].(*schema.Set)),
Subnets: flex.ExpandStringValueSet(tfMap["subnets"].(*schema.Set)),
}

return a
}

func flattenAugmentedManifests(apiObjects []types.AugmentedManifestsListItem) []interface{} {
if len(apiObjects) == 0 {
return nil
}

var l []interface{}

for _, apiObject := range apiObjects {
l = append(l, flattenAugmentedManifestsListItem(&apiObject))
}

return l
}

func flattenAugmentedManifestsListItem(apiObject *types.AugmentedManifestsListItem) map[string]interface{} {
if apiObject == nil {
return nil
}

m := map[string]interface{}{
"attribute_names": flex.FlattenStringValueList(apiObject.AttributeNames),
"s3_uri": aws.ToString(apiObject.S3Uri),
"document_type": apiObject.DocumentType,
"split": apiObject.Split,
}

if v := apiObject.AnnotationDataS3Uri; v != nil {
m["annotation_data_s3_uri"] = aws.ToString(v)
}

if v := apiObject.SourceDocumentsS3Uri; v != nil {
m["source_documents_s3_uri"] = aws.ToString(v)
}

return m
}

func expandAugmentedManifests(tfSet *schema.Set) []types.AugmentedManifestsListItem {
if tfSet.Len() == 0 {
return nil
}

var s []types.AugmentedManifestsListItem

for _, r := range tfSet.List() {
m, ok := r.(map[string]interface{})

if !ok {
continue
}

a := expandAugmentedManifestsListItem(m)

if a == nil {
continue
}

s = append(s, *a)
}

return s
}

func expandAugmentedManifestsListItem(tfMap map[string]interface{}) *types.AugmentedManifestsListItem {
if tfMap == nil {
return nil
}

a := &types.AugmentedManifestsListItem{
AttributeNames: flex.ExpandStringValueList(tfMap["attribute_names"].([]interface{})),
S3Uri: aws.String(tfMap["s3_uri"].(string)),
DocumentType: types.AugmentedManifestsDocumentTypeFormat(tfMap["document_type"].(string)),
Split: types.Split(tfMap["split"].(string)),
}

if v, ok := tfMap["annotation_data_s3_uri"].(string); ok && v != "" {
a.AnnotationDataS3Uri = aws.String(v)
}

if v, ok := tfMap["source_documents_s3_uri"].(string); ok && v != "" {
a.SourceDocumentsS3Uri = aws.String(v)
}

return a
}
9 changes: 8 additions & 1 deletion internal/service/comprehend/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,12 @@ import (
const iamPropagationTimeout = 2 * time.Minute

// Avoid service throttling
const entityRegcognizerDelay = 1 * time.Minute
const entityRegcognizerCreatedDelay = 10 * time.Minute
const entityRegcognizerStoppedDelay = 0
const entityRegcognizerDeletedDelay = 5 * time.Minute
const entityRegcognizerPollInterval = 1 * time.Minute

const documentClassifierCreatedDelay = 15 * time.Minute
const documentClassifierStoppedDelay = 0
const documentClassifierDeletedDelay = 5 * time.Minute
const documentClassifierPollInterval = 1 * time.Minute
78 changes: 77 additions & 1 deletion internal/service/comprehend/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package comprehend

import (
"regexp"
"strings"

"github.com/aws/aws-sdk-go-v2/aws/arn"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
tfkms "github.com/hashicorp/terraform-provider-aws/internal/service/kms"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
)

func diffSuppressKMSKeyId(k, oldValue, newValue string, d *schema.ResourceData) bool {
func diffSuppressKMSKeyId(_, oldValue, newValue string, _ *schema.ResourceData) bool {
if oldValue == newValue {
return true
}
Expand All @@ -30,6 +32,41 @@ func diffSuppressKMSKeyId(k, oldValue, newValue string, d *schema.ResourceData)
return false
}

func diffSuppressKMSAlias(_, oldValue, newValue string, _ *schema.ResourceData) bool {
if oldValue == newValue {
return true
}

oldAlias := oldValue
if arn.IsARN(oldValue) {
oldAlias = kmsKeyAliasFromARN(oldValue)
}

newAlias := newValue
if arn.IsARN(newValue) {
newAlias = kmsKeyAliasFromARN(newValue)
}

if oldAlias == newAlias {
return true
}

return false
}

func diffSuppressKMSKeyOrAlias(k, oldValue, newValue string, d *schema.ResourceData) bool {
if arn.IsARN(newValue) {
if isKMSKeyARN(newValue) {
return diffSuppressKMSKeyId(k, oldValue, newValue, d)
} else {
return diffSuppressKMSAlias(k, oldValue, newValue, d)
}
} else if isKMSAliasName(newValue) {
return diffSuppressKMSAlias(k, oldValue, newValue, d)
}
return diffSuppressKMSKeyId(k, oldValue, newValue, d)
}

func kmsKeyIdFromARN(s string) string {
arn, err := arn.Parse(s)
if err != nil {
Expand All @@ -47,5 +84,44 @@ func kmsKeyIdFromARNResource(s string) string {
}

return matches[1]
}

func kmsKeyAliasFromARN(s string) string {
arn, err := arn.Parse(s)
if err != nil {
return ""
}

return kmsKeyAliasNameFromARNResource(arn.Resource)
}

func kmsKeyAliasNameFromARNResource(s string) string {
re := regexp.MustCompile("^" + tfkms.AliasNameRegexPattern + "$")
if re.MatchString(s) {
return s
}

return ""
}

func isKMSKeyARN(s string) bool {
parsedARN, err := arn.Parse(s)
if err != nil {
return false
}

return kmsKeyIdFromARNResource(parsedARN.Resource) != ""
}

func isKMSAliasName(s string) bool {
return strings.HasPrefix(s, "alias/")
}

func isKMSAliasARN(s string) bool {
parsedARN, err := arn.Parse(s)
if err != nil {
return false
}

return isKMSAliasName(parsedARN.Resource)
}
1 change: 0 additions & 1 deletion internal/service/comprehend/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,4 @@ func TestDiffSuppressKMSKeyId(t *testing.T) {
}
})
}

}
Loading

0 comments on commit b694181

Please sign in to comment.