Skip to content

Commit

Permalink
feat(stepfunctions-task): add CodeBuild stop-build, batch-get-reports…
Browse files Browse the repository at this point in the history
…, batch-delete-builds
  • Loading branch information
Sumeet Badyal authored and NovakGu committed Feb 18, 2021
1 parent 1ce7df8 commit cbefa3e
Show file tree
Hide file tree
Showing 13 changed files with 633 additions and 0 deletions.
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,
}),
};
}
}
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,
}),
};
}
}
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,
}),
};
}
}
3 changes: 3 additions & 0 deletions packages/@aws-cdk/aws-stepfunctions-tasks/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
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'],
},
});
});
});
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'],
},
});
});
});
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"
}
}
}
}
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();
Loading

0 comments on commit cbefa3e

Please sign in to comment.