-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAwsEC2Service.js
91 lines (80 loc) · 2.23 KB
/
AwsEC2Service.js
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
const _ = require('lodash');
const AWS = require('aws-sdk');
function AwsEC2Service(credentials) {
AWS.config.update(credentials);
this._ec2 = promisifyMethods(new AWS.EC2());
}
AwsEC2Service.prototype.listVolumesWithTag = async function(tagName) {
const response = await this._ec2.describeVolumes({
Filters: [
{Name: 'tag-key', Values: [tagName]}
]
});
return response.Volumes;
};
AwsEC2Service.prototype.createSnapshot = async function(volumeId, description) {
return this._ec2.createSnapshot({
VolumeId: volumeId,
Description: description,
DryRun: false
});
};
AwsEC2Service.prototype.getSnapshot = async function(snapshotId) {
const response = await this._ec2.describeSnapshots({
Filters: [
{Name: 'snapshot-id', Values: [snapshotId]}
]
});
return _.first(response.Snapshots);
};
AwsEC2Service.prototype.copySnapshot = async function(snapshotId, sourceRegion, destinationRegion, description) {
return this._ec2.copySnapshot({
SourceSnapshotId: snapshotId,
SourceRegion: sourceRegion,
DestinationRegion: destinationRegion,
Description: description,
DryRun: false
});
};
AwsEC2Service.prototype.listSnapshotsWithTag = async function(tagName) {
const response = await this._ec2.describeSnapshots({
Filters: [
{Name: 'tag-key', Values: [tagName]}
]
});
return response.Snapshots;
};
AwsEC2Service.prototype.deleteSnapshot = async function(snapshotId) {
return this._ec2.deleteSnapshot({
SnapshotId: snapshotId,
DryRun: false
});
};
AwsEC2Service.prototype.createTags = async function(resources, tags) {
return this._ec2.createTags({
Resources: [].concat(resources),
Tags: tags,
DryRun: false
});
};
function promisifyMethods(obj) {
return _.mapValues(_.pick(obj, _.functionsIn(obj)), function(method) {
return promisify(method.bind(obj));
});
}
function promisify(fn) {
return function() {
const context = this;
const args = _.toArray(arguments);
return new Promise(function(resolve, reject) {
fn.apply(context, args.concat(function(error, result) {
if (error) {
reject(error);
} else {
resolve(result);
}
}));
});
};
}
module.exports = AwsEC2Service;