-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Step Function] 1. Set up skeleton code (#144)
* [Step Function] 1. Set up skeleton code
- Loading branch information
Showing
4 changed files
with
120 additions
and
5 deletions.
There are no files selected for viewing
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
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,43 @@ | ||
import { InputEvent, OutputEvent, SUCCESS, Resources } from "../types"; | ||
import log from "loglevel"; | ||
import { StateMachine, StateMachineProperties } from "step_function/types"; | ||
|
||
const STATE_MACHINE_RESOURCE_TYPE = "AWS::StepFunctions::StateMachine"; | ||
|
||
export async function instrumentStateMachines(event: InputEvent): Promise<OutputEvent> { | ||
const fragment = event.fragment; | ||
const resources = fragment.Resources; | ||
|
||
const stateMachines = findStateMachines(resources); | ||
for (const stateMachine of stateMachines) { | ||
instrumentStateMachine(resources, stateMachine); | ||
} | ||
|
||
return { | ||
requestId: event.requestId, | ||
status: SUCCESS, | ||
fragment, | ||
}; | ||
} | ||
|
||
function instrumentStateMachine(resources: Resources, stateMachine: StateMachine): void { | ||
log.debug(`Instrumenting State Machine ${stateMachine.resourceKey}`); | ||
} | ||
|
||
export function findStateMachines(resources: Resources): StateMachine[] { | ||
return Object.entries(resources) | ||
.map(([key, resource]) => { | ||
if (resource.Type !== STATE_MACHINE_RESOURCE_TYPE) { | ||
log.debug(`Resource ${key} is not a State Machine, skipping...`); | ||
return; | ||
} | ||
|
||
const properties: StateMachineProperties = resource.Properties; | ||
|
||
return { | ||
properties: properties, | ||
resourceKey: key, | ||
} as StateMachine; | ||
}) | ||
.filter((resource) => resource !== undefined) as StateMachine[]; | ||
} |
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,33 @@ | ||
// Parsed state machine info from CloudFormation template. For internal use. | ||
// Not aimed to match any CloudFormation type. | ||
export interface StateMachine { | ||
properties: StateMachineProperties; | ||
resourceKey: string; | ||
} | ||
|
||
// Necessary fields from AWS::StepFunctions::StateMachine's Properties field | ||
export interface StateMachineProperties { | ||
LoggingConfiguration?: LoggingConfiguration; | ||
RoleArn?: string | { [key: string]: any }; | ||
} | ||
|
||
// Matches AWS::StepFunctions::StateMachine LoggingConfiguration | ||
export interface LoggingConfiguration { | ||
Destinations?: LogDestination[]; | ||
IncludeExecutionData?: boolean; | ||
Level?: string; | ||
} | ||
|
||
// Matches AWS::StepFunctions::StateMachine LogDestination | ||
export interface LogDestination { | ||
CloudWatchLogsLogGroup: CloudWatchLogsLogGroup; | ||
} | ||
|
||
// Matches AWS::StepFunctions::StateMachine CloudWatchLogsLogGroup | ||
export interface CloudWatchLogsLogGroup { | ||
LogGroupArn: | ||
| string | ||
| { | ||
"Fn::GetAtt": string[]; | ||
}; | ||
} |
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,41 @@ | ||
import { findStateMachines } from "../../src/step_function/step_function"; | ||
|
||
describe("findStateMachines", () => { | ||
it("returns an empty array when no state machines are present", () => { | ||
const resources = { | ||
SomeOtherResource: { | ||
Type: "AWS::Lambda::Function", | ||
Properties: {}, | ||
}, | ||
}; | ||
|
||
const result = findStateMachines(resources); | ||
expect(result).toEqual([]); | ||
}); | ||
|
||
it("returns an array with state machines when they are present", () => { | ||
const resources = { | ||
FirstStateMachine: { | ||
Type: "AWS::StepFunctions::StateMachine", | ||
Properties: { | ||
DefinitionUri: "state_machine/first.asl.json", | ||
}, | ||
}, | ||
SecondStateMachine: { | ||
Type: "AWS::StepFunctions::StateMachine", | ||
Properties: { | ||
DefinitionUri: "state_machine/second.asl.json", | ||
}, | ||
}, | ||
SomeOtherResource: { | ||
Type: "AWS::Lambda::Function", | ||
Properties: {}, | ||
}, | ||
}; | ||
|
||
const result = findStateMachines(resources); | ||
expect(result).toHaveLength(2); | ||
expect(result[0].resourceKey).toBe("FirstStateMachine"); | ||
expect(result[1].resourceKey).toBe("SecondStateMachine"); | ||
}); | ||
}); |