Class: Route
Constant Summary
collapse
- @@instance =
nil
Class Method Summary
collapse
Instance Method Summary
collapse
-
#add_zone(zone) ⇒ Object
-
#available_file(data) ⇒ Object
-
#cache_filename(data) ⇒ Object
-
#copy_zone(from, to) ⇒ Object
-
#customer_suffix(suffix) ⇒ Object
-
#delete_zone(id) ⇒ Object
-
#denormalize_group(group_arr) ⇒ Object
-
#denormalize_zone(zone) ⇒ Object
-
#each_record(group, &block) ⇒ Object
-
#get_all_zones(status = nil, zone_type = nil) ⇒ Object
-
#get_available(data) ⇒ Object
-
#get_available_hc_rm ⇒ Object
-
#get_available_hc_types ⇒ Object
-
#get_available_http_methods ⇒ Object
-
#get_available_ip_versions ⇒ Object
-
#get_available_record_types ⇒ Object
-
#get_available_route_groups ⇒ Object
-
#get_available_zone_statuses ⇒ Object
-
#get_available_zone_types ⇒ Object
-
#get_zone(id, name) ⇒ Object
-
#initialize ⇒ Route
constructor
-
#non_customer_suffix(suffix) ⇒ Object
-
#normalize_group(group_arr) ⇒ Object
-
#normalize_zone(zone) ⇒ Object
-
#translate_groups(group_array) ⇒ Object
-
#translate_hc(rec_array) ⇒ Object
-
#translate_value(data, hash, key) ⇒ Object
-
#translate_zone(zone) ⇒ Object
-
#translate_zonelist(zonelist) ⇒ Object
-
#untranslate_groups(group_array) ⇒ Object
-
#untranslate_hc(rec_array) ⇒ Object
-
#untranslate_value(data, hash, key) ⇒ Object
-
#untranslate_zone(zone) ⇒ Object
-
#update_zone(zone) ⇒ Object
-
#xlate_id(symbol, id) ⇒ Object
-
#xlate_name(symbol, name) ⇒ Object
#config_dir, #config_file, #mkdir, #param, #user_file
Constructor Details
#initialize ⇒ Route
Returns a new instance of Route.
18
19
20
21
22
23
24
25
|
# File 'lib/route.rb', line 18
def initialize
rest_base_url = param("rest_base_url")
if (rest_base_url.nil? || rest_base_url.length == 0)
raise "configuration file does not contain expected parameters -- try config init"
end
@common = CommonRestUtils.new(rest_base_url + "mcc/")
@availables = { }
end
|
Class Method Details
.do ⇒ Object
10
11
12
13
14
15
16
|
# File 'lib/route.rb', line 10
def self.do
if @@instance
@@instance
else
@@instance = Route.new
end
end
|
Instance Method Details
#add_zone(zone) ⇒ Object
218
219
220
221
|
# File 'lib/route.rb', line 218
def add_zone(zone)
suffix = customer_suffix("dns/routezone")
@common.callMethod(suffix, :post, zone)
end
|
#available_file(data) ⇒ Object
250
251
252
253
254
255
256
257
258
|
# File 'lib/route.rb', line 250
def available_file(data)
fname = cache_filename(data)
if File.exists?(fname)
days_old = (Time.new - File.mtime(fname)) / (24*60*60)
if days_old < 7.0
fname
end
end
end
|
#cache_filename(data) ⇒ Object
246
247
248
|
# File 'lib/route.rb', line 246
def cache_filename(data)
config_file(data.to_s + ".data")
end
|
#copy_zone(from, to) ⇒ Object
207
208
209
210
211
|
# File 'lib/route.rb', line 207
def copy_zone(from, to)
suffix = customer_suffix("dns/routezone/copy")
body = {:FromDomainName => from, :ToDomainName => to}
@common.callMethod(suffix, :post, body)
end
|
#customer_suffix(suffix) ⇒ Object
27
28
29
|
# File 'lib/route.rb', line 27
def customer_suffix(suffix)
"customers/" + param("acct-num") + "/" + suffix
end
|
#delete_zone(id) ⇒ Object
213
214
215
216
|
# File 'lib/route.rb', line 213
def delete_zone(id)
suffix = customer_suffix("dns/routezone/#{id}")
@common.callMethod(suffix, :delete)
end
|
#denormalize_group(group_arr) ⇒ Object
140
141
142
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/route.rb', line 140
def denormalize_group(group_arr)
return if group_arr.nil?
group_arr.each { |group|
ttl = group['TTL']
name = group['Name']
each_record(group) { |entry|
rdata = entry['Rdata']
entry.delete('Rdata')
entry['Record'] = { 'Name' => name, 'Rdata' => rdata, 'TTL' => ttl }
}
group.delete('TTL')
}
end
|
#denormalize_zone(zone) ⇒ Object
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/route.rb', line 154
def denormalize_zone(zone)
records = zone['Records']
empty = true
if records
records.keys.each { |key|
if records[key].length > 0
empty = false
break
end
}
if empty
zone.delete('Records')
end
end
denormalize_group(zone['LoadBalancingGroups'])
denormalize_group(zone['FailoverGroups'])
end
|
#each_record(group, &block) ⇒ Object
118
119
120
121
122
123
124
125
|
# File 'lib/route.rb', line 118
def each_record(group, &block)
group['Group'].each { |rtype, array|
array.each { |entry|
yield entry
}
}
end
|
#get_all_zones(status = nil, zone_type = nil) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/route.rb', line 35
def get_all_zones(status=nil, zone_type=nil)
suffix = "dns/routezones"
if status != nil && zone_type != nil
suffix = suffix + "?status=" + status + "&zonetype=" + zone_type
elsif status != nil
suffix = suffix + "?status=" + status
elsif zone_type != nil
suffix = suffix + "?zonetype=" + zone_type
end
@common.callMethod(customer_suffix(suffix), :get)
end
|
#get_available(data) ⇒ Object
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
# File 'lib/route.rb', line 260
def get_available(data)
if @availables[data]
@availables[data]
elsif available_file(data)
@availables[data] = JSON.parse File.read(cache_filename(data))
else
suffix =
{:zone_type => "routezonetypes",
:record_type => "routezonerecordtypes",
:zone_status => "routezonestatus",
:route_group => "routegrouptypes",
:http_method => "routehttpmethodtypes",
:ip_version => "routehealthcheckipversion",
:hc_type => "routehealthchecktypes",
:hc_rm => "routereintegrationmethodtypes"}[data]
result = @common.callMethod(non_customer_suffix(suffix), :get)
File.write(cache_filename(data), JSON.generate(result))
@availables[data] = result
end
end
|
#get_available_hc_rm ⇒ Object
309
310
311
|
# File 'lib/route.rb', line 309
def get_available_hc_rm()
get_available(:hc_rm)
end
|
#get_available_hc_types ⇒ Object
305
306
307
|
# File 'lib/route.rb', line 305
def get_available_hc_types()
get_available(:hc_type)
end
|
#get_available_http_methods ⇒ Object
297
298
299
|
# File 'lib/route.rb', line 297
def get_available_http_methods()
get_available(:http_method)
end
|
#get_available_ip_versions ⇒ Object
301
302
303
|
# File 'lib/route.rb', line 301
def get_available_ip_versions()
get_available(:ip_version)
end
|
#get_available_record_types ⇒ Object
285
286
287
|
# File 'lib/route.rb', line 285
def get_available_record_types()
get_available(:record_type)
end
|
#get_available_route_groups ⇒ Object
293
294
295
|
# File 'lib/route.rb', line 293
def get_available_route_groups()
get_available(:route_group)
end
|
#get_available_zone_statuses ⇒ Object
289
290
291
|
# File 'lib/route.rb', line 289
def get_available_zone_statuses()
get_available(:zone_status)
end
|
#get_available_zone_types ⇒ Object
281
282
283
|
# File 'lib/route.rb', line 281
def get_available_zone_types()
get_available(:zone_type)
end
|
#get_zone(id, name) ⇒ Object
47
48
49
50
51
52
53
54
55
|
# File 'lib/route.rb', line 47
def get_zone(id, name)
suffix = "dns/routezone?"
if id
suffix = suffix + "id=" + id.to_s
else
suffix = suffix + "name=" + name
end
@common.callMethod(customer_suffix(suffix), :get)
end
|
#non_customer_suffix(suffix) ⇒ Object
31
32
33
|
# File 'lib/route.rb', line 31
def non_customer_suffix(suffix)
"/dns/" + suffix
end
|
#normalize_group(group_arr) ⇒ Object
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/route.rb', line 127
def normalize_group(group_arr)
group_arr.each { |group|
ttl = -1
each_record(group) { |entry|
record = entry['Record']
if ttl == -1 then ttl = record['TTL'] end
entry['Rdata'] = record['Rdata']
entry.delete('Record')
}
group['TTL'] = ttl
}
end
|
#normalize_zone(zone) ⇒ Object
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
# File 'lib/route.rb', line 172
def normalize_zone(zone)
if zone['Records'].nil?
zone['Records'] = {
"A"=> [ ],
"AAAA"=> [ ],
"CNAME"=> [ ],
"MX"=> [ ],
"NS"=> [ ],
"SPF"=> [ ],
"SRV"=> [ ],
"TXT"=> [ ]
}
end
normalize_group(zone['LoadBalancingGroups'])
normalize_group(zone['FailoverGroups'])
end
|
#translate_groups(group_array) ⇒ Object
91
92
93
94
95
96
97
98
|
# File 'lib/route.rb', line 91
def translate_groups(group_array)
group_array.each { |group|
group_element = group["Group"]
["A", "AAAA", "CNAME"].each { |record_type|
translate_hc(group_element[record_type])
}
}
end
|
#translate_hc(rec_array) ⇒ Object
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/route.rb', line 69
def translate_hc(rec_array)
rec_array.each { |rec|
hc = rec["HealthCheck"]
return nil unless hc
translate_value(:hc_type, hc, "CheckTypeId")
translate_value(:http_method, hc, "HTTPMethodId")
translate_value(:ip_version, hc, "IPVersion")
translate_value(:hc_rm, hc, "ReintegrationMethodId")
}
end
|
#translate_value(data, hash, key) ⇒ Object
57
58
59
60
61
|
# File 'lib/route.rb', line 57
def translate_value(data, hash, key)
if hash.has_key?(key)
hash[key] = xlate_id(data, hash[key])
end
end
|
#translate_zone(zone) ⇒ Object
189
190
191
192
193
194
195
196
|
# File 'lib/route.rb', line 189
def translate_zone(zone)
return nil if zone.nil?
translate_value(:zone_status, zone, "Status")
translate_value(:zone_type, zone, "ZoneType")
translate_groups zone["LoadBalancingGroups"]
translate_groups zone["FailoverGroups"]
normalize_zone(zone)
end
|
#translate_zonelist(zonelist) ⇒ Object
110
111
112
113
114
115
116
|
# File 'lib/route.rb', line 110
def translate_zonelist(zonelist)
return nil if zonelist.nil?
zonelist.each { |zone|
translate_value(:zone_status, zone, "Status")
translate_value(:zone_type, zone, "ZoneType")
}
end
|
#untranslate_groups(group_array) ⇒ Object
100
101
102
103
104
105
106
107
108
|
# File 'lib/route.rb', line 100
def untranslate_groups(group_array)
return if group_array.nil?
group_array.each { |group|
group_element = group["Group"]
["A", "AAAA", "CNAME"].each { |record_type|
untranslate_hc(group_element[record_type])
}
}
end
|
#untranslate_hc(rec_array) ⇒ Object
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/route.rb', line 80
def untranslate_hc(rec_array)
rec_array.each { |rec|
hc = rec["HealthCheck"]
return nil unless hc
untranslate_value(:hc_type, hc, "CheckTypeId")
untranslate_value(:http_method, hc, "HTTPMethodId")
untranslate_value(:ip_version, hc, "IPVersion")
untranslate_value(:hc_rm, hc, "ReintegrationMethodId")
}
end
|
#untranslate_value(data, hash, key) ⇒ Object
63
64
65
66
67
|
# File 'lib/route.rb', line 63
def untranslate_value(data, hash, key)
if hash.has_key?(key)
hash[key] = xlate_name(data, hash[key])
end
end
|
#untranslate_zone(zone) ⇒ Object
198
199
200
201
202
203
204
205
|
# File 'lib/route.rb', line 198
def untranslate_zone(zone)
return nil if zone.nil?
denormalize_zone(zone)
untranslate_value(:zone_status, zone, "Status")
untranslate_value(:zone_type, zone, "ZoneType")
untranslate_groups zone["LoadBalancingGroups"]
untranslate_groups zone["FailoverGroups"]
end
|
#update_zone(zone) ⇒ Object
223
224
225
226
|
# File 'lib/route.rb', line 223
def update_zone(zone)
suffix = customer_suffix("dns/routezone")
@common.callMethod(suffix, :put, zone)
end
|
#xlate_id(symbol, id) ⇒ Object
237
238
239
240
241
242
243
244
|
# File 'lib/route.rb', line 237
def xlate_id(symbol, id)
get_available(symbol).each { |hash|
if hash["Id"] == id
return hash["Name"]
end
}
return nil
end
|
#xlate_name(symbol, name) ⇒ Object
228
229
230
231
232
233
234
235
|
# File 'lib/route.rb', line 228
def xlate_name(symbol, name)
return nil if name.nil?
get_available(symbol).each { |hash|
if hash["Name"] == name
return hash["Id"]
end
}
end
|