Skip to content

Commit

Permalink
Refactor AWS CDK L3 Components (#52)
Browse files Browse the repository at this point in the history
* refactor cdk components

previous interface of CDK components was tuned for the simplicity
on creating standalone apps. The interface was not optimal for
integrating event handlers into large cdk app.
  • Loading branch information
fogfish authored Mar 23, 2024
1 parent 0efe5b8 commit 3990593
Show file tree
Hide file tree
Showing 20 changed files with 501 additions and 586 deletions.
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,16 +375,24 @@ package main

import (
"github.com/fogfish/scud"
"github.com/fogfish/swarm/queue/eventbridge"
"github.com/fogfish/swarm/broker/eventbridge"
)

func main() {
app := eventbridge.NewServerlessApp()
app := awscdk.NewApp(nil)
stack := awscdk.NewStack(app, jsii.String("swarm-example-eventbridge"),
&awscdk.StackProps{
Env: &awscdk.Environment{
Account: jsii.String(os.Getenv("CDK_DEFAULT_ACCOUNT")),
Region: jsii.String(os.Getenv("CDK_DEFAULT_REGION")),
},
},
)

stack := app.NewStack("swarm-example-eventbridge")
stack.NewEventBus()
broker := eventbridge.NewBroker(stack, jsii.String("Broker"), nil)
broker.NewEventBus(nil)

stack.NewSink(
broker.NewSink(
&eventbridge.SinkProps{
Source: []string{"swarm-example-eventbridge"},
Lambda: &scud.FunctionGoProps{
Expand Down
116 changes: 27 additions & 89 deletions broker/eventbridge/awscdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
package eventbridge

import (
"os"
"strconv"

"github.com/aws/aws-cdk-go/awscdk/v2"
Expand Down Expand Up @@ -94,117 +93,56 @@ func NewSink(scope constructs.Construct, id *string, props *SinkProps) *Sink {

//------------------------------------------------------------------------------
//
// AWS CDK Stack Construct
// AWS CDK Broker Construct
//
//------------------------------------------------------------------------------

type ServerlessStackProps struct {
*awscdk.StackProps
Version string
System string
type BrokerProps struct {
System string
}

type ServerlessStack struct {
awscdk.Stack
acc int
type Broker struct {
constructs.Construct
Bus awsevents.IEventBus
acc int
}

func NewServerlessStack(app awscdk.App, id *string, props *ServerlessStackProps) *ServerlessStack {
sid := *id
if props.Version != "" {
sid = sid + "-" + props.Version
}

stack := &ServerlessStack{
Stack: awscdk.NewStack(app, jsii.String(sid), props.StackProps),
}
func NewBroker(scope constructs.Construct, id *string, props *BrokerProps) *Broker {
broker := &Broker{Construct: constructs.NewConstruct(scope, id)}

return stack
return broker
}

func (stack *ServerlessStack) NewEventBus(eventBusName ...string) awsevents.IEventBus {
name := awscdk.Aws_STACK_NAME()
if len(eventBusName) > 0 {
name = &eventBusName[0]
func (broker *Broker) NewEventBus(props *awsevents.EventBusProps) awsevents.IEventBus {
if props == nil {
props = &awsevents.EventBusProps{}
}

stack.Bus = awsevents.NewEventBus(stack.Stack, jsii.String("Bus"),
&awsevents.EventBusProps{EventBusName: name},
)

return stack.Bus
}

func (stack *ServerlessStack) AddEventBus(eventBusName string) awsevents.IEventBus {
stack.Bus = awsevents.EventBus_FromEventBusName(stack.Stack, jsii.String("Bus"), jsii.String(eventBusName))

return stack.Bus
}

func (stack *ServerlessStack) NewSink(props *SinkProps) *Sink {
if stack.Bus == nil {
panic("EventBus is not defined.")
if props.EventBusName == nil {
props.EventBusName = awscdk.Aws_STACK_NAME()
}

props.System = stack.Bus
broker.Bus = awsevents.NewEventBus(broker.Construct, jsii.String("Bus"), props)

stack.acc++
name := "Sink" + strconv.Itoa(stack.acc)
sink := NewSink(stack.Stack, jsii.String(name), props)

return sink
return broker.Bus
}

//------------------------------------------------------------------------------
//
// AWS CDK App Construct
//
//------------------------------------------------------------------------------
func (broker *Broker) AddEventBus(eventBusName string) awsevents.IEventBus {
broker.Bus = awsevents.EventBus_FromEventBusName(broker.Construct, jsii.String("Bus"), jsii.String(eventBusName))

type ServerlessApp struct {
awscdk.App
return broker.Bus
}

func NewServerlessApp() *ServerlessApp {
app := awscdk.NewApp(nil)
return &ServerlessApp{App: app}
}

func (app *ServerlessApp) NewStack(name string, props ...*awscdk.StackProps) *ServerlessStack {
config := &awscdk.StackProps{
Env: &awscdk.Environment{
Account: jsii.String(os.Getenv("CDK_DEFAULT_ACCOUNT")),
Region: jsii.String(os.Getenv("CDK_DEFAULT_REGION")),
},
}

if len(props) == 1 {
config = props[0]
func (broker *Broker) NewSink(props *SinkProps) *Sink {
if broker.Bus == nil {
panic("EventBus is not defined.")
}

return NewServerlessStack(app.App, jsii.String(name), &ServerlessStackProps{
StackProps: config,
Version: FromContextVsn(app),
System: name,
})
}

func FromContext(app awscdk.App, key string) string {
val := app.Node().TryGetContext(jsii.String(key))
switch v := val.(type) {
case string:
return v
default:
return ""
}
}
props.System = broker.Bus

func FromContextVsn(app awscdk.App) string {
vsn := FromContext(app, "vsn")
if vsn == "" {
return "latest"
}
broker.acc++
name := "Sink" + strconv.Itoa(broker.acc)
sink := NewSink(broker.Construct, jsii.String(name), props)

return vsn
return sink
}
13 changes: 8 additions & 5 deletions broker/eventbridge/awscdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@ package eventbridge_test
import (
"testing"

"github.com/aws/aws-cdk-go/awscdk/v2"
"github.com/aws/aws-cdk-go/awscdk/v2/assertions"
"github.com/aws/jsii-runtime-go"
"github.com/fogfish/scud"
"github.com/fogfish/swarm/broker/eventbridge"
)

func TestEventBridgeCDK(t *testing.T) {
app := eventbridge.NewServerlessApp()
stack := app.NewStack("swarm-example-eventbridge", nil)
stack.NewEventBus()
app := awscdk.NewApp(nil)
stack := awscdk.NewStack(app, jsii.String("text"), &awscdk.StackProps{})

stack.NewSink(
broker := eventbridge.NewBroker(stack, jsii.String("Test"), nil)
broker.NewEventBus(nil)

broker.NewSink(
&eventbridge.SinkProps{
Source: []string{"swarm-example-eventbridge"},
Lambda: &scud.FunctionGoProps{
Expand All @@ -40,7 +43,7 @@ func TestEventBridgeCDK(t *testing.T) {
jsii.String("Custom::LogRetention"): jsii.Number(1),
}

template := assertions.Template_FromStack(stack.Stack, nil)
template := assertions.Template_FromStack(stack, nil)
for key, val := range require {
template.ResourceCountIs(key, val)
}
Expand Down
Loading

0 comments on commit 3990593

Please sign in to comment.