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

feat: add support for event archive in EventBridge resource #17270

Merged
merged 26 commits into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
13bbde0
Use dot internal instead of dot local
jrobison-sb Sep 8, 2020
873750e
feat: initial impl. eventbridge event replay
heitorlessa Jan 24, 2021
5a1d717
fix: remove creation time attr
heitorlessa Jan 24, 2021
dc7cd62
fix: rename resource to Archive not Replay
heitorlessa Jan 25, 2021
9fbcf2f
feat: add cloudwatch_event_archive resource
heitorlessa Jan 25, 2021
6426fd5
improv: group required fields; add computed arn
heitorlessa Jan 25, 2021
dfd4516
fix: account for value being saved as int, not int64
heitorlessa Jan 25, 2021
5fc5079
improv: add acceptance tests
heitorlessa Jan 25, 2021
5648d21
docs: add new event archive section
heitorlessa Jan 25, 2021
55701ca
chore: terrafmt linting fix
heitorlessa Jan 25, 2021
cde2067
chore: import order and group linting fix
heitorlessa Jan 26, 2021
b81753a
improv: test event pattern update
heitorlessa Jan 26, 2021
da6259b
improv: add another example with all arguments
heitorlessa Jan 26, 2021
35b22df
chore: lint website docs markdown
heitorlessa Jan 26, 2021
5ab7d94
feat: add import support
heitorlessa Jan 26, 2021
f729325
docs: add import support
heitorlessa Jan 26, 2021
74232ee
improv: use name instead of archive_name argument
heitorlessa Jan 26, 2021
bc48c21
Merge pull request #15081 from jrobison-sb/dont-use-dot-local-example
breathingdust Feb 11, 2021
9a2f39b
fix: address Bill's review.
heitorlessa Feb 18, 2021
2d569ea
fix: address Bill's review on tests.
heitorlessa Feb 18, 2021
06f3f6b
fix: address Bill's review on docs.
heitorlessa Feb 18, 2021
a826846
chore: add changelog entry.
heitorlessa Feb 18, 2021
3e995c6
Merge branch 'main' into feat/eventbridge_replay
heitorlessa Feb 18, 2021
e1da594
Merge branch 'main' of https://github.com/hashicorp/terraform-provide…
heitorlessa Feb 18, 2021
f1a4348
Merge branch 'main' into feat/eventbridge_replay
heitorlessa Feb 18, 2021
d341f8f
chore: lint security hub test
heitorlessa Feb 18, 2021
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
197 changes: 197 additions & 0 deletions aws/resource_aws_cloudwatch_event_replay.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
package aws

import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
events "github.com/aws/aws-sdk-go/service/cloudwatchevents"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"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"
"log"
)

func resourceAwsCloudWatchEventReplay() *schema.Resource {
return &schema.Resource{
Create: resourceAwsCloudWatchEventArchiveCreate,
Read: resourceAwsCloudWatchEventArchiveRead,
Update: resourceAwsCloudWatchEventArchiveUpdate,
Delete: resourceAwsCloudWatchEventArchiveDelete,

Schema: map[string]*schema.Schema{
"archive_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateCloudWatchEventArchiveName,
},

"description": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(0, 512),
},

"event_pattern": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateEventPatternValue(),
StateFunc: func(v interface{}) string {
json, _ := structure.NormalizeJsonString(v.(string))
return json
},
},

"event_source_arn": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateArn,
},

"retention_days": {
Type: schema.TypeInt,
Optional: true,
},
},
}
}

func resourceAwsCloudWatchEventArchiveCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudwatcheventsconn

input, err := buildCreateArchiveInputStruct(d)

if err != nil {
return fmt.Errorf("Creating CloudWatch Events Archive parameters failed: %w", err)
}

log.Printf("[DEBUG] Creating CloudWatch Events Archive: %s", input)

_, err = conn.CreateArchive(input)
if err != nil {
return fmt.Errorf("Creating CloudWatch Events Archive failed: %w", err)
}

d.SetId(d.Get("archive_name").(string))

log.Printf("[INFO] CloudWatch Events Archive (%s) created", d.Id())

