Class: Api::V2::DiscoveredHostsController

Inherits:
BaseController
  • Object
show all
Includes:
Foreman::Controller::DiscoveredExtensions, Foreman::Controller::Parameters::DiscoveredHost
Defined in:
app/controllers/api/v2/discovered_hosts_controller.rb

Instance Method Summary collapse

Instance Method Details

#auto_provisionObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/controllers/api/v2/discovered_hosts_controller.rb', line 133

def auto_provision
  if rule = find_discovery_rule(@discovered_host)
    if perform_auto_provision(@discovered_host, rule)
      msg = _("Host %{host} was provisioned with rule %{rule}") % {:host => @discovered_host.name, :rule => rule.name}
      render :json => {:message => msg}
    else
      msg = _("Unable to provision %{host}: %{errors}") % {:host => @discovered_host.name, :errors => @discovered_host.errors.full_messages.join(' ')}
      render :json => {:message => msg}, :status => :unprocessable_entity
    end
  else
    render_error :custom_error, :status => :not_found,
                 :locals => {
                     :message => _("No rule found for host %s") % @discovered_host.name
                 }
  end
rescue ::Foreman::Exception => e
  render :json => {'message' => e.to_s}, :status => :unprocessable_entity
end

#auto_provision_allObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'app/controllers/api/v2/discovered_hosts_controller.rb', line 154

def auto_provision_all
  result = true
  error_message = _("Errors during auto provisioning: %s")

  if Host::Discovered.count == 0
    error_message = _("No discovered hosts to provision")
    result = false
  end

  total_count = 0
  overall_errors = ""
  Host::Discovered.all.each do |discovered_host|
    if rule = find_discovery_rule(discovered_host)
      result &= perform_auto_provision(discovered_host, rule)
      unless discovered_host.errors.empty?
        errors = discovered_host.errors.full_messages.join(' ')
        logger.warn "Failed to auto provision host %s: %s" % [discovered_host.name, errors]
        overall_errors << "#{discovered_host.name}: #{errors} "
      else
        total_count += 1
      end
    else
      logger.warn "No rule found for host %s" % discovered_host.name
    end
  end

  if result
    msg = _("%s discovered hosts were provisioned") % total_count
    render :json => {:message => msg}
  else
    render_error :custom_error,
      :status => :unprocessable_entity,
      :locals => {
        :message => error_message % overall_errors
      }
  end
end

#createObject



44
45
46
47
48
# File 'app/controllers/api/v2/discovered_hosts_controller.rb', line 44

def create
  @discovered_host = Host::Discovered.new(discovered_host_params)
  @discovered_host.suggest_default_pxe_loader if params[:discovered_host] && params[:discovered_host][:pxe_loader].nil?
  process_response @discovered_host.save
end

#destroyObject



98
99
100
# File 'app/controllers/api/v2/discovered_hosts_controller.rb', line 98

def destroy
  process_response @discovered_host.destroy
end

#factsObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/controllers/api/v2/discovered_hosts_controller.rb', line 105

def facts
  # creating a host from facts is not a mass assignment - we store them individually
  facts = params['facts'].to_unsafe_h
  state = true
  User.as_anonymous_admin do
    @discovered_host = Host::Discovered.import_host(facts)
    # Host::Based.set_taxonomies sets taxonomies during import from either
    # facts or via Global Foreman setting "default_taxonomy", reset it back so
    # the anonymous admin can see all records. We set taxonomy explicitly via
    # discovery SubnetAndTaxonomy import hook and enforce taxnomy manually
    # in find_discovery_rule method via validate_rule_by_taxonomy.
    Taxonomy.no_taxonomy_scope do
      Rails.logger.warn 'Discovered facts import unsuccessful, skipping auto provisioning' unless @discovered_host
      if Setting['discovery_auto'] && @discovered_host && (rule = find_discovery_rule(@discovered_host))
        state = perform_auto_provision(@discovered_host, rule)
      end
    end
  end
  process_response state
rescue Exception => e
  ForemanDiscovery::UINotifications::FailedDiscovery.new(e).deliver!
  Foreman::Logging.exception("Host discovery failed, facts: #{facts}", e)
  render :json => {'message'=>e.to_s}, :status => :unprocessable_entity
end

#indexObject



22
23
24
# File 'app/controllers/api/v2/discovered_hosts_controller.rb', line 22

def index
  @discovered_hosts = resource_scope.search_for(*search_options).paginate(paginate_options)
end

#rebootObject



204
205
206
207
208
# File 'app/controllers/api/v2/discovered_hosts_controller.rb', line 204

def reboot
  process_response @discovered_host.reboot
rescue ::Foreman::Exception => e
  render :json => {'message'=>e.to_s}
end

#reboot_allObject



212
213
214
215
216
217
218
219
220
221
222
223
# File 'app/controllers/api/v2/discovered_hosts_controller.rb', line 212

def reboot_all
  error_message = perform_reboot_all
  if error_message
    render_error :custom_error,
                 :status => :unprocessable_entity,
                 :locals => {
                     :message => error_message
                 }
  else
    process_success _("Discovered hosts are rebooting now")
  end
end

#refresh_factsObject



195
196
197
198
199
# File 'app/controllers/api/v2/discovered_hosts_controller.rb', line 195

def refresh_facts
  process_response @discovered_host.refresh_facts
rescue ::Foreman::Exception => e
  render :json => { 'message' => e.to_s }
end

#showObject



29
30
# File 'app/controllers/api/v2/discovered_hosts_controller.rb', line 29

def show
end

#updateObject



88
89
90
91
92
93
# File 'app/controllers/api/v2/discovered_hosts_controller.rb', line 88

def update
  @host = ::ForemanDiscovery::HostConverter.to_managed(@discovered_host, true, managed_host_params[:build], managed_host_params)
  forward_request_url
  update_response = @host.save
  process_response update_response
end