Sha256: 93a066e1a1f3fa395f3de8fca970ca3a9b1144646eca2f358ef741bef567672c

Contents?: true

Size: 1.73 KB

Versions: 3

Compression:

Stored size: 1.73 KB

Contents

require 'addressable/uri'
require 'bolt/error'

module Bolt
  class Target
    attr_reader :uri, :options

    # Satisfies the Puppet datatypes API
    def self.from_asserted_hash(hash)
      new(hash['uri'], hash['options'])
    end

    def initialize(uri, options = nil)
      @uri = uri
      @uri_obj = parse(uri)
      @options = options || {}
    end

    def update_conf(conf)
      @protocol = conf[:transport]

      t_conf = conf[:transports][protocol.to_sym]
      # Override url methods
      url_keys = %i[user password port]
      @user = t_conf[:user]
      @password = t_conf[:password]
      @port = t_conf[:port]

      @options = t_conf.reject { |k, _| url_keys.include?(k) }.merge(@options)

      self
    end

    def parse(string)
      if string =~ %r{^[^:]+://}
        Addressable::URI.parse(string)
      else
        # Initialize with an empty scheme to ensure we parse the hostname correctly
        Addressable::URI.parse("//#{string}")
      end
    end
    private :parse

    def eql?(other)
      self.class.equal?(other.class) && @uri == other.uri
    end
    alias == eql?

    def hash
      @uri.hash ^ @options.hash
    end

    def to_s
      "Target('#{@uri}', #{@options})"
    end

    def host
      @uri_obj.hostname
    end

    # name is currently just uri but should be be used instead to identify the
    # Target ouside the transport or uri options.
    def name
      uri
    end

    def port
      @uri_obj.port || @port
    end

    def protocol
      @uri_obj.scheme || @protocol
    end

    def user
      Addressable::URI.unencode_component(
        @uri_obj.user || @user
      )
    end

    def password
      Addressable::URI.unencode_component(
        @uri_obj.password || @password
      )
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bolt-0.16.4 lib/bolt/target.rb
bolt-0.16.3 lib/bolt/target.rb
bolt-0.16.2 lib/bolt/target.rb