Class: Netdot::RestClient

Inherits:
Object
  • Object
show all
Defined in:
lib/netdot/restclient.rb,
lib/netdot/restclient/version.rb

Overview

RestClient

Constant Summary collapse

VERSION =
'2.0.3'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv = {}) ⇒ RestClient

Constructor and login method

Parameters:

  • argv (Hash) (defaults to: {})

    a customizable set of options

Options Hash (argv):

  • :server (String) — default: REQUIRED

    Netdot server URL

  • :username (String) — default: REQUIRED

    Netdot Username

  • :password (String) — default: REQUIRED

    Netdot password

  • :retries (Fixnum) — default: 3

    number of attempts

  • :timeout (Fixnum) — default: 10

    timeout in seconds

  • :format (String) — default: xml

    content format <xml>

  • :ssl_version (String) — default: ''

    specify version of SSL; see HTTPClient

  • :ssl_verify (String) — default: true

    verify server cert (default: yes)

  • :ssl_ca_file (String) — default: ''

    path to SSL CA cert file

  • :ssl_ca_dir (String) — default: ''

    path to SSL CA cert directory

  • :cookie_file (String) — default: './cookie.dat'

    cookie filename



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
62
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
# 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

   = @server + '/NetdotLogin'

  resp = nil

  @retries.times do
    resp = ua.post ,
                   '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

Instance Attribute Details

#base_urlObject

Returns the value of attribute base_url.



9
10
11
# File 'lib/netdot/restclient.rb', line 9

def base_url
  @base_url
end

#formatObject

Returns the value of attribute format.



9
10
11
# File 'lib/netdot/restclient.rb', line 9

def format
  @format
end

#uaObject

Returns the value of attribute ua.



9
10
11
# File 'lib/netdot/restclient.rb', line 9

def ua
  @ua
end

#xsObject

Returns the value of attribute xs.



9
10
11
# File 'lib/netdot/restclient.rb', line 9

def xs
  @xs
end

Instance Method Details

#build_url(resource) ⇒ Object

Builds a URL for the specified resource.

Parameters:

  • resource (String)

    a URI



102
103
104
# File 'lib/netdot/restclient.rb', line 102

def build_url(resource)
  base_url + '/' + resource
end

#delete(resource) ⇒ Truth

Deletes a resource.

Parameters:

  • resource (String)

    a URI

Returns:

  • (Truth)

    true when successful; exception when not



138
139
140
141
142
143
144
145
146
# 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

#extheaderObject

Builds the Extra headers.



96
97
98
# File 'lib/netdot/restclient.rb', line 96

def extheader
  { 'Accept' => 'text/' + format + '; version=1.0' }
end

#get(resource) ⇒ Hash

Gets a resource.

Parameters:

  • resource (String)

    a URI

Returns:

  • (Hash)

    hash when successful; exception when not



109
110
111
112
113
114
115
116
117
# 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) ⇒ Hash

Updates or creates a resource.

Parameters:

  • resource (String)

    a URI

  • data (Hash)

Returns:

  • (Hash)

    new or modified record hash when successful; exception when not



124
125
126
127
128
129
130
131
132
133
# 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

#versionObject



7
8
9
# File 'lib/netdot/restclient/version.rb', line 7

def version
  VERSION
end