-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathaws.service.js
188 lines (155 loc) · 5.24 KB
/
aws.service.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
var AWS = require('aws-sdk');
var config = require('./config');
// configure the aws sdk
AWS.config.update(
{
accessKeyId: config.AWS.ACCESSKEY,
secretAccessKey: config.AWS.SECRETKEY,
region: 'us-east-1'
}
);
// aws services declarations
var ec2 = new AWS.EC2();
var elb = new AWS.ELB();
var s3 = new AWS.S3();
var rds = new AWS.RDS();
var elasticache = new AWS.ElastiCache();
var route53 = new AWS.Route53();
// query the aws api and expose methods for jobs
module.exports = {
/// EC2 ///
getEC2InstanceLimit: function (next) {
ec2.describeAccountAttributes(function(err, data){
if (err) return next(err, null);
data.AccountAttributes.forEach(function(attr){
if (attr.AttributeName === "max-instances") {
next(null, attr.AttributeValues[0].AttributeValue);
}
});
});
},
getEC2Instances: function (next){
ec2.describeInstances(function(err, data){
if (err) return next(err, null);
next(null, data.Reservations);
});
},
getElasticIPsLimit: function (next) {
ec2.describeAccountAttributes(function(err, data){
if (err) return next(err, null);
data.AccountAttributes.forEach(function(attr){
if (attr.AttributeName === "max-elastic-ips") {
next(null, attr.AttributeValues[0].AttributeValue);
}
});
});
},
getElasticIPs: function (next) {
ec2.describeAddresses(function(err, data) {
if (err) return next(err, null);
next(null, data.Addresses);
});
},
getSecurityGroups: function (next) {
ec2.describeSecurityGroups(function(err, data) {
if (err) return next(err, null);
next(null, data.SecurityGroups);
});
},
// EBS //
getEBSVolumes: function (next) {
ec2.describeVolumes(function(err, data){
if (err) return next(err, null);
next(null, data.Volumes);
});
},
getEBSSnapshots: function (next) {
ec2.describeSnapshots(function (err, data) {
if (err) return next(err, null);
next(null, data.Snapshots);
});
},
/// ELB ///
getELBs: function (next) {
elb.describeLoadBalancers(function(err, data) {
if (err) return next(err, null);
next(null, data.LoadBalancerDescriptions);
});
},
/// S3 ///
getS3Buckets: function (next) {
s3.listBuckets(function(err, data) {
if (err) return next(err, null);
next(null, data.Buckets);
});
},
getS3Objects: function (bucket_name, next) {
var listS3Objects = function (bucket_name, index, objects, next) {
s3.listObjects({Bucket:bucket_name, Marker:index}, function (err, data) {
if (err) return next(err, null);
var length = data.Contents.length
objects += length;
if (data.IsTruncated == true){
listS3Objects(bucket_name, data.Contents[length-1].Key, objects, next);
} else {
next(null, objects);
}
})
};
listS3Objects(bucket_name, "", 0, next);
},
/// RDS ///
getRDSInstances: function (next) {
rds.describeDBInstances(function(err, data) {
if (err) return next(err, null);
next(null, data.DBInstances);
});
},
/// ElastiCache ///
getElastiCacheClusters: function (next) {
elasticache.describeCacheClusters(function (err, data) {
if (err) return next(err, null);
next(null, data.CacheClusters);
});
},
getElastiCacheNodes: function (next) {
elasticache.describeReservedCacheNodes(function(err, data) {
if (err) return next(err, null);
next(null, data.ReservedCacheNodes);
});
},
getElastiCacheSecurityGroups: function (next) {
elasticache.describeCacheSecurityGroups(function (err, data) {
if (err) return next(err, null);
next(null, data.CacheSecurityGroups);
});
},
/// R53 ///
getR53HostedZones: function (next) {
route53.listHostedZones(function(err, data) {
if (err) return next(err, null);
next(null, data.HostedZones);
});
},
getR53RecordsSet: function (hosted_zones_id, next) {
route53.listResourceRecordSets({HostedZoneId: hosted_zones_id}, function (err, data){
if (err) return next(err, null);
next(null, data.ResourceRecordSets);
});
}
}
/*
route53.listHostedZones(function(err, data) {
if (err) console.log(err, err.stack);
else console.log('hosted zones: ' + data.HostedZones.length);
send_event('hz', {current: data.HostedZones.length })
var records = 0
data.HostedZones.forEach(function(hz){
route53.listResourceRecordSets({HostedZoneId: hz.Id}, function (err, data){
if (err) console.log(err, err.stack);
else records += data.ResourceRecordSets.length
send_event('record', {current: records })
})
});
});
*/