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
].freeze
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

  • :validator_client (String) — default: default: nil
  • :validator_path (String) — default: default: nil
  • :encrypted_data_bag_secret_path (String) — default: default: nil
  • :thread_count (Integer)
  • :ssh (Hash)

    authentication credentials for bootstrapping or connecting to nodes (default: Hash.new)

  • :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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/ridley/connection.rb', line 100

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

  @client_name      = options[:client_name]
  @client_key       = File.expand_path(options[:client_key])
  @organization     = options[:organization]
  @thread_count     = options[:thread_count]
  @ssh              = options[:ssh]
  @validator_client = options[:validator_client]
  @validator_path   = options[:validator_path]
  @encrypted_data_bag_secret_path = options[:encrypted_data_bag_secret_path]

  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

  if org_match = options[:server_url].match(/.*\/organizations\/(.*)/)
    @organization ||= org_match[1]
  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)



192
193
194
195
196
197
198
199
200
201
202
# File 'lib/ridley/connection.rb', line 192

def method_missing(method, *args, &block)
  if block_given?
    @self_before_instance_eval ||= eval("self", block.binding)
  end

  if @self_before_instance_eval.nil?
    super
  end

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

Instance Attribute Details

#client_keyObject (readonly)

Returns the value of attribute client_key.



36
37
38
# File 'lib/ridley/connection.rb', line 36

def client_key
  @client_key
end

#client_nameObject (readonly)

Returns the value of attribute client_name.



35
36
37
# File 'lib/ridley/connection.rb', line 35

def client_name
  @client_name
end

#encrypted_data_bag_secret_pathObject (readonly)

Returns the value of attribute encrypted_data_bag_secret_path.



42
43
44
# File 'lib/ridley/connection.rb', line 42

def encrypted_data_bag_secret_path
  @encrypted_data_bag_secret_path
end

#organizationObject (readonly)

Returns the value of attribute organization.



37
38
39
# File 'lib/ridley/connection.rb', line 37

def organization
  @organization
end

#sshObject (readonly)

Returns the value of attribute ssh.



38
39
40
# File 'lib/ridley/connection.rb', line 38

def ssh
  @ssh
end

#thread_countObject

Returns the value of attribute thread_count.



44
45
46
# File 'lib/ridley/connection.rb', line 44

def thread_count
  @thread_count
end

#validator_clientObject (readonly)

Returns the value of attribute validator_client.



40
41
42
# File 'lib/ridley/connection.rb', line 40

def validator_client
  @validator_client
end

#validator_pathObject (readonly)

Returns the value of attribute validator_path.



41
42
43
# File 'lib/ridley/connection.rb', line 41

def validator_path
  @validator_path
end

Class Method Details

.default_optionsHash

A hash of default options to be used in the Connection initializer

Returns:

  • (Hash)


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

def default_options
  {
    thread_count: DEFAULT_THREAD_COUNT,
    ssh: Hash.new
  }
end

.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)


152
153
154
# File 'lib/ridley/connection.rb', line 152

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

#encrypted_data_bag_secretString?

The encrypted data bag secret for this connection.

Returns:

  • (String, nil)

Raises:



175
176
177
178
179
180
181
# File 'lib/ridley/connection.rb', line 175

def encrypted_data_bag_secret
  return nil if encrypted_data_bag_secret_path.nil?

  IO.read(encrypted_data_bag_secret_path).chomp
rescue Errno::ENOENT => e
  raise Errors::EncryptedDataBagSecretNotFound, "Encrypted data bag secret provided but not found at '#{encrypted_data_bag_secret_path}'"
end

#foss?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/ridley/connection.rb', line 162

def foss?
  api_type == :foss
end

#hosted?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/ridley/connection.rb', line 157

def hosted?
  api_type == :hosted
end

#server_urlObject



166
167
168
# File 'lib/ridley/connection.rb', line 166

def server_url
  self.url_prefix.to_s
end

#sync(&block) ⇒ Object



143
144
145
146
147
148
149
# File 'lib/ridley/connection.rb', line 143

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

  evaluate(&block)
end