Class: SccAccountsController

Inherits:
ApplicationController
  • Object
show all
Includes:
Foreman::Controller::AutoCompleteSearch
Defined in:
app/controllers/scc_accounts_controller.rb

Instance Method Summary collapse

Instance Method Details

#bulk_subscribeObject



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

def bulk_subscribe
  scc_products_to_subscribe =
    @scc_account.scc_products.where(id: scc_bulk_subscribe_params[:scc_subscribe_product_ids])

  if scc_products_to_subscribe.count > 0
    ForemanTasks.async_task(::Actions::BulkAction,
                            ::Actions::SccManager::SubscribeProduct,
                            scc_products_to_subscribe)
    success _('Task to subscribe products started.')
  else
    warning _('No products selected.')
  end
rescue ::Foreman::Exception => e
  error _('Failed to add task to queue: %s') % e.to_s
rescue ForemanTasks::Lock::LockConflict => e
  error _('Lock on SCC account already taken: %s') % e.to_s
ensure
  redirect_to scc_accounts_path
end

#createObject

POST /scc_accounts



27
28
29
30
31
32
33
# File 'app/controllers/scc_accounts_controller.rb', line 27

def create
  @scc_account = SccAccount.new()
  @scc_account.save_with_logic!
  process_success
rescue ActiveRecord::RecordInvalid
  process_error
end

#create_nested_product_tree(scc_products, subscribed_only:) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/controllers/scc_accounts_controller.rb', line 101

def create_nested_product_tree(scc_products, subscribed_only:)
  if subscribed_only
    scc_products_base = scc_products.where(:product_type => 'base').where.not(:product_id => nil).includes([:scc_extensions])
  else
    scc_products_base = scc_products.where(:product_type => 'base').includes([:scc_extensions])
  end
  tree = []
  scc_products_base.each do |p|
    tree.push(get_product_tree_hash(p))
  end

  tree
end

#destroyObject

DELETE /scc_accounts/1



60
61
62
63
64
65
66
# File 'app/controllers/scc_accounts_controller.rb', line 60

def destroy
  if @scc_account.destroy
    process_success
  else
    process_error
  end
end

#editObject

GET /scc_accounts/1/edit



36
# File 'app/controllers/scc_accounts_controller.rb', line 36

def edit; end

#get_product_tree_hash(scc_product_base) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/controllers/scc_accounts_controller.rb', line 133

def get_product_tree_hash(scc_product_base)
  if scc_product_base.scc_extensions.blank?
    scc_product_hash(scc_product_base)
  else
    children = []
    scc_product_base.scc_extensions.each do |ext|
      children.push(get_product_tree_hash(ext))
    end
    scc_product_base_hash = scc_product_hash(scc_product_base)
    scc_product_base_hash['children'] = children
    scc_product_base_hash
  end
end

#indexObject

GET /scc_accounts



10
11
12
13
14
15
16
17
18
# File 'app/controllers/scc_accounts_controller.rb', line 10

def index
  @scc_accounts = resource_base.where(organization: @organization)
                               .search_for(params[:search], order: params[:order])
                               .paginate(:page => params[:page], :per_page => params[:per_page])
  # overwrite the product list with filtered products that do not include products with empty repositories
  @scc_accounts.each do ||
    .scc_products_with_repos_count = .scc_products.only_products_with_repos.count
  end
end

#newObject

GET /scc_accounts/new



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

def new
  @scc_account = SccAccount.new
  @scc_account.organization = @organization
end

#scc_product_hash(scc_product) ⇒ Object



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

def scc_product_hash(scc_product)
  scc_product_json = scc_product.as_json(:only => [:scc_id, :id, :arch, :version, :product_id, :subscription_valid],
                                         include: { :scc_repositories => { :only => [:id, :name, :subscription_valid] } })
                                .merge('name' => scc_product.pretty_name, 'product_category' => scc_product.product_category)
  # find if and which Katello root repository is associated with this SCC product
  repo_ids_katello = scc_product.product.blank? || scc_product.product.root_repository_ids.blank? ? nil : scc_product.product.root_repository_ids
  scc_product_json['scc_repositories'].each do |repo|
    # byebug
    if repo_ids_katello.blank?
      repo['katello_root_repository_id'] = nil
    else
      repo_ids_scc = scc_product.scc_repositories.find(repo['id']).katello_root_repository_ids
      repo['katello_root_repository_id'] = repo_ids_scc.blank? ? nil : (repo_ids_scc & repo_ids_katello).first
    end
  end
  scc_product_json
end

#syncObject

PUT /scc_accounts/1/sync



69
70
71
72
73
74
75
76
77
78
79
# File 'app/controllers/scc_accounts_controller.rb', line 69

def sync
  sync_task = ForemanTasks.async_task(::Actions::SccManager::Sync, @scc_account)
  @scc_account.update! sync_task: sync_task
  success _('Sync task started.')
rescue ::Foreman::Exception => e
  error _('Failed to add task to queue: %s') % e.to_s
rescue ForemanTasks::Lock::LockConflict => e
  error _('Lock on SCC account already taken: %s') % e.to_s
ensure
  redirect_to scc_accounts_path
end

#test_connectionObject

POST /scc_accounts/test_connection



39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/scc_accounts_controller.rb', line 39

def test_connection
  @scc_account = SccAccount.new()
  @scc_account.password = SccAccount.find(params[:scc_account_id]).password if params[:scc_account_id].present? && [:password].empty?
  respond_to do |format|
    if @scc_account.test_connection
      format.json { render json: nil, status: :ok }
    else
      format.json { render json: nil, status: :not_found }
    end
  end
end

#updateObject

PATCH/PUT /scc_accounts/1



52
53
54
55
56
57
# File 'app/controllers/scc_accounts_controller.rb', line 52

def update
  @scc_account.update_attributes_with_logic!()
  process_success
rescue ActiveRecord::RecordInvalid
  process_error
end