Sha256: 107d2d63d3f3c8db51fff0c2203c078c92d564aa9c2112bb693c2a6157f7dd99

Contents?: true

Size: 956 Bytes

Versions: 2

Compression:

Stored size: 956 Bytes

Contents

require 'diplomat'

module Skalera
  module Services
    class Controllers
      def self.for(controller_uuid)
        controllers = Diplomat::Kv.get("controllers/#{controller_uuid}", recurse: true)
        host = extract(controllers, 'address')
        port = extract(controllers, 'port')
        [host['address'], port['port']]
      rescue Diplomat::KeyNotFound
        STDERR.puts "ERROR: key not found: controllers/#{controller_uuid}"
      end

      def self.extract(controllers, field)
        result = {}
        controllers.select { |c| c[:key].match(%r{/#{field}}) }.each do |hash|
          host = hash[:key].sub(%r{controllers/}, '').split('/')[1]
          result[host] = hash[:value]
        end
        result
      end

      def self.add(controller_uuid, host, port)
        Diplomat::Kv.put("controllers/#{controller_uuid}/address", host)
        Diplomat::Kv.put("controllers/#{controller_uuid}/port", port)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
skalera-services-0.2.5 lib/skalera/services/controllers.rb
skalera-services-0.2.4 lib/skalera/services/controllers.rb