Class: ForemanPipeline::Api::JobsController

Inherits:
Katello::Api::V2::ApiController
  • Object
show all
Includes:
Rendering, Concerns::ApiControllerExtensions
Defined in:
app/controllers/foreman_pipeline/api/jobs_controller.rb

Instance Method Summary collapse

Methods included from Concerns::ApiControllerExtensions

#resource_class

Methods included from Rendering

#respond_with_template

Instance Method Details

#add_projectsObject



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'app/controllers/foreman_pipeline/api/jobs_controller.rb', line 191

def add_projects
  rollback = { :occured => false }
  Job.transaction do
    names = params[:projects].delete_if { |name| @job.jenkins_projects.map(&:name).include? name }
    projects = names.map do |name|
      JenkinsProject.create(:name => name, :organization_id => @organization.id, :job_id => @job.id)
    end

    projects.each do |project|
      task = sync_task(::Actions::ForemanPipeline::Jenkins::GetBuildParams, :job_id => @job.id, :name => project.name)

      unless task.output[:build_params]
        raise ActiveRecord::Rollback
        rollback[:occured] = true
        rollback[:project_name] = project.name
      end
      task.output[:build_params].each do |param|
        JenkinsProjectParam.create(:name => param[:name],
                                   :type => param[:type],
                                   :description => param[:description],
                                   :value => param[:default],
                                   :organization => @organization,
                                   :jenkins_project => project)
      end
    end
  end
  if rollback[:occured]
    fail ::Katello::HttpErrors::NotFound, "Could not retrieve build params for Jenkins project: #{rollback[:project_name]}"
  else
    respond_for_show
  end
end

#available_pathsObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'app/controllers/foreman_pipeline/api/jobs_controller.rb', line 140

def available_paths
  available = []
  paths = []
  if params[:all_paths]
    available = @organization.readable_promotion_paths
    paths = available.inject([]) do |result, path|
      result << { :environments => [@organization.library] + path }
    end
    paths = [{ :environments => [@organization.library] }] if paths.empty?
  else
    available = @job.environment.full_paths rescue []
    paths = available.inject([]) do |result, path|
      result << { :environments => path }
    end
    paths = [{ :environments => [] }] if paths.empty?
  end

  collection = {
    :results => paths,
    :total => paths.size,
    :subtotal => paths.size
  }

  respond_for_index(:collection => collection)
end

#available_resourcesObject



168
169
170
171
# File 'app/controllers/foreman_pipeline/api/jobs_controller.rb', line 168

def available_resources
  @compute_resources = @job.available_compute_resources
  render "api/v2/compute_resources/index"
end

#createObject



56
57
58
59
60
61
62
# File 'app/controllers/foreman_pipeline/api/jobs_controller.rb', line 56

def create
  @job = Job.new(job_params)
  @job.organization = @organization
  @job.save!

  respond_for_show(:resource => @job)
end

#destroyObject



75
76
77
78
# File 'app/controllers/foreman_pipeline/api/jobs_controller.rb', line 75

def destroy
  @job.destroy
  respond_for_show(:resource => @job)
end

#indexObject



33
34
35
# File 'app/controllers/foreman_pipeline/api/jobs_controller.rb', line 33

def index
  respond_for_index(:collection => scoped_search(index_relation.uniq, params[:sort_by], params[:sort_order], :includes => job_includes))
end

#index_relationObject



37
38
39
40
41
42
43
44
45
# File 'app/controllers/foreman_pipeline/api/jobs_controller.rb', line 37

def index_relation
  query = Job.readable.where(:organization_id => @organization.id)
  query = query.where(:name => params[:name]) if params[:name]
  query = query.where(:manual_trigger => params[:manual_trigger]) if params[:manual_trigger]
  query = query.where(:sync_trigger => params[:sync_trigger]) if params[:sync_trigger]
  query = query.where(:levelup_trigger => params[:levelup_trigger]) if params[:levelup_trigger]
  query = query.where(:promote => params[:promote]) if params[:promote]
  query
end

#remove_projectsObject



227
228
229
230
231
232
# File 'app/controllers/foreman_pipeline/api/jobs_controller.rb', line 227

def remove_projects
  ids = params[:project_ids]
  projects = JenkinsProject.where(:id => ids)
  projects.map(&:destroy)
  respond_for_show
end

#run_jobObject



175
176
177
178
179
180
181
182
183
184
185
186
# File 'app/controllers/foreman_pipeline/api/jobs_controller.rb', line 175

def run_job
  if @job.manual_trigger
    if @filter.allow_run_for? @job
      task = async_task(::Actions::ForemanPipeline::Job::RunJobManually, @job)
      render :nothing => true
    else
      fail ::Katello::HttpErrors::Forbidden, "Job cannot be allowed to run, check your configuration"
    end
  else
    fail ::Katello::HttpErrors::Forbidden, "Running manually not allowed for Job: #{@job.name}. Try setting it's :manual_trigger property."
  end
end

#set_content_viewObject



83
84
85
86
87
88
# File 'app/controllers/foreman_pipeline/api/jobs_controller.rb', line 83

def set_content_view
  cv = Katello::ContentView.find(params[:content_view_id])
  @job.content_view = cv
  @job.save!
  respond_for_show
end

#set_environmentObject



113
114
115
116
117
118
# File 'app/controllers/foreman_pipeline/api/jobs_controller.rb', line 113

def set_environment
  @job.environment = Katello::KTEnvironment.find(params[:environment_id])
  @job.to_environments = []
  @job.save!
  respond_for_show
end

#set_hostgroupObject



93
94
95
96
97
98
# File 'app/controllers/foreman_pipeline/api/jobs_controller.rb', line 93

def set_hostgroup
  @job.hostgroup = Hostgroup.find(params[:hostgroup_id])
  @job.compute_resource = nil
  @job.save!
  respond_for_show
end

#set_jenkinsObject



103
104
105
106
107
108
# File 'app/controllers/foreman_pipeline/api/jobs_controller.rb', line 103

def set_jenkins
  instance = JenkinsInstance.find(params[:jenkins_instance_id])
  @job.jenkins_instance = instance
  @job.save!
  respond_for_show
end

#set_resourceObject



123
124
125
126
127
# File 'app/controllers/foreman_pipeline/api/jobs_controller.rb', line 123

def set_resource
  @job.compute_resource = ComputeResource.find(params[:resource_id])
  @job.save!
  respond_for_show
end

#set_to_environmentsObject



132
133
134
135
136
# File 'app/controllers/foreman_pipeline/api/jobs_controller.rb', line 132

def set_to_environments
  @job.to_environment_ids = params[:to_environment_ids]
  @job.save!
  respond_for_show
end

#showObject



49
50
51
# File 'app/controllers/foreman_pipeline/api/jobs_controller.rb', line 49

def show
  respond_for_show(:resource => @job)
end

#updateObject



67
68
69
70
71
# File 'app/controllers/foreman_pipeline/api/jobs_controller.rb', line 67

def update
  @job.update_attributes!(job_params)
  @job.save!
  respond_for_show(:resource => @job)
end