Skip to content

Commit

Permalink
Add coverage/integ test & refactor using EKS L2 Construct
Browse files Browse the repository at this point in the history
  • Loading branch information
NovakGu committed Feb 4, 2021
1 parent fb639aa commit b831188
Show file tree
Hide file tree
Showing 7 changed files with 1,691 additions and 79 deletions.
15 changes: 12 additions & 3 deletions packages/@aws-cdk/aws-stepfunctions-tasks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -678,11 +678,20 @@ The service integration APIs correspond to Amazon EKS APIs.
Read and write Kubernetes resource objects via a Kubernetes API endpoint.
Corresponds to the [`call`](https://docs.aws.amazon.com/step-functions/latest/dg/connect-eks.html) API in Step Functions Connector.

The following code snippet includes a Task state that uses eks:call to list the pods.

```ts
import * as eks from '@aws-cdk/aws-eks';
import * as sfn from '@aws-cdk/aws-stepfunctions';
import * as tasks from '@aws-cdk/aws-stepfunctions-tasks';

const myEksCluster = new eks.Cluster(this, 'my sample cluster', {
version: eks.KubernetesVersion.V1_18,
clusterName: 'myEksCluster',
});

new tasks.EksCall(stack, 'Call a EKS Endpoint', {
clusterName: 'clusterName',
certificateAuthority: 'certificateAuthority',
endpoint: 'https://apiid.gr7.us-east-1.eks.amazonaws.com',
cluster: myEksCluster,
httpMethod: MethodType.GET,
path: '/api/v1/namespaces/default/pods',
});
Expand Down
33 changes: 13 additions & 20 deletions packages/@aws-cdk/aws-stepfunctions-tasks/lib/eks/call.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,29 @@
import * as eks from '@aws-cdk/aws-eks';
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 calling a EKS endpoint with EksCall
* @experimental
*/
export interface EksCallProps extends sfn.TaskStateBaseProps {

/**
* Name of the cluster
* The EKS cluster
*/
readonly clusterName: string;

/**
* Base 64 encoded certificate data required to communicate with your cluster
*/
readonly certificateAuthority: string;

/**
* API endpoint to communicate with your cluster
*/
readonly endpoint: string;
readonly cluster: eks.ICluster;

/**
* HTTP method ("GET", "POST", "PUT", ...) part of HTTP request
*/
readonly httpMethod: MethodType;
readonly httpMethod: HttpMethods;

/**
* Path of cluster
* HTTP path of the Kubernetes REST API operation
*/
readonly path: string;
readonly httpPath: string;

/**
* Query Parameters part of HTTP request
Expand All @@ -50,6 +42,7 @@ export interface EksCallProps extends sfn.TaskStateBaseProps {
* Call a EKS endpoint as a Task
*
* @see https://docs.aws.amazon.com/step-functions/latest/dg/connect-eks.html
* @experimental
*/
export class EksCall extends sfn.TaskStateBase {

Expand Down Expand Up @@ -77,11 +70,11 @@ export class EksCall extends sfn.TaskStateBase {
return {
Resource: integrationResourceArn('eks', 'call', this.integrationPattern),
Parameters: sfn.FieldUtils.renderObject({
ClusterName: this.props.clusterName,
CertificateAuthority: this.props.certificateAuthority,
Endpoint: this.props.endpoint,
ClusterName: this.props.cluster.clusterName,
CertificateAuthority: this.props.cluster.clusterCertificateAuthorityData,
Endpoint: this.props.cluster.clusterEndpoint,
Method: this.props.httpMethod,
Path: this.props.path,
Path: this.props.httpPath,
QueryParameters: this.props.queryParameters,
RequestBody: this.props.requestBody,
}),
Expand All @@ -92,7 +85,7 @@ export class EksCall extends sfn.TaskStateBase {
/**
* Method type of a EKS call
*/
export enum MethodType {
export enum HttpMethods {
/**
* Retrieve data from a server at the specified resource
*/
Expand Down
1 change: 0 additions & 1 deletion packages/@aws-cdk/aws-stepfunctions-tasks/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export * from './dynamodb/update-item';
export * from './dynamodb/delete-item';
export * from './dynamodb/shared-types';
export * from './codebuild/start-build';
export * from './eks/call';
export * from './athena/start-query-execution';
export * from './athena/stop-query-execution';
export * from './athena/get-query-execution';
Expand Down
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-stepfunctions-tasks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"@aws-cdk/aws-ecr": "0.0.0",
"@aws-cdk/aws-ecr-assets": "0.0.0",
"@aws-cdk/aws-ecs": "0.0.0",
"@aws-cdk/aws-eks": "0.0.0",
"@aws-cdk/aws-glue": "0.0.0",
"@aws-cdk/aws-iam": "0.0.0",
"@aws-cdk/aws-kms": "0.0.0",
Expand All @@ -103,6 +104,7 @@
"@aws-cdk/aws-ecr": "0.0.0",
"@aws-cdk/aws-ecr-assets": "0.0.0",
"@aws-cdk/aws-ecs": "0.0.0",
"@aws-cdk/aws-eks": "0.0.0",
"@aws-cdk/aws-glue": "0.0.0",
"@aws-cdk/aws-iam": "0.0.0",
"@aws-cdk/aws-kms": "0.0.0",
Expand Down
135 changes: 92 additions & 43 deletions packages/@aws-cdk/aws-stepfunctions-tasks/test/eks/call.test.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,103 @@
import * as eks from '@aws-cdk/aws-eks';
import * as sfn from '@aws-cdk/aws-stepfunctions';
import * as cdk from '@aws-cdk/core';
import { EksCall, MethodType } from '../../lib/eks/call';
import { Stack } from '@aws-cdk/core';
import { EksCall, HttpMethods } from '../../lib/eks/call';

describe('Call an EKS endpoint', () => {
let stack: Stack;
let cluster: eks.Cluster;

test('default settings', () => {
// GIVEN
const stack = new cdk.Stack();
beforeEach(() => {
//GIVEN
stack = new Stack();
cluster = new eks.Cluster(stack, 'Cluster', {
version: eks.KubernetesVersion.V1_18,
clusterName: 'eksCluster',
});
});

// WHEN
const task = new EksCall(stack, 'Call', {
clusterName: 'clusterName',
certificateAuthority: 'certificateAuthority',
endpoint: 'endpoint',
httpMethod: MethodType.GET,
path: 'path',
requestBody: sfn.TaskInput.fromObject({
RequestBody: 'requestBody',
}),
});
test('Call an EKS endpoint', () => {
// WHEN
const task = new EksCall(stack, 'Call', {
cluster: cluster,
httpMethod: HttpMethods.GET,
httpPath: 'path',
requestBody: sfn.TaskInput.fromObject({
RequestBody: 'requestBody',
}),
});

// THEN
expect(stack.resolve(task.toStateJson())).toEqual({
Type: 'Task',
Resource: {
'Fn::Join': [
'',
[
'arn:',
{
Ref: 'AWS::Partition',
},
':states:::eks:call',
],
// THEN
expect(stack.resolve(task.toStateJson())).toEqual({
Type: 'Task',
Resource: {
'Fn::Join': [
'',
[
'arn:',
{
Ref: 'AWS::Partition',
},
':states:::eks:call',
],
],
},
End: true,
Parameters: {
ClusterName: {
Ref: 'Cluster9EE0221C',
},
End: true,
Parameters: {
ClusterName: 'clusterName',
CertificateAuthority: 'certificateAuthority',
Endpoint: 'endpoint',
Method: 'GET',
Path: 'path',
RequestBody: {
type: 1,
value: {
RequestBody: 'requestBody',
},
CertificateAuthority: {
'Fn::GetAtt': [
'Cluster9EE0221C',
'CertificateAuthorityData',
],
},
Endpoint: {
'Fn::GetAtt': [
'Cluster9EE0221C',
'Endpoint',
],
},
Method: 'GET',
Path: 'path',
RequestBody: {
type: 1,
value: {
RequestBody: 'requestBody',
},
},
});
},
});
});

test('Task throws if RUN_JOB is supplied as service integration pattern', () => {
expect(() => {
new EksCall(stack, 'Call', {
cluster: cluster,
httpMethod: HttpMethods.GET,
httpPath: 'path',
requestBody: sfn.TaskInput.fromObject({
RequestBody: 'requestBody',
}),
integrationPattern: sfn.IntegrationPattern.RUN_JOB,
});
}).toThrow(
/Unsupported service integration pattern. Supported Patterns: REQUEST_RESPONSE. Received: RUN_JOB/,
);
});

test('Task throws if WAIT_FOR_TASK_TOKEN is supplied as service integration pattern', () => {
expect(() => {
new EksCall(stack, 'Call', {
cluster: cluster,
httpMethod: HttpMethods.GET,
httpPath: 'path',
requestBody: sfn.TaskInput.fromObject({
RequestBody: 'requestBody',
}),
integrationPattern: sfn.IntegrationPattern.WAIT_FOR_TASK_TOKEN,
});
}).toThrow(
/Unsupported service integration pattern. Supported Patterns: REQUEST_RESPONSE. Received: WAIT_FOR_TASK_TOKEN/,
);
});
Loading

0 comments on commit b831188

Please sign in to comment.