Class: Radfish::Core::BaseClient

Inherits:
Object
  • Object
show all
Includes:
Debuggable
Defined in:
lib/radfish/core/base_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Debuggable

#debug

Constructor Details

#initialize(host:, username:, password:, port: 443, use_ssl: true, verify_ssl: false, retry_count: 3, retry_delay: 1, host_header: nil, **options) ⇒ BaseClient

Returns a new instance of BaseClient.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/radfish/core/base_client.rb', line 13

def initialize(host:, username:, password:, port: 443, use_ssl: true, verify_ssl: false, 
               retry_count: 3, retry_delay: 1, host_header: nil, **options)
  @host = host
  @username = username
  @password = password
  @port = port
  @use_ssl = use_ssl
  @verify_ssl = verify_ssl
  @host_header = host_header
  @verbosity = 0
  @retry_count = retry_count
  @retry_delay = retry_delay
  
  # Store any vendor-specific options
  @options = options
  
  # Create the HTTP client
  @http_client = HttpClient.new(
    host: host,
    port: port,
    use_ssl: use_ssl,
    verify_ssl: verify_ssl,
    username: username,
    password: password,
    verbosity: verbosity,
    retry_count: retry_count,
    retry_delay: retry_delay
  )
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



10
11
12
# File 'lib/radfish/core/base_client.rb', line 10

def host
  @host
end

#host_headerObject (readonly)

Returns the value of attribute host_header.



10
11
12
# File 'lib/radfish/core/base_client.rb', line 10

def host_header
  @host_header
end

#passwordObject (readonly)

Returns the value of attribute password.



10
11
12
# File 'lib/radfish/core/base_client.rb', line 10

def password
  @password
end

#portObject (readonly)

Returns the value of attribute port.



10
11
12
# File 'lib/radfish/core/base_client.rb', line 10

def port
  @port
end

#retry_countObject

Returns the value of attribute retry_count.



11
12
13
# File 'lib/radfish/core/base_client.rb', line 11

def retry_count
  @retry_count
end

#retry_delayObject

Returns the value of attribute retry_delay.



11
12
13
# File 'lib/radfish/core/base_client.rb', line 11

def retry_delay
  @retry_delay
end

#use_sslObject (readonly)

Returns the value of attribute use_ssl.



10
11
12
# File 'lib/radfish/core/base_client.rb', line 10

def use_ssl
  @use_ssl
end

#usernameObject (readonly)

Returns the value of attribute username.



10
11
12
# File 'lib/radfish/core/base_client.rb', line 10

def username
  @username
end

#verbosityObject

Returns the value of attribute verbosity.



11
12
13
# File 'lib/radfish/core/base_client.rb', line 11

def verbosity
  @verbosity
end

#verify_sslObject (readonly)

Returns the value of attribute verify_ssl.



10
11
12
# File 'lib/radfish/core/base_client.rb', line 10

def verify_ssl
  @verify_ssl
end

Instance Method Details

#authenticated_request(method, path, **options) ⇒ Object

Raises:

  • (NotImplementedError)


109
110
111
# File 'lib/radfish/core/base_client.rb', line 109

def authenticated_request(method, path, **options)
  raise NotImplementedError, "Subclass must implement #authenticated_request"
end

#base_urlObject



43
44
45
# File 'lib/radfish/core/base_client.rb', line 43

def base_url
  @http_client.base_url
end

#http_delete(path, **options) ⇒ Object



69
70
71
# File 'lib/radfish/core/base_client.rb', line 69

def http_delete(path, **options)
  @http_client.delete(path, **options)
end

#http_get(path, **options) ⇒ Object

Delegate HTTP methods to the client



53
54
55
# File 'lib/radfish/core/base_client.rb', line 53

def http_get(path, **options)
  @http_client.get(path, **options)
end

#http_patch(path, **options) ⇒ Object



65
66
67
# File 'lib/radfish/core/base_client.rb', line 65

def http_patch(path, **options)
  @http_client.patch(path, **options)
end

#http_post(path, **options) ⇒ Object



57
58
59
# File 'lib/radfish/core/base_client.rb', line 57

def http_post(path, **options)
  @http_client.post(path, **options)
end

#http_put(path, **options) ⇒ Object



61
62
63
# File 'lib/radfish/core/base_client.rb', line 61

def http_put(path, **options)
  @http_client.put(path, **options)
end

#loginObject

Raises:

  • (NotImplementedError)


101
102
103
# File 'lib/radfish/core/base_client.rb', line 101

def 
  raise NotImplementedError, "Subclass must implement #login"
end

#logoutObject

Raises:

  • (NotImplementedError)


105
106
107
# File 'lib/radfish/core/base_client.rb', line 105

def logout
  raise NotImplementedError, "Subclass must implement #logout"
end

#redfish_versionObject



113
114
115
116
117
118
119
120
121
# File 'lib/radfish/core/base_client.rb', line 113

def redfish_version
  response = authenticated_request(:get, "/redfish/v1")
  if response.status == 200
    data = JSON.parse(response.body)
    data["RedfishVersion"]
  else
    raise Error, "Failed to get Redfish version: #{response.status}"
  end
end

#service_rootObject



123
124
125
126
127
128
129
130
# File 'lib/radfish/core/base_client.rb', line 123

def service_root
  response = authenticated_request(:get, "/redfish/v1")
  if response.status == 200
    JSON.parse(response.body)
  else
    raise Error, "Failed to get service root: #{response.status}"
  end
end

#vendorObject

Vendor-specific methods to be overridden

Raises:

  • (NotImplementedError)


97
98
99
# File 'lib/radfish/core/base_client.rb', line 97

def vendor
  raise NotImplementedError, "Subclass must implement #vendor"
end

#with_retries(max_retries = nil, initial_delay = nil, error_classes = nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/radfish/core/base_client.rb', line 73

def with_retries(max_retries = nil, initial_delay = nil, error_classes = nil)
  max_retries ||= @retry_count
  initial_delay ||= @retry_delay
  error_classes ||= [StandardError]
  
  retries = 0
  begin
    yield
  rescue *error_classes => e
    retries += 1
    if retries <= max_retries
      delay = initial_delay * (retries ** 1.5).to_i
      debug "RETRY: #{e.message} - Attempt #{retries}/#{max_retries}, waiting #{delay}s", 1, :yellow
      sleep delay
      retry
    else
      debug "MAX RETRIES REACHED: #{e.message} after #{max_retries} attempts", 1, :red
      raise e
    end
  end
end