Class: ForemanTasks::Api::TasksController

Inherits:
Api::V2::BaseController
  • Object
show all
Includes:
Foreman::Controller::SmartProxyAuth, FindTasksCommon
Defined in:
app/controllers/foreman_tasks/api/tasks_controller.rb

Defined Under Namespace

Classes: BadRequest

Instance Method Summary collapse

Methods included from FindTasksCommon

#current_taxonomy_search, #search_query

Instance Method Details

#bulk_cancelObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/controllers/foreman_tasks/api/tasks_controller.rb', line 126

def bulk_cancel
  if params[:search].nil? && params[:task_ids].nil?
    raise BadRequest, _('Please provide at least one of search or task_ids parameters in the request')
  end
  filtered_scope = bulk_scope
  cancelled, skipped = filtered_scope.partition(&:cancellable?)
  cancelled.each(&:cancel)
  if params[:search]
    notification = UINotifications::Tasks::TaskBulkCancel.new(filtered_scope.first, cancelled.length, skipped.length)
    notification.deliver!
  end
  render :json => {
    total: cancelled.length + skipped.length,
    cancelled: cancelled,
    skipped: skipped,
  }
end

#bulk_resumeObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/controllers/foreman_tasks/api/tasks_controller.rb', line 86

def bulk_resume
  if params[:search].nil? && params[:task_ids].nil?
    params[:search] = 'state = paused and result = error'
  end
  resumed = []
  failed = []
  skipped = []
  filtered_scope = bulk_scope
  filtered_scope.each do |task|
    if task.resumable?
      begin
        ForemanTasks.dynflow.world.execute(task.execution_plan.id)
        resumed << task_hash(task)
      rescue RuntimeError
        failed << task_hash(task)
      end
    else
      skipped << task_hash(task)
    end
  end
  if params[:search]
    notification = UINotifications::Tasks::TaskBulkResume.new(filtered_scope.first, resumed.length, failed.length, skipped.length)
    notification.deliver!
  end
  render :json => {
    total: resumed.length + failed.length + skipped.length,
    resumed: resumed,
    failed: failed,
    skipped: skipped,
  }
end

#bulk_searchObject



67
68
69
70
71
72
73
74
75
76
# File 'app/controllers/foreman_tasks/api/tasks_controller.rb', line 67

def bulk_search
  searches = Array(params[:searches])
  @tasks   = {}

  ret = searches.map do |search_params|
    { search_params: search_params,
      results:       search_tasks(search_params) }
  end
  render :json => ret
end

#bulk_stopObject



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'app/controllers/foreman_tasks/api/tasks_controller.rb', line 152

def bulk_stop
  if params[:search].nil? && params[:task_ids].nil?
    raise BadRequest, _('Please provide at least one of search or task_ids parameters in the request')
  end

  filtered_scope = bulk_scope
  total_length = filtered_scope.count
  to_stop = filtered_scope.where.not(state: :stopped)
  to_stop_length = to_stop.count
  skipped_length = total_length - to_stop_length

  to_stop.find_each { |task| task.update(state: :stopped) }

  if params[:search]
    notification = UINotifications::Tasks::TaskBulkStop.new(filtered_scope.first, to_stop_length, skipped_length)
    notification.deliver!
  end

  render :json => {
    total: total_length,
    stopped_length: to_stop_length,
    skipped_length: skipped_length,
  }
end

#callbackObject



219
220
221
222
223
224
225
226
227
228
229
# File 'app/controllers/foreman_tasks/api/tasks_controller.rb', line 219

def callback
  callbacks = params.key?(:callback) ? Array(params) : params[:callbacks]
  ids = callbacks.map { |payload| payload[:callback][:task_id] }
  external_map = Hash[*ForemanTasks::Task.where(:id => ids).pluck(:id, :external_id).flatten]
  callbacks.each do |payload|
    # We need to call .to_unsafe_h to unwrap the hash from ActionController::Parameters
    callback = payload[:callback]
    process_callback(external_map[callback[:task_id]], callback[:step_id].to_i, payload[:data].to_unsafe_h, :request_id => ::Logging.mdc['request'])
  end
  render :json => { :message => 'processing' }.to_json
end

#detailsObject



33
# File 'app/controllers/foreman_tasks/api/tasks_controller.rb', line 33

def details; end

#indexObject



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'app/controllers/foreman_tasks/api/tasks_controller.rb', line 181

def index
  if params[:sort_by] || params[:sort_order]
    Foreman::Deprecation.api_deprecation_warning(
      "The sort params sort_by and sort_order are deprecated.
      Please use the order param instead as one string 'order=started_at desc'"
    )

    ordering_params = {
      sort_by: params[:sort_by] || 'started_at',
      sort_order: params[:sort_order] || 'DESC',
    }
    params[:order] = "#{ordering_params[:sort_by]} #{ordering_params[:sort_order]}"
  end
  params[:order] ||= 'started_at DESC'
  @tasks = resource_scope_for_index.order(params[:order].to_s)
end

#search_optionsObject



198
199
200
# File 'app/controllers/foreman_tasks/api/tasks_controller.rb', line 198

def search_options
  [search_query, {}]
end

#showObject



29
# File 'app/controllers/foreman_tasks/api/tasks_controller.rb', line 29

def show; end

#summaryObject



23
24
25
# File 'app/controllers/foreman_tasks/api/tasks_controller.rb', line 23

def summary
  render :json => ForemanTasks::Task::Summarizer.new(resource_scope).summarize_by_status
end