forked from aws/aws-cdk
-
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.
feat(stepfunctions-task): add CodeBuild stop-build, batch-get-reports…
…, batch-delete-builds
- Loading branch information
Showing
13 changed files
with
633 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
packages/@aws-cdk/aws-stepfunctions-tasks/lib/codebuild/batch-delete-builds.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,52 @@ | ||
import * as iam from '@aws-cdk/aws-iam'; | ||
import * as sfn from '@aws-cdk/aws-stepfunctions'; | ||
import { Construct } from 'constructs'; | ||
import { integrationResourceArn, validatePatternSupported } from '../private/task-utils'; | ||
|
||
/** Properties for CodeBuildBatchDeleteBuilds */ | ||
export interface CodeBuildBatchDeleteBuildsProps extends sfn.TaskStateBaseProps { | ||
/** An array of ids of builds to delete. */ | ||
readonly ids: string[]; | ||
} | ||
|
||
/** | ||
* Delete one or more builds as a task | ||
* | ||
* @see https://docs.aws.amazon.com/step-functions/latest/dg/connect-codebuild.html | ||
*/ | ||
export class CodeBuildBatchDeleteBuilds extends sfn.TaskStateBase { | ||
private static readonly SUPPORTED_INTEGRATION_PATTERNS: sfn.IntegrationPattern[] = [ | ||
sfn.IntegrationPattern.REQUEST_RESPONSE, | ||
]; | ||
|
||
protected readonly taskMetrics?: sfn.TaskMetricsConfig; | ||
protected readonly taskPolicies?: iam.PolicyStatement[]; | ||
|
||
private readonly integrationPattern: sfn.IntegrationPattern; | ||
|
||
constructor(scope: Construct, id: string, private readonly props: CodeBuildBatchDeleteBuildsProps) { | ||
super(scope, id, props); | ||
this.integrationPattern = props.integrationPattern ?? sfn.IntegrationPattern.REQUEST_RESPONSE; | ||
|
||
validatePatternSupported(this.integrationPattern, CodeBuildBatchDeleteBuilds.SUPPORTED_INTEGRATION_PATTERNS); | ||
|
||
this.taskPolicies = [ | ||
new iam.PolicyStatement({ | ||
resources: ['*'], | ||
actions: ['codebuild:BatchDeleteBuilds'], | ||
}), | ||
]; | ||
} | ||
/** | ||
* Provides the CodeBuild BatchDeleteBuilds service integration task configuration | ||
* @internal | ||
*/ | ||
protected _renderTask(): any { | ||
return { | ||
Resource: integrationResourceArn('codebuild', 'batchDeleteBuilds', this.integrationPattern), | ||
Parameters: sfn.FieldUtils.renderObject({ | ||
Ids: this.props.ids, | ||
}), | ||
}; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
packages/@aws-cdk/aws-stepfunctions-tasks/lib/codebuild/batch-get-reports.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,52 @@ | ||
import * as iam from '@aws-cdk/aws-iam'; | ||
import * as sfn from '@aws-cdk/aws-stepfunctions'; | ||
import { Construct } from 'constructs'; | ||
import { integrationResourceArn, validatePatternSupported } from '../private/task-utils'; | ||
|
||
/**Properties for CodeBuildBatchGetReports */ | ||
export interface CodeBuildBatchGetReportsProps extends sfn.TaskStateBaseProps { | ||
/** An array of ARNs that identify the Reports. */ | ||
readonly reportArns: string[]; | ||
} | ||
|
||
/** | ||
* Return an array of reports as a task | ||
* | ||
* @see https://docs.aws.amazon.com/step-functions/latest/dg/connect-codebuild.html | ||
*/ | ||
export class CodeBuildBatchGetReports extends sfn.TaskStateBase { | ||
private static readonly SUPPORTED_INTEGRATION_PATTERNS: sfn.IntegrationPattern[] = [ | ||
sfn.IntegrationPattern.REQUEST_RESPONSE, | ||
]; | ||
|
||
protected readonly taskMetrics?: sfn.TaskMetricsConfig; | ||
protected readonly taskPolicies?: iam.PolicyStatement[]; | ||
|
||
private readonly integrationPattern: sfn.IntegrationPattern; | ||
|
||
constructor(scope: Construct, id: string, private readonly props: CodeBuildBatchGetReportsProps) { | ||
super(scope, id, props); | ||
this.integrationPattern = props.integrationPattern ?? sfn.IntegrationPattern.REQUEST_RESPONSE; | ||
|
||
validatePatternSupported(this.integrationPattern, CodeBuildBatchGetReports.SUPPORTED_INTEGRATION_PATTERNS); | ||
|
||
this.taskPolicies = [ | ||
new iam.PolicyStatement({ | ||
resources: ['*'], | ||
actions: ['codebuild:BatchGetReports'], | ||
}), | ||
]; | ||
} | ||
/** | ||
* Provides the CodeBuild BatchGetReports service integration task configuration | ||
* @internal | ||
*/ | ||
protected _renderTask(): any { | ||
return { | ||
Resource: integrationResourceArn('codebuild', 'batchGetReports', this.integrationPattern), | ||
Parameters: sfn.FieldUtils.renderObject({ | ||
ReportArns: this.props.reportArns, | ||
}), | ||
}; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
packages/@aws-cdk/aws-stepfunctions-tasks/lib/codebuild/stop-build.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,52 @@ | ||
import * as iam from '@aws-cdk/aws-iam'; | ||
import * as sfn from '@aws-cdk/aws-stepfunctions'; | ||
import { Construct } from 'constructs'; | ||
import { integrationResourceArn, validatePatternSupported } from '../private/task-utils'; | ||
|
||
/** Properties for CodeBuildStopBuild*/ | ||
export interface CodeBuildStopBuildProps extends sfn.TaskStateBaseProps { | ||
/**CodeBuild project Id to stop */ | ||
readonly projectId: string; | ||
} | ||
|
||
/** | ||
* Stop a CodeBuild Build as a task | ||
* | ||
* @see https://docs.aws.amazon.com/step-functions/latest/dg/connect-codebuild.html | ||
*/ | ||
export class CodeBuildStopBuild extends sfn.TaskStateBase { | ||
private static readonly SUPPORTED_INTEGRATION_PATTERNS: sfn.IntegrationPattern[] = [ | ||
sfn.IntegrationPattern.REQUEST_RESPONSE, | ||
]; | ||
|
||
protected readonly taskMetrics?: sfn.TaskMetricsConfig; | ||
protected readonly taskPolicies?: iam.PolicyStatement[]; | ||
|
||
private readonly integrationPattern: sfn.IntegrationPattern; | ||
|
||
constructor(scope: Construct, id: string, private readonly props: CodeBuildStopBuildProps) { | ||
super(scope, id, props); | ||
this.integrationPattern = props.integrationPattern ?? sfn.IntegrationPattern.REQUEST_RESPONSE; | ||
|
||
validatePatternSupported(this.integrationPattern, CodeBuildStopBuild.SUPPORTED_INTEGRATION_PATTERNS); | ||
|
||
this.taskPolicies = [ | ||
new iam.PolicyStatement({ | ||
resources: ['*'], | ||
actions: ['codebuild:StopBuild'], | ||
}), | ||
]; | ||
} | ||
/** | ||
* Provides the CodeBuild StopBuild service integration task configuration | ||
* @internal | ||
*/ | ||
protected _renderTask(): any { | ||
return { | ||
Resource: integrationResourceArn('codebuild', 'stopBuild', this.integrationPattern), | ||
Parameters: sfn.FieldUtils.renderObject({ | ||
Id: this.props.projectId, | ||
}), | ||
}; | ||
} | ||
} |
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
36 changes: 36 additions & 0 deletions
36
packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/batch-delete-builds.test.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,36 @@ | ||
import * as cdk from '@aws-cdk/core'; | ||
import { CodeBuildBatchDeleteBuilds } from '../../lib/codebuild/batch-delete-builds'; | ||
|
||
describe('Delete Builds', () => { | ||
|
||
test('default settings', () => { | ||
// GIVEN | ||
const stack = new cdk.Stack(); | ||
|
||
// WHEN | ||
const task = new CodeBuildBatchDeleteBuilds(stack, 'Delete Build', { | ||
ids: ['CodeBuildId'], | ||
}); | ||
|
||
// THEN | ||
expect(stack.resolve(task.toStateJson())).toEqual({ | ||
Type: 'Task', | ||
Resource: { | ||
'Fn::Join': [ | ||
'', | ||
[ | ||
'arn:', | ||
{ | ||
Ref: 'AWS::Partition', | ||
}, | ||
':states:::codebuild:batchDeleteBuilds', | ||
], | ||
], | ||
}, | ||
End: true, | ||
Parameters: { | ||
Ids: ['CodeBuildId'], | ||
}, | ||
}); | ||
}); | ||
}); |
36 changes: 36 additions & 0 deletions
36
packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/batch-get-reports.test.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,36 @@ | ||
import * as cdk from '@aws-cdk/core'; | ||
import { CodeBuildBatchGetReports } from '../../lib/codebuild/batch-get-reports'; | ||
|
||
describe('Get Reports', () => { | ||
|
||
test('default settings', () => { | ||
// GIVEN | ||
const stack = new cdk.Stack(); | ||
|
||
// WHEN | ||
const task = new CodeBuildBatchGetReports(stack, 'Get Report', { | ||
reportArns: ['reportArn'], | ||
}); | ||
|
||
// THEN | ||
expect(stack.resolve(task.toStateJson())).toEqual({ | ||
Type: 'Task', | ||
Resource: { | ||
'Fn::Join': [ | ||
'', | ||
[ | ||
'arn:', | ||
{ | ||
Ref: 'AWS::Partition', | ||
}, | ||
':states:::codebuild:batchGetReports', | ||
], | ||
], | ||
}, | ||
End: true, | ||
Parameters: { | ||
ReportArns: ['reportArn'], | ||
}, | ||
}); | ||
}); | ||
}); |
87 changes: 87 additions & 0 deletions
87
...s/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/integ.batch-delete-builds.expected.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,87 @@ | ||
{ | ||
"Resources": { | ||
"StateMachineRoleB840431D": { | ||
"Type": "AWS::IAM::Role", | ||
"Properties": { | ||
"AssumeRolePolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": "sts:AssumeRole", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": { | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
"states.", | ||
{ | ||
"Ref": "AWS::Region" | ||
}, | ||
".amazonaws.com" | ||
] | ||
] | ||
} | ||
} | ||
} | ||
], | ||
"Version": "2012-10-17" | ||
} | ||
} | ||
}, | ||
"StateMachineRoleDefaultPolicyDF1E6607": { | ||
"Type": "AWS::IAM::Policy", | ||
"Properties": { | ||
"PolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": "codebuild:BatchDeleteBuilds", | ||
"Effect": "Allow", | ||
"Resource": "*" | ||
} | ||
], | ||
"Version": "2012-10-17" | ||
}, | ||
"PolicyName": "StateMachineRoleDefaultPolicyDF1E6607", | ||
"Roles": [ | ||
{ | ||
"Ref": "StateMachineRoleB840431D" | ||
} | ||
] | ||
} | ||
}, | ||
"StateMachine2E01A3A5": { | ||
"Type": "AWS::StepFunctions::StateMachine", | ||
"Properties": { | ||
"RoleArn": { | ||
"Fn::GetAtt": [ | ||
"StateMachineRoleB840431D", | ||
"Arn" | ||
] | ||
}, | ||
"DefinitionString": { | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
"{\"StartAt\":\"batch-delete-builds\",\"States\":{\"batch-delete-builds\":{\"End\":true,\"Type\":\"Task\",\"Resource\":\"arn:", | ||
{ | ||
"Ref": "AWS::Partition" | ||
}, | ||
":states:::codebuild:batchDeleteBuilds\",\"Parameters\":{\"Ids\":[\"ids\"]}}}}" | ||
] | ||
] | ||
} | ||
}, | ||
"DependsOn": [ | ||
"StateMachineRoleDefaultPolicyDF1E6607", | ||
"StateMachineRoleB840431D" | ||
] | ||
} | ||
}, | ||
"Outputs": { | ||
"StateMachineArn": { | ||
"Value": { | ||
"Ref": "StateMachine2E01A3A5" | ||
} | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/integ.batch-delete-builds.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,35 @@ | ||
import * as sfn from '@aws-cdk/aws-stepfunctions'; | ||
import * as cdk from '@aws-cdk/core'; | ||
import * as tasks from '../../lib'; | ||
|
||
/* | ||
* Stack verification steps: | ||
* * aws stepfunctions start-execution --state-machine-arn <deployed state machine arn> : should return execution arn | ||
* * aws codebuild list-builds-for-project --project-name <deployed project name>: should return a list of projects with size greater than 0 | ||
* * | ||
* * aws codebuild batch-get-builds --ids <build id returned by list-builds-for-project> --query 'builds[0].buildStatus': wait until the status is 'SUCCEEDED' | ||
* * aws stepfunctions describe-execution --execution-arn <exection-arn generated before> --query 'status': should return status as SUCCEEDED | ||
*/ | ||
class StartBuildStack extends cdk.Stack { | ||
constructor(scope: cdk.App, id: string, props: cdk.StackProps = {}) { | ||
super(scope, id, props); | ||
|
||
const batchDeleteBuildsJob = new tasks.CodeBuildBatchDeleteBuilds(this, 'batch-delete-builds', { | ||
ids: ['ids'], | ||
}); | ||
|
||
const definition = sfn.Chain.start(batchDeleteBuildsJob); | ||
|
||
const stateMachine = new sfn.StateMachine(this, 'StateMachine', { | ||
definition, | ||
}); | ||
|
||
new cdk.CfnOutput(this, 'StateMachineArn', { | ||
value: stateMachine.stateMachineArn, | ||
}); | ||
} | ||
} | ||
|
||
const app = new cdk.App(); | ||
new StartBuildStack(app, 'aws-stepfunctions-tasks-codebuild-batch-delete-builds-integ'); | ||
app.synth(); |
Oops, something went wrong.