Class: Katello::ProductContentImporter

Inherits:
Object
  • Object
show all
Defined in:
app/services/katello/product_content_importer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cp_products = []) ⇒ ProductContentImporter

Returns a new instance of ProductContentImporter.



25
26
27
28
29
30
31
# File 'app/services/katello/product_content_importer.rb', line 25

def initialize(cp_products = [])
  @contents_to_create = []
  @product_contents_to_create = []
  @product_mapping = {}
  @content_url_updated = []
  @cp_products = cp_products
end

Instance Attribute Details

#content_url_updatedObject (readonly)

Example product_content json structure {

"content":{
   "uuid":"4028f9f7677f3f4c0167a30cc92c5d55",
   "id":"4010",
   "type":"file",
   "label":"rhel-6-server-satellite-5.7-isos",
   "name":"Red Hat Satellite 5.7 (ISOs)",
   "vendor":"Red Hat",
   "contentUrl":"/content/dist/rhel/server/6/$releasever/$basearch/satellite/5.7/iso",
   "requiredTags":"rhel-6-server",
   "gpgUrl":"file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release",
   "modifiedProductIds":[],
   "arches":"x86_64",
   "requiredProductIds":[],
   "metadataExpire":1,
   "releaseVer":null
},
"enabled":false

}



23
24
25
# File 'app/services/katello/product_content_importer.rb', line 23

def content_url_updated
  @content_url_updated
end

Instance Method Details

#add_product_content(product, product_content_json) ⇒ Object



33
34
35
# File 'app/services/katello/product_content_importer.rb', line 33

def add_product_content(product, product_content_json)
  @product_mapping[product] = product_content_json.map(&:with_indifferent_access)
end

#fetch_product_contents_to_move(product, prod_contents_json) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/services/katello/product_content_importer.rb', line 46

def fetch_product_contents_to_move(product, prod_contents_json)
  content_ids = prod_contents_json.map { |pc| pc[:content][:id] }
  # Identify if there are any product_content that should not be
  # part of this product.
  product_contents_to_delete_or_move = product.
                                  product_contents.
                                  joins(:content).
                                  where.not(content: { cp_content_id: content_ids })
  # Identify if product content actually moved between 2 different products
  product_contents_to_delete_or_move.select do |pc|
    content_exists?(product.organization, pc.content)
  end
end

#find_product_for_content(content_id) ⇒ Object



37
38
39
40
41
42
43
44
# File 'app/services/katello/product_content_importer.rb', line 37

def find_product_for_content(content_id)
  prod = @cp_products.find do |prod_json|
    prod_json['productContent'].any? do |product_content_json|
      product_content_json["content"]["id"] == content_id
    end
  end
  ::Katello::Product.find_by(cp_id: prod["id"]) if prod
end

#handle_product_moves(product, prod_contents_json) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/services/katello/product_content_importer.rb', line 60

def handle_product_moves(product, prod_contents_json)
  product_contents_to_move = fetch_product_contents_to_move(product, prod_contents_json)
  moved_product_contents = []
  product_contents_to_move.each do |pc|
    content = pc.content
    root_repo = product.root_repositories.find_by(content_id: content.cp_content_id)
    actual_product = find_product_for_content(content.cp_content_id)
    if actual_product.present? && root_repo.present? && root_repo.product != actual_product
      root_repo.update!(product_id: actual_product.id)
      pc.update!(product_id: actual_product.id)
      moved_product_contents << pc
    else
      pc.destroy!
    end
  end
  product.reload unless product_contents_to_move.blank?

  moved_product_contents
end

#importObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/services/katello/product_content_importer.rb', line 80

def import
  return if @product_mapping.blank?
  moved_product_contents = []
  @product_mapping.each do |product, prod_contents_json|
    moved_product_contents += handle_product_moves(product, prod_contents_json)
    existing_product_contents = product.product_contents.to_a
    prod_contents_json.each do |prod_content_json|
      content = create_or_update_content(product, prod_content_json)
      existing_content_map[content.cp_content_id] = content if content.new_record?
      create_or_update_product_content(product, existing_product_contents, content, prod_content_json[:enabled])
    end
  end
  ::Katello::Content.import(@contents_to_create, recursive: true)
  ::Katello::ProductContent.import(fetch_product_contents_to_create(moved_product_contents))
end