Skip to content

Commit

Permalink
Add AWS API Task State
Browse files Browse the repository at this point in the history
  • Loading branch information
mweagle committed Oct 22, 2021
1 parent 7ab5b99 commit e129c4c
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
65 changes: 65 additions & 0 deletions aws/step/aws_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package step

import (
"fmt"
"math/rand"
)

// DynamoDBGetItemState represents bindings for
// https://docs.aws.amazon.com/step-functions/latest/dg/connectors-ddb.html
type AWSSDKState struct {
BaseTask
serviceName string
apiAction string
serviceIntegrationPattern string
parameters map[string]interface{}
}

// MarshalJSON for custom marshalling, since this will be stringified and we need it
// to turn into a stringified
// Ref: https://aws.amazon.com/blogs/aws/now-aws-step-functions-supports-200-aws-services-to-enable-easier-workflow-automation/
func (awsstate *AWSSDKState) MarshalJSON() ([]byte, error) {
resourceURL := fmt.Sprintf("arn:aws:states:::aws-sdk:%s:%s",
awsstate.serviceName,
awsstate.apiAction)
if awsstate.serviceIntegrationPattern != "" {
resourceURL += fmt.Sprintf(".[%s]", awsstate.serviceIntegrationPattern)
}
return awsstate.BaseTask.marshalMergedParams(resourceURL, &awsstate.parameters)
}

// NewAWSSDKState returns an initialized AWSSDKState state
func NewAWSSDKState(stateName string,
serviceName string,
apiAction string,
serviceIntegrationPattern string,
parameters map[string]interface{}) *AWSSDKState {

return NewAWSSDKIntegrationState(stateName,
serviceName,
apiAction,
"",
parameters)
}

// NewAWSSDKIntegrationState returns an initialized AWSSDKState state
func NewAWSSDKIntegrationState(stateName string,
serviceName string,
apiAction string,
serviceIntegrationPattern string,
parameters map[string]interface{}) *AWSSDKState {

awssdk := &AWSSDKState{
serviceName: serviceName,
apiAction: apiAction,
serviceIntegrationPattern: serviceIntegrationPattern,
BaseTask: BaseTask{
baseInnerState: baseInnerState{
name: stateName,
id: rand.Int63(),
},
},
parameters: parameters,
}
return awssdk
}
24 changes: 24 additions & 0 deletions aws/step/aws_api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package step

import (
"testing"

gof "github.com/awslabs/goformation/v5/cloudformation"
sparta "github.com/mweagle/Sparta"
)

func TestAWSAPIStepFunction(t *testing.T) {
awsGetMetadataState := NewAWSSDKState("getMetadata",
"s3",
"headObject",
"",
map[string]interface{}{
"Bucket": "weagle",
"Key.$": "$.SampleDataInputKey",
})
startMachine := NewStateMachine("AWSAPIMachine", awsGetMetadataState).WithRoleArn(gof.GetAtt("StepMachineRole", "Arn"))

testStepProvision(t,
[]*sparta.LambdaAWSInfo{},
startMachine)
}

0 comments on commit e129c4c

Please sign in to comment.