This repository has been archived by the owner on Mar 20, 2023. It is now read-only.
forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.ts
93 lines (78 loc) · 3.88 KB
/
common.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// eslint-disable-next-line import/no-extraneous-dependencies
import { IsCompleteResponse, OnEventResponse } from '@aws-cdk/custom-resources/lib/provider-framework/types';
// eslint-disable-next-line import/no-extraneous-dependencies
import * as aws from 'aws-sdk';
export interface EksUpdateId {
/**
* If this field is included in an event passed to "IsComplete", it means we
* initiated an EKS update that should be monitored using eks:DescribeUpdate
* instead of just looking at the cluster status.
*/
EksUpdateId?: string
}
export type ResourceEvent = AWSLambda.CloudFormationCustomResourceEvent & EksUpdateId;
export abstract class ResourceHandler {
protected readonly requestId: string;
protected readonly logicalResourceId: string;
protected readonly requestType: 'Create' | 'Update' | 'Delete';
protected readonly physicalResourceId?: string;
protected readonly event: ResourceEvent;
constructor(protected readonly eks: EksClient, event: ResourceEvent) {
this.requestType = event.RequestType;
this.requestId = event.RequestId;
this.logicalResourceId = event.LogicalResourceId;
this.physicalResourceId = (event as any).PhysicalResourceId;
this.event = event;
const roleToAssume = event.ResourceProperties.AssumeRoleArn;
if (!roleToAssume) {
throw new Error('AssumeRoleArn must be provided');
}
eks.configureAssumeRole({
RoleArn: roleToAssume,
RoleSessionName: `AWSCDK.EKSCluster.${this.requestType}.${this.requestId}`,
});
}
public onEvent() {
// eslint-disable-next-line @typescript-eslint/no-require-imports, import/no-extraneous-dependencies
const ProxyAgent: any = require('proxy-agent');
aws.config.update({
httpOptions: { agent: new ProxyAgent() },
});
switch (this.requestType) {
case 'Create': return this.onCreate();
case 'Update': return this.onUpdate();
case 'Delete': return this.onDelete();
}
throw new Error(`Invalid request type ${this.requestType}`);
}
public isComplete() {
switch (this.requestType) {
case 'Create': return this.isCreateComplete();
case 'Update': return this.isUpdateComplete();
case 'Delete': return this.isDeleteComplete();
}
throw new Error(`Invalid request type ${this.requestType}`);
}
protected log(x: any) {
// eslint-disable-next-line no-console
console.log(JSON.stringify(x, undefined, 2));
}
protected abstract async onCreate(): Promise<OnEventResponse>;
protected abstract async onDelete(): Promise<OnEventResponse | void>;
protected abstract async onUpdate(): Promise<(OnEventResponse & EksUpdateId) | void>;
protected abstract async isCreateComplete(): Promise<IsCompleteResponse>;
protected abstract async isDeleteComplete(): Promise<IsCompleteResponse>;
protected abstract async isUpdateComplete(): Promise<IsCompleteResponse>;
}
export interface EksClient {
configureAssumeRole(request: aws.STS.AssumeRoleRequest): void;
createCluster(request: aws.EKS.CreateClusterRequest): Promise<aws.EKS.CreateClusterResponse>;
deleteCluster(request: aws.EKS.DeleteClusterRequest): Promise<aws.EKS.DeleteClusterResponse>;
describeCluster(request: aws.EKS.DescribeClusterRequest): Promise<aws.EKS.DescribeClusterResponse>;
updateClusterConfig(request: aws.EKS.UpdateClusterConfigRequest): Promise<aws.EKS.UpdateClusterConfigResponse>;
updateClusterVersion(request: aws.EKS.UpdateClusterVersionRequest): Promise<aws.EKS.UpdateClusterVersionResponse>;
describeUpdate(req: aws.EKS.DescribeUpdateRequest): Promise<aws.EKS.DescribeUpdateResponse>;
createFargateProfile(request: aws.EKS.CreateFargateProfileRequest): Promise<aws.EKS.CreateFargateProfileResponse>;
describeFargateProfile(request: aws.EKS.DescribeFargateProfileRequest): Promise<aws.EKS.DescribeFargateProfileResponse>;
deleteFargateProfile(request: aws.EKS.DeleteFargateProfileRequest): Promise<aws.EKS.DeleteFargateProfileResponse>;
}