Class: RbVmomi::TrivialSoap
- Inherits:
-
Object
- Object
- RbVmomi::TrivialSoap
- Defined in:
- lib/rbvmomi/trivial_soap.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#cookie ⇒ Object
Returns the value of attribute cookie.
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#http ⇒ Object
readonly
Returns the value of attribute http.
-
#operation_id ⇒ Object
Returns the value of attribute operation_id.
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
- #host ⇒ Object
-
#initialize(opts) ⇒ TrivialSoap
constructor
A new instance of TrivialSoap.
- #request(action, body) ⇒ Object
- #restart_http ⇒ Object
- #soap_envelope ⇒ Object
Constructor Details
#initialize(opts) ⇒ TrivialSoap
Returns a new instance of TrivialSoap.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/rbvmomi/trivial_soap.rb', line 15 def initialize opts raise unless opts.is_a? Hash @opts = opts return unless @opts[:host] # for testcases @debug = @opts[:debug] @cookie = @opts[:cookie] @sso = @opts[:sso] @operation_id = @opts[:operation_id] @lock = Mutex.new @http = nil restart_http end |
Instance Attribute Details
#cookie ⇒ Object
Returns the value of attribute cookie.
12 13 14 |
# File 'lib/rbvmomi/trivial_soap.rb', line 12 def @cookie end |
#debug ⇒ Object
Returns the value of attribute debug.
12 13 14 |
# File 'lib/rbvmomi/trivial_soap.rb', line 12 def debug @debug end |
#http ⇒ Object (readonly)
Returns the value of attribute http.
13 14 15 |
# File 'lib/rbvmomi/trivial_soap.rb', line 13 def http @http end |
#operation_id ⇒ Object
Returns the value of attribute operation_id.
12 13 14 |
# File 'lib/rbvmomi/trivial_soap.rb', line 12 def operation_id @operation_id end |
Class Method Details
.on_connect ⇒ Object
57 58 59 |
# File 'lib/rbvmomi/trivial_soap.rb', line 57 def @http.on_connect @socket.io.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) end |
Instance Method Details
#close ⇒ Object
34 35 36 |
# File 'lib/rbvmomi/trivial_soap.rb', line 34 def close @http.finish rescue IOError end |
#host ⇒ Object
30 31 32 |
# File 'lib/rbvmomi/trivial_soap.rb', line 30 def host @opts[:host] end |
#request(action, body) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/rbvmomi/trivial_soap.rb', line 83 def request action, body headers = { 'content-type' => 'text/xml; charset=utf-8', 'SOAPAction' => action } headers['cookie'] = @cookie if @cookie RbVmomi.logger.debug("Request:\n#{body}") if @debug if @cookie.nil? && @sso @sso.request_token unless @sso.assertion_id body = @sso.sign_request(body) end start_time = Time.now response = @lock.synchronize do begin @http.request_post(@opts[:path], body, headers) rescue Exception restart_http raise end end end_time = Time.now raise 'Got HTTP 503: Service unavailable' if response.is_a? Net::HTTPServiceUnavailable self. = response['set-cookie'] if response.key? 'set-cookie' nk = Nokogiri(response.body) RbVmomi.logger.debug("Response (in #{'%.3f' % (end_time - start_time)} s)\n#{nk}") if @debug [nk.xpath('//soapenv:Body/*').select(&:element?).first, response.body.size] end |
#restart_http ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rbvmomi/trivial_soap.rb', line 38 def restart_http begin @http.finish if @http rescue Exception => ex puts "WARNING: Ignoring exception: #{ex.}" puts ex.backtrace.join("\n") end @http = Net::HTTP.new(@opts[:host], @opts[:port], @opts[:proxyHost], @opts[:proxyPort]) if @opts[:ssl] require 'net/https' @http.use_ssl = true @http.verify_mode = OpenSSL::SSL::VERIFY_NONE if @opts[:insecure] @http.ca_file = @opts[:ca_file] if @opts[:ca_file] @http.cert = OpenSSL::X509::Certificate.new(@opts[:cert]) if @opts[:cert] @http.key = OpenSSL::PKey::RSA.new(@opts[:key]) if @opts[:key] end @http.set_debug_output(STDERR) if $DEBUG @http.read_timeout = @opts[:read_timeout] || 1000000 @http.open_timeout = @opts[:open_timeout] || 60 def @http.on_connect @socket.io.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) end @http.start end |
#soap_envelope ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/rbvmomi/trivial_soap.rb', line 63 def soap_envelope xsd = 'http://www.w3.org/2001/XMLSchema' env = 'http://schemas.xmlsoap.org/soap/envelope/' xsi = 'http://www.w3.org/2001/XMLSchema-instance' xml = Builder::XmlMarkup.new indent: 0 xml.tag!('env:Envelope', 'xmlns:xsd' => xsd, 'xmlns:env' => env, 'xmlns:xsi' => xsi) do if @vcSessionCookie || @operation_id xml.tag!('env:Header') do xml.tag!('vcSessionCookie', @vcSessionCookie) if @vcSessionCookie xml.tag!('operationID', @operation_id) if @operation_id end end xml.tag!('env:Body') do yield xml if block_given? end end xml end |