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, #mkdir, #param

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 ⇒ Object



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

def add_zone()
end

#available_file(data) ⇒ Object



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

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



158
159
160
# File 'lib/route.rb', line 158

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

#copy_zone(from, to) ⇒ Object



123
124
125
126
127
# File 'lib/route.rb', line 123

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



129
130
131
132
# File 'lib/route.rb', line 129

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



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/route.rb', line 172

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



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

def get_available_hc_rm()
  get_available(:hc_rm, "routereintegrationmethodtypes")
end

#get_available_hc_types ⇒ Object



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

def get_available_hc_types()
  get_available(:hc_type, "routehealthchecktypes")
end

#get_available_http_methods ⇒ Object



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

def get_available_http_methods()
  get_available(:http_method, "routehttpmethodtypes")
end

#get_available_ip_versions ⇒ Object



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

def get_available_ip_versions()
  get_available(:ip_version, "routehealthcheckipversion")
end

#get_available_record_types ⇒ Object



197
198
199
# File 'lib/route.rb', line 197

def get_available_record_types()
  get_available(:record_type, "routezonerecordtypes")
end

#get_available_route_groups ⇒ Object



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

def get_available_route_groups()
  get_available(:route_group, "routegrouptypes")
end

#get_available_zone_statuses ⇒ Object



201
202
203
# File 'lib/route.rb', line 201

def get_available_zone_statuses()
  get_available(:zone_status, "routezonestatus")
end

#get_available_zone_types ⇒ Object



193
194
195
# File 'lib/route.rb', line 193

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



89
90
91
92
93
94
95
96
# File 'lib/route.rb', line 89

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

def translate_hc(rec_array)
  rec_array.each { |rec|
    hc = rec["HealthCheck"]
    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



107
108
109
110
111
112
113
# File 'lib/route.rb', line 107

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

#untranslate_groups(group_array) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/route.rb', line 98

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



79
80
81
82
83
84
85
86
87
# File 'lib/route.rb', line 79

def untranslate_hc(rec_array)
  rec_array.each { |rec|
    hc = rec["HealthCheck"]
    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



115
116
117
118
119
120
121
# File 'lib/route.rb', line 115

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



137
138
139
140
# File 'lib/route.rb', line 137

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

#xlate_id(symbol, id) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/route.rb', line 150

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

#xlate_name(symbol, name) ⇒ Object



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

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