Skip to content

Commit

Permalink
Merge pull request #27159 from GlennChia/f-aws_evidently_segment
Browse files Browse the repository at this point in the history
r/aws_evidently_segment
  • Loading branch information
ewbankkit authored Nov 3, 2022
2 parents 8607006 + b06ae7b commit 57d75d1
Show file tree
Hide file tree
Showing 7 changed files with 620 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .changelog/27159.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_evidently_segment
```
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1517,6 +1517,7 @@ func New(_ context.Context) (*schema.Provider, error) {
"aws_emrserverless_application": emrserverless.ResourceApplication(),

"aws_evidently_project": evidently.ResourceProject(),
"aws_evidently_segment": evidently.ResourceSegment(),

"aws_kinesis_firehose_delivery_stream": firehose.ResourceDeliveryStream(),

Expand Down
25 changes: 25 additions & 0 deletions internal/service/evidently/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,28 @@ func FindProjectByNameOrARN(ctx context.Context, conn *cloudwatchevidently.Cloud

return output.Project, nil
}

func FindSegmentByNameOrARN(ctx context.Context, conn *cloudwatchevidently.CloudWatchEvidently, nameOrARN string) (*cloudwatchevidently.Segment, error) {
input := &cloudwatchevidently.GetSegmentInput{
Segment: aws.String(nameOrARN),
}

output, err := conn.GetSegmentWithContext(ctx, input)

if tfawserr.ErrCodeEquals(err, cloudwatchevidently.ErrCodeResourceNotFoundException) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

if output == nil || output.Segment == nil {
return nil, tfresource.NewEmptyResultError(input)
}

return output.Segment, nil
}
192 changes: 192 additions & 0 deletions internal/service/evidently/segment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
package evidently

import (
"context"
"log"
"regexp"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatchevidently"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
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"
)

func ResourceSegment() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceSegmentCreate,
ReadWithoutTimeout: resourceSegmentRead,
UpdateWithoutTimeout: resourceSegmentUpdate,
DeleteWithoutTimeout: resourceSegmentDelete,

Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"created_time": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 160),
},
"experiment_count": {
Type: schema.TypeInt,
Computed: true,
},
"last_updated_time": {
Type: schema.TypeString,
Computed: true,
},
"launch_count": {
Type: schema.TypeInt,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.All(
validation.StringLenBetween(1, 64),
validation.StringMatch(regexp.MustCompile(`^[-a-zA-Z0-9._]*$`), "alphanumeric and can contain hyphens, underscores, and periods"),
),
},
"pattern": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.All(
validation.StringLenBetween(1, 1024),
validation.StringIsJSON,
),
DiffSuppressFunc: verify.SuppressEquivalentJSONDiffs,
StateFunc: func(v interface{}) string {
json, _ := structure.NormalizeJsonString(v)
return json
},
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
},

CustomizeDiff: verify.SetTagsDiff,
}
}

func resourceSegmentCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).EvidentlyConn
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(d.Get("tags").(map[string]interface{})))

name := d.Get("name").(string)
input := &cloudwatchevidently.CreateSegmentInput{
Name: aws.String(name),
Pattern: aws.String(d.Get("pattern").(string)),
}

if v, ok := d.GetOk("description"); ok {
input.Description = aws.String(v.(string))
}

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

log.Printf("[DEBUG] Creating CloudWatch Evidently Segment: %s", input)
output, err := conn.CreateSegmentWithContext(ctx, input)

if err != nil {
return diag.Errorf("creating CloudWatch Evidently Segment (%s): %s", name, err)
}

d.SetId(aws.StringValue(output.Segment.Arn))

return resourceSegmentRead(ctx, d, meta)
}

func resourceSegmentRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).EvidentlyConn
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

segment, err := FindSegmentByNameOrARN(ctx, conn, d.Id())

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

if err != nil {
return diag.Errorf("reading CloudWatch Evidently Segment (%s): %s", d.Id(), err)
}

d.Set("arn", segment.Arn)
d.Set("created_time", aws.TimeValue(segment.CreatedTime).Format(time.RFC3339))
d.Set("description", segment.Description)
d.Set("experiment_count", segment.ExperimentCount)
d.Set("last_updated_time", aws.TimeValue(segment.LastUpdatedTime).Format(time.RFC3339))
d.Set("launch_count", segment.LaunchCount)
d.Set("name", segment.Name)
d.Set("pattern", segment.Pattern)

tags := KeyValueTags(segment.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig)

if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return diag.Errorf("setting tags: %s", err)
}

if err := d.Set("tags_all", tags.Map()); err != nil {
return diag.Errorf("setting tags_all: %s", err)
}

return nil
}

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

if d.HasChange("tags_all") {
o, n := d.GetChange("tags_all")

if err := UpdateTagsWithContext(ctx, conn, d.Id(), o, n); err != nil {
return diag.Errorf("updating tags: %s", err)
}
}

return resourceSegmentRead(ctx, d, meta)
}

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

log.Printf("[DEBUG] Deleting CloudWatch Evidently Segment: %s", d.Id())
_, err := conn.DeleteSegmentWithContext(ctx, &cloudwatchevidently.DeleteSegmentInput{
Segment: aws.String(d.Id()),
})

if tfawserr.ErrCodeEquals(err, cloudwatchevidently.ErrCodeResourceNotFoundException) {
return nil
}

if err != nil {
return diag.Errorf("deleting CloudWatch Evidently Segment (%s): %s", d.Id(), err)
}

return nil
}
Loading

0 comments on commit 57d75d1

Please sign in to comment.