Skip to content

Commit dd076af

Browse files
authored
Merge pull request #44487 from hashicorp/f-eventbridge-put-events
New action: `aws_events_put_events`
2 parents 27a9e34 + 2c35b49 commit dd076af

File tree

5 files changed

+686
-0
lines changed

5 files changed

+686
-0
lines changed

.changelog/44487.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-action
2+
aws_events_put_events
3+
```
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package events
5+
6+
import (
7+
"context"
8+
"strconv"
9+
10+
"github.com/aws/aws-sdk-go-v2/service/eventbridge"
11+
"github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes"
12+
"github.com/hashicorp/terraform-plugin-framework/action"
13+
"github.com/hashicorp/terraform-plugin-framework/action/schema"
14+
"github.com/hashicorp/terraform-plugin-framework/types"
15+
"github.com/hashicorp/terraform-plugin-log/tflog"
16+
"github.com/hashicorp/terraform-provider-aws/internal/framework"
17+
fwflex "github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
18+
fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types"
19+
"github.com/hashicorp/terraform-provider-aws/names"
20+
)
21+
22+
// @Action(aws_events_put_events, name="Put Events")
23+
// nosemgrep: ci.events-in-func-name -- "PutEvents" matches AWS API operation name (PutEvents). Required for consistent generated/action naming; safe to ignore.
24+
func newPutEventsAction(_ context.Context) (action.ActionWithConfigure, error) {
25+
return &putEventsAction{}, nil
26+
}
27+
28+
var (
29+
_ action.Action = (*putEventsAction)(nil)
30+
)
31+
32+
type putEventsAction struct {
33+
framework.ActionWithModel[putEventsActionModel]
34+
}
35+
36+
type putEventsActionModel struct {
37+
framework.WithRegionModel
38+
Entry fwtypes.ListNestedObjectValueOf[putEventEntryModel] `tfsdk:"entry"`
39+
}
40+
41+
type putEventEntryModel struct {
42+
Detail types.String `tfsdk:"detail"`
43+
DetailType types.String `tfsdk:"detail_type"`
44+
EventBusName types.String `tfsdk:"event_bus_name"`
45+
Resources fwtypes.ListValueOf[types.String] `tfsdk:"resources"`
46+
Source types.String `tfsdk:"source"`
47+
Time timetypes.RFC3339 `tfsdk:"time"`
48+
}
49+
50+
func (a *putEventsAction) Schema(ctx context.Context, req action.SchemaRequest, resp *action.SchemaResponse) {
51+
resp.Schema = schema.Schema{
52+
Description: "Sends custom events to Amazon EventBridge so that they can be matched to rules.",
53+
Blocks: map[string]schema.Block{
54+
"entry": schema.ListNestedBlock{
55+
Description: "The entry that defines an event in your system.",
56+
CustomType: fwtypes.NewListNestedObjectTypeOf[putEventEntryModel](ctx),
57+
NestedObject: schema.NestedBlockObject{
58+
Attributes: map[string]schema.Attribute{
59+
"detail": schema.StringAttribute{
60+
Description: "A valid JSON string. There is no other schema imposed.",
61+
Optional: true,
62+
},
63+
"detail_type": schema.StringAttribute{
64+
Description: "Free-form string used to decide what fields to expect in the event detail.",
65+
Optional: true,
66+
},
67+
"event_bus_name": schema.StringAttribute{
68+
Description: "The name or ARN of the event bus to receive the event.",
69+
Optional: true,
70+
},
71+
names.AttrResources: schema.ListAttribute{
72+
Description: "AWS resources, identified by Amazon Resource Name (ARN), which the event primarily concerns.",
73+
CustomType: fwtypes.ListOfStringType,
74+
Optional: true,
75+
},
76+
names.AttrSource: schema.StringAttribute{
77+
Description: "The source of the event.",
78+
Required: true,
79+
},
80+
"time": schema.StringAttribute{
81+
Description: "The time stamp of the event, per RFC3339.",
82+
Optional: true,
83+
CustomType: timetypes.RFC3339Type{},
84+
},
85+
},
86+
},
87+
},
88+
},
89+
}
90+
}
91+
92+
func (a *putEventsAction) Invoke(ctx context.Context, req action.InvokeRequest, resp *action.InvokeResponse) {
93+
var model putEventsActionModel
94+
95+
resp.Diagnostics.Append(req.Config.Get(ctx, &model)...)
96+
if resp.Diagnostics.HasError() {
97+
return
98+
}
99+
100+
conn := a.Meta().EventsClient(ctx)
101+
102+
tflog.Info(ctx, "Putting events", map[string]any{
103+
"entry_count": len(model.Entry.Elements()),
104+
})
105+
106+
resp.SendProgress(action.InvokeProgressEvent{
107+
Message: "Putting events to EventBridge...",
108+
})
109+
110+
var input eventbridge.PutEventsInput
111+
resp.Diagnostics.Append(fwflex.Expand(ctx, model, &input)...)
112+
if resp.Diagnostics.HasError() {
113+
return
114+
}
115+
116+
output, err := conn.PutEvents(ctx, &input)
117+
if err != nil {
118+
resp.Diagnostics.AddError(
119+
"Putting Events",
120+
"Could not put events: "+err.Error(),
121+
)
122+
return
123+
}
124+
125+
if output.FailedEntryCount > 0 {
126+
resp.Diagnostics.AddError(
127+
"Putting Events",
128+
strconv.Itoa(int(output.FailedEntryCount))+" entries failed to be processed",
129+
)
130+
return
131+
}
132+
133+
resp.SendProgress(action.InvokeProgressEvent{
134+
Message: "Events put successfully",
135+
})
136+
137+
tflog.Info(ctx, "Put events completed", map[string]any{
138+
"successful_entries": len(output.Entries),
139+
})
140+
}

0 commit comments

Comments
 (0)