Class: ForemanProbing::ScanComposer

Inherits:
Object
  • Object
show all
Defined in:
app/models/foreman_probing/scan_composer.rb

Constant Summary collapse

TARGETING_TYPES =
%w(direct subnet host proxy).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#portsObject

Returns the value of attribute ports.



4
5
6
# File 'app/models/foreman_probing/scan_composer.rb', line 4

def ports
  @ports
end

#probeObject

Returns the value of attribute probe.



4
5
6
# File 'app/models/foreman_probing/scan_composer.rb', line 4

def probe
  @probe
end

#proxyObject

Returns the value of attribute proxy.



4
5
6
# File 'app/models/foreman_probing/scan_composer.rb', line 4

def proxy
  @proxy
end

#targetingObject

Returns the value of attribute targeting.



4
5
6
# File 'app/models/foreman_probing/scan_composer.rb', line 4

def targeting
  @targeting
end

Class Method Details

.new_from_params(params) ⇒ Object



8
9
10
11
12
13
14
15
# File 'app/models/foreman_probing/scan_composer.rb', line 8

def self.new_from_params(params)
  self.new.tap do |c|
    c.targeting = c.targeting_from_params(params)
    c.ports = c.ports_from_params(params)
    c.probe = params[:scan_type]
    c.proxy = c.proxy_from_params(params)
  end
end

.new_from_scan(scan) ⇒ Object



17
18
19
20
21
22
23
24
# File 'app/models/foreman_probing/scan_composer.rb', line 17

def self.new_from_scan(scan)
  self.new.tap do |c|
    c.targeting = scan.targeting.dup
    c.ports = scan.raw_ports
    c.probe = scan.scan_type
    c.proxy = scan.smart_proxy
  end
end

Instance Method Details

#compose!Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/foreman_probing/scan_composer.rb', line 26

def compose!
  if @scan.nil?
    @scan = ForemanProbing::Scan.new
    @scan.targeting = targeting
    @scan.target_kind = targeting.target_kind
    @scan.smart_proxy = proxy
    @scan.scan_type = probe
    @scan.raw_ports = ports

    case targeting
    when ::ForemanProbing::Targeting::Direct
      @scan.direct = targeting.raw_targets
    when ::ForemanProbing::Targeting::Subnet
      @scan.subnet_id = targeting.raw_targets.to_i
    when ::ForemanProbing::Targeting::Search
      @scan.search_query = targeting.raw_targets
    end
  end
  @scan
end

#ports_from_params(params) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'app/models/foreman_probing/scan_composer.rb', line 51

def ports_from_params(params)
  # ranges = params[:ports].split(',')
  # ports = ranges.map do |range|
  #   range.split('-').map do |from, to|
  #     to.nil? ? from : (from..to).entries
  #   end
  # end
  # ports.flatten.map(&:chomp).map(&:to_i)
  params[:raw_ports]
end

#proxy_from_params(params) ⇒ Object



47
48
49
# File 'app/models/foreman_probing/scan_composer.rb', line 47

def proxy_from_params(params)
  SmartProxy.authorized.find(params['smart_proxy_id'])
end

#targeting_from_params(params) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/models/foreman_probing/scan_composer.rb', line 62

def targeting_from_params(params)
  case params[:target_kind].downcase
  when 'direct'
    ::ForemanProbing::Targeting::Direct.new(:raw_targets => params[:direct])
  when 'subnet'
    ::ForemanProbing::Targeting::Subnet.new(:raw_targets => params[:subnet_id])
  when 'host'
    ::ForemanProbing::Targeting::Search.new(:raw_targets => params[:search_query])
  when 'proxy'
    ::ForemanProbing::Targeting::SubnetDiscovery.new
  end
end