-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
http-call.ts
68 lines (62 loc) · 2.49 KB
/
http-call.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { Aspects, CfnOutput, CustomResource, Lazy, Token } from 'aws-cdk-lib';
import { Construct, IConstruct } from 'constructs';
import { ApiCallBase, IApiCall } from './api-call-base';
import { ExpectedResult } from './common';
import { AssertionsProvider, HttpRequestParameters, HTTP_RESOURCE_TYPE } from './providers';
import { WaiterStateMachine, WaiterStateMachineOptions } from './waiter-state-machine';
/**
* Options for creating an HttpApiCall provider
*/
export interface HttpCallProps extends HttpRequestParameters { }
/**
* Construct that creates a custom resource that will perform
* an HTTP API Call
*/
export class HttpApiCall extends ApiCallBase {
protected readonly apiCallResource: CustomResource;
public readonly provider: AssertionsProvider;
constructor(scope: Construct, id: string, props: HttpCallProps) {
super(scope, id);
let name = '';
if (!Token.isUnresolved(props.url)) {
const url = new URL(props.url);
name = `${url.hostname}${url.pathname}`.replace(/\/|\.|:/g, '');
}
this.provider = new AssertionsProvider(this, 'HttpProvider');
this.apiCallResource = new CustomResource(this, 'Default', {
serviceToken: this.provider.serviceToken,
properties: {
parameters: props,
expected: Lazy.any({ produce: () => this.expectedResult }),
stateMachineArn: Lazy.string({ produce: () => this.stateMachineArn }),
salt: Date.now().toString(),
},
resourceType: `${HTTP_RESOURCE_TYPE}${name}`.substring(0, 60),
});
// Needed so that all the policies set up by the provider should be available before the custom resource is provisioned.
this.apiCallResource.node.addDependency(this.provider);
Aspects.of(this).add({
visit(node: IConstruct) {
if (node instanceof HttpApiCall) {
if (node.expectedResult) {
const result = node.apiCallResource.getAttString('assertion');
new CfnOutput(node, 'AssertionResults', {
value: result,
}).overrideLogicalId(`AssertionResults${id}`);
}
}
},
});
}
public assertAtPath(_path: string, _expected: ExpectedResult): IApiCall {
return this;
}
public waitForAssertions(options?: WaiterStateMachineOptions | undefined): IApiCall {
const waiter = new WaiterStateMachine(this, 'WaitFor', {
...options,
});
this.stateMachineArn = waiter.stateMachineArn;
this.provider.addPolicyStatementFromSdkCall('states', 'StartExecution');
return this;
};
}