-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
resource.ts
144 lines (127 loc) · 3.23 KB
/
resource.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import * as dynamodb from '@aws-cdk/aws-dynamodb';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as efs from '@aws-cdk/aws-efs';
import * as rds from '@aws-cdk/aws-rds';
import { Stack } from '@aws-cdk/core';
import { Construct } from 'constructs';
// v2 - keep this import as a separate section to reduce merge conflict when forward merging with the v2 branch.
// eslint-disable-next-line
import { Construct as CoreConstruct } from '@aws-cdk/core';
/**
* An operation that is applied to a key-value pair
*/
export enum TagOperation {
/**
* StringEquals
*/
STRING_EQUALS = 'STRINGEQUALS',
/**
* Dummy member
*/
DUMMY = 'dummy'
}
/**
* A tag condition
*/
export interface TagCondition {
/**
* The key in a key-value pair.
*
* For example, in `"ec2:ResourceTag/Department": "accounting"`,
* `ec2:ResourceTag/Department` is the key.
*/
readonly key: string;
/**
* An operation that is applied to a key-value pair used to filter
* resources in a selection.
*
* @default STRING_EQUALS
*/
readonly operation?: TagOperation;
/**
* The value in a key-value pair.
*
* For example, in `"ec2:ResourceTag/Department": "accounting"`,
* `accounting` is the value.
*/
readonly value: string;
}
/**
* A resource to backup
*/
export class BackupResource {
/**
* Adds all supported resources in a construct
*
* @param construct The construct containing resources to backup
*/
public static fromConstruct(construct: Construct) {
return new BackupResource(undefined, undefined, construct);
}
/**
* A DynamoDB table
*/
public static fromDynamoDbTable(table: dynamodb.ITable) {
return BackupResource.fromArn(table.tableArn);
}
/**
* An EC2 instance
*/
public static fromEc2Instance(instance: ec2.IInstance) {
return BackupResource.fromArn(Stack.of(instance).formatArn({
service: 'ec2',
resource: 'instance',
resourceName: instance.instanceId,
}));
}
/**
* An EFS file system
*/
public static fromEfsFileSystem(fileSystem: efs.IFileSystem) {
return BackupResource.fromArn(Stack.of(fileSystem).formatArn({
service: 'elasticfilesystem',
resource: 'file-system',
resourceName: fileSystem.fileSystemId,
}));
}
/**
* A RDS database instance
*/
public static fromRdsDatabaseInstance(instance: rds.IDatabaseInstance) {
return BackupResource.fromArn(instance.instanceArn);
}
/**
* A list of ARNs or match patterns such as
* `arn:aws:ec2:us-east-1:123456789012:volume/*`
*/
public static fromArn(arn: string) {
return new BackupResource(arn);
}
/**
* A tag condition
*/
public static fromTag(key: string, value: string, operation?: TagOperation) {
return new BackupResource(undefined, {
key,
value,
operation,
});
}
/**
* A resource
*/
public readonly resource?: string;
/**
* A condition on a tag
*/
public readonly tagCondition?: TagCondition;
/**
* A construct
*/
public readonly construct?: CoreConstruct;
constructor(resource?: string, tagCondition?: TagCondition, construct?: Construct) {
this.resource = resource;
this.tagCondition = tagCondition;
this.construct = construct as CoreConstruct;
}
}