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



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/controllers/api/v2/scc_accounts_controller.rb', line 119

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



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

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



70
71
72
# File 'app/controllers/api/v2/scc_accounts_controller.rb', line 70

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



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

def show
end

#syncObject



104
105
106
107
108
109
110
111
112
113
114
# File 'app/controllers/api/v2/scc_accounts_controller.rb', line 104

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/controllers/api/v2/scc_accounts_controller.rb', line 80

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
63
64
65
66
# File 'app/controllers/api/v2/scc_accounts_controller.rb', line 60

def update
  if params[:scc_account].present?
    process_response @scc_account.update()
  else
    render json: { error: 'No input data provided for changing the SCC account.', status: :expectation_failed }
  end
end