Class: Radfish::Core::Session
- Inherits:
-
Object
- Object
- Radfish::Core::Session
- Includes:
- Debuggable
- Defined in:
- lib/radfish/core/session.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#session_id ⇒ Object
readonly
Returns the value of attribute session_id.
-
#x_auth_token ⇒ Object
readonly
Returns the value of attribute x_auth_token.
Instance Method Summary collapse
- #connection ⇒ Object
- #create ⇒ Object
- #delete ⇒ Object
-
#initialize(client) ⇒ Session
constructor
A new instance of Session.
- #valid? ⇒ Boolean
- #verbosity ⇒ Object
Methods included from Debuggable
Constructor Details
#initialize(client) ⇒ Session
Returns a new instance of Session.
10 11 12 13 14 |
# File 'lib/radfish/core/session.rb', line 10 def initialize(client) @client = client @x_auth_token = nil @session_id = nil end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
8 9 10 |
# File 'lib/radfish/core/session.rb', line 8 def client @client end |
#session_id ⇒ Object (readonly)
Returns the value of attribute session_id.
8 9 10 |
# File 'lib/radfish/core/session.rb', line 8 def session_id @session_id end |
#x_auth_token ⇒ Object (readonly)
Returns the value of attribute x_auth_token.
8 9 10 |
# File 'lib/radfish/core/session.rb', line 8 def x_auth_token @x_auth_token end |
Instance Method Details
#connection ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/radfish/core/session.rb', line 16 def connection @connection ||= begin url = URI(client.base_url) HttpClient.new(host: url.host, port: url.port, use_ssl: url.scheme == 'https', verify_ssl: client.verify_ssl, host_header: client.host_header, retry_count: client.retry_count, retry_delay: client.retry_delay, verbosity: verbosity) end end |
#create ⇒ Object
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 |
# File 'lib/radfish/core/session.rb', line 28 def create debug "Creating Redfish session for #{client.host}", 1 payload = { UserName: client.username, Password: client.password }.to_json headers = { 'Content-Type' => 'application/json', 'Accept' => 'application/json' } begin response = connection.post('/redfish/v1/SessionService/Sessions', body: payload, headers: headers) if response.status == 201 @x_auth_token = response.headers['x-auth-token'] if response.headers['location'] @session_id = response.headers['location'].split('/').last end begin body = JSON.parse(response.body) @session_id ||= body["Id"] if body.is_a?(Hash) rescue JSON::ParserError end debug "Session #{@session_id || '?'} created successfully (token #{@x_auth_token ? 'received' : 'missing'})", 1, :green return true else debug "Failed to create session. Status: #{response.status}", 1, :red debug "Response: #{HttpClient.scrub(response.body)}", 2 return false end rescue Radfish::Error => e debug "Connection error creating session: #{e.}", 1, :red return false end end |
#delete ⇒ Object
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/radfish/core/session.rb', line 70 def delete return unless @x_auth_token && @session_id debug "Deleting session #{@session_id}", 1 headers = { 'X-Auth-Token' => @x_auth_token, 'Accept' => 'application/json' } begin response = connection.delete("/redfish/v1/SessionService/Sessions/#{@session_id}", headers: headers) if response.status == 204 || response.status == 200 debug "Session deleted successfully", 1, :green @x_auth_token = nil @session_id = nil return true else debug "Failed to delete session. Status: #{response.status}", 1, :yellow return false end rescue Radfish::Error => e debug "Error deleting session: #{e.}", 1, :yellow return false end end |
#valid? ⇒ Boolean
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/radfish/core/session.rb', line 98 def valid? return false unless @x_auth_token headers = { 'X-Auth-Token' => @x_auth_token, 'Accept' => 'application/json' } begin response = connection.get("/redfish/v1/SessionService/Sessions/#{@session_id}", headers: headers) response.status == 200 rescue false end end |
#verbosity ⇒ Object
114 115 116 |
# File 'lib/radfish/core/session.rb', line 114 def verbosity client.verbosity end |