1
|
module ForemanPipeline
|
2
|
class Api::JenkinsInstancesController < Katello::Api::V2::ApiController
|
3
|
respond_to :json
|
4
|
|
5
|
include Api::Rendering
|
6
|
|
7
|
before_filter :find_jenkins_instance, :only => [:show, :update, :destroy, :check_jenkins, :set_jenkins_user]
|
8
|
before_filter :find_organization, :only => [:index, :create]
|
9
|
before_filter :load_search_service, :only => [:index]
|
10
|
|
11
|
def_param_group :jenkins_instance do
|
12
|
param :name, String, :desc => N_("Jenkins instance's name")
|
13
|
param :url, String, :desc => N_("Jenkins instance's url")
|
14
|
param :cert_path, String, :desc => ("Path to the private certificate for passwordless access to jenkins server")
|
15
|
param :jenkins_home, String, :desc => ("Location of $JENKINS_HOME")
|
16
|
end
|
17
|
|
18
|
def_param_group :jenkins_instance_id do
|
19
|
param :organization_id, :number, :desc => N_("Organization identifier"), :required => true
|
20
|
param :id, :number, :desc => N_("Jenkins instance identifier"), :required => true
|
21
|
end
|
22
|
|
23
|
api :GET, "/organizations/:organization_id/jenkins_instances", N_("List jenkins instances")
|
24
|
param :organization_id, :number, :desc => N_("organization identifier"), :required => true
|
25
|
param :name, String, :desc => N_("Name of the jenkins instance")
|
26
|
def index
|
27
|
ids = JenkinsInstance.readable.where(:organization_id => @organization.id).pluck(:id)
|
28
|
filters = [:terms => {:id => ids}]
|
29
|
filters << {:term => {:name => params[:name]}} if params[:name]
|
30
|
|
31
|
options = {
|
32
|
:filters => filters,
|
33
|
:load_records? => true
|
34
|
}
|
35
|
|
36
|
respond_for_index(:collection => item_search(JenkinsInstance, params, options))
|
37
|
end
|
38
|
|
39
|
api :POST, "/organizations/:organization_id/jenkins_instances/:id", N_("Create jenkins instance")
|
40
|
param_group :jenkins_instance_id
|
41
|
param_group :jenkins_instance
|
42
|
def create
|
43
|
@jenkins_instance = JenkinsInstance.new(jenkins_instance_params)
|
44
|
@jenkins_instance.organization = @organization
|
45
|
|
46
|
rollback = false
|
47
|
JenkinsInstance.transaction do
|
48
|
@jenkins_instance.save!
|
49
|
task = sync_task(::Actions::ForemanPipeline::JenkinsInstance::CreateJenkinsInstanceKeys,
|
50
|
:jenkins_url => jenkins_instance_params[:url],
|
51
|
:cert_path => @jenkins_instance.cert_path,
|
52
|
:jenkins_home => jenkins_instance_params[:jenkins_home])
|
53
|
|
54
|
@jenkins_instance.pubkey = task.output.fetch(:pubkey)
|
55
|
@jenkins_instance.save!
|
56
|
|
57
|
if task.output.fetch(:status) == 1
|
58
|
raise ActiveRecord::Rollback
|
59
|
rollback = true
|
60
|
end
|
61
|
end
|
62
|
|
63
|
if rollback
|
64
|
fail ::Katello::HttpErrors::Conflict, "Could not access Jenkins server, are you sure you set up certificates?"
|
65
|
else
|
66
|
respond_for_show(:resource => @jenkins_instance)
|
67
|
end
|
68
|
end
|
69
|
|
70
|
api :PUT, "/organizations/:organization_id/jenkins_instances/:id", N_("Update jenkins instance")
|
71
|
param_group :jenkins_instance_id
|
72
|
param_group :jenkins_instance
|
73
|
def update
|
74
|
@jenkins_instance.update_attributes!(jenkins_instance_params.except(:url).except(:jenkins_home))
|
75
|
@jenkins_instance.save!
|
76
|
|
77
|
respond_for_show(:resource => @jenkins_instance)
|
78
|
end
|
79
|
|
80
|
api :GET, "/organizations/:organization_id/jenkins_instances/:id", N_("Get jenkins_instance by identifier")
|
81
|
param_group :jenkins_instance_id
|
82
|
def show
|
83
|
respond_for_show(:resource => @jenkins_instance)
|
84
|
end
|
85
|
|
86
|
api :DELETE, "/organizations/:organization_id/jenkins_instances/:id", N_("Delete jenkins_instance")
|
87
|
param_group :jenkins_instance_id
|
88
|
def destroy
|
89
|
@jenkins_instance.destroy
|
90
|
respond_for_show(:resource => @jenkins_instance)
|
91
|
end
|
92
|
|
93
|
api :GET, "/organizations/:organization_id/jenkins_instances/:id/check_jenkins", N_("Check jenkins instance reachability")
|
94
|
param_group :jenkins_instance_id
|
95
|
def check_jenkins
|
96
|
task = sync_task(::Actions::ForemanPipeline::Jenkins::GetVersion,
|
97
|
:id => @jenkins_instance.id,
|
98
|
:name => @jenkins_instance.name)
|
99
|
@jenkins_instance.server_version = task.output[:version]
|
100
|
respond_for_show
|
101
|
end
|
102
|
|
103
|
api :PUT, "/organizations/:organization_id/jenkins_instances/:id", N_("Set jenkins user")
|
104
|
param_group :jenkins_instance_id
|
105
|
param :jenkins_user_id, :number, :desc => N_("Jenkins user identifier to be set")
|
106
|
def set_jenkins_user
|
107
|
@jenkins_instance.jenkins_user = JenkinsUser.find(params[:jenkins_user_id])
|
108
|
@jenkins_instance.save!
|
109
|
respond_for_show
|
110
|
end
|
111
|
|
112
|
protected
|
113
|
|
114
|
def find_jenkins_instance
|
115
|
@jenkins_instance = JenkinsInstance.find_by_id(params[:id])
|
116
|
fail ::Katello::HttpErrors::NotFound, "Could not find Jenkins Instance with id: #{params[:id]}" if @jenkins_instance.nil?
|
117
|
@jenkins_instance
|
118
|
end
|
119
|
|
120
|
def jenkins_instance_params
|
121
|
params.require(:jenkins_instance).permit(:name, :url, :jenkins_home, :cert_path)
|
122
|
end
|
123
|
end
|
124
|
end
|