-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created icav2 statechange handler construct
Logs and raises an internal event
- Loading branch information
Showing
3 changed files
with
188 additions
and
6 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
lib/workload/components/dynamodb-icav2-handle-event-change-sfn/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { Construct } from 'constructs'; | ||
import * as sfn from 'aws-cdk-lib/aws-stepfunctions'; | ||
import * as dynamodb from 'aws-cdk-lib/aws-dynamodb'; | ||
import * as path from 'path'; | ||
import * as events from 'aws-cdk-lib/aws-events'; | ||
import * as events_targets from 'aws-cdk-lib/aws-events-targets'; | ||
|
||
export interface Icav2AnalysisEventHandlerConstructProps { | ||
/* Names of objects to get */ | ||
tableName: string; // Name of the table to get / update / query | ||
|
||
/* Names of objects to create */ | ||
stateMachineName: string; // Name of the state machine to create | ||
|
||
/* Event configurations to push to */ | ||
detailType: string; // Detail type of the event to raise | ||
eventBusName: string; // Detail of the eventbus to push the event to | ||
source: string; // Source of the event we push | ||
|
||
/* Event filter rule */ | ||
pipelineId: string; | ||
} | ||
|
||
export class Icav2AnalysisEventHandlerConstruct extends Construct { | ||
public readonly stateMachineObj: sfn.StateMachine; | ||
|
||
constructor(scope: Construct, id: string, props: Icav2AnalysisEventHandlerConstructProps) { | ||
super(scope, id); | ||
|
||
// Get table object | ||
const table_obj = dynamodb.TableV2.fromTableName(this, 'table_obj', props.tableName); | ||
|
||
// Get the event bus object | ||
const eventbus_obj = events.EventBus.fromEventBusName( | ||
this, | ||
'orcabus_eventbus_obj', | ||
props.eventBusName | ||
); | ||
|
||
// Build state machine object | ||
this.stateMachineObj = new sfn.StateMachine(this, 'state_machine', { | ||
stateMachineName: props.stateMachineName, | ||
definitionBody: sfn.DefinitionBody.fromFile( | ||
path.join( | ||
__dirname, | ||
'step_functions_templates/icav2_get_workflow_status_and_raise_internal_event.asl.json' | ||
) | ||
), | ||
definitionSubstitutions: { | ||
/* Table object */ | ||
__table_name__: table_obj.tableName, | ||
/* Event metadata */ | ||
__detail_type__: props.detailType, | ||
__eventbus_name__: props.eventBusName, | ||
__eventsource__: props.source, | ||
}, | ||
}); | ||
|
||
/* Grant the state machine read and write access to the table */ | ||
table_obj.grantReadWriteData(this.stateMachineObj); | ||
|
||
// Create a rule for this state machine | ||
const rule = new events.Rule(this, 'rule', { | ||
eventBus: eventbus_obj, | ||
ruleName: `${props.stateMachineName}-rule`, | ||
eventPattern: { | ||
source: [props.source], | ||
detailType: [props.detailType], | ||
detail: { | ||
'pipeline.id': props.pipelineId, | ||
}, | ||
}, | ||
}); | ||
|
||
/* Add rule as a target to the state machine */ | ||
rule.addTarget( | ||
new events_targets.SfnStateMachine(this.stateMachineObj, { | ||
input: events.RuleTargetInput.fromEventPath('$.detail'), | ||
}) | ||
); | ||
|
||
/* Grant the state machine the ability to submit events to the event bus */ | ||
eventbus_obj.grantPutEventsTo(this.stateMachineObj.role); | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
...-sfn/step_functions_templates/icav2_get_workflow_status_and_raise_internal_event.asl.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
{ | ||
"Comment": "Handle icav2 state change", | ||
"StartAt": "Move event detail", | ||
"States": { | ||
"Move event detail": { | ||
"Type": "Pass", | ||
"Next": "DynamoDB Get UUID from ICAv2 Analysis ID", | ||
"Parameters": { | ||
"event_detail.$": "$" | ||
} | ||
}, | ||
"DynamoDB Get UUID from ICAv2 Analysis ID": { | ||
"Type": "Task", | ||
"Resource": "arn:aws:states:::dynamodb:getItem", | ||
"Parameters": { | ||
"TableName": "${__table_name__}", | ||
"Key": { | ||
"id.$": "$.event_detail.id", | ||
"id_type": "icav2_analysis_id" | ||
} | ||
}, | ||
"Next": "Check Analysis ID in DataBase", | ||
"ResultPath": "$.get_analysis_id_in_db_step", | ||
"ResultSelector": { | ||
"db_response.$": "$" | ||
} | ||
}, | ||
"Check Analysis ID in DataBase": { | ||
"Type": "Choice", | ||
"Choices": [ | ||
{ | ||
"Not": { | ||
"Variable": "$.get_analysis_id_in_db_step.db_response.Item", | ||
"IsPresent": true | ||
}, | ||
"Next": "Success" | ||
} | ||
], | ||
"Default": "DynamoDB Update Status" | ||
}, | ||
"DynamoDB Update Status": { | ||
"Type": "Task", | ||
"Resource": "arn:aws:states:::dynamodb:updateItem", | ||
"Parameters": { | ||
"TableName": "${__table_name__}", | ||
"Key": { | ||
"id.$": "$.get_analysis_id_in_db_step.db_response.db_uuid", | ||
"id_type": "db_uuid" | ||
}, | ||
"UpdateExpression": "SET status = :status", | ||
"ExpressionAttributeValues": { | ||
":status": { | ||
"S.$": "$.get_analysis_id_in_db_step.db_response.status" | ||
} | ||
} | ||
}, | ||
"Next": "Wait 1 Second", | ||
"ResultPath": "$.update_analysis_id_in_db_step" | ||
}, | ||
"Wait 1 Second": { | ||
"Type": "Wait", | ||
"Seconds": 1, | ||
"Next": "DynamoDB Get UUID Row" | ||
}, | ||
"DynamoDB Get UUID Row": { | ||
"Type": "Task", | ||
"Resource": "arn:aws:states:::dynamodb:getItem", | ||
"Parameters": { | ||
"TableName": "${__table_name__}", | ||
"Key": { | ||
"id.$": "$.get_analysis_id_in_db_step.db_response.db_uuid", | ||
"id_type": "db_uuid" | ||
} | ||
}, | ||
"Next": "PutEvent", | ||
"ResultPath": "$.update_analysis_id_in_db_step" | ||
}, | ||
"PutEvent": { | ||
"Type": "Task", | ||
"Resource": "arn:aws:states:::events:putEvents", | ||
"Parameters": { | ||
"Entries": [ | ||
{ | ||
"Detail.$": "$.update_analysis_id_in_db_step", | ||
"DetailType": "${__detail_type__}", | ||
"EventBusName": "${__eventbus_name__}", | ||
"Source": "${__eventsource__}" | ||
} | ||
] | ||
}, | ||
"Next": "Success" | ||
}, | ||
"Success": { | ||
"Type": "Succeed" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters