Class: Api::V2::JobInvocationsController

Inherits:
BaseController
  • Object
show all
Includes:
Api::Version2, Foreman::Renderer
Defined in:
app/controllers/api/v2/job_invocations_controller.rb

Instance Method Summary collapse

Instance Method Details

#cancelObject



129
130
131
132
133
134
135
136
137
# File 'app/controllers/api/v2/job_invocations_controller.rb', line 129

def cancel
  if @job_invocation.task.cancellable?
    result = @job_invocation.cancel(params.fetch('force', false))
    render :json => { :cancelled => result, :id => @job_invocation.id }
  else
    render :json => { :message => _('The job could not be cancelled.') },
      :status => :unprocessable_entity
  end
end

#createObject



89
90
91
92
93
94
95
96
97
98
99
# File 'app/controllers/api/v2/job_invocations_controller.rb', line 89

def create
  composer = JobInvocationComposer.from_api_params(
    job_invocation_params
  )
  composer.trigger!
  @job_invocation = composer.job_invocation
  @hosts = @job_invocation.targeting.hosts
  process_response @job_invocation
rescue JobInvocationComposer::JobTemplateNotFound, JobInvocationComposer::FeatureNotFound => e
  not_found(error: { message: e.message })
end

#indexObject



15
16
17
# File 'app/controllers/api/v2/job_invocations_controller.rb', line 15

def index
  @job_invocations = resource_scope_for_index
end

#outputObject



105
106
107
108
109
110
111
112
# File 'app/controllers/api/v2/job_invocations_controller.rb', line 105

def output
  if @nested_obj.task.scheduled?
    render :json => delayed_task_output(@nested_obj.task, :default => [])
    return
  end

  render :json => host_output(@nested_obj, @host, :default => [], :since => params[:since])
end

#outputsObject



159
160
161
162
163
164
165
166
167
168
169
170
# File 'app/controllers/api/v2/job_invocations_controller.rb', line 159

def outputs
  hosts = @job_invocation.targeting.hosts.authorized(:view_hosts, Host)
  hosts = hosts.search_for(params['search_query']) if params['search_query']
  raw = ActiveRecord::Type::Boolean.new.cast params['raw']
  default_value = raw ? '' : []
  outputs = hosts.map do |host|
    host_output(@job_invocation, host, :default => default_value, :since => params['since'], :raw => raw)
      .merge(host_id: host.id)
  end

  render :json => { :outputs => outputs }
end

#raw_outputObject



117
118
119
120
121
122
123
124
# File 'app/controllers/api/v2/job_invocations_controller.rb', line 117

def raw_output
  if @nested_obj.task.scheduled?
    render :json => delayed_task_output(@nested_obj.task)
    return
  end

  render :json => host_output(@nested_obj, @host, :raw => true)
end

#rerunObject



142
143
144
145
146
147
148
149
150
151
152
# File 'app/controllers/api/v2/job_invocations_controller.rb', line 142

def rerun
  composer = JobInvocationComposer.from_job_invocation(@job_invocation, params)
  if composer.rerun_possible?
    composer.trigger!
    @job_invocation = composer.job_invocation
    process_response @job_invocation
  else
    render :json => { :error => _('Could not rerun job %{id} because its template could not be found') % { :id => composer.reruns } },
      :status => :not_found
  end
end

#resource_name(resource = controller_name) ⇒ Object



172
173
174
175
176
177
178
179
# File 'app/controllers/api/v2/job_invocations_controller.rb', line 172

def resource_name(resource = controller_name)
  case resource
  when 'organization', 'location'
    nil
  else
    'job_invocation'
  end
end

#showObject



22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/api/v2/job_invocations_controller.rb', line 22

def show
  @hosts = @job_invocation.targeting.hosts.authorized(:view_hosts, Host)
  @template_invocations = @job_invocation.template_invocations
                                         .where(host: @hosts)
                                         .includes(:input_values)

  if params[:host_status] == 'true'
    template_invocations = @template_invocations.includes(:run_host_job_task).to_a
    @host_statuses = Hash[template_invocations.map { |ti| [ti.host_id, template_invocation_status(ti)] }]
  end
end