Class: ForemanPuppet::Puppetclass
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- ForemanPuppet::Puppetclass
- Extended by:
- FriendlyId
- Includes:
- Authorizable, Parameterizable::ByIdName, ScopedSearchExtensions
- Defined in:
- app/models/foreman_puppet/puppetclass.rb
Class Method Summary collapse
-
.classes2hash(classes) ⇒ Object
returns a hash containing modules and associated classes.
-
.classes2hash_v2(classes) ⇒ Object
For API v2 - eliminate node :puppetclass for each object.
- .search_by_host(_key, operator, value) ⇒ Object
Instance Method Summary collapse
- #all_hostgroups(with_descendants: true, unsorted: false) ⇒ Object
-
#hostgroup_ids=(hg_ids) ⇒ Object
We are going through two associations, so we are on our own to define modifiers.
- #hosts_count ⇒ Object
-
#klass ⇒ Object
returns class name (excluding of the module name).
-
#location_ids ⇒ Object
For Audits to be correctly taxed for Puppetclass creation Puppetclass gets saved before the environment class and thus taxonomy ids are empty We collect the ids from unsaved environment_classes for the Audits correct taxation.
-
#module_name ⇒ Object
returns module name (excluding of the class name) if class separator does not exists (the “::” chars), then returns the whole class name.
- #organization_ids ⇒ Object
Class Method Details
.classes2hash(classes) ⇒ Object
returns a hash containing modules and associated classes
56 57 58 59 60 61 62 63 64 |
# File 'app/models/foreman_puppet/puppetclass.rb', line 56 def self.classes2hash(classes) hash = {} classes.each do |klass| next unless (mod = klass.module_name) hash[mod] ||= [] hash[mod] << klass end hash end |
.classes2hash_v2(classes) ⇒ Object
For API v2 - eliminate node :puppetclass for each object. returns a hash containing modules and associated classes
67 68 69 70 71 72 73 74 75 76 |
# File 'app/models/foreman_puppet/puppetclass.rb', line 67 def self.classes2hash_v2(classes) hash = {} classes.each do |klass| if (mod = klass.module_name) hash[mod] ||= [] hash[mod] << { id: klass.id, name: klass.name, created_at: klass.created_at, updated_at: klass.updated_at } end end hash end |
.search_by_host(_key, operator, value) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'app/models/foreman_puppet/puppetclass.rb', line 129 def self.search_by_host(_key, operator, value) conditions = sanitize_sql_for_conditions(["hosts.name #{operator} ?", value_to_sql(operator, value)]) direct = Puppetclass.joins(host_puppet_facets: :host).where(conditions).pluck(:id).uniq hostgroup = Hostgroup.joins(:hosts).find_by(conditions) indirect = if hostgroup.blank? [] else HostgroupClass.joins(hostgroup_puppet_facet: :hostgroup) .where(Hostgroup.arel_table[:id].in(hostgroup.path_ids)) .pluck(:puppetclass_id).uniq end return { conditions: '1=0' } if direct.blank? && indirect.blank? puppet_classes = (direct + indirect).uniq { conditions: "puppetclasses.id IN(#{puppet_classes.join(',')})" } end |
Instance Method Details
#all_hostgroups(with_descendants: true, unsorted: false) ⇒ Object
100 101 102 103 104 105 106 107 108 |
# File 'app/models/foreman_puppet/puppetclass.rb', line 100 def all_hostgroups(with_descendants: true, unsorted: false) hgs = Hostgroup. .left_outer_joins(puppet: [:hostgroup_classes, { config_groups: [:config_group_classes] }]) .where("#{id} IN (hostgroup_classes.puppetclass_id, config_group_classes.puppetclass_id)") .distinct hgs = hgs.reorder('') if unsorted hgs = hgs.flat_map(&:subtree).uniq if with_descendants hgs end |
#hostgroup_ids=(hg_ids) ⇒ Object
We are going through two associations, so we are on our own to define modifiers
121 122 123 124 125 126 127 |
# File 'app/models/foreman_puppet/puppetclass.rb', line 121 def hostgroup_ids=(hg_ids) hg_ids = Array(hg_ids).reject(&:blank?) hg_to_facets_ids = HostgroupPuppetFacet.where(hostgroup_id: hg_ids).pluck(:hostgroup_id, :id).to_h missing_facet_ids = hg_ids.map(&:to_i) - hg_to_facets_ids.keys new_facet_ids = missing_facet_ids.map { |hg_id| HostgroupPuppetFacet.create(hostgroup_id: hg_id).id } self.hostgroup_puppet_facet_ids = hg_to_facets_ids.values + new_facet_ids end |
#hosts_count ⇒ Object
110 111 112 113 114 115 116 117 118 |
# File 'app/models/foreman_puppet/puppetclass.rb', line 110 def hosts_count hostgroup_ids = all_hostgroups(unsorted: true).map(&:id) Host::Managed. .reorder(nil) .left_outer_joins(puppet: [:host_classes, { config_groups: [:config_group_classes] }]) .where('(? IN (host_classes.puppetclass_id, config_group_classes.puppetclass_id)) OR (hosts.hostgroup_id IN (?))', id, hostgroup_ids) .distinct .count end |
#klass ⇒ Object
returns class name (excluding of the module name)
96 97 98 |
# File 'app/models/foreman_puppet/puppetclass.rb', line 96 def klass name.gsub("#{module_name}::", '') end |
#location_ids ⇒ Object
For Audits to be correctly taxed for Puppetclass creation Puppetclass gets saved before the environment class and thus taxonomy ids are empty We collect the ids from unsaved environment_classes for the Audits correct taxation
81 82 83 |
# File 'app/models/foreman_puppet/puppetclass.rb', line 81 def location_ids environment_classes.select(&:new_record?).flat_map { |ec| ec.environment.location_ids }.concat(super).uniq end |
#module_name ⇒ Object
returns module name (excluding of the class name) if class separator does not exists (the “::” chars), then returns the whole class name
91 92 93 |
# File 'app/models/foreman_puppet/puppetclass.rb', line 91 def module_name (i = name.index('::')) ? name[0..i - 1] : name end |
#organization_ids ⇒ Object
85 86 87 |
# File 'app/models/foreman_puppet/puppetclass.rb', line 85 def organization_ids environment_classes.select(&:new_record?).flat_map { |ec| ec.environment.organization_ids }.concat(super).uniq end |