return resourceAwsCloudWatchEventArchiveRead(d, meta)
}

func resourceAwsCloudWatchEventArchiveRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudwatcheventsconn
archiveName := d.Get("archive_name").(string)
input := &events.DescribeArchiveInput{
ArchiveName: aws.String(archiveName),
}

out, err := conn.DescribeArchive(input)

if err != nil {
return fmt.Errorf("Error reading CloudWatch Events archive: %w", err)
}

log.Printf("[DEBUG] Found Archive: #{*out}")

// Review Question - Is there a problem in setting more than should name and Arn?
d.Set("archive_name", out.ArchiveName)
d.Set("description", out.Description)
d.Set("event_pattern", out.EventPattern)
d.Set("event_source_arn", out.EventSourceArn)
d.Set("arn", out.ArchiveArn)
d.Set("retention_days", out.RetentionDays)
d.Set("event_count", out.EventCount)
d.Set("state", out.State)
d.Set("creation_time", out.CreationTime)

return nil
}

func resourceAwsCloudWatchEventArchiveUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudwatcheventsconn

input, err := buildUpdateArchiveInputStruct(d)

if err != nil {
return fmt.Errorf("Creating CloudWatch Events Archive parameters failed: %w", err)
}

log.Printf("[DEBUG] Updating CloudWatch Events Archive: %s", input)
_, err = conn.UpdateArchive(input)
if err != nil {
return fmt.Errorf("error updating CloudWatch Events Archive (%s): %w", d.Id(), err)
}

return resourceAwsCloudWatchEventArchiveRead(d, meta)
}

func resourceAwsCloudWatchEventArchiveDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudwatcheventsconn

input := &events.DeleteArchiveInput{
ArchiveName: aws.String(d.Get("archive_name").(string)),
}

_, err := conn.DeleteArchive(input)
if err != nil {
if tfawserr.ErrCodeEquals(err, events.ErrCodeResourceNotFoundException) {
return nil
}
return fmt.Errorf("error deleting CloudWatch Events Archive (%s): %w", d.Id(), err)
}

return nil
}

func buildCreateArchiveInputStruct(d *schema.ResourceData) (*events.CreateArchiveInput, error) {
input := events.CreateArchiveInput{
ArchiveName: aws.String(d.Get("archive_name").(string)),
}

if v, ok := d.GetOk("event_pattern"); ok {
pattern, err := structure.NormalizeJsonString(v)
if err != nil {
return nil, fmt.Errorf("event pattern contains an invalid JSON: %w", err)
}
input.EventPattern = aws.String(pattern)
}

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

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

if v, ok := d.GetOk("retention_days"); ok {
input.RetentionDays = aws.Int64(v.(int64))
}

return &input, nil
}

func buildUpdateArchiveInputStruct(d *schema.ResourceData) (*events.UpdateArchiveInput, error) {
input := events.UpdateArchiveInput{
ArchiveName: aws.String(d.Get("archive_name").(string)),
}

if v, ok := d.GetOk("event_pattern"); ok {
pattern, err := structure.NormalizeJsonString(v)
if err != nil {
return nil, fmt.Errorf("event pattern contains an invalid JSON: %w", err)
}
input.EventPattern = aws.String(pattern)
}

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

if v, ok := d.GetOk("retention_days"); ok {
input.RetentionDays = aws.Int64(v.(int64))
}

return &input, nil
}
5 changes: 5 additions & 0 deletions aws/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -2501,6 +2501,11 @@ var validateCloudWatchEventBusName = validation.All(
validation.StringMatch(regexp.MustCompile(`^[a-zA-Z0-9._\-]+$`), ""),
)

var validateCloudWatchEventArchiveName = validation.All(
validation.StringLenBetween(1, 48),
validation.StringMatch(regexp.MustCompile(`^[\.\-_A-Za-z0-9]+`), ""),
)

var validateServiceDiscoveryNamespaceName = validation.All(
validation.StringLenBetween(1, 1024),
validation.StringMatch(regexp.MustCompile(`^[0-9A-Za-z._-]+$`), ""),
Expand Down