Class: ForemanDiscovery::NodeAPI::NodeResource

Inherits:
Object
  • Object
show all
Defined in:
app/services/foreman_discovery/node_api/node_resource.rb

Direct Known Subclasses

Inventory, PowerService

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ NodeResource

Returns a new instance of NodeResource.

Raises:

  • (ArgumentError)


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/services/foreman_discovery/node_api/node_resource.rb', line 3

def initialize(args)
  raise ArgumentError, _("Options must be hash") unless args.is_a?(Hash)
  raise ArgumentError, _("Option 'url' must be provided") unless args[:url]
  @args = args
  @connect_params = {
    :headers => { :accept => :json },
    :timeout => 20, # tighter timeout, discovery performs up to two calls per request
  }

  if url.match(/^https/i) && Rails.env != "test"
    if is_proxy?
      logger.debug "Connecting to proxy, setting up SSL client cert"
      cert         = Setting[:ssl_certificate]
      ca_cert      = Setting[:ssl_ca_file]
      hostprivkey  = Setting[:ssl_priv_key]

      @connect_params.merge!(
        :ssl_client_cert  =>  OpenSSL::X509::Certificate.new(File.read(cert)),
        :ssl_client_key   =>  OpenSSL::PKey::RSA.new(File.read(hostprivkey)),
        :ssl_ca_file      =>  ca_cert,
        :verify_ssl       =>  OpenSSL::SSL::VERIFY_PEER
      )
    else
      logger.debug "Connecting without proxy, not using SSL client cert"
      @connect_params.merge!(
        :verify_ssl       =>  OpenSSL::SSL::VERIFY_NONE
      )
    end
  end
end

Instance Method Details

#is_proxy?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'app/services/foreman_discovery/node_api/node_resource.rb', line 56

def is_proxy?
  root_path.starts_with?('/discovery')
end

#portObject



44
45
46
47
48
# File 'app/services/foreman_discovery/node_api/node_resource.rb', line 44

def port
  URI.parse(url).port
rescue Exception => e
  raise ArgumentError, _("Option 'url' must be valid URI: %s") % e
end

#root_pathObject



50
51
52
53
54
# File 'app/services/foreman_discovery/node_api/node_resource.rb', line 50

def root_path
  URI.parse(url).path
rescue Exception => e
  raise ArgumentError, _("Option 'url' must be valid URI: %s") % e
end

#schemeObject



38
39
40
41
42
# File 'app/services/foreman_discovery/node_api/node_resource.rb', line 38

def scheme
  URI.parse(url).scheme
rescue Exception => e
  raise ArgumentError, _("Option 'url' must be valid URI: %s") % e
end

#urlObject



34
35
36
# File 'app/services/foreman_discovery/node_api/node_resource.rb', line 34

def url
  @args[:url]
end