Class: DiscoveredHostsController

Inherits:
ApplicationController
  • Object
show all
Includes:
Foreman::Controller::AutoCompleteSearch, Foreman::Controller::TaxonomyMultiple
Defined in:
app/controllers/discovered_hosts_controller.rb

Instance Method Summary collapse

Instance Method Details

#auto_complete_searchObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/controllers/discovered_hosts_controller.rb', line 109

def auto_complete_search
  begin
    @items = Host::Discovered.complete_for(params[:search])
    @items = @items.map do |item|
      category = (['and','or','not','has'].include?(item.to_s.sub(/^.*\s+/,''))) ? 'Operators' : ''
      part = item.to_s.sub(/^.*\b(and|or)\b/i) {|match| match.sub(/^.*\s+/,'')}
      completed = item.to_s.chomp(part)
      {:completed => completed, :part => part, :label => item, :category => category}
    end
  rescue ScopedSearch::QueryNotSupported => e
    @items = [{:error =>e.to_s}]
  end
  render :json => @items
end

#createObject

Importing yaml is restricted to puppetmasters, so instead we take the ip as a parameter and use refresh_facts to get the rest



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/controllers/discovered_hosts_controller.rb', line 29

def create
  Taxonomy.no_taxonomy_scope do
    host, imported = Host::Discovered.new(:ip => get_ip_from_env).refresh_facts
    respond_to do |format|
      format.yml {
        if imported
          render :text => _("Imported Host::Discovered"), :status => 200 and return
        else
          render :text => _("Failed to import facts for Host::Discovered"), :status => 400
        end
      }
    end
  end
rescue Exception => e
  logger.warn "Failed to import facts for Host::Discovered: #{e}"
  render :text => _("Failed to import facts for Host::Discovered: %s") % (e), :status => 400
end

#destroyObject



55
56
57
58
# File 'app/controllers/discovered_hosts_controller.rb', line 55

def destroy
  @host.destroy
  redirect_to :action => 'index'
end

#editObject



60
61
62
63
64
65
66
67
# File 'app/controllers/discovered_hosts_controller.rb', line 60

def edit
  @host         = @host.becomes(::Host::Managed)
  @host.type    = 'Host::Managed'
  @host.managed = true
  @host.build   = true

  render :template => 'hosts/edit'
end

#indexObject



17
18
19
20
21
22
23
24
25
# File 'app/controllers/discovered_hosts_controller.rb', line 17

def index
  search = resource_base.search_for(params[:search], :order => params[:order])
  respond_to do |format|
    format.html do
      @hosts = search.paginate(:page => params[:page])
    end
    format.json { render :json => search }
  end
end

#multiple_destroyObject



93
94
# File 'app/controllers/discovered_hosts_controller.rb', line 93

def multiple_destroy
end

#refresh_factsObject



85
86
87
88
89
90
91
# File 'app/controllers/discovered_hosts_controller.rb', line 85

def refresh_facts
  if @host.is_a?(::Host::Discovered) and @host.refresh_facts
    process_success :success_msg => _("Facts refreshed for %s") % @host.name, :success_redirect => :back
  else
    process_error :error_msg => _("Failed to refresh facts for %s") % @host.name, :redirect => :back
  end
end

#showObject



47
48
49
50
51
52
53
# File 'app/controllers/discovered_hosts_controller.rb', line 47

def show
  # filter graph time range
  @range = nil

  # summary report text
  @report_summary = nil
end

#submit_multiple_destroyObject



96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/controllers/discovered_hosts_controller.rb', line 96

def submit_multiple_destroy
  # keep all the ones that were not deleted for notification.
  @hosts.delete_if {|host| host.destroy}

  missed_hosts = @hosts.map(&:name).join('<br/>')
  if @hosts.empty?
    notice _("Destroyed selected hosts")
  else
    error _("The following hosts were not deleted: %s") % missed_hosts
  end
  redirect_to(discovered_hosts_path)
end

#updateObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/controllers/discovered_hosts_controller.rb', line 69

def update
  @host         = @host.becomes(::Host::Managed)
  @host.type    = 'Host::Managed'
  forward_url_options
  Taxonomy.no_taxonomy_scope do
    if @host.update_attributes(params[:host])
      process_success :success_redirect => host_path(@host), :redirect_xhr => request.xhr?
    else
      taxonomy_scope
      load_vars_for_ajax
      offer_to_overwrite_conflicts
      process_error :object => @host, :render => 'hosts/edit'
    end
  end
end