Class: Netdot::RestClient
- Inherits:
-
Object
- Object
- Netdot::RestClient
- Defined in:
- lib/netdot/restclient.rb,
lib/netdot/restclient/version.rb
Constant Summary collapse
- VERSION =
"1.1"
Instance Attribute Summary collapse
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#format ⇒ Object
Returns the value of attribute format.
-
#ua ⇒ Object
Returns the value of attribute ua.
-
#xs ⇒ Object
Returns the value of attribute xs.
Instance Method Summary collapse
-
#build_url(resource) ⇒ Object
Build URL given a resource.
-
#delete(resource) ⇒ Object
Delete a resource.
-
#extheader ⇒ Object
Build the Extra headers.
-
#get(resource) ⇒ Object
Get a resource.
-
#initialize(argv = {}) ⇒ RestClient
constructor
Constructor and login method.
-
#post(resource, data) ⇒ Object
Update or create a resource.
- #version ⇒ Object
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_verify - Verify server cert (default: yes) ssl_ca_file - Path to SSL CA cert file ssl_ca_dir - Path to SSL CA cert directory
Returns:
Netdot::RestClient object
Example:
Netdot::Restclient.new(args)
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 94 95 96 |
# File 'lib/netdot/restclient.rb', line 28 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' 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.("cookie.dat") # 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 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. @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_url ⇒ Object
Returns the value of attribute base_url.
7 8 9 |
# File 'lib/netdot/restclient.rb', line 7 def base_url @base_url end |
#format ⇒ Object
Returns the value of attribute format.
7 8 9 |
# File 'lib/netdot/restclient.rb', line 7 def format @format end |
#ua ⇒ Object
Returns the value of attribute ua.
7 8 9 |
# File 'lib/netdot/restclient.rb', line 7 def ua @ua end |
#xs ⇒ Object
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
107 108 109 |
# File 'lib/netdot/restclient.rb', line 107 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
158 159 160 161 162 163 164 165 166 167 |
# File 'lib/netdot/restclient.rb', line 158 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 |
#extheader ⇒ Object
Build the Extra headers
101 102 103 |
# File 'lib/netdot/restclient.rb', line 101 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
118 119 120 121 122 123 124 125 126 |
# File 'lib/netdot/restclient.rb', line 118 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
137 138 139 140 141 142 143 144 145 146 |
# File 'lib/netdot/restclient.rb', line 137 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 |
#version ⇒ Object
5 6 7 |
# File 'lib/netdot/restclient/version.rb', line 5 def version VERSION end |