Module: ChefAPI::Defaults

Defined in:
lib/chef-api/defaults.rb

Constant Summary collapse

ENDPOINT =

Default API endpoint

'https://api.opscode.com/'.freeze
USER_AGENT =

Default User Agent header string

"ChefAPI Ruby Gem #{ChefAPI::VERSION}".freeze

Class Method Summary collapse

Class Method Details

.chef_api_config_pathString?

String representation of path to configuration file

Returns:

  • (String, nil)

    Path to config file, or nil



48
49
50
51
52
53
54
# File 'lib/chef-api/defaults.rb', line 48

def chef_api_config_path
  ENV['CHEF_API_CONFIG'] || if ENV.key?('HOME')
                              '~/.chef-api'
                            else
                              nil
                            end
end

.clientString?

The name of the Chef API client. This is the equivalent of client_name in Chef terminology. In most cases, this is your Chef username.

Returns:

  • (String, nil)


113
114
115
# File 'lib/chef-api/defaults.rb', line 113

def client
  ENV['CHEF_API_CLIENT'] || config['CHEF_API_CLIENT']
end

.configHash

The Chef API configuration

Returns:

  • (Hash)


27
28
29
30
# File 'lib/chef-api/defaults.rb', line 27

def config
  path = config_path
  @config ||= path.exist? ? JSON.parse(path.read) : {}
end

.config_pathPathname

Pathname to configuration file, or a blank Pathname.

Returns:

  • (Pathname)

    an expanded Pathname or a non-existent Pathname



36
37
38
39
40
41
42
# File 'lib/chef-api/defaults.rb', line 36

def config_path
  if result = chef_api_config_path
    Pathname(result).expand_path
  else
    Pathname('')
  end
end

.endpointString

The endpoint where the Chef Server lives. This is equivalent to the chef_server_url in Chef terminology. If you are using Enterprise Hosted Chef or Enterprise Chef on premise, this endpoint should include your organization name. For example:

https://api.opscode.com/organizations/bacon

If you are running Open Source Chef Server or Chef Zero, this is the full URL to your Chef Server instance, including the server port and FQDN.

https://chef.server.local:4567/

Returns:

  • (String)

    (default: https://api.opscode.com/)



72
73
74
# File 'lib/chef-api/defaults.rb', line 72

def endpoint
  ENV['CHEF_API_ENDPOINT'] || config['CHEF_API_ENDPOINT'] || ENDPOINT
end

.flavortrue, false

The flavor of the target Chef Server. There are two possible values:

- enterprise
- open_source

“Enterprise” covers both Hosted Chef and Enterprise Chef. “Open Source” covers both Chef Zero and Open Source Chef Server.

Returns:

  • (true, false)


87
88
89
90
91
92
93
94
95
# File 'lib/chef-api/defaults.rb', line 87

def flavor
  if ENV['CHEF_API_FLAVOR']
    ENV['CHEF_API_FLAVOR'].to_sym
  elsif config['CHEF_API_FLAVOR']
    config['CHEF_API_FLAVOR']
  else
    endpoint.include?('/organizations') ? :enterprise : :open_source
  end
end

.keyString?

The private key to authentication against the Chef Server. This is equivalent to the client_key in Chef terminology. This value can be the client key in plain text or a path to the key on disk.

Returns:

  • (String, nil)


124
125
126
# File 'lib/chef-api/defaults.rb', line 124

def key
   ENV['CHEF_API_KEY'] || config['CHEF_API_KEY']
end

.optionsHash

The list of calculated default options for the configuration.

Returns:

  • (Hash)


19
20
21
# File 'lib/chef-api/defaults.rb', line 19

def options
  Hash[Configurable.keys.map { |key| [key, send(key)] }]
end

.proxy_addressString?

The HTTP Proxy server address as a string

Returns:

  • (String, nil)


132
133
134
# File 'lib/chef-api/defaults.rb', line 132

def proxy_address
  ENV['CHEF_API_PROXY_ADDRESS'] || config['CHEF_API_PROXY_ADDRESS']
end

.proxy_passwordString?

The HTTP Proxy user password as a string

Returns:

  • (String, nil)


141
142
143
# File 'lib/chef-api/defaults.rb', line 141

def proxy_password
  ENV['CHEF_API_PROXY_PASSWORD'] || config['CHEF_API_PROXY_PASSWORD']
end

.proxy_portString?

The HTTP Proxy server port as a string

Returns:

  • (String, nil)


150
151
152
# File 'lib/chef-api/defaults.rb', line 150

def proxy_port
  ENV['CHEF_API_PROXY_PORT'] || config['CHEF_API_PROXY_PORT']
end

.proxy_usernameString?

The HTTP Proxy server username as a string

Returns:

  • (String, nil)


159
160
161
# File 'lib/chef-api/defaults.rb', line 159

def proxy_username
  ENV['CHEF_API_PROXY_USERNAME'] || config['CHEF_API_PROXY_USERNAME']
end

.read_timeoutInteger?

Network request read timeout in seconds (default: 60)

Returns:

  • (Integer, nil)


190
191
192
193
194
# File 'lib/chef-api/defaults.rb', line 190

def read_timeout
  timeout_from_env = ENV['CHEF_API_READ_TIMEOUT'] || config['CHEF_API_READ_TIMEOUT']

  Integer(timeout_from_env) unless timeout_from_env.nil?
end

.ssl_pem_fileString?

The path to a pem file on disk for use with a custom SSL verification

Returns:

  • (String, nil)


168
169
170
# File 'lib/chef-api/defaults.rb', line 168

def ssl_pem_file
  ENV['CHEF_API_SSL_PEM_FILE'] || config['CHEF_API_SSL_PEM_FILE']
end

.ssl_verifytrue, false

Verify SSL requests (default: true)

Returns:

  • (true, false)


177
178
179
180
181
182
183
# File 'lib/chef-api/defaults.rb', line 177

def ssl_verify
  if ENV['CHEF_API_SSL_VERIFY'].nil? && config['CHEF_API_SSL_VERIFY'].nil?
    true
  else
    %w[t y].include?(ENV['CHEF_API_SSL_VERIFY'].downcase[0]) || config['CHEF_API_SSL_VERIFY']
  end
end

.user_agentString

The User Agent header to send along.

Returns:

  • (String)


102
103
104
# File 'lib/chef-api/defaults.rb', line 102

def user_agent
  ENV['CHEF_API_USER_AGENT'] || config['CHEF_API_USER_AGENT'] || USER_AGENT
end