Class: Netscaler::Transaction

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/netscaler/transaction.rb

Instance Method Summary collapse

Methods included from Logging

configure, log, #log

Constructor Details

#initialize(config, &block) ⇒ Transaction

Returns a new instance of Transaction.



10
11
12
13
14
15
# File 'lib/netscaler/transaction.rb', line 10

def initialize(config, &block)
  @config = config
  if block_given?
    execute(&block)
  end
end

Instance Method Details

#execute(&block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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/netscaler/transaction.rb', line 17

def execute(&block)
  if !block_given?
    raise Netscaler::TransactionError.new("No execution block given.")
  end
  
  log.debug("Beginning the transaction execution: #{@config.host}")
  begin
    client = Savon::Client.new do 
      wsdl.endpoint = url
      wsdl.namespace = Netscaler::NSCONFIG_NAMESPACE
    end
    client.http.auth.ssl.verify_mode = :none

    log.debug("Logging in to the Netscaler host.")
    body = { :username => @config.username, :password => @config.password }

    response = client.request :login do
      #soap.namespace = Netscaler::NSCONFIG_NAMESPACE
      soap.body = body
    end

    auth_cookie = response.http.headers['Set-Cookie']
    client.http.headers['Cookie'] = auth_cookie
    log.debug("Got authorization cookie: #{auth_cookie}")

    log.debug("Yielding client control to the calling context")
    yield client
  rescue SystemExit => e
    raise
  rescue Exception => e
    log.fatal(e)
    log.fatal("Unable to execute transaction.")
    raise Netscaler::TransactionError.new(e)
  ensure
    begin
      log.debug("Logging out of the Netscaler host.")
      client.request :logout
    rescue Exception => e
      log.fatal(e)
      log.fatal("Unable to logout.")
    end
  end

  log.debug("Ending the transaction execution: #{@config.host}")
end