Class: Supermicro::Session

Inherits:
Object
  • Object
show all
Includes:
Debuggable
Defined in:
lib/supermicro/session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Debuggable

#debug

Constructor Details

#initialize(client) ⇒ Session

Returns a new instance of Session.



13
14
15
16
17
# File 'lib/supermicro/session.rb', line 13

def initialize(client)
  @client = client
  @x_auth_token = nil
  @session_id = nil
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'lib/supermicro/session.rb', line 9

def client
  @client
end

#session_idObject (readonly)

Returns the value of attribute session_id.



9
10
11
# File 'lib/supermicro/session.rb', line 9

def session_id
  @session_id
end

#x_auth_tokenObject (readonly)

Returns the value of attribute x_auth_token.



9
10
11
# File 'lib/supermicro/session.rb', line 9

def x_auth_token
  @x_auth_token
end

Instance Method Details

#connectionObject



19
20
21
22
23
24
# File 'lib/supermicro/session.rb', line 19

def connection
  @connection ||= Faraday.new(url: client.base_url, ssl: { verify: client.verify_ssl }) do |faraday|
    faraday.request :url_encoded
    faraday.adapter Faraday.default_adapter
  end
end

#createObject



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
# File 'lib/supermicro/session.rb', line 26

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'
  }
  headers['Host'] = client.host_header if client.host_header
  
  begin
    response = connection.post('/redfish/v1/SessionService/Sessions', payload, 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 created successfully. Token: #{@x_auth_token ? @x_auth_token[0..10] + '...' : 'nil'}", 1, :green
      return true
    else
      debug "Failed to create session. Status: #{response.status}", 1, :red
      debug "Response: #{response.body}", 2
      return false
    end
  rescue Faraday::Error => e
    debug "Connection error creating session: #{e.message}", 1, :red
    return false
  end
end

#deleteObject



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/supermicro/session.rb', line 69

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'
  }
  headers['Host'] = client.host_header if client.host_header
  
  begin
    response = connection.delete("/redfish/v1/SessionService/Sessions/#{@session_id}", nil, 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 Faraday::Error => e
    debug "Error deleting session: #{e.message}", 1, :yellow
    return false
  end
end

#valid?Boolean

Returns:

  • (Boolean)


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/supermicro/session.rb', line 98

def valid?
  return false unless @x_auth_token
  
  headers = {
    'X-Auth-Token' => @x_auth_token,
    'Accept' => 'application/json'
  }
  headers['Host'] = client.host_header if client.host_header
  
  begin
    response = connection.get("/redfish/v1/SessionService/Sessions/#{@session_id}", nil, headers)
    response.status == 200
  rescue
    false
  end
end