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.
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 95 96 97 |
# File 'lib/vagrant-vmware-desktop/helper/vagrant_utility.rb', line 63 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)
57 58 59 |
# File 'lib/vagrant-vmware-desktop/helper/vagrant_utility.rb', line 57 def connection @connection end |
#headers ⇒ Hash (readonly)
59 60 61 |
# File 'lib/vagrant-vmware-desktop/helper/vagrant_utility.rb', line 59 def headers @headers end |
#logger ⇒ Log4r::Logger (readonly)
61 62 63 |
# File 'lib/vagrant-vmware-desktop/helper/vagrant_utility.rb', line 61 def logger @logger end |
Instance Method Details
#delete(path, payload = nil) ⇒ Response
Perform DELETE
130 131 132 |
# File 'lib/vagrant-vmware-desktop/helper/vagrant_utility.rb', line 130 def delete(path, payload=nil) perform_request(:delete, path, payload) end |
#get(path) ⇒ Response
Perform GET
103 104 105 |
# File 'lib/vagrant-vmware-desktop/helper/vagrant_utility.rb', line 103 def get(path) perform_request(:get, path) end |
#perform_request(method, path, data = nil, rheaders = {}) ⇒ Response
Perform the remote request and process the result
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/vagrant-vmware-desktop/helper/vagrant_utility.rb', line 141 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
121 122 123 |
# File 'lib/vagrant-vmware-desktop/helper/vagrant_utility.rb', line 121 def post(path, payload=nil) perform_request(:post, path, payload) end |
#process_response ⇒ Response
Wraps response into Response instance
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/vagrant-vmware-desktop/helper/vagrant_utility.rb', line 161 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
112 113 114 |
# File 'lib/vagrant-vmware-desktop/helper/vagrant_utility.rb', line 112 def put(path, payload=nil) perform_request(:put, path, payload) end |