class F5::Icontrol::API

Attributes

api_path[RW]

Public Class Methods

new(api_path = nil, **params) click to toggle source
# File lib/f5/icontrol/api.rb, line 6
def initialize(api_path = nil, **params)
  @params = params.dup
  @username = params[:username]
  @password = params[:password]
  @hostname = params[:host] || params[:hostname]
  @enable_logging = params[:enable_logging] || false
  @log_level = params[:log_level] ? params[:log_level].to_sym : :debug
  @pretty_print_xml = params[:pretty_print_xml] || true
  @client_cache = {}
  @api_path = api_path
end

Public Instance Methods

method_missing(method, args = nil, &block) click to toggle source
# File lib/f5/icontrol/api.rb, line 18
def method_missing(method, args = nil, &block)
  if terminal_node? && supported_method?(method)
      response_key = "#{method.to_s}_response".to_sym

      response = client.call(method) do
        if args
          message args
        end
      end

      response.to_hash[response_key][:return]

  elsif supported_path? append_path(method)
    self.class.new append_path(method), @params
  else
    raise NameError, "#{method} is not supported by #{@api_path}"
  end
end

Private Instance Methods

append_path(item) click to toggle source
# File lib/f5/icontrol/api.rb, line 51
def append_path(item)
  @api_path ? "#{@api_path}.#{item}" : item.to_s
end
client() click to toggle source
# File lib/f5/icontrol/api.rb, line 59
def client
  api_namespace = @api_path.gsub /\./, '/'
  endpoint = '/iControl/iControlPortal.cgi'
  @client_cache[@api_path] ||=
    Savon.client(wsdl: "#{wsdl_path}#{@api_path}.wsdl",
                 endpoint: "https://#{@hostname || F5::Icontrol.configuration.host}#{endpoint}",
                 ssl_verify_mode: :none,
                 basic_auth: [@username || F5::Icontrol.configuration.username, @password || F5::Icontrol.configuration.password],
                 log: @enable_logging,
                 logger: Logger.new(STDOUT),
                 pretty_print_xml: @pretty_print_xml,
                 log_level: @log_level,
                 namespace: "urn:iControl:#{api_namespace}",
                 convert_request_keys_to: :none
                )
end
supported_method?(method) click to toggle source
# File lib/f5/icontrol/api.rb, line 43
def supported_method?(method)
  client.operations.include?(method)
end
supported_path?(path) click to toggle source
# File lib/f5/icontrol/api.rb, line 47
def supported_path?(path)
  ! Dir.glob("#{wsdl_path}/#{path}.*wsdl").empty?
end
terminal_node?() click to toggle source
# File lib/f5/icontrol/api.rb, line 39
def terminal_node?
  ::File.exists? "#{wsdl_path}/#{@api_path}.wsdl"
end
wsdl_path() click to toggle source
# File lib/f5/icontrol/api.rb, line 55
def wsdl_path
  File.dirname(__FILE__).gsub /(f5-icontrol[^\/]*\/lib)\/.*/, "\\1/wsdl/"
end