require "berkshelf/api-client" require "berkshelf/ssl_policies" require "openssl" module Berkshelf class Source include Comparable attr_accessor :source # @param [String, Berkshelf::SourceURI] source def initialize(source) @source = source @universe = nil end def ssl_policy @ssl_policy ||= SSLPolicy.new end def api_client @api_client ||= begin ssl_options = { verify: Berkshelf::Config.instance.ssl.verify } ssl_options[:cert_store] = ssl_policy.store if ssl_policy.store if source == :chef_server APIClient.chef_server( ssl: ssl_options, timeout: api_timeout, open_timeout: [(api_timeout / 10), 3].max, client_name: Berkshelf::Config.instance.chef.node_name, server_url: Berkshelf::Config.instance.chef.chef_server_url, client_key: Berkshelf::Config.instance.chef.client_key ) else APIClient.new(uri, timeout: api_timeout, open_timeout: [(api_timeout / 10), 3].max, ssl: Berkshelf::Config.instance.ssl ) end end end def uri @uri ||= if source == :chef_server SourceURI.parse(Berkshelf::Config.instance.chef.chef_server_url) else SourceURI.parse(source) end end # Forcefully obtain the universe from the API endpoint and assign it to {#universe}. This # will reload the value of {#universe} even if it has been loaded before. # # @return [Array] def build_universe @universe = api_client.universe rescue => ex @universe = Array.new raise ex end # Return the universe from the API endpoint. # # This is lazily loaded so the universe will be retrieved from the API endpoint on the first # call and cached for future calls. Send the {#build_universe} message if you want to reload # the cached universe. # # @return [Array] def universe @universe || build_universe end # @param [String] name # @param [String] version # # @return [APIClient::RemoteCookbook] def cookbook(name, version) universe.find { |cookbook| cookbook.name == name && cookbook.version == version } end # The list of remote cookbooks that match the given query. # # @param [String] name # # @return [Array] def versions(name) universe.select { |cookbook| cookbook.name == name } end def to_s "#{uri}" end def inspect "#<#{self.class.name} uri: #{@uri.to_s.inspect}>" end def hash @uri.host.hash end def ==(other) return false unless other.is_a?(self.class) uri == other.uri end private def api_timeout Berkshelf::Config.instance.api.timeout.to_i end end end