Class: Contrast::Utils::InventoryUtil

Inherits:
Object
  • Object
show all
Includes:
Components::Interface
Defined in:
lib/contrast/utils/inventory_util.rb

Overview

Utilities for getting inventory information from the application

Constant Summary collapse

AC_TYPE_DB =

TeamServer only accepts certain values for ArchitectureComponents. DO NOT CHANGE THIS!

'db'
ADAPTER =

TeamServer only accepts certain values for FlowMap Services. DO NOT CHANGE THIS

'adapter'
HOST =
'host'
PORT =
'port'
DATABASE =
'database'
DEFAULT =
'default'
LOCALHOST =
'localhost'

Class Method Summary collapse

Methods included from Components::Interface

included

Class Method Details

.active_record_configObject



27
28
29
30
31
# File 'lib/contrast/utils/inventory_util.rb', line 27

def self.active_record_config
  return @_active_record_config if instance_variable_defined?(:@_active_record_config)

  @_active_record_config = ActiveRecord::Base.connection_config rescue nil # rubocop:disable Style/RescueModifier
end

.append_db_config(activity_or_update, hash_or_str = Contrast::Utils::InventoryUtil.active_record_config) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/contrast/utils/inventory_util.rb', line 33

def self.append_db_config activity_or_update, hash_or_str = Contrast::Utils::InventoryUtil.active_record_config
  arr = build_from_db_config(hash_or_str)
  return unless arr&.any?

  arr.each do |a|
    next unless a

    if activity_or_update.is_a?(Contrast::Api::Dtm::Activity)
      activity_or_update.architectures << a
    else
      activity_or_update.components << a
    end
  end
rescue StandardError => e
  logger.error('Unable to append db config', e)
  nil
end

.build_from_db_config(hash_or_str) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/contrast/utils/inventory_util.rb', line 51

def self.build_from_db_config hash_or_str
  return unless hash_or_str

  if hash_or_str.is_a?(Hash)
    build_from_db_hash(hash_or_str)
  else
    build_from_db_string(hash_or_str.to_s)
  end
end

.build_from_db_hash(hash) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/contrast/utils/inventory_util.rb', line 61

def self.build_from_db_hash hash
  ac = Contrast::Api::Dtm::ArchitectureComponent.new
  ac.vendor = hash[:adapter] || hash[ADAPTER] || Contrast::Utils::ObjectShare::EMPTY_STRING
  ac.remote_host = host_from_hash(hash)
  ac.remote_port = port_from_hash(hash)
  ac.type = AC_TYPE_DB
  ac.url = hash[:database] || hash[DATABASE] || DEFAULT
  [ac]
end

.build_from_db_string(str) ⇒ Object

Examples: mongodb://[user:pass@]host1[,host2,[,hostN]][/[database]] postgresql://scott:tiger@localhost/mydatabase mysql+mysqlconnector://scott:tiger@localhost/foo



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/contrast/utils/inventory_util.rb', line 84

def self.build_from_db_string str
  adapter, hosts, database = split_connection_str(str)
  acs = []
  hosts.split(Contrast::Utils::ObjectShare::COMMA).map do |s|
    host, port = s.split(Contrast::Utils::ObjectShare::COLON)

    ac = Contrast::Api::Dtm::ArchitectureComponent.new
    ac.vendor = Contrast::Utils::StringUtils.force_utf8(adapter)
    ac.remote_host = Contrast::Utils::StringUtils.force_utf8(host)
    ac.remote_port = port.to_i
    ac.type = AC_TYPE_DB
    ac.url = Contrast::Utils::StringUtils.force_utf8(database)
    acs << ac
  end
  acs
end

.host_from_hash(hash) ⇒ Object



71
72
73
# File 'lib/contrast/utils/inventory_util.rb', line 71

def self.host_from_hash hash
  hash[:host] || hash[HOST] || Contrast::Utils::ObjectShare::EMPTY_STRING
end

.port_from_hash(hash) ⇒ Object



75
76
77
78
# File 'lib/contrast/utils/inventory_util.rb', line 75

def self.port_from_hash hash
  p = hash[:port] || hash[PORT] || Contrast::Utils::ObjectShare::EMPTY_STRING
  p.to_i
end

.split_connection_str(str) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/contrast/utils/inventory_util.rb', line 101

def self.split_connection_str str
  adapter, str = str.split(Contrast::Utils::ObjectShare::COLON_SLASH_SLASH)
  _auth, str = str.split(Contrast::Utils::ObjectShare::AT)
  # Not currently used
  # user, pass = auth.split(Contrast::Utils::ObjectShare::COLON)
  hosts, db_and_options = str.split(Contrast::Utils::ObjectShare::SLASH)
  hosts << LOCALHOST if hosts.empty?
  database, _options = db_and_options.split(Contrast::Utils::ObjectShare::QUESTION_MARK)

  [adapter, hosts, database]
end