Class: ConsulCluster

Inherits:
Object
  • Object
show all
Includes:
HTTPUtils, Utils
Defined in:
lib/panteras_api/consul_cluster.rb

Instance Method Summary collapse

Methods included from Utils

fully_qualified_hostname, hostname

Methods included from HTTPUtils

#construct_uri, #get_response_with_redirect, #to_j, #valid_url

Constructor Details

#initialize(host, port = 8500) ⇒ ConsulCluster

Returns a new instance of ConsulCluster.



5
6
7
8
9
10
11
# File 'lib/panteras_api/consul_cluster.rb', line 5

def initialize(host,port=8500)
  @host = host
  @port = port
  @datacenters = datacenters
  @dc = ""
  @hostname = Utils.hostname
end

Instance Method Details

#datacentersObject



18
19
20
# File 'lib/panteras_api/consul_cluster.rb', line 18

def datacenters
  to_j(get_response_with_redirect(@host, '/v1/catalog/datacenters', @port))
end

#dc=(datacenter) ⇒ Object

Raises:

  • (ArgumentError)


13
14
15
16
# File 'lib/panteras_api/consul_cluster.rb', line 13

def dc=(datacenter)
  raise ArgumentError, "#{datacenter} is not a valid datacenter" unless (@datacenters.include? datacenter)
  @dc = datacenter
end

#my_service_idsObject



37
38
39
# File 'lib/panteras_api/consul_cluster.rb', line 37

def my_service_ids
   my_services.collect { |s| s[:ServiceID].split(/:/)[1] }.compact
end

#my_services(hostname = @hostname) ⇒ Object

Raises:

  • (ArgumentError)


32
33
34
35
# File 'lib/panteras_api/consul_cluster.rb', line 32

def my_services(hostname=@hostname)
  raise ArgumentError, "missing hostname argument", caller if hostname.nil?
  services.collect { |s| s.select { |i| i[:Node] =~ /^#{hostname}$/ } }.flatten
end

#service(service) ⇒ Object



41
42
43
# File 'lib/panteras_api/consul_cluster.rb', line 41

def service(service)
  to_j(get_response_with_redirect(@host, '/v1/catalog/service/' + service + '?dc=' + @dc, @port))
end

#service_namesObject



26
27
28
29
30
# File 'lib/panteras_api/consul_cluster.rb', line 26

def service_names
  services = to_j(get_response_with_redirect(@host, '/v1/catalog/services?dc=' + @dc, @port)).keys
  # exclude the consul and other PanteraS related services
  services.reject { |s| s.to_s == "consul" or s.to_s == "consul-ui" or s.to_s == "marathon" or s.to_s == "mesos" }
end

#servicesObject



22
23
24
# File 'lib/panteras_api/consul_cluster.rb', line 22

def services
  service_names.collect { |s| service s.to_s }
end