Class: Netdot::RestClient

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

Constant Summary collapse

VERSION =
"1.4"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv = {}) ⇒ RestClient

Constructor and login method

Arguments (hash):

server - Netdot server URL username - Netdot Username password - Netdot password retries - Number of attempts timeout - Timeout in seconds format - Content format <xml> ssl_version - Specify version of SSL (see httpclient) ssl_verify - Verify server cert (default: yes) ssl_ca_file - Path to SSL CA cert file ssl_ca_dir - Path to SSL CA cert directory cookie_file - Cookie filename

Returns:

Netdot::RestClient object

Example:

Netdot::Restclient.new(args)


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
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/netdot/restclient.rb', line 30

def initialize(argv = {})
  
  [:server, :username, :password].each do |k|
    raise 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) or @ssl_verify = true

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

  ua = HTTPClient.new(:agent_name => "Netdot::RestClient/#{self.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
  if ( @ssl_version )
    ua.ssl_config.ssl_version = @ssl_version
  end

   = @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

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

end

Instance Attribute Details

#base_urlObject

Returns the value of attribute base_url.



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

def base_url
  @base_url
end

#formatObject

Returns the value of attribute format.



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

def format
  @format
end

#uaObject

Returns the value of attribute ua.



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

def ua
  @ua
end

#xsObject

Returns the value of attribute xs.



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

def xs
  @xs
end

Instance Method Details

#build_url(resource) ⇒ Object

Build URL given a resource



115
116
117
# File 'lib/netdot/restclient.rb', line 115

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

#delete(resource) ⇒ Object

Delete a resource

Arguments:

resource - A URI

Returns:

true when successful
exception when not


166
167
168
169
170
171
172
173
174
175
# File 'lib/netdot/restclient.rb', line 166

def delete(resource)
  url = self.build_url(resource)
  resp = self.ua.delete(url, nil, self.extheader)
  if ( resp.status == 200 )
    return true
  else
    raise "Could not delete #{url}: #{resp.status}"
  end

end

#extheaderObject

Build the Extra headers



109
110
111
# File 'lib/netdot/restclient.rb', line 109

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

#get(resource) ⇒ Object

Get a resource

Arguments:

resource - A URI

Returns:

hash when successful
exception when not


126
127
128
129
130
131
132
133
134
# File 'lib/netdot/restclient.rb', line 126

def get(resource)
  url = self.build_url(resource)
  resp = self.ua.get(url, nil, self.extheader)
  if ( resp.status == 200 )
    self.xs.xml_in(resp.content)
  else
    raise "Could not get #{url}: #{resp.status}" 
  end
end

#post(resource, data) ⇒ Object

Update or create a resource

Arguments:

resource - A URI
data     - Hash with key/values

Returns:

new or modified record hash when successful
exception when not

Raises:

  • (ArgumentError)


145
146
147
148
149
150
151
152
153
154
# File 'lib/netdot/restclient.rb', line 145

def post(resource, data)
  url = self.build_url(resource)
  raise ArgumentError, "Data must be hash" unless data.is_a?(Hash)
  resp = self.ua.post(url, data, self.extheader)
  if ( resp.status == 200 )
    self.xs.xml_in(resp.content)
  else
    raise "Could not post to #{url}: #{resp.status}"
  end
end

#versionObject



5
6
7
# File 'lib/netdot/restclient/version.rb', line 5

def version
  VERSION
end