Class: ReceptorController::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/receptor_controller/client.rb,
lib/receptor_controller/client/version.rb,
lib/receptor_controller/client/exception.rb

Defined Under Namespace

Classes: Configuration, ControllerResponseError, Directive, DirectiveBlocking, DirectiveNonBlocking, Error, ResponseError, ResponseTimeoutError, ResponseWorker, UnknownResponseTypeError

Constant Summary collapse

STATUS_DISCONNECTED =
{'status' => 'disconnected'}.freeze
VERSION =
"0.0.7".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: Configuration.default, logger: ManageIQ::Messaging::NullLogger.new) ⇒ Client

Returns a new instance of Client.



28
29
30
31
32
33
# File 'lib/receptor_controller/client.rb', line 28

def initialize(config: Configuration.default, logger: ManageIQ::Messaging::NullLogger.new)
  self.config          = config
  self.default_headers = {"Content-Type" => "application/json"}
  self.logger          = logger
  self.response_worker = ResponseWorker.new(config, logger)
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



13
14
15
# File 'lib/receptor_controller/client.rb', line 13

def config
  @config
end

#default_headersObject

Returns the value of attribute default_headers.



12
13
14
# File 'lib/receptor_controller/client.rb', line 12

def default_headers
  @default_headers
end

#identity_headerObject

Returns the value of attribute identity_header.



12
13
14
# File 'lib/receptor_controller/client.rb', line 12

def identity_header
  @identity_header
end

#loggerObject

Returns the value of attribute logger.



12
13
14
# File 'lib/receptor_controller/client.rb', line 12

def logger
  @logger
end

#response_workerObject

Returns the value of attribute response_worker.



12
13
14
# File 'lib/receptor_controller/client.rb', line 12

def response_worker
  @response_worker
end

Class Method Details

.configureObject



18
19
20
21
22
23
24
# File 'lib/receptor_controller/client.rb', line 18

def configure
  if block_given?
    yield(Configuration.default)
  else
    Configuration.default
  end
end

Instance Method Details

#connection_status(account_number, node_id) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/receptor_controller/client.rb', line 35

def connection_status(, node_id)
  body = {
    :account => ,
    :node_id => node_id
  }.to_json

  response = Faraday.post(config.connection_status_url, body, headers())
  if response.success?
    JSON.parse(response.body)
  else
    logger.error(receptor_log_msg("Connection_status failed: HTTP #{response.status}", , node_id))
    STATUS_DISCONNECTED
  end
rescue Faraday::Error => e
  logger.error(receptor_log_msg("Connection_status failed", , node_id, e))
  STATUS_DISCONNECTED
end

#directive(account_number, node_id, payload:, directive:, log_message_common: nil, type: :non_blocking) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/receptor_controller/client.rb', line 53

def directive(, node_id,
              payload:,
              directive:,
              log_message_common: nil,
              type: :non_blocking)
  klass = type == :non_blocking ? DirectiveNonBlocking : DirectiveBlocking
  klass.new(:name               => directive,
            :account            => ,
            :node_id            => node_id,
            :payload            => payload,
            :log_message_common => log_message_common,
            :client             => self)
end

#headers(account = nil) ⇒ Object



67
68
69
# File 'lib/receptor_controller/client.rb', line 67

def headers( = nil)
  default_headers.merge(auth_headers() || {})
end

#receptor_log_msg(msg, account, node_id, exception = nil) ⇒ Object



71
72
73
74
75
# File 'lib/receptor_controller/client.rb', line 71

def receptor_log_msg(msg, , node_id, exception = nil)
  message = "Receptor: #{msg}"
  message += "; #{exception.class.name}: #{exception.message}" if exception.present?
  message + " [Account number: #{}; Receptor node: #{node_id}]"
end