Sha256: f61d032e45280ec0ec2fc914c650a15dffe95e98e80851aadd8596dd71a1ebe8

Contents?: true

Size: 1.94 KB

Versions: 8

Compression:

Stored size: 1.94 KB

Contents

require 'yao'
require 'yao/config'
require 'faraday'
require 'yao/plugins/default_client_generator'

module Yao
  module Client
    class ClientSet
      def initialize
        @pool       = {}
        @admin_pool = {}
      end
      attr_reader :pool, :admin_pool

      %w(default compute network image metering volume orchestration identity).each do |type|
        define_method(type) do
          self.pool[type]
        end

        define_method("#{type}_admin") do
          self.admin_pool[type]
        end
      end

      def register_endpoints(endpoints, token: nil)
        endpoints.each_pair do |type, urls|
          # XXX: neutron just have v2.0 API and endpoint may not have version prefix
          if type == "network"
            urls = urls.map {|public_or_admin, url|
              url = URI.parse(url).path == "/" ? File.join(url, "v2.0") : url
              [public_or_admin, url]
            }.to_h
          end

          self.pool[type]       = Yao::Client.gen_client(urls[:public_url], token: token)
          self.admin_pool[type] = Yao::Client.gen_client(urls[:admin_url],  token: token)
        end
      end
    end

    class << self
      attr_accessor :default_client

      def client_generator
        Plugins::Registry.instance[:client_generator][Yao.config.client_generator].new
      end

      def gen_client(endpoint, token: nil)
        Faraday.new( endpoint ) do |f|
          client_generator.call(f, token)
        end
      end

      def reset_client(new_endpoint=nil)
        set = ClientSet.new
        set.register_endpoints("default" => {public_url: new_endpoint || Yao.config.endpoint})
        self.default_client = set
      end
    end

    Yao.config.param :auth_url, nil do |endpoint|
      if endpoint
        Yao::Client.reset_client(endpoint)
      end
    end
  end

  def self.default_client
    Yao::Client.default_client
  end

  Yao.config.param :debug, false
  Yao.config.param :debug_record_response, false
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
yao-0.2.7 lib/yao/client.rb
yao-0.2.6 lib/yao/client.rb
yao-0.2.5 lib/yao/client.rb
yao-0.2.4 lib/yao/client.rb
yao-0.2.2 lib/yao/client.rb
yao-0.2.1 lib/yao/client.rb
yao-0.2.0 lib/yao/client.rb
yao-0.2.0.rc1 lib/yao/client.rb