Class: HashiCorp::VagrantVMwareDesktop::Helper::VagrantUtility
- Inherits:
-
Object
- Object
- HashiCorp::VagrantVMwareDesktop::Helper::VagrantUtility
- Defined in:
- lib/vagrant-vmware-desktop/helper/vagrant_utility.rb
Overview
This is a class for dealing the vagrant vmware utility API
Defined Under Namespace
Classes: Response
Instance Attribute Summary collapse
- #connection ⇒ Net::HTTP readonly
- #headers ⇒ Hash readonly
- #logger ⇒ Log4r::Logger readonly
Instance Method Summary collapse
-
#delete(path, payload = nil) ⇒ Response
Perform DELETE.
-
#get(path) ⇒ Response
Perform GET.
-
#initialize(host, port, **opts) ⇒ VagrantUtility
constructor
A new instance of VagrantUtility.
-
#perform_request(method, path, data = nil, rheaders = {}) ⇒ Response
Perform the remote request and process the result.
-
#post(path, payload = nil) ⇒ Response
Perform POST.
-
#process_response ⇒ Response
Wraps response into Response instance.
-
#put(path, payload = nil) ⇒ Response
Perform PUT.
Constructor Details
#initialize(host, port, **opts) ⇒ VagrantUtility
Returns a new instance of VagrantUtility.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/vagrant-vmware-desktop/helper/vagrant_utility.rb', line 60 def initialize(host, port, **opts) @logger = Log4r::Logger.new("hashicorp::provider::vmware::vagrant_utility") @logger.debug("initialize HOST=#{host} PORT=#{port}") @connection = Net::HTTP.new(host, port) @connection.use_ssl = true @connection.verify_mode = OpenSSL::SSL::VERIFY_PEER @connection.ca_file = File.join(opts[:certificate_path], "vagrant-utility.crt") @headers = { "Content-Type" => "application/vnd.hashicorp.vagrant.vmware.rest-v1+json", "Origin" => "https://#{host}:#{port}", "User-Agent" => Vagrant::Util::Downloader::USER_AGENT + " - VagrantVMWareDesktop/#{VagrantVMwareDesktop::VERSION}", "X-Requested-With" => "Vagrant", } cert_path = File.join(opts[:certificate_path], "vagrant-utility.client.crt") key_path = File.join(opts[:certificate_path], "vagrant-utility.client.key") begin @connection.cert = OpenSSL::X509::Certificate.new(File.read(cert_path)) rescue => err @logger.debug("certificate load failure - #{err.class}: #{err}") raise Errors::DriverAPICertificateError.new( path: cert_path, message: err. ) end begin @connection.key = OpenSSL::PKey::RSA.new(File.read(key_path)) rescue => err @logger.debug("key load failure - #{err.class}: #{err}") raise Errors::DriverAPIKeyError.new( path: key_path, message: err. ) end end |
Instance Attribute Details
#connection ⇒ Net::HTTP (readonly)
54 55 56 |
# File 'lib/vagrant-vmware-desktop/helper/vagrant_utility.rb', line 54 def connection @connection end |
#headers ⇒ Hash (readonly)
56 57 58 |
# File 'lib/vagrant-vmware-desktop/helper/vagrant_utility.rb', line 56 def headers @headers end |
#logger ⇒ Log4r::Logger (readonly)
58 59 60 |
# File 'lib/vagrant-vmware-desktop/helper/vagrant_utility.rb', line 58 def logger @logger end |
Instance Method Details
#delete(path, payload = nil) ⇒ Response
Perform DELETE
127 128 129 |
# File 'lib/vagrant-vmware-desktop/helper/vagrant_utility.rb', line 127 def delete(path, payload=nil) perform_request(:delete, path, payload) end |
#get(path) ⇒ Response
Perform GET
100 101 102 |
# File 'lib/vagrant-vmware-desktop/helper/vagrant_utility.rb', line 100 def get(path) perform_request(:get, path) end |
#perform_request(method, path, data = nil, rheaders = {}) ⇒ Response
Perform the remote request and process the result
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/vagrant-vmware-desktop/helper/vagrant_utility.rb', line 138 def perform_request(method, path, data=nil, rheaders={}) req_headers = headers.merge(rheaders) if data && !data.is_a?(String) data = JSON.generate(data) end method = method.to_s.upcase response = process_response do connection.send_request(method, path, data, req_headers) end if !response.success? error = "ERROR=#{response.get(:content, :message)}" end @logger.debug("request METHOD=#{method} PATH=#{path} RESPONSE=#{response.get(:code)} #{error}") response end |
#post(path, payload = nil) ⇒ Response
Perform POST
118 119 120 |
# File 'lib/vagrant-vmware-desktop/helper/vagrant_utility.rb', line 118 def post(path, payload=nil) perform_request(:post, path, payload) end |
#process_response ⇒ Response
Wraps response into Response instance
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/vagrant-vmware-desktop/helper/vagrant_utility.rb', line 158 def process_response begin response = yield result = { code: response.code.to_i, success: response.code.start_with?('2') } if response.class.body_permitted? body = response.body begin result[:content] = JSON.parse(body, :symbolize_names => true) rescue result[:content] = body end else result[:content] = nil end Response.new(result) rescue Net::HTTPServiceUnavailable raise Errors::DriverAPIConnectionFailed rescue => err raise Errors::DriverAPIRequestUnexpectedError, error: err end end |
#put(path, payload = nil) ⇒ Response
Perform PUT
109 110 111 |
# File 'lib/vagrant-vmware-desktop/helper/vagrant_utility.rb', line 109 def put(path, payload=nil) perform_request(:put, path, payload) end |