Class: SccAccountsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/scc_accounts_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /scc_accounts



24
25
26
27
28
29
30
# File 'app/controllers/scc_accounts_controller.rb', line 24

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



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/controllers/scc_accounts_controller.rb', line 79

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



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

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

#editObject

GET /scc_accounts/1/edit



33
34
# File 'app/controllers/scc_accounts_controller.rb', line 33

def edit
end

#get_product_tree_hash(scc_product_base) ⇒ Object



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

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



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

def index
  @scc_accounts = resource_base.where(organization: @organization)
  # 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



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

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

#scc_product_hash(scc_product) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/controllers/scc_accounts_controller.rb', line 93

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&.root_repository_ids
  scc_product_json['scc_repositories'].each do |repo|
    if repo_ids_katello.blank?
      repo['katello_repository_id'] = nil
    else
      repo_ids_scc = scc_product.scc_repositories.find(repo['id']).katello_root_repository_ids
      if repo_ids_scc.blank?
        repo['katello_repository_id'] = nil
      else
        # we need to extract the library instance id for the correct repo link in the UI
        root_repo_id = (repo_ids_scc & repo_ids_katello).first
        repo['katello_repository_id'] = ::Katello::Repository.where({ root_id: root_repo_id, library_instance: nil }).pick(:id)
      end
    end
  end
  scc_product_json
end

#syncObject

PUT /scc_accounts/1/sync



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

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



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

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



50
51
52
53
54
55
# File 'app/controllers/scc_accounts_controller.rb', line 50

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