Class: Etcd::Node

Inherits:
Object
  • Object
show all
Includes:
Constants, Requestable
Defined in:
lib/etcd/node.rb

Constant Summary

Constants included from Constants

Constants::S_ACTION, Constants::S_AND, Constants::S_DIR, Constants::S_EXPIRATION, Constants::S_INDEX, Constants::S_KEY, Constants::S_KEYS, Constants::S_LOCATION, Constants::S_NODE, Constants::S_NODES, Constants::S_PREV_NODE, Constants::S_SLASH, Constants::S_TTL, Constants::S_VALUE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Requestable

#http_client, #request, #request_data, #reset_http_client!

Methods included from Loggable

#logger, #reset_logger!

Constructor Details

#initialize(opts = {}) ⇒ Node

Returns a new instance of Node.



21
22
23
24
25
26
27
28
# File 'lib/etcd/node.rb', line 21

def initialize(opts={})
  check_required(opts)
  @name        = opts[:name]
  @id          = opts[:id]
  @peer_urls   = opts[:peer_urls]
  @client_urls = opts[:client_urls]
  @status = :unknown
end

Instance Attribute Details

#client_urlsObject

Returns the value of attribute client_urls.



5
6
7
# File 'lib/etcd/node.rb', line 5

def client_urls
  @client_urls
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/etcd/node.rb', line 5

def id
  @id
end

#is_leaderObject

Returns the value of attribute is_leader.



8
9
10
# File 'lib/etcd/node.rb', line 8

def is_leader
  @is_leader
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/etcd/node.rb', line 5

def name
  @name
end

#peer_urlsObject

Returns the value of attribute peer_urls.



5
6
7
# File 'lib/etcd/node.rb', line 5

def peer_urls
  @peer_urls
end

#statusObject

possible values: :unknown, :running, :down



7
8
9
# File 'lib/etcd/node.rb', line 7

def status
  @status
end

Class Method Details

.parse_node_data(attrs) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/etcd/node.rb', line 11

def parse_node_data(attrs)
  {
    :id          => attrs["id"],
    :name        => attrs["name"],
    :peer_urls   => attrs["peerURLs"],
    :client_urls => attrs["clientURLs"]
  }
end

Instance Method Details

#check_required(opts) ⇒ Object

Raises:

  • (ArgumentError)


30
31
32
33
# File 'lib/etcd/node.rb', line 30

def check_required(opts)
  raise ArgumentError, "Client URL is required!" unless opts[:client_urls] && opts[:client_urls].any?
  raise ArgumentError, "Node ID is required!" unless opts[:id]
end

#inspectObject



49
50
51
# File 'lib/etcd/node.rb', line 49

def inspect
  %Q(<#{self.class} - #{id} - #{name_with_status} - #{peer_urls}>)
end

#leader_uriObject



45
46
47
# File 'lib/etcd/node.rb', line 45

def leader_uri
  "#{@client_urls.first}/v2/members/leader"
end

#name_with_statusObject



53
54
55
56
# File 'lib/etcd/node.rb', line 53

def name_with_status
  print_status = @is_leader ? "leader" : status
  "#{name} (#{print_status})"
end

#to_jsonObject



58
59
60
# File 'lib/etcd/node.rb', line 58

def to_json
  { :name => name, :id => id, :client_urls => client_urls, :peer_urls => peer_urls }.to_json
end

#update_statusObject



35
36
37
38
39
40
41
42
43
# File 'lib/etcd/node.rb', line 35

def update_status
  begin
    leader_data = request_data(:get, leader_uri)
    @status     = :running
    @is_leader  = (leader_data["id"] == @id)
  rescue HTTPClient::TimeoutError, Errno::ECONNREFUSED => e
    @status = :down
  end
end