class TrainPlugins::Rest::Connection

Attributes

auth_handler[W]
options[R]

Public Class Methods

new(options) click to toggle source
Calls superclass method
# File lib/train-rest/connection.rb, line 13
def initialize(options)
  super(options)

  # Plugin was called with an URI only
  options[:endpoint] = options[:target].sub("rest://", "https://") unless options[:endpoint]

  # Accept string (CLI) and boolean (API) options
  options[:verify_ssl] = options[:verify_ssl].to_s == "false" ? false : true

  setup_vcr

  connect
end

Public Instance Methods

auth_parameters() click to toggle source

Auth Handlers-faced API

# File lib/train-rest/connection.rb, line 119
def auth_parameters
  auth_handler.auth_parameters
end
close() click to toggle source
# File lib/train-rest/connection.rb, line 55
def close
  logout if auth_handlers.include? auth_type
end
connect() click to toggle source
# File lib/train-rest/connection.rb, line 51
def connect
  login if auth_handlers.include? auth_type
end
inventory() click to toggle source
# File lib/train-rest/connection.rb, line 65
def inventory
  # Faking it for Chef Target Mode only
  OpenStruct.new({
    name: "rest",
    release: TrainPlugins::Rest::VERSION,
    family_hierarchy: %w{rest api},
    family: "api",
    platform: "rest",
    platform_version: 0,
  })
end
Also aliased as: os
os()
Alias for: inventory
platform() click to toggle source
# File lib/train-rest/connection.rb, line 79
def platform
  Train::Platforms.name("rest").in_family("api")

  force_platform!("rest", { release: TrainPlugins::Rest::VERSION })
end
request(path, method = :get, request_parameters: {}, data: nil, headers: {}, json_processing: true) click to toggle source
# File lib/train-rest/connection.rb, line 94
def request(path, method = :get, request_parameters: {}, data: nil, headers: {}, json_processing: true)
  parameters = global_parameters.merge(request_parameters)

  parameters[:method] = method
  parameters[:url] = full_url(path)

  if json_processing
    parameters[:headers]["Content-Type"] = "application/json"
    parameters[:payload] = JSON.generate(data)
  else
    parameters[:payload] = data
  end

  parameters[:headers].merge! headers
  parameters.compact!

  logger.info format("[REST] => %s", parameters.to_s) if options[:debug_rest]
  response = RestClient::Request.execute(parameters)

  logger.info format("[REST] <= %s", response.to_s) if options[:debug_rest]
  transform_response(response, json_processing)
end
setup_vcr() click to toggle source
# File lib/train-rest/connection.rb, line 27
def setup_vcr
  return unless options[:vcr_cassette]

  require "vcr"

  # TODO: Starts from "/" :(
  library = options[:vcr_library]
  match_on = options[:vcr_match_on].split.map(&:to_sym)

  VCR.configure do |config|
    config.cassette_library_dir = library
    config.hook_into options[:vcr_hook_into]
    config.default_cassette_options = {
      record: options[:vcr_record].to_sym,
      match_requests_on: match_on,
    }
  end

  VCR.insert_cassette options[:vcr_cassette]
rescue LoadError
  logger.fatal "Install the vcr gem to use HTTP(S) playback capability"
  raise
end
uri() click to toggle source
# File lib/train-rest/connection.rb, line 59
def uri
  components = URI(options[:endpoint])
  components.scheme = "rest"
  components.to_s
end

Private Instance Methods

auth_handler() click to toggle source
# File lib/train-rest/connection.rb, line 167
def auth_handler
  desired_handler = auth_handler_classes.detect { |handler| handler.name == auth_type.to_s }
  raise NameError.new(format("Authentication handler %s not found", auth_type.to_s)) unless desired_handler

  @auth_handler ||= desired_handler.new(self)
end
auth_handler_classes() click to toggle source
# File lib/train-rest/connection.rb, line 159
def auth_handler_classes
  AuthHandler.descendants
end
auth_handlers() click to toggle source
# File lib/train-rest/connection.rb, line 163
def auth_handlers
  auth_handler_classes.map { |handler| handler.name.to_sym }
end
auth_type() click to toggle source
# File lib/train-rest/connection.rb, line 151
def auth_type
  return options[:auth_type].to_sym if options[:auth_type]

  :basic if options[:username] && options[:password]
end
full_url(path) click to toggle source
# File lib/train-rest/connection.rb, line 138
def full_url(path)
  (URI(@options[:endpoint]) + path).to_s
end
global_parameters() click to toggle source
# File lib/train-rest/connection.rb, line 125
def global_parameters
  params = {
    verify_ssl: options[:verify_ssl] ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE,
    timeout: options[:timeout],
    proxy: options[:proxy],
    headers: options[:headers],
  }

  params.merge!(auth_parameters)

  params
end
login() click to toggle source
# File lib/train-rest/connection.rb, line 174
def login
  logger.info format("REST Login via %s authentication handler", auth_type.to_s) if auth_type != :anonymous

  auth_handler.options = options
  auth_handler.login
end
logout() click to toggle source
# File lib/train-rest/connection.rb, line 181
def logout
  logger.info format("REST Logout via %s authentication handler", auth_type.to_s) if auth_type != :anonymous

  auth_handler.logout
end
transform_response(response, json_processing = true) click to toggle source
# File lib/train-rest/connection.rb, line 142
def transform_response(response, json_processing = true)
  OpenStruct.new(
    code: response.code,
    headers: response.raw_headers,
    data: json_processing ? JSON.load(response.to_s) : response.to_s,
    duration: response.duration
  )
end