-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaws.gs
182 lines (171 loc) · 4.55 KB
/
aws.gs
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
/*
* basic AWS client library for Google Apps Script
*/
var AWS = {
_config: {},
config: function(config) {
if(config) this._config = config;
return this._config;
}
};
AWS.services = {
EC2: {
actions: [
'DescribeRegions',
'DescribeInstances',
'DescribeImages',
'DescribeAvailabilityZones',
'DescribeHosts',
'DescribeInstanceAttribute',
'DescribeTags'
],
apiVersion: '2016-11-15',
endpointPrefix: 'ec2'
},
ECS: {
actions: [
'DescribeClusters',
'DescribeContainerInstances',
'DescribeServices',
'DescribeTaskDefinition',
'DescribeTasks',
'ListAttributes',
'ListClusters',
'ListContainerInstances',
'ListServices',
'ListTaskDefinitionFamilies',
'ListTaskDefinitions',
'ListTasks'
],
apiVersion: '2014-11-13',
endpointPrefix: 'ecs'
},
RDS: {
actions: [
'DescribeAccountAttributes',
'DescribeDBClusters',
'DescribeDBInstances',
'DescribeDBParameterGroups',
'DescribeDBParameters',
'DescribeSourceRegions',
'ListTagsForResource'
],
apiVersion: '2014-10-31',
endpointPrefix: 'ec2'
},
CostExplorer: {
actions: [
'GetCostAndUsage',
'GetDimensionValues',
'GetReservationCoverage',
'GetReservationPurchaseRecommendation'
],
apiVersion: '2017-10-25',
endpointPrefix: 'costexplorer'
},
Pricing: {
actions: [
'DescribeServices',
'GetAttributeValues',
'GetProducts'
],
apiVersion: '2017-10-15',
endpointPrefix: 'pricing'
}
};
AWS.Service = function(){};
AWS.Service.prototype.callApi = function(service, action, params, callback){
var config = AWS.config();
var accessKeyId = config.accessKeyId;
var secretAccessKey = config.secretAccessKey;
var region = config.region;
var method = 'GET';
params.Action = action;
params.Version = service.apiVersion;
var path = '?' + this.buildQueryString(params).sort().join('&');
var host = service.endpointPrefix + '.' + region + '.amazonaws.com';
var url = 'https://' + host + path;
var sign = new AWSSign({
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey,
region: region,
body: '',
host: host,
method: method,
url: url,
serviceName: service.endpointPrefix
});
var headers = sign.toHeaders();
var http_params = {
method: method,
payload: "",
headers: headers,
muteHttpExceptions: true
}
Logger.log(url);
Logger.log(http_params);
var response = UrlFetchApp.fetch(url, http_params);
var xml = response.getContentText();
var json = this.xmlToJson(xml);
callback(null, json); // TODO: error handling
};
AWS.Service.prototype.xmlToJson = function(xml){
var doc = XmlService.parse(xml);
var result = {};
var root = doc.getRootElement();
result = this.elementToJson(root);
return result;
};
AWS.Service.prototype.elementToJson = function(element){
var result = null;
var self = this;
element.getChildren().forEach(function(child) {
var key = child.getName();
if(result === null){
result = (key == 'item' || key == 'member') ? [] : {};
}
var value = self.elementToJson(child);
if (result instanceof Array) {
result.push(value);
} else {
result[key] = value;
}
});
var text = element.getText();
if (text) {
text = text.trim();
if(text !== '') result = text;
}
return result;
};
AWS.Service.prototype.buildQueryString = function (params, result, keyString) {
if(typeof result === 'undefined') result = [];
var suffix = (typeof keyString === 'undefined') ? '' : keyString + '.';
if(params instanceof Array){
for(var i=0;i<params.length;i++){
this.buildQueryString(params[i], result, suffix + (i+1));
}
}else if(params instanceof Object){
var self = this;
Object.keys(params).forEach(function (key) {
self.buildQueryString(params[key], result, suffix + key);
});
}else{
result.push(encodeURIComponent(keyString) + '=' + encodeURIComponent(params));
}
return result;
}
Object.keys(AWS.services).forEach(function(name){
AWS[name] = function(){};
AWS[name].prototype = new AWS.Service();
var service = AWS.services[name];
for(var i=0;i<service.actions.length;i++){
var action = service.actions[i];
var method = action.charAt(0).toLowerCase() + action.slice(1);
AWS[name].prototype[method] = (function(service, action){
return function(params, callback){
this.callApi(service, action, params, callback);
};
})(service, action);
}
});