Class: IDMClient

Inherits:
Object
  • Object
show all
Defined in:
lib/idmclient.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, options = Hash.new) ⇒ IDMClient

Returns a new instance of IDMClient.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/idmclient.rb', line 23

def initialize(uri, options=Hash.new)
  # Set defaults
  options.reverse_merge!({
    :ca_file => '/etc/ipa/ca.crt',
    :api_version => '2.228',
    :debug => false
  })
  @uri_base = uri
  @uri_auth = URI("#{uri_base}/session/login_password")
  @uri_data = URI("#{uri_base}/session/json")
  @api_version = options[:api_version]
  # Prepare the connection
  @http = Net::HTTP.new(uri_auth.host, uri_auth.port)
  http.use_ssl = uri_auth.scheme == 'https'
  http.ca_file = options[:ca_file]
  # Configure debug output
  if options[:debug]
    http.set_debug_output($stdout)
  end
  # Prepare cookie storage
  @cookiejar = HTTP::CookieJar.new
end

Instance Attribute Details

#api_versionObject (readonly)

Returns the value of attribute api_version.



21
22
23
# File 'lib/idmclient.rb', line 21

def api_version
  @api_version
end

#cookiejarObject (readonly)

Returns the value of attribute cookiejar.



21
22
23
# File 'lib/idmclient.rb', line 21

def cookiejar
  @cookiejar
end

#httpObject (readonly)

Returns the value of attribute http.



21
22
23
# File 'lib/idmclient.rb', line 21

def http
  @http
end

#uri_authObject (readonly)

Returns the value of attribute uri_auth.



21
22
23
# File 'lib/idmclient.rb', line 21

def uri_auth
  @uri_auth
end

#uri_baseObject (readonly)

Returns the value of attribute uri_base.



21
22
23
# File 'lib/idmclient.rb', line 21

def uri_base
  @uri_base
end

#uri_dataObject (readonly)

Returns the value of attribute uri_data.



21
22
23
# File 'lib/idmclient.rb', line 21

def uri_data
  @uri_data
end

Instance Method Details

#authenticate(username, password) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/idmclient.rb', line 46

def authenticate(username, password)
  http.start {
    # Prepare the authentication request
    req = Net::HTTP::Post.new(uri_auth, 'Referer' => uri_base)
    req.form_data = {
      :user => username,
      # Cloudforms / ManageIQ - Decrypt password if necessary
      :password => (MIQ_METHOD && password.match(/^v\d\:\{.*\}$/)) ? MiqPassword.decrypt(password) : password
    }
    # Make the authentication request
    res = http.request req
    # Save the returned cookies
    res.get_fields('Set-Cookie').each { |value| cookiejar.parse(value, req.uri) }
    # Expecting an HTTP 200 response
    return res.code == '200'
  }
end

#call(method, args = Array.new, options = Hash.new) ⇒ Object



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
# File 'lib/idmclient.rb', line 64

def call(method, args=Array.new, options=Hash.new)
  # Update options
  options['version'] = options['version'] || api_version
  # Start a connection
  http.start {
    # Prepare the data request
    req_id = SecureRandom.uuid
    req = Net::HTTP::Post.new(
      uri_data,
      'Content-Type' => 'application/json',
      'Referer' => uri_base,
      'Cookie' => HTTP::Cookie.cookie_value(cookiejar.cookies(uri_data)))
    req.body = {
      "method": method,
      "params": [args, options],
      "id": req_id,
    }.to_json
    # Make the data request and parse response
    res = http.request req
    data = JSON.parse(res.body)
    # Check for error
    if data['error']
      raise data['error']['message']
    end
    # Validate request Id and return results
    return (data['id'] == req_id) ? data['result']['result'] : nil
  }
end