def authenticated_request(method, path, options = {}, retry_count = 0)
Make an authenticated request to the iDRAC
def authenticated_request(method, path, options = {}, retry_count = 0)
# Limit retries to prevent infinite loops
if retry_count >= 3
puts "Maximum retry count reached for authenticated request".red.bold
raise Error, "Maximum retry count reached for authenticated request"
end
debug "Authenticated request: #{method.to_s.upcase} #{path}", 1
# If we're in direct mode, use Basic Auth
if @direct_mode
# Create Basic Auth header
auth_header = "Basic #{Base64.strict_encode64("#{username}:#{password}")}"
# Add the Authorization header to the request
options[:headers] ||= {}
options[:headers]['Authorization'] = auth_header
debug "Using Basic Auth for request", 2
# Make the request
begin
response = connection.send(method, path) do |req|
req.headers.merge!(options[:headers])
req.body = options[:body] if options[:body]
end
debug "Response status: #{response.status}", 1
debug "Response headers: #{response.headers.inspect}", 2
debug "Response body: #{response.body}", 3 if response.body
return response
rescue => e
puts "Error during authenticated request (direct mode): #{e.message}".red.bold
raise Error, "Error during authenticated request: #{e.message}"
end
else
# Use X-Auth-Token if available
if session.x_auth_token
# Add the X-Auth-Token header to the request
options[:headers] ||= {}
options[:headers]['X-Auth-Token'] = session.x_auth_token
debug "Using X-Auth-Token for request", 2
# Make the request
begin
response = connection.send(method, path) do |req|
req.headers.merge!(options[:headers])
req.body = options[:body] if options[:body]
end
debug "Response status: #{response.status}", 1
debug "Response headers: #{response.headers.inspect}", 2
debug "Response body: #{response.body}", 3 if response.body
# Check if the session is still valid
if response.status == 401 || response.status == 403
puts "Session expired or invalid, attempting to create a new session...".light_yellow
# Try to create a new session
if session.create
puts "Successfully created a new session, retrying request...".green
return authenticated_request(method, path, options, retry_count + 1)
else
puts "Failed to create a new session, falling back to direct mode...".light_yellow
@direct_mode = true
return authenticated_request(method, path, options, retry_count + 1)
end
end
return response
rescue => e
puts "Error during authenticated request (token mode): #{e.message}".red.bold
# Try to create a new session
if session.create
puts "Successfully created a new session after error, retrying request...".green
return authenticated_request(method, path, options, retry_count + 1)
else
puts "Failed to create a new session after error, falling back to direct mode...".light_yellow
@direct_mode = true
return authenticated_request(method, path, options, retry_count + 1)
end
end
else
# If we don't have a token, try to create a session
if session.create
puts "Successfully created a new session, making request...".green
return authenticated_request(method, path, options, retry_count + 1)
else
puts "Failed to create a session, falling back to direct mode...".light_yellow
@direct_mode = true
return authenticated_request(method, path, options, retry_count + 1)
end
end
end
end