From cbefa3ea9635c48561f098a47657218d293e0c0c Mon Sep 17 00:00:00 2001 From: Sumeet Badyal Date: Thu, 17 Dec 2020 13:02:26 -0800 Subject: [PATCH] feat(stepfunctions-task): add CodeBuild stop-build, batch-get-reports, batch-delete-builds --- .../lib/codebuild/batch-delete-builds.ts | 52 +++++++++++ .../lib/codebuild/batch-get-reports.ts | 52 +++++++++++ .../lib/codebuild/stop-build.ts | 52 +++++++++++ .../aws-stepfunctions-tasks/lib/index.ts | 3 + .../codebuild/batch-delete-builds.test.ts | 36 ++++++++ .../test/codebuild/batch-get-reports.test.ts | 36 ++++++++ .../integ.batch-delete-builds.expected.json | 87 +++++++++++++++++++ .../codebuild/integ.batch-delete-builds.ts | 35 ++++++++ .../integ.batch-get-reports.expected.json | 87 +++++++++++++++++++ .../test/codebuild/integ.batch-get-reports.ts | 35 ++++++++ .../codebuild/integ.stop-build.expected.json | 87 +++++++++++++++++++ .../test/codebuild/integ.stop-build.ts | 35 ++++++++ .../test/codebuild/stop-build.test.ts | 36 ++++++++ 13 files changed, 633 insertions(+) create mode 100644 packages/@aws-cdk/aws-stepfunctions-tasks/lib/codebuild/batch-delete-builds.ts create mode 100644 packages/@aws-cdk/aws-stepfunctions-tasks/lib/codebuild/batch-get-reports.ts create mode 100644 packages/@aws-cdk/aws-stepfunctions-tasks/lib/codebuild/stop-build.ts create mode 100644 packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/batch-delete-builds.test.ts create mode 100644 packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/batch-get-reports.test.ts create mode 100644 packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/integ.batch-delete-builds.expected.json create mode 100644 packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/integ.batch-delete-builds.ts create mode 100644 packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/integ.batch-get-reports.expected.json create mode 100644 packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/integ.batch-get-reports.ts create mode 100644 packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/integ.stop-build.expected.json create mode 100644 packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/integ.stop-build.ts create mode 100644 packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/stop-build.test.ts diff --git a/packages/@aws-cdk/aws-stepfunctions-tasks/lib/codebuild/batch-delete-builds.ts b/packages/@aws-cdk/aws-stepfunctions-tasks/lib/codebuild/batch-delete-builds.ts new file mode 100644 index 0000000000000..e7b7cbf80cb1a --- /dev/null +++ b/packages/@aws-cdk/aws-stepfunctions-tasks/lib/codebuild/batch-delete-builds.ts @@ -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, + }), + }; + } +} diff --git a/packages/@aws-cdk/aws-stepfunctions-tasks/lib/codebuild/batch-get-reports.ts b/packages/@aws-cdk/aws-stepfunctions-tasks/lib/codebuild/batch-get-reports.ts new file mode 100644 index 0000000000000..0829c999b5ccd --- /dev/null +++ b/packages/@aws-cdk/aws-stepfunctions-tasks/lib/codebuild/batch-get-reports.ts @@ -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, + }), + }; + } +} diff --git a/packages/@aws-cdk/aws-stepfunctions-tasks/lib/codebuild/stop-build.ts b/packages/@aws-cdk/aws-stepfunctions-tasks/lib/codebuild/stop-build.ts new file mode 100644 index 0000000000000..76ddeb6523db7 --- /dev/null +++ b/packages/@aws-cdk/aws-stepfunctions-tasks/lib/codebuild/stop-build.ts @@ -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, + }), + }; + } +} diff --git a/packages/@aws-cdk/aws-stepfunctions-tasks/lib/index.ts b/packages/@aws-cdk/aws-stepfunctions-tasks/lib/index.ts index 84b790beff216..364d287dee85d 100644 --- a/packages/@aws-cdk/aws-stepfunctions-tasks/lib/index.ts +++ b/packages/@aws-cdk/aws-stepfunctions-tasks/lib/index.ts @@ -44,3 +44,6 @@ export * from './athena/stop-query-execution'; export * from './athena/get-query-execution'; export * from './athena/get-query-results'; export * from './databrew/start-job-run'; +export * from './codebuild/stop-build'; +export * from './codebuild/batch-delete-builds'; +export * from './codebuild/batch-get-reports'; diff --git a/packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/batch-delete-builds.test.ts b/packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/batch-delete-builds.test.ts new file mode 100644 index 0000000000000..b2fb30c2d2777 --- /dev/null +++ b/packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/batch-delete-builds.test.ts @@ -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'], + }, + }); + }); +}); \ No newline at end of file diff --git a/packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/batch-get-reports.test.ts b/packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/batch-get-reports.test.ts new file mode 100644 index 0000000000000..87a27ac7d98aa --- /dev/null +++ b/packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/batch-get-reports.test.ts @@ -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'], + }, + }); + }); +}); \ No newline at end of file diff --git a/packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/integ.batch-delete-builds.expected.json b/packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/integ.batch-delete-builds.expected.json new file mode 100644 index 0000000000000..9d61ccc9cb46c --- /dev/null +++ b/packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/integ.batch-delete-builds.expected.json @@ -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" + } + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/integ.batch-delete-builds.ts b/packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/integ.batch-delete-builds.ts new file mode 100644 index 0000000000000..2049300ba4095 --- /dev/null +++ b/packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/integ.batch-delete-builds.ts @@ -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 : should return execution arn + * * aws codebuild list-builds-for-project --project-name : should return a list of projects with size greater than 0 + * * + * * aws codebuild batch-get-builds --ids --query 'builds[0].buildStatus': wait until the status is 'SUCCEEDED' + * * aws stepfunctions describe-execution --execution-arn --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(); diff --git a/packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/integ.batch-get-reports.expected.json b/packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/integ.batch-get-reports.expected.json new file mode 100644 index 0000000000000..2ccee66ea0683 --- /dev/null +++ b/packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/integ.batch-get-reports.expected.json @@ -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:BatchGetReports", + "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-get-reports\",\"States\":{\"batch-get-reports\":{\"End\":true,\"Type\":\"Task\",\"Resource\":\"arn:", + { + "Ref": "AWS::Partition" + }, + ":states:::codebuild:batchGetReports\",\"Parameters\":{\"ReportArns\":[\"reportArns\"]}}}}" + ] + ] + } + }, + "DependsOn": [ + "StateMachineRoleDefaultPolicyDF1E6607", + "StateMachineRoleB840431D" + ] + } + }, + "Outputs": { + "StateMachineArn": { + "Value": { + "Ref": "StateMachine2E01A3A5" + } + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/integ.batch-get-reports.ts b/packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/integ.batch-get-reports.ts new file mode 100644 index 0000000000000..366462bb2acc7 --- /dev/null +++ b/packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/integ.batch-get-reports.ts @@ -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 : should return execution arn + * * aws codebuild list-builds-for-project --project-name : should return a list of projects with size greater than 0 + * * + * * aws codebuild batch-get-builds --ids --query 'builds[0].buildStatus': wait until the status is 'SUCCEEDED' + * * aws stepfunctions describe-execution --execution-arn --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 batchGetReportsJob = new tasks.CodeBuildBatchGetReports(this, 'batch-get-reports', { + reportArns: ['reportArns'], + }); + + const definition = sfn.Chain.start(batchGetReportsJob); + + 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-get-reports-integ'); +app.synth(); diff --git a/packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/integ.stop-build.expected.json b/packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/integ.stop-build.expected.json new file mode 100644 index 0000000000000..c39d6bca7d9e1 --- /dev/null +++ b/packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/integ.stop-build.expected.json @@ -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:StopBuild", + "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\":\"stop-build\",\"States\":{\"stop-build\":{\"End\":true,\"Type\":\"Task\",\"Resource\":\"arn:", + { + "Ref": "AWS::Partition" + }, + ":states:::codebuild:stopBuild\",\"Parameters\":{\"Id.$\":\"$.projectId\"}}}}" + ] + ] + } + }, + "DependsOn": [ + "StateMachineRoleDefaultPolicyDF1E6607", + "StateMachineRoleB840431D" + ] + } + }, + "Outputs": { + "StateMachineArn": { + "Value": { + "Ref": "StateMachine2E01A3A5" + } + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/integ.stop-build.ts b/packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/integ.stop-build.ts new file mode 100644 index 0000000000000..99edcee498955 --- /dev/null +++ b/packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/integ.stop-build.ts @@ -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 : should return execution arn + * * aws codebuild list-builds-for-project --project-name : should return a list of projects with size greater than 0 + * * + * * aws codebuild batch-get-builds --ids --query 'builds[0].buildStatus': wait until the status is 'SUCCEEDED' + * * aws stepfunctions describe-execution --execution-arn --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 stopBuildJob = new tasks.CodeBuildStopBuild(this, 'stop-build', { + projectId: sfn.JsonPath.stringAt('$.projectId'), + }); + + const definition = sfn.Chain.start(stopBuildJob); + + 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-stop-build-integ'); +app.synth(); diff --git a/packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/stop-build.test.ts b/packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/stop-build.test.ts new file mode 100644 index 0000000000000..6eba92c59c8da --- /dev/null +++ b/packages/@aws-cdk/aws-stepfunctions-tasks/test/codebuild/stop-build.test.ts @@ -0,0 +1,36 @@ +import * as cdk from '@aws-cdk/core'; +import { CodeBuildStopBuild } from '../../lib/codebuild/stop-build'; + +describe('Stop Build', () => { + + test('default settings', () => { + // GIVEN + const stack = new cdk.Stack(); + + // WHEN + const task = new CodeBuildStopBuild(stack, 'Stop Build', { + projectId: 'CodeBuildProject', + }); + + // THEN + expect(stack.resolve(task.toStateJson())).toEqual({ + Type: 'Task', + Resource: { + 'Fn::Join': [ + '', + [ + 'arn:', + { + Ref: 'AWS::Partition', + }, + ':states:::codebuild:stopBuild', + ], + ], + }, + End: true, + Parameters: { + Id: 'CodeBuildProject', + }, + }); + }); +}); \ No newline at end of file