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



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/controllers/api/v2/scc_accounts_controller.rb', line 123

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



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'app/controllers/api/v2/scc_accounts_controller.rb', line 156

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



54
55
56
57
# File 'app/controllers/api/v2/scc_accounts_controller.rb', line 54

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

#destroyObject



74
75
76
# File 'app/controllers/api/v2/scc_accounts_controller.rb', line 74

def destroy
  process_response @scc_account.destroy
end

#indexObject



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

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



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

def show
end

#syncObject



108
109
110
111
112
113
114
115
116
117
118
# File 'app/controllers/api/v2/scc_accounts_controller.rb', line 108

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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/controllers/api/v2/scc_accounts_controller.rb', line 84

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



62
63
64
65
66
67
68
69
70
# File 'app/controllers/api/v2/scc_accounts_controller.rb', line 62

def update
  if params[:scc_account].present?
    process_response @scc_account.update_attributes_with_logic!()
  else
    render json: { error: 'No input data provided for changing the SCC account.' }, status: :expectation_failed
  end
rescue ActiveRecord::RecordInvalid => e
  render json: { error: e.to_s }, status: :unprocessable_entity
end