class Netdot::RestClient

Manage RestClient objects.

RestClient

Constants

VERSION

Attributes

base_url[RW]
format[RW]
ua[RW]
xs[RW]

Public Class Methods

new(argv = {}) click to toggle source

Constructor and login method @option argv [String] :server (REQUIRED) Netdot server URL @option argv [String] :username (REQUIRED) Netdot Username @option argv [String] :password (REQUIRED) Netdot password @option argv [Fixnum] :retries (3) number of attempts @option argv [Fixnum] :timeout (10) timeout in seconds @option argv [String] :format (xml) content format <xml> @option argv [String] :ssl_version ('') specify version of SSL;

see HTTPClient

@option argv [String] :ssl_verify (true) verify server cert (default: yes) @option argv [String] :ssl_ca_file ('') path to SSL CA cert file @option argv [String] :ssl_ca_dir ('') path to SSL CA cert directory @option argv [String] :cookie_file ('./cookie.dat') cookie filename

# File lib/netdot/restclient.rb, line 24
def initialize(argv = {})
  [:server, :username, :password].each do |k|
    fail ArgumentError, "Missing required argument '#{k}'" unless argv[k]
  end

  argv.each { |k, v| instance_variable_set("@#{k}", v) }

  @timeout ||= 10
  @retries ||= 3
  @format ||= 'xml'
  @cookie_file ||= './cookie.dat'
  defined?(@ssl_verify) || @ssl_verify = true

  if (@format == 'xml')
    begin
      require 'xmlsimple'
    rescue LoadError
      raise LoadError,
            "Cannot load XML library. Try running 'gem install xml-simple'"
    end
    xs = XmlSimple.new('ForceArray' => true, 'KeyAttr' => 'id')
    @xs = xs
  else
    fail ArgumentError, 'Only XML formatting supported at this time'
  end

  ua = HTTPClient.new(agent_name: "Netdot::RestClient/#{version}")
  ua.set_cookie_store("#{@cookie_file}")

  # SSL stuff
  if @ssl_verify
    if @ssl_ca_dir
      # We are told to add a certs path
      # We'll want to clear the default cert store first
      ua.ssl_config.clear_cert_store
      ua.ssl_config.set_trust_ca(@ssl_ca_dir)
    elsif @ssl_ca_file
      ua.ssl_config.set_trust_ca(@ssl_ca_file)
    end
  else
    ua.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  # If version given, set it
  ua.ssl_config.ssl_version = @ssl_version if @ssl_version

  login_url = @server + '/NetdotLogin'

  resp = nil

  @retries.times do
    resp = ua.post login_url,
                   'destination'       => 'index.html',
                   'credential_0'      => @username,
                   'credential_1'      => @password,
                   'permanent_session' => 1

    if (resp.status == 302)
      ua.save_cookie_store
      @ua = ua
      @base_url = @server + '/rest'
      break
    else
      $stderr.puts "Warning: Connection attempt to #{@server} failed"
    end
  end

  return if (resp.status == 302)
  fail "Could not log into #{@server}. Status Code: '#{resp.status}'"
end

Public Instance Methods

build_url(resource) click to toggle source

Builds a URL for the specified resource. @param [String] resource a URI

# File lib/netdot/restclient.rb, line 102
def build_url(resource)
  base_url + '/' + resource
end
delete(resource) click to toggle source

Deletes a resource. @param [String] resource a URI @return [Truth] true when successful; exception when not

# File lib/netdot/restclient.rb, line 138
def delete(resource)
  url = build_url(resource)
  resp = ua.delete(url, nil, extheader)
  if (resp.status == 200)
    return true
  else
    fail "Could not delete #{url}: #{resp.status}"
  end
end
extheader() click to toggle source

Builds the Extra headers.

# File lib/netdot/restclient.rb, line 96
def extheader
  { 'Accept' => 'text/' + format + '; version=1.0' }
end
get(resource) click to toggle source

Gets a resource. @param [String] resource a URI @return [Hash] hash when successful; exception when not

# File lib/netdot/restclient.rb, line 109
def get(resource)
  url = build_url(resource)
  resp = ua.get(url, nil, extheader)
  if (resp.status == 200)
    xs.xml_in(resp.content)
  else
    fail "Could not get #{url}: #{resp.status}"
  end
end
post(resource, data) click to toggle source

Updates or creates a resource. @param [String] resource a URI @param [Hash] data @return [Hash] new or modified record hash when successful; exception

when not
# File lib/netdot/restclient.rb, line 124
def post(resource, data)
  url = build_url(resource)
  fail ArgumentError, 'Data must be hash' unless data.is_a?(Hash)
  resp = ua.post(url, data, extheader)
  if (resp.status == 200)
    xs.xml_in(resp.content)
  else
    fail "Could not post to #{url}: #{resp.status}"
  end
end
version() click to toggle source
# File lib/netdot/restclient/version.rb, line 7
def version
  VERSION
end