Class: DynectRest::GSLB

Inherits:
Object
  • Object
show all
Defined in:
lib/dynect_rest/gslb.rb

Instance Method Summary collapse

Constructor Details

#initialize(init_hash = {}) ⇒ GSLB

Returns a new instance of GSLB.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/dynect_rest/gslb.rb', line 22

def initialize(init_hash={})
  validate(init_hash, [:dynect, :zone])

  @dynect = init_hash[:dynect]
  @zone = init_hash[:zone]
  @fqdn = init_hash[:fqdn]
  @ttl = init_hash[:ttl] || 30
  @host_list = init_hash[:host_list] || {}
  @contact_nick = init_hash[:contact_nick] || 'owner'
  
  @region_code = init_hash[:region_code] || 'global'
  @monitor = init_hash[:monitor] || {}
  @serve_count = init_hash[:serve_count] || 1
  @min_healthy = init_hash[:min_healthy] || 1
end

Instance Method Details

#[](host_list_key) ⇒ Object



38
39
40
# File 'lib/dynect_rest/gslb.rb', line 38

def [](host_list_key)
  @host_list[host_list_key]
end

#add_host(value) ⇒ Object



77
78
79
80
81
# File 'lib/dynect_rest/gslb.rb', line 77

def add_host(value)
  # :address => 'x.x.x.x', :label => 'friendly-name', :weight => 10, :serve_mode => 'obey'
  @host_list[value[:address]] = value
  self
end

#contact_nick(value = nil) ⇒ Object



46
47
48
# File 'lib/dynect_rest/gslb.rb', line 46

def contact_nick(value=nil)
  value ? (@contact_nick = value; self) : @contact_nick
end

#deleteObject



146
147
148
# File 'lib/dynect_rest/gslb.rb', line 146

def delete
  @dynect.delete("#{resource_path}/#{fqdn}")
end

#find(fqdn, query_hash) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/dynect_rest/gslb.rb', line 127

def find(fqdn, query_hash)
  results = []
  get(fqdn).each do |rr|
    query_hash.each do |key, value|
      results << rr if rr[key.to_s] == value
    end
  end
  results
end

#fqdn(value = nil) ⇒ Object



42
43
44
# File 'lib/dynect_rest/gslb.rb', line 42

def fqdn(value=nil)
  value ? (@fqdn = value; self) : @fqdn
end

#get(fqdn = nil, region_code = 'global') ⇒ Object



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
# File 'lib/dynect_rest/gslb.rb', line 92

def get(fqdn=nil, region_code='global')
  if fqdn
    results = @dynect.get("#{resource_path}/#{fqdn}")
    region = {}
    results["region"].each {|r| region = r if r["region_code"] == region_code}
    raise DynectRest::Exceptions::RequestFailed, "Cannot find #{region_code} GSLB pool for #{fqdn}" if region.empty?

    # Default monitor timeout is 0, but specifying timeout 0 on a put or post results in an exception
    results["monitor"].delete("timeout") if results["monitor"]["timeout"] == 0

    host_list = {}
    region["pool"].each do |h|
      host_list[h["address"]] = {
                                :address => h["address"],
                                :label => h["label"],
                                :weight => h["weight"],
                                :serve_mode => h["serve_mode"]
                                }
    end
    DynectRest::GSLB.new(:dynect => @dynect,
                             :zone => results["zone"],
                             :fqdn => results["fqdn"],
                             :ttl => results["ttl"],
                             :host_list => host_list,
                             :contact_nick => results["contact_nickname"],
                             :region_code => region["region_code"],
                             :monitor => results["monitor"],
                             :serve_count => region["serve_count"],
                             :min_healthy => region["min_healthy"]
                             )
  else
    @dynect.get(resource_path)
  end
end

#host_list(value = nil) ⇒ Object



67
68
69
# File 'lib/dynect_rest/gslb.rb', line 67

def host_list(value=nil)
  value ? (@host_list = value; self) : @host_list
end

#min_healthy(value = nil) ⇒ Object



54
55
56
# File 'lib/dynect_rest/gslb.rb', line 54

def min_healthy(value=nil)
  value ? (@min_healthy = value; self) : @min_healthy
end

#monitor(value = nil) ⇒ Object



71
72
73
74
75
# File 'lib/dynect_rest/gslb.rb', line 71

def monitor(value=nil)
  # :protocol => 'HTTP', :interval => 1, :retries => 2, :timeout => 10, :port => 8000,
  # :path => '/healthcheck', :host => 'example.com', :header => 'X-User-Agent: DynECT Health\n', :expected => 'passed'
  value ? (@monitor = value; self) : @monitor
end

#region_code(value = nil) ⇒ Object



62
63
64
65
# File 'lib/dynect_rest/gslb.rb', line 62

def region_code(value=nil)
  # US West, US Central, US East, EU West, EU Central, EU East, Asia, global
  value ? (@region_code = value; self) : @region_code
end

#resource_path(full = false) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/dynect_rest/gslb.rb', line 83

def resource_path(full=false)
  service_type = "GSLB"
  if (full == true || full == :full)
    "/REST/#{service_type}/#{@zone}"
  else
    "#{service_type}/#{@zone}"
  end
end

#save(replace = false) ⇒ Object



137
138
139
140
141
142
143
144
# File 'lib/dynect_rest/gslb.rb', line 137

def save(replace=false)
  if replace == true || replace == :replace
    @dynect.put("#{resource_path}/#{@fqdn}", self)
  else
    @dynect.post("#{resource_path}/#{@fqdn}", self)
  end
  self
end

#serve_count(value = nil) ⇒ Object



58
59
60
# File 'lib/dynect_rest/gslb.rb', line 58

def serve_count(value=nil)
  value ? (@serve_count = value; self) : @serve_count
end

#to_hashObject



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/dynect_rest/gslb.rb', line 156

def to_hash
  {
    "ttl" => @ttl,
    "monitor" => @monitor,
    "region" => {
      "region_code" => @region_code,
      "serve_count" => @serve_count,
      "min_healthy" => @min_healthy,
      "pool" => @host_list.values
    },
    "contact_nickname" => @contact_nick
  }
end

#to_jsonObject



170
171
172
# File 'lib/dynect_rest/gslb.rb', line 170

def to_json
  to_hash.to_json
end

#ttl(value = nil) ⇒ Object



50
51
52
# File 'lib/dynect_rest/gslb.rb', line 50

def ttl(value=nil)
  value ? (@ttl = value; self) : @ttl
end

#validate(init_hash, required_keys) ⇒ Object



150
151
152
153
154
# File 'lib/dynect_rest/gslb.rb', line 150

def validate(init_hash, required_keys)
  required_keys.each do |k|
    raise ArgumentError, "You must provide a value for #{k}" unless init_hash[k]
  end
end