Class: Api::V2::SccAccountsController

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

Instance Method Summary collapse

Instance Method Details

#bulk_subscribeObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/controllers/api/v2/scc_accounts_controller.rb', line 115

def bulk_subscribe
  respond_to do |format|
    if params[:scc_subscribe_product_ids].count > 0
      # we need to pass two parameters to the product subscribe task,
      # the product itself and an array of repo ids
      # if the id array is empty, all repositories will be subscribed to
      scc_products = @scc_account.scc_products.where(:id => params[:scc_subscribe_product_ids])
      subscribe_task = ForemanTasks.async_task(::Actions::BulkAction,
                                               ::Actions::SccManager::SubscribeProduct,
                                               scc_products,
                                               {})
      format.json { render json: subscribe_task.to_json, status: :ok }
    else
      format.json { render json: { error: 'No Product selected' }, status: :expectation_failed }
    end
  end
rescue ::Foreman::Exception => e
  render json: { error: ('Failed to add task to queue: %s' % e).to_s }, status: :unprocessable_entity
rescue ForemanTasks::Lock::LockConflict => e
  render json: { error: ('Lock on SCC account already taken: %s' % e).to_s }, status: :unprocessable_entity
end

#bulk_subscribe_with_reposObject



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'app/controllers/api/v2/scc_accounts_controller.rb', line 148

def bulk_subscribe_with_repos
  respond_to do |format|
    # if we want to subscribe to specific repos, we need to pass the product and the
    # corresponding repository ids instead of scc products only
    if params[:scc_product_data].count > 0
      scc_products = @scc_account.scc_products.where(:id => params[:scc_product_data].pluck(:scc_product_id))
      if scc_products.empty?
        format.json { render json: { error: _('The selected products cannot be found for this SCC account.') }, status: :unprocessable_entity }
      else
        action_args = params[:scc_product_data].map { |p| { p['scc_product_id'] => p['repository_list'] } }.inject(:merge)
        subscribe_task = ForemanTasks.async_task(::Actions::BulkAction,
                                                 ::Actions::SccManager::SubscribeProduct,
                                                 scc_products,
                                                 action_args)
        format.json { render json: subscribe_task.to_json, status: :ok }
      end
    else
      format.json { render json: { error: 'No Product selected' }, status: :expectation_failed }
    end
  end
rescue ::Foreman::Exception => e
  render json: { error: ('Failed to add task to queue: %s' % e).to_s }, status: :unprocessable_entity
rescue ForemanTasks::Lock::LockConflict => e
  render json: { error: ('Lock on SCC account already taken: %s' % e).to_s }, status: :unprocessable_entity
end

#createObject



52
53
54
55
# File 'app/controllers/api/v2/scc_accounts_controller.rb', line 52

def create
  @scc_account = resource_class.new()
  process_response @scc_account.save_with_logic!
end

#destroyObject



66
67
68
# File 'app/controllers/api/v2/scc_accounts_controller.rb', line 66

def destroy
  process_response @scc_account.destroy
end

#indexObject



18
19
20
21
22
# File 'app/controllers/api/v2/scc_accounts_controller.rb', line 18

def index
  scope = resource_scope
  scope = scope.where(:organization => params[:organization_id]) if params[:organization_id].present?
  @scc_accounts = scope.search_for(params[:search], :order => params[:order]).paginate(:page => params[:page], :per_page => params[:per_page])
end

#showObject



27
# File 'app/controllers/api/v2/scc_accounts_controller.rb', line 27

def show; end

#syncObject



100
101
102
103
104
105
106
107
108
109
110
# File 'app/controllers/api/v2/scc_accounts_controller.rb', line 100

def sync
  sync_task = ForemanTasks.async_task(::Actions::SccManager::Sync, @scc_account)
  synced = @scc_account.update! sync_task: sync_task
  respond_to do |format|
    format.json { render json: sync_task.to_json } if synced
  end
rescue ::Foreman::Exception => e
  render json: { error: ('Failed to add task to queue: %s' % e).to_s }, status: :unprocessable_entity
rescue ForemanTasks::Lock::LockConflict => e
  render json: { error: ('Lock on SCC account already taken: %s' % e).to_s }, status: :unprocessable_entity
end

#test_connectionObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/controllers/api/v2/scc_accounts_controller.rb', line 76

def test_connection
  if params[:id].present?
    find_resource
    begin
       = 
    rescue ActionController::ParameterMissing
       = {}
    end
    @scc_account. = [:login] unless [:login].empty?
    @scc_account.password = [:password] unless [:password].empty?
  else
    @scc_account = resource_class.new()
  end
  respond_to do |format|
    if @scc_account.test_connection
      format.json { render json: { 'success' => true }.to_json, status: :ok }
    else
      format.json { render json: { 'success' => false, 'error' => 'Test failed. Check your credentials.' }.to_json, status: :not_found }
    end
  end
end

#updateObject



60
61
62
# File 'app/controllers/api/v2/scc_accounts_controller.rb', line 60

def update
  process_response @scc_account.update()
end