Class: ForemanChef::ChefServerImporter

Inherits:
Object
  • Object
show all
Defined in:
app/services/foreman_chef/chef_server_importer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ ChefServerImporter

Returns a new instance of ChefServerImporter.



3
4
5
6
7
8
# File 'app/services/foreman_chef/chef_server_importer.rb', line 3

def initialize(args = {})
  @chef_proxy = args[:chef_proxy]
  @environment = args[:env]
  @proxy = args[:proxy]
  @url_arg = args[:url]
end

Instance Attribute Details

#proxyObject



10
11
12
13
14
15
16
17
18
19
20
# File 'app/services/foreman_chef/chef_server_importer.rb', line 10

def proxy
  if @proxy
    @proxy
  elsif @url_arg
    @proxy = ProxyAPI::ForemanChef::ChefProxy.new(:url => @url_arg)
  else
    url = SmartProxy.with_features('Chef').first.try(:url)
    raise "Can't find a valid Proxy with a Chef feature" if url.blank?
    @proxy = ProxyAPI::ForemanChef::ChefProxy.new(:url => url)
  end
end

Instance Method Details

#actual_environmentsObject



62
63
64
# File 'app/services/foreman_chef/chef_server_importer.rb', line 62

def actual_environments
  @actual_environments ||= (fetch_environments_from_proxy.map { |e| e['name'] } - ignored_environments)
end

#changesObject

return changes hash, currently exists to keep compatibility with importer html



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/services/foreman_chef/chef_server_importer.rb', line 24

def changes
  changes = {'new' => {}, 'obsolete' => {}, 'updated' => {}}

  if @environment.nil?
    new_environments.each do |env|
      changes['new'][env] = {}
    end

    old_environments.each do |env|
      changes['obsolete'][env] = {}
    end
  else
    env = @environment
    changes['new'][env] = {} if new_environments.include?(@environment)
    changes['obsolete'][env] = {} if old_environments.include?(@environment)
  end
  changes
end

#db_environmentsObject



58
59
60
# File 'app/services/foreman_chef/chef_server_importer.rb', line 58

def db_environments
  @db_environments ||= (ForemanChef::Environment.where(:chef_proxy_id => @chef_proxy.id).pluck('name') - ignored_environments)
end

#new_environmentsObject



66
67
68
# File 'app/services/foreman_chef/chef_server_importer.rb', line 66

def new_environments
  actual_environments - db_environments
end

#obsolete_and_new(changes = { }) ⇒ Object

Update the environments based upon the user’s selection changes : Hash with two keys: :new and :obsolete.

changed[:/new|obsolete/] is and Array of Strings

Returns : Array of Strings containing all record errors



47
48
49
50
51
52
53
54
55
56
# File 'app/services/foreman_chef/chef_server_importer.rb', line 47

def obsolete_and_new(changes = { })
  return if changes.empty?
  if changes['new'].present?
    changes['new'].each { |name, _| ForemanChef::Environment.create(:name => name, :chef_proxy_id => @chef_proxy.id) }
  end
  if changes['obsolete'].present?
    changes['obsolete'].each { |name, _| ForemanChef::Environment.where(:chef_proxy_id => @chef_proxy.id).find_by_name(name).destroy }
  end
  []
end

#old_environmentsObject



70
71
72
# File 'app/services/foreman_chef/chef_server_importer.rb', line 70

def old_environments
  db_environments - actual_environments
end