Class: Route

Inherits:
Object
  • Object
show all
Includes:
ConfigReader
Defined in:
lib/route.rb

Constant Summary collapse

@@instance =
nil

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ConfigReader

#config_dir, #config_file, #imkdir, #mk_config_dir, #mk_user_dir, #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



144
145
146
147
# File 'lib/route.rb', line 144

def add_zone(zone)
  suffix = customer_suffix("dns/routezone")
  @common.callMethod(suffix, :post, zone)
end

#available_file(data) ⇒ Object



175
176
177
178
179
180
181
182
183
# File 'lib/route.rb', line 175

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



171
172
173
# File 'lib/route.rb', line 171

def cache_filename(data)
  config_file(data.to_s + ".data")
end

#copy_zone(from, to) ⇒ Object



133
134
135
136
137
# File 'lib/route.rb', line 133

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



139
140
141
142
# File 'lib/route.rb', line 139

def delete_zone(id)
  suffix = customer_suffix("dns/routezone/#{id}")
  @common.callMethod(suffix, :delete)
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



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/route.rb', line 185

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



234
235
236
# File 'lib/route.rb', line 234

def get_available_hc_rm()
  get_available(:hc_rm)
end

#get_available_hc_types ⇒ Object



230
231
232
# File 'lib/route.rb', line 230

def get_available_hc_types()
  get_available(:hc_type)
end

#get_available_http_methods ⇒ Object



222
223
224
# File 'lib/route.rb', line 222

def get_available_http_methods()
  get_available(:http_method)
end

#get_available_ip_versions ⇒ Object



226
227
228
# File 'lib/route.rb', line 226

def get_available_ip_versions()
  get_available(:ip_version)
end

#get_available_record_types ⇒ Object



210
211
212
# File 'lib/route.rb', line 210

def get_available_record_types()
  get_available(:record_type)
end

#get_available_route_groups ⇒ Object



218
219
220
# File 'lib/route.rb', line 218

def get_available_route_groups()
  get_available(:route_group)
end

#get_available_zone_statuses ⇒ Object



214
215
216
# File 'lib/route.rb', line 214

def get_available_zone_statuses()
  get_available(:zone_status)
end

#get_available_zone_types ⇒ Object



206
207
208
# File 'lib/route.rb', line 206

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

#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



117
118
119
120
121
122
123
# File 'lib/route.rb', line 117

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"]
end

#translate_zonelist(zonelist) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/route.rb', line 109

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
# File 'lib/route.rb', line 100

def untranslate_groups(group_array)
  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



125
126
127
128
129
130
131
# File 'lib/route.rb', line 125

def untranslate_zone(zone)
  return nil if zone.nil?
  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



149
150
151
152
# File 'lib/route.rb', line 149

def update_zone(zone)
  suffix = customer_suffix("dns/routezone")
  @common.callMethod(suffix, :put, zone)
end

#xlate_id(symbol, id) ⇒ Object



162
163
164
165
166
167
168
169
# File 'lib/route.rb', line 162

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



154
155
156
157
158
159
160
# File 'lib/route.rb', line 154

def xlate_name(symbol, name)
  get_available(symbol).each { |hash|
    if hash["Name"] == name
      return hash["Id"]
    end
  }
end