Module: Katello::Glue::Candlepin::CandlepinObject::ClassMethods

Defined in:
app/models/katello/glue/candlepin/candlepin_object.rb

Instance Method Summary collapse

Instance Method Details

#candlepin_records_by_id(organization) ⇒ Object



6
7
8
9
10
11
12
13
# File 'app/models/katello/glue/candlepin/candlepin_object.rb', line 6

def candlepin_records_by_id(organization)
  records = get_for_owner(organization.label)
  records_by_id = {}
  records.each do |record|
    records_by_id[record['id']] = record
  end
  records_by_id
end

#import_all(organization = nil, import_managed_associations = true) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/katello/glue/candlepin/candlepin_object.rb', line 48

def import_all(organization = nil, import_managed_associations = true)
  organizations = organization ? [organization] : Organization.all

  organizations.each do |org|
    candlepin_records = candlepin_records_by_id(org)
    import_candlepin_records(candlepin_records.values, org)

    objects = self.in_organization(org)
    objects.each do |item|
      exists_in_candlepin = candlepin_records.key?(item.cp_id)

      Katello::Logging.time("Imported #{self}", data: { cp_id: item.cp_id, destroyed: !exists_in_candlepin }) do
        if exists_in_candlepin
          item.import_data
          item.import_managed_associations if import_managed_associations && item.respond_to?(:import_managed_associations)
        else
          item.destroy
        end
      end
    end
  end
end

#import_candlepin_record(record:, organization:) {|db_attrs| ... } ⇒ Object

Yields:

  • (db_attrs)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/katello/glue/candlepin/candlepin_object.rb', line 21

def import_candlepin_record(record:, organization:)
  db_attrs = {
    cp_id: record['id'],
    organization: organization,
  }

  yield(db_attrs) if block_given?

  persisted = nil
  Katello::Util::Support.active_record_retry do
    persisted = self.where(db_attrs).first_or_create!
  end

  persisted
end

#import_candlepin_records(candlepin_objects, org) ⇒ Object



15
16
17
18
19
# File 'app/models/katello/glue/candlepin/candlepin_object.rb', line 15

def import_candlepin_records(candlepin_objects, org)
  candlepin_objects.each do |object|
    import_candlepin_record(record: object, organization: org)
  end
end

#with_identifier(ids) ⇒ Object



37
38
39
# File 'app/models/katello/glue/candlepin/candlepin_object.rb', line 37

def with_identifier(ids)
  self.with_identifiers(ids).first
end

#with_identifiers(ids) ⇒ Object



41
42
43
44
45
46
# File 'app/models/katello/glue/candlepin/candlepin_object.rb', line 41

def with_identifiers(ids)
  ids = [ids] unless ids.is_a?(Array)
  ids.map!(&:to_s)
  id_integers = ids.map { |string| Integer(string) rescue -1 }
  where("#{self.table_name}.id = (?) or #{self.table_name}.cp_id = (?)", id_integers, ids)
end