Class: Ridley::Connection
- Inherits:
-
Object
- Object
- Ridley::Connection
- Extended by:
- Forwardable
- Includes:
- DSL
- Defined in:
- lib/ridley/connection.rb
Overview
Constant Summary collapse
- REQUIRED_OPTIONS =
[ :server_url, :client_name, :client_key ].freeze
- DEFAULT_THREAD_COUNT =
8
Instance Attribute Summary collapse
-
#client_key ⇒ Object
readonly
Returns the value of attribute client_key.
-
#client_name ⇒ Object
readonly
Returns the value of attribute client_name.
-
#encrypted_data_bag_secret_path ⇒ Object
readonly
Returns the value of attribute encrypted_data_bag_secret_path.
-
#organization ⇒ Object
readonly
Returns the value of attribute organization.
-
#ssh ⇒ Object
readonly
Returns the value of attribute ssh.
-
#thread_count ⇒ Object
Returns the value of attribute thread_count.
-
#validator_client ⇒ Object
readonly
Returns the value of attribute validator_client.
-
#validator_path ⇒ Object
readonly
Returns the value of attribute validator_path.
Class Method Summary collapse
-
.default_options ⇒ Hash
A hash of default options to be used in the Connection initializer.
- .sync(options, &block) ⇒ Object
- .validate_options(options) ⇒ Boolean
Instance Method Summary collapse
- #api_type ⇒ Symbol
-
#encrypted_data_bag_secret ⇒ String?
The encrypted data bag secret for this connection.
- #foss? ⇒ Boolean
- #hosted? ⇒ Boolean
-
#initialize(options = {}) ⇒ Connection
constructor
A new instance of Connection.
- #server_url ⇒ Object
- #sync(&block) ⇒ Object
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.
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( = {}) = self.class..merge() self.class.() @client_name = [:client_name] @client_key = File.([:client_key]) @organization = [:organization] @thread_count = [:thread_count] @ssh = [:ssh] @validator_client = [:validator_client] @validator_path = [:validator_path] @encrypted_data_bag_secret_path = [:encrypted_data_bag_secret_path] unless @client_key.present? && File.exist?(@client_key) raise Errors::ClientKeyFileNotFound, "client key not found at: '#{@client_key}'" end = .slice(:params, :headers, :request, :ssl, :proxy) uri_hash = Addressable::URI.parse([: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 = [: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, ) 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_key ⇒ Object (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_name ⇒ Object (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_path ⇒ Object (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 |
#organization ⇒ Object (readonly)
Returns the value of attribute organization.
37 38 39 |
# File 'lib/ridley/connection.rb', line 37 def organization @organization end |
#ssh ⇒ Object (readonly)
Returns the value of attribute ssh.
38 39 40 |
# File 'lib/ridley/connection.rb', line 38 def ssh @ssh end |
#thread_count ⇒ Object
Returns the value of attribute thread_count.
44 45 46 |
# File 'lib/ridley/connection.rb', line 44 def thread_count @thread_count end |
#validator_client ⇒ Object (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_path ⇒ Object (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_options ⇒ Hash
A hash of default options to be used in the Connection initializer
24 25 26 27 28 29 |
# File 'lib/ridley/connection.rb', line 24 def { 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(, &block) new().sync(&block) end |
.validate_options(options) ⇒ Boolean
12 13 14 15 16 17 18 19 |
# File 'lib/ridley/connection.rb', line 12 def () missing = (REQUIRED_OPTIONS - .keys) unless missing.empty? missing.collect! { |opt| "'#{opt}'" } raise ArgumentError, "Missing required option(s): #{missing.join(', ')}" end end |
Instance Method Details
#api_type ⇒ Symbol
152 153 154 |
# File 'lib/ridley/connection.rb', line 152 def api_type organization.nil? ? :foss : :hosted end |
#encrypted_data_bag_secret ⇒ String?
The encrypted data bag secret for this connection.
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
162 163 164 |
# File 'lib/ridley/connection.rb', line 162 def foss? api_type == :foss end |
#hosted? ⇒ Boolean
157 158 159 |
# File 'lib/ridley/connection.rb', line 157 def hosted? api_type == :hosted end |
#server_url ⇒ Object
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 |