Project

General

Profile

Download (2.79 KB) Statistics
| Branch: | Tag: | Revision:

foreman_pipeline / app / controllers / foreman_pipeline / api / jenkins_instances_controller.rb @ be52ca64

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]
8
    before_filter :find_organization, :only => [:index, :create]
9
    before_filter :load_search_service, :only => [:index]
10

    
11
    def index
12
      ids = JenkinsInstance.readable.where(:organization_id => @organization.id).pluck(:id)
13
      filters = [:terms => {:id => ids}]
14

    
15
      options = {
16
        :filters => filters,
17
        :load_records? => true
18
      }
19

    
20
      respond_for_index(:collection => item_search(JenkinsInstance, params, options))
21
    end
22

    
23
    def create
24
      @jenkins_instance = JenkinsInstance.new(jenkins_instance_params)
25
      @jenkins_instance.organization = @organization          
26

    
27
      rollback = false
28
      JenkinsInstance.transaction do
29
        @jenkins_instance.save!
30
        task = sync_task(::Actions::ForemanPipeline::JenkinsInstance::CreateJenkinsInstanceKeys, 
31
                      :jenkins_url => jenkins_instance_params[:url],
32
                      :cert_path => @jenkins_instance.cert_path,
33
                      :jenkins_home => jenkins_instance_params[:jenkins_home])
34

    
35
        @jenkins_instance.pubkey = task.output.fetch(:pubkey)
36
        @jenkins_instance.save!
37

    
38
        if task.output.fetch(:status) == 1 
39
          raise ActiveRecord::Rollback
40
          rollback = true
41
        end
42
      end       
43
      
44
      if rollback
45
        fail ::Katello::HttpErrors::Conflict, "Could not access Jenkins server, are you sure you set up certificates?"         
46
      else
47
        respond_for_show(:resource => @jenkins_instance)          
48
      end     
49
    end
50

    
51
    def update
52
      @jenkins_instance.update_attributes!(jenkins_instance_params.except(:url).except(:jenkins_home))
53
      @jenkins_instance.save!
54

    
55
      respond_for_show(:resource => @jenkins_instance)
56
    end
57

    
58
    def show
59
      respond_for_show(:resource => @jenkins_instance)
60
    end
61

    
62
    def destroy
63
      @jenkins_instance.destroy
64
      respond_for_show(:resource => @jenkins_instance)
65
    end
66

    
67
    def check_jenkins
68
      task = sync_task(::Actions::ForemanPipeline::Jenkins::GetVersion,
69
                         :id => @jenkins_instance.id, 
70
                         :name => @jenkins_instance.name)
71
      @jenkins_instance.server_version = task.output[:version]
72
      respond_for_show
73
    end
74

    
75
    protected
76

    
77
    def find_jenkins_instance
78
      @jenkins_instance = JenkinsInstance.find_by_id(params[:id])
79
      fail ::Katello::HttpErrors::NotFound, "Could not find Jenkins Instance with id: #{params[:id]}" if @jenkins_instance.nil?
80
      @jenkins_instance
81
    end
82

    
83
    def jenkins_instance_params
84
      params.require(:jenkins_instance).permit(:name, :url, :jenkins_home, :cert_path)
85
    end
86
  end
87
end