Class: Ridley::Connection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
DSL
Defined in:
lib/ridley/connection.rb

Overview

Author:

Constant Summary collapse

REQUIRED_OPTIONS =
[
  :server_url,
  :client_name,
  :client_key
]
DEFAULT_THREAD_COUNT =
8

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL

#client, #cookbook, #data_bag, #environment, #node, #role, #sandbox, #search, #search_indexes

Constructor Details

#initialize(options = {}) ⇒ Connection

Returns a new instance of Connection.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :server_url (String)

    URL to the Chef API

  • :client_name (String)

    name of the client used to authenticate with the Chef API

  • :client_key (String)

    filepath to the client’s private key used to authenticate with the Chef API

  • :organization (String)

    the Organization to connect to. This is only used if you are connecting to private Chef or hosted Chef

  • :thread_count (Integer)
  • :params (Hash)

    URI query unencoded key/value pairs

  • :headers (Hash)

    unencoded HTTP header key/value pairs

  • :request (Hash)

    request options

  • :ssl (Hash)

    SSL options

  • :proxy (URI, String, Hash)

    URI, String, or Hash of HTTP proxy options



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ridley/connection.rb', line 76

def initialize(options = {})
  self.class.validate_options(options)

  @client_name  = options.fetch(:client_name)
  @client_key   = options.fetch(:client_key)
  @organization = options.fetch(:organization, nil)
  @thread_count = options.fetch(:thread_count, DEFAULT_THREAD_COUNT)

  unless @client_key.present? && File.exist?(@client_key)
    raise Errors::ClientKeyFileNotFound, "client key not found at: '#{@client_key}'"
  end

  faraday_options = options.slice(:params, :headers, :request, :ssl, :proxy)
  uri_hash = Addressable::URI.parse(options[:server_url]).to_hash.slice(:scheme, :host, :port)

  unless uri_hash[:port]
    uri_hash[:port] = (uri_hash[:scheme] == "https" ? 443 : 80)
  end

  unless organization.nil?
    uri_hash[:path] = "/organizations/#{organization}"
  end

  server_uri = Addressable::URI.new(uri_hash)

  @conn = Faraday.new(server_uri, faraday_options) do |c|
    c.request :chef_auth, client_name, client_key
    c.response :chef_response
    c.response :json

    c.adapter Faraday.default_adapter
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



142
143
144
# File 'lib/ridley/connection.rb', line 142

def method_missing(method, *args, &block)
  @self_before_instance_eval.send(method, *args, &block)
end

Instance Attribute Details

#client_keyObject (readonly)

Returns the value of attribute client_key.



26
27
28
# File 'lib/ridley/connection.rb', line 26

def client_key
  @client_key
end

#client_nameObject (readonly)

Returns the value of attribute client_name.



25
26
27
# File 'lib/ridley/connection.rb', line 25

def client_name
  @client_name
end

#organizationObject (readonly)

Returns the value of attribute organization.



27
28
29
# File 'lib/ridley/connection.rb', line 27

def organization
  @organization
end

#thread_countObject

Returns the value of attribute thread_count.



29
30
31
# File 'lib/ridley/connection.rb', line 29

def thread_count
  @thread_count
end

Class Method Details

.sync(options, &block) ⇒ Object



5
6
7
# File 'lib/ridley/connection.rb', line 5

def sync(options, &block)
  new(options).sync(&block)
end

.validate_options(options) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
# File 'lib/ridley/connection.rb', line 12

def validate_options(options)
  missing = (REQUIRED_OPTIONS - options.keys)

  unless missing.empty?
    missing.collect! { |opt| "'#{opt}'" }
    raise ArgumentError, "Missing required option(s): #{missing.join(', ')}"
  end
end

Instance Method Details

#api_typeSymbol

Returns:

  • (Symbol)


119
120
121
# File 'lib/ridley/connection.rb', line 119

def api_type
  organization.nil? ? :foss : :hosted
end

#foss?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/ridley/connection.rb', line 129

def foss?
  api_type == :foss
end

#hosted?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/ridley/connection.rb', line 124

def hosted?
  api_type == :hosted
end

#sync(&block) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/ridley/connection.rb', line 110

def sync(&block)
  unless block
    raise Errors::InternalError, "A block must be given to synchronously process requests."
  end

  evaluate(&block)
end