Class: VagrantCloud::Client

Inherits:
Object
  • Object
show all
Includes:
Logify, Configurable
Defined in:
lib/vagrant-cloud/client.rb

Constant Summary

Constants included from Defaults

Defaults::USER_AGENT

Instance Method Summary collapse

Methods included from Configurable

#configure, keys, #reset!

Methods included from Defaults

access_token, endpoint, options, proxy_address, proxy_password, proxy_port, proxy_username, ssl_pem_file, ssl_verify, user_agent, username

Constructor Details

#initialize(options = {}, &block) ⇒ Client

Create a new Vagrant Cloud client (connection) object.

Examples:

Create a simple connection

VagrantCloud::Client.new

Create a custom connection

VagrantCloud::Client.new(endpoint: '...', access_token: '...')

Create a custom connection using a block

VagrantCloud::Client.new do |client|
  client.endpoint = '...'
  client.access_token = '...'
end

Parameters:

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

    the list of options to create the client with

  • block (Proc)

    an optional block that is called against self



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/vagrant-cloud/client.rb', line 32

def initialize(options = {}, &block)
  Configurable.keys.each do |key|
    value = options[key] || VagrantCloud.instance_variable_get(:"@#{key}")
    instance_variable_set(:"@#{key}", value)
  end

  block.call(self) if block

  # Setup log filtering for the access_token
  Logify.filter(access_token) if access_token
end

Instance Method Details

#boxesObject



45
46
47
# File 'lib/vagrant-cloud/client.rb', line 45

def boxes
  Resource::Box
end

#delete(path, params = {}) ⇒ Object

See Also:



82
83
84
# File 'lib/vagrant-cloud/client.rb', line 82

def delete(path, params = {})
  request(:delete, path, params)
end

#get(path, params = {}) ⇒ Object

See Also:



61
62
63
# File 'lib/vagrant-cloud/client.rb', line 61

def get(path, params = {})
  request(:get, path, params)
end

#post(path, data) ⇒ Object

See Also:



68
69
70
# File 'lib/vagrant-cloud/client.rb', line 68

def post(path, data)
  request(:post, path, data)
end

#put(path, data) ⇒ Object

See Also:



75
76
77
# File 'lib/vagrant-cloud/client.rb', line 75

def put(path, data)
  request(:put, path, data)
end

#request(verb, path, data = {}, headers = {}) ⇒ String, Hash

Make an HTTP request with the given verb, data, params, and headers. If the response has a return type of JSON, the JSON is automatically parsed and returned as a hash; otherwise it is returned as a string.

Parameters:

  • verb (Symbol)

    the lowercase symbol of the HTTP verb (e.g. :get, :delete)

  • path (String)

    the absolute or relative path from Defaults.endpoint to make the request against

  • data (#read, Hash, nil) (defaults to: {})

    the data to use (varies based on the verb)

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

    the list of headers to use

Returns:

  • (String, Hash)

    the response body

Raises:

  • (Error::HTTPError)

    if the request is not an HTTP 200 OK



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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/vagrant-cloud/client.rb', line 107

def request(verb, path, data = {}, headers = {})
  log.info { "#{verb.to_s.upcase} #{path}" }

  # Add the access_token to the data, unless it was explicitly given to us
  # as part of the request object. If you explicitly give an access token,
  # it probably won't be filtered in log output...
  data['access_token'] ||= access_token if access_token

  # Build the URI and request object from the given information
  uri = build_uri(verb, path, data)
  request = class_for_request(verb).new(uri.request_uri)

  # Add headers
  add_request_headers(request, headers)

  # Setup PATCH/POST/PUT
  if [:patch, :post, :put].include?(verb)
    if data.respond_to?(:read)
      log.debug { 'Detected data is a stream or file' }
      log.debug { 'Setting body_stream' }
      request.body_stream = data
    elsif data.is_a?(Hash)
      log.debug { 'Detected data is a hash' }
      log.debug { 'Posting data as form_data' }
      request.form_data = data
    else
      log.debug { 'Detected data as unknown' }
      log.debug { 'Setting body to data and hoping it all works out' }
      request.body = data
    end
  end

  if proxy_address || proxy_port || proxy_username || proxy_password
    log.info  { 'Setting proxy information' }
    log.debug { "address:  #{proxy_address}" }
    log.debug { "port:     #{proxy_port}" }
    log.debug { "username: #{proxy_username}" }
    log.debug { "password: #{'*'*20}" }
  end

  # Create the HTTP connection object - since the proxy information defaults
  # to +nil+, we can just pass it to the initializer method instead of doing
  # crazy strange conditionals.
  connection = Net::HTTP.new(uri.host, uri.port,
    proxy_address, proxy_port, proxy_username, proxy_password)

  # Apply SSL, if applicable
  if uri.scheme == 'https'
    log.info { 'Detected request as SSL' }

    require 'net/https' unless defined?(Net::HTTPS)

    # Turn on SSL
    log.debug { 'Enabling SSL' }
    connection.use_ssl = true

    # Custom pem files, no problem!
    if ssl_pem_file
      log.debug { 'Detected a custom SSL pem file given' }
      log.debug { "Using SSL pem file from #{ssl_pem_file}" }
      pem = File.read(ssl_pem_file)
      connection.cert = OpenSSL::X509::Certificate.new(pem)
      connection.key = OpenSSL::PKey::RSA.new(pem)
      connection.verify_mode = OpenSSL::SSL::VERIFY_PEER
    end

    # Naughty, naughty, naughty! Don't blame when when someone hops in
    # and executes a MITM attack!
    unless ssl_verify
      log.debug { 'Disabling SSL verification' }
      connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end
  end

  # Create a connection using the block form, which will ensure the socket
  # is properly closed in the event of an error.
  connection.start do |http|
    # Make the request
    response = http.request(request)

    # Log the raw response
    log.debug { response.inspect }

    case response
    when Net::HTTPRedirection
      redirect = URI.parse(response['location'])
      log.info { "Performing HTTP redirect to #{redirect}" }
      request(verb, redirect, params, headers)
    when Net::HTTPSuccess
      success(response)
    else
      error(response)
    end
  end
rescue SocketError, Errno::ECONNREFUSED, EOFError
  log.fatal { 'Something really bad happened with the request' }
  raise RuntimeError, 'Something really bad happened!'
end

#same_options?(opts) ⇒ Boolean

Determine if the given options are the same as ours.

Returns:

  • (Boolean)


54
55
56
# File 'lib/vagrant-cloud/client.rb', line 54

def same_options?(opts)
  opts.hash == options.hash
